Skip to content

Commit 95d01ab

Browse files
authored
Merge pull request #17 from obfusk/fix-shell
add --shell option
2 parents 396b6e6 + 7e4e86d commit 95d01ab

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,24 @@ ghci:
5757

5858
# TEST
5959

60-
test: testunix testexamples
60+
test: testunix testexamples testbash
6161

6262
# run cross-platform and unix-specific shell tests with default shelltest build
6363
testunix: build
64-
$(SHELLTEST) tests -x /examples -x .windows -w $(DEFAULTEXE)
64+
$(SHELLTEST) tests -x /bash -x /examples -x .windows -w $(DEFAULTEXE)
6565

6666
# run cross-platform and windows-specific shell tests
6767
# (though if you are running make on windows, you may be able to, or have to, use testunix)
6868
testwindows:
69-
$(SHELLTEST) tests -x /examples -x .unix -w $(DEFAULTEXE)
69+
$(SHELLTEST) tests -x /bash -x /examples -x .unix -w $(DEFAULTEXE)
7070

7171
testexamples: build
7272
$(SHELLTEST) tests/examples
7373

74+
testbash: build
75+
! $(SHELLTEST) tests/bash
76+
$(SHELLTEST) tests/bash --shell /bin/bash
77+
7478
# run shell tests with several ghc versions
7579
# test-with-resolvers: build-with-resolvers $(foreach r,$(RESOLVERS),test-with-resolver-$r)
7680

src/shelltest.hs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import System.FilePath (takeDirectory)
1818
import System.FilePath.Find (findWithHandler, (==?), always)
1919
import qualified System.FilePath.Find as Find (extension)
2020
import System.IO (Handle, hGetContents, hPutStr)
21-
import System.Process (StdStream (CreatePipe), shell, createProcess, CreateProcess (..), waitForProcess, ProcessHandle)
21+
import System.Process (StdStream (CreatePipe), shell, proc, createProcess, CreateProcess (..), waitForProcess, ProcessHandle)
2222
import Test.Framework (defaultMainWithArgs)
2323
import Test.Framework.Providers.HUnit (hUnitTestToTests)
2424
import Test.HUnit
@@ -62,6 +62,7 @@ data Args = Args {
6262
,with :: String
6363
,timeout :: Int
6464
,threads :: Int
65+
,shell_cmd :: Maybe FilePath
6566
,debug :: Bool
6667
,debug_parse :: Bool
6768
,testpaths :: [FilePath]
@@ -83,6 +84,7 @@ argdefs = Args {
8384
,with = def &= typ "EXE" &= help "Replace the first word of test commands with EXE (unindented commands only)"
8485
,timeout = def &= name "o" &= typ "SECS" &= help "Number of seconds a test may run (default: no limit)"
8586
,threads = def &= name "j" &= typ "N" &= help "Number of threads for running tests (default: 1)"
87+
,shell_cmd = def &= explicit &= name "shell" &= typ "EXE" &= help "The shell program to use (must accept -c CMD; default: /bin/sh on POSIX, cmd.exe on Windows)"
8688
,debug = def &= help "Show debug info while running"
8789
,debug_parse = def &= help "Show test file parsing results and stop"
8890
,testpaths = def &= args &= typ "TESTFILES|TESTDIRS"
@@ -165,7 +167,7 @@ shellTestToHUnitTest args ShellTest{testname=n,command=c,stdin=i,stdoutExpected=
165167
trim' = if all_ args then id else trim
166168
when (debug args) $ do
167169
printf "actual command was: %s\n" (show cmd)
168-
(o_actual, e_actual, x_actual) <- runCommandWithInput dir cmd i
170+
(o_actual, e_actual, x_actual) <- runCommandWithInput (shell_cmd args) dir cmd i
169171
when (debug args) $ do
170172
printf "actual stdout was : %s\n" (show $ trim' o_actual)
171173
printf "actual stderr was : %s\n" (show $ trim' e_actual)
@@ -193,10 +195,10 @@ shellTestToHUnitTest args ShellTest{testname=n,command=c,stdin=i,stdoutExpected=
193195
-- | Run a shell command line, passing it standard input if provided,
194196
-- and return the standard output, standard error output and exit code.
195197
-- Note on unix, at least with ghc 6.12, command (and filepath) are assumed to be utf8-encoded.
196-
runCommandWithInput :: Maybe FilePath -> String -> Maybe String -> IO (String, String, Int)
197-
runCommandWithInput wd cmd input = do
198+
runCommandWithInput :: Maybe FilePath -> Maybe FilePath -> String -> Maybe String -> IO (String, String, Int)
199+
runCommandWithInput sh wd cmd input = do
198200
-- this has to be done carefully
199-
(ih,oh,eh,ph) <- runInteractiveCommandInDir wd cmd
201+
(ih,oh,eh,ph) <- runInteractiveCommandInDir sh wd cmd
200202
when (isJust input) $ forkIO (hPutStr ih $ fromJust input) >> return ()
201203
o <- newEmptyMVar
202204
e <- newEmptyMVar
@@ -207,16 +209,18 @@ runCommandWithInput wd cmd input = do
207209
e_actual <- takeMVar e
208210
return (o_actual, e_actual, x_actual)
209211

210-
runInteractiveCommandInDir :: Maybe FilePath -> String -> IO (Handle, Handle, Handle, ProcessHandle)
211-
runInteractiveCommandInDir wd cmd = do
212+
runInteractiveCommandInDir :: Maybe FilePath -> Maybe FilePath -> String -> IO (Handle, Handle, Handle, ProcessHandle)
213+
runInteractiveCommandInDir sh wd cmd = do
212214
(mb_in, mb_out, mb_err, p) <-
213215
createProcess $
214-
(shell cmd) { cwd = wd
215-
, std_in = CreatePipe
216-
, std_out = CreatePipe
217-
, std_err = CreatePipe }
216+
run { cwd = wd
217+
, std_in = CreatePipe
218+
, std_out = CreatePipe
219+
, std_err = CreatePipe }
218220
-- these should all be Just since we used CreatePipe
219221
return (fromJust mb_in, fromJust mb_out, fromJust mb_err, p)
222+
where
223+
run = maybe (shell cmd) (\shcmd -> proc shcmd ["-c", cmd]) sh
220224

221225
hGetContentsStrictlyAnd :: Handle -> (String -> IO b) -> IO b
222226
hGetContentsStrictlyAnd h f = hGetContents h >>= \s -> length s `seq` f s

tests/bash/dollar-quote.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo $'\'o\' `elem` p'
2+
>>>
3+
'o' `elem` p
4+
>>>= 0

0 commit comments

Comments
 (0)