add newtypes for QuickCheck to avoid LANG=C issues
All properties changed to use them, except for prop_encode_c_decode_c_roundtrip, which already filtered to ascii for other reasons. A few modules had to be split out, because Setup does not build-depend on QuickCheck.
This commit is contained in:
parent
aad4129669
commit
885974be99
17 changed files with 211 additions and 130 deletions
65
Utility/ShellEscape.hs
Normal file
65
Utility/ShellEscape.hs
Normal file
|
@ -0,0 +1,65 @@
|
|||
{- shell escaping
|
||||
-
|
||||
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- License: BSD-2-clause
|
||||
-}
|
||||
|
||||
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
||||
|
||||
module Utility.ShellEscape (
|
||||
shellWrap,
|
||||
shellEscape,
|
||||
shellUnEscape,
|
||||
prop_isomorphic_shellEscape,
|
||||
prop_isomorphic_shellEscape_multiword,
|
||||
) where
|
||||
|
||||
import Utility.QuickCheck
|
||||
import Utility.Split
|
||||
|
||||
import Data.List
|
||||
import Prelude
|
||||
|
||||
-- | Wraps a shell command line inside sh -c, allowing it to be run in a
|
||||
-- login shell that may not support POSIX shell, eg csh.
|
||||
shellWrap :: String -> String
|
||||
shellWrap cmdline = "sh -c " ++ shellEscape cmdline
|
||||
|
||||
-- | Escapes a filename or other parameter to be safely able to be exposed to
|
||||
-- the shell.
|
||||
--
|
||||
-- This method works for POSIX shells, as well as other shells like csh.
|
||||
shellEscape :: String -> String
|
||||
shellEscape f = "'" ++ escaped ++ "'"
|
||||
where
|
||||
-- replace ' with '"'"'
|
||||
escaped = intercalate "'\"'\"'" $ splitc '\'' f
|
||||
|
||||
-- | Unescapes a set of shellEscaped words or filenames.
|
||||
shellUnEscape :: String -> [String]
|
||||
shellUnEscape [] = []
|
||||
shellUnEscape s = word : shellUnEscape rest
|
||||
where
|
||||
(word, rest) = findword "" s
|
||||
findword w [] = (w, "")
|
||||
findword w (c:cs)
|
||||
| c == ' ' = (w, cs)
|
||||
| c == '\'' = inquote c w cs
|
||||
| c == '"' = inquote c w cs
|
||||
| otherwise = findword (w++[c]) cs
|
||||
inquote _ w [] = (w, "")
|
||||
inquote q w (c:cs)
|
||||
| c == q = findword w cs
|
||||
| otherwise = inquote q (w++[c]) cs
|
||||
|
||||
prop_isomorphic_shellEscape :: TestableString -> Bool
|
||||
prop_isomorphic_shellEscape ts = [s] == (shellUnEscape . shellEscape) s
|
||||
where
|
||||
s = fromTestableString ts
|
||||
|
||||
prop_isomorphic_shellEscape_multiword :: [TestableString] -> Bool
|
||||
prop_isomorphic_shellEscape_multiword ts =
|
||||
l == (shellUnEscape . unwords . map shellEscape) l
|
||||
where
|
||||
l = map fromTestableString ts
|
Loading…
Add table
Add a link
Reference in a new issue