From 17c587494f5d2d051ba23ea3af07b39a8892b467 Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Sat, 28 Sep 2019 18:34:53 +0100 Subject: [PATCH] Shell: Use "cmd /q /c" on Windows, not "sh". --- fixtures/broken-specs/smoke.yaml | 12 ++++-------- fixtures/shell/local.yaml | 10 +++++----- package.yaml | 8 ++++++++ spec/io/shell.out-unix | 6 +++--- spec/io/shell.out-windows | 17 ++++++++++++----- src/Test/Smoke/Executable.hs | 20 +++++++------------- src/unix/Test/Smoke/Shell.hs | 13 +++++++++++++ src/windows/Test/Smoke/Shell.hs | 15 +++++++++++++++ 8 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 src/unix/Test/Smoke/Shell.hs create mode 100644 src/windows/Test/Smoke/Shell.hs diff --git a/fixtures/broken-specs/smoke.yaml b/fixtures/broken-specs/smoke.yaml index 8651337..ce0c0cc 100644 --- a/fixtures/broken-specs/smoke.yaml +++ b/fixtures/broken-specs/smoke.yaml @@ -2,14 +2,12 @@ tests: - name: no-command - name: no-outputs - command: - - echo + command: echo args: - input - name: missing-input-file - command: - - echo + command: echo args: - something stdin: @@ -18,16 +16,14 @@ tests: output - name: missing-output-file - command: - - echo + command: echo args: - something stdout: file: io/missing.out - name: missing-error-file - command: - - echo + command: echo args: - something stderr: diff --git a/fixtures/shell/local.yaml b/fixtures/shell/local.yaml index 2d73c71..3ed8070 100644 --- a/fixtures/shell/local.yaml +++ b/fixtures/shell/local.yaml @@ -1,9 +1,9 @@ tests: - - name: use the default shell + - name: use the default shell, on Unix or Windows command: | - echo 'Something.' >&2 + echo Something.>&2 false - echo 'Something else.' >&2 + echo Something else.>&2 stderr: | Something. Something else. @@ -41,7 +41,7 @@ tests: stderr: | Something else. - - name: pass args to a shell command + - name: pass args to the default shell command command: | echo $1 $2 $3 args: @@ -53,7 +53,7 @@ tests: stdout: | a b c - - name: pass args to a shell command with a custom shell + - name: pass args to a custom shell command command: shell: - ruby diff --git a/package.yaml b/package.yaml index aea2675..a42017e 100644 --- a/package.yaml +++ b/package.yaml @@ -40,6 +40,14 @@ library: ghc-options: - -Wall - -Werror + when: + condition: os(windows) + then: + source-dirs: + - src/windows + else: + source-dirs: + - src/unix executables: smoke: diff --git a/spec/io/shell.out-unix b/spec/io/shell.out-unix index c912f53..8ff190c 100644 --- a/spec/io/shell.out-unix +++ b/spec/io/shell.out-unix @@ -2,7 +2,7 @@ global/runs the command with a custom shell succeeded global/runs the filter with a custom shell succeeded -local/use the default shell +local/use the default shell, on Unix or Windows succeeded local/pipe STDIN to the script succeeded @@ -10,9 +10,9 @@ local/use custom shell flags succeeded local/use a custom shell succeeded -local/pass args to a shell command +local/pass args to the default shell command succeeded -local/pass args to a shell command with a custom shell +local/pass args to a custom shell command succeeded local/use a custom shell with an absolute path succeeded diff --git a/spec/io/shell.out-windows b/spec/io/shell.out-windows index 9650f8e..fa4497a 100644 --- a/spec/io/shell.out-windows +++ b/spec/io/shell.out-windows @@ -2,7 +2,7 @@ global/runs the command with a custom shell succeeded global/runs the filter with a custom shell succeeded -local/use the default shell +local/use the default shell, on Unix or Windows succeeded local/pipe STDIN to the script succeeded @@ -10,9 +10,16 @@ local/use custom shell flags succeeded local/use a custom shell succeeded -local/pass args to a shell command - succeeded -local/pass args to a shell command with a custom shell +local/pass args to the default shell command + args: a + b + c + d + e + stdout: @@ -1 +1 @@ + -a b c + +$1 $2 $3 +local/pass args to a custom shell command succeeded local/use a custom shell with an absolute path The executable "bin\sh" could not be found. @@ -21,4 +28,4 @@ local/use a shell that doesn't exist local/use a shell that isn't executable The file at "fixtures\non_executable_application" is not executable. -11 tests, 3 failures +11 tests, 4 failures diff --git a/src/Test/Smoke/Executable.hs b/src/Test/Smoke/Executable.hs index 81dd9fe..5c16701 100644 --- a/src/Test/Smoke/Executable.hs +++ b/src/Test/Smoke/Executable.hs @@ -1,5 +1,3 @@ -{-# LANGUAGE OverloadedStrings #-} - module Test.Smoke.Executable where import Control.Monad.Trans.Except (ExceptT) @@ -12,18 +10,9 @@ import System.IO.Temp (withSystemTempFile) import System.Process (CreateProcess(..), proc) import System.Process.Text (readCreateProcessWithExitCode) import Test.Smoke.Paths +import Test.Smoke.Shell import Test.Smoke.Types -defaultShell :: ExceptT PathError IO Shell -defaultShell = do - sh <- findExecutable $ parseFile "sh" - return $ Shell sh mempty - -shellFromCommandLine :: CommandLine -> ExceptT PathError IO Shell -shellFromCommandLine (CommandLine shellName shellArgs) = do - shellCommand <- findExecutable shellName - return $ Shell shellCommand shellArgs - runExecutable :: Executable -> Args @@ -38,7 +27,7 @@ runExecutable (ExecutableProgram executablePath executableArgs) args (StdIn stdI {cwd = toFilePath . unWorkingDirectory <$> workingDirectory}) stdIn runExecutable (ExecutableScript (Shell shellPath shellArgs) (Script script)) args stdIn workingDirectory = - withSystemTempFile "smoke.sh" $ \scriptPath scriptHandle -> do + withSystemTempFile defaultShellScriptName $ \scriptPath scriptHandle -> do Text.IO.hPutStr scriptHandle script hClose scriptHandle let executableArgs = shellArgs <> Args (Vector.singleton scriptPath) @@ -61,3 +50,8 @@ convertCommandToExecutable (Just shell) (CommandScript Nothing script) = convertCommandToExecutable _ (CommandScript (Just commandLine) script) = do shell <- shellFromCommandLine commandLine return $ ExecutableScript shell script + +shellFromCommandLine :: CommandLine -> ExceptT PathError IO Shell +shellFromCommandLine (CommandLine shellName shellArgs) = do + shellCommand <- findExecutable shellName + return $ Shell shellCommand shellArgs diff --git a/src/unix/Test/Smoke/Shell.hs b/src/unix/Test/Smoke/Shell.hs new file mode 100644 index 0000000..5828158 --- /dev/null +++ b/src/unix/Test/Smoke/Shell.hs @@ -0,0 +1,13 @@ +module Test.Smoke.Shell where + +import Control.Monad.Trans.Except (ExceptT) +import Test.Smoke.Paths +import Test.Smoke.Types + +defaultShellScriptName :: String +defaultShellScriptName = "smoke.sh" + +defaultShell :: ExceptT PathError IO Shell +defaultShell = do + sh <- findExecutable $ parseFile "sh" + return $ Shell sh mempty diff --git a/src/windows/Test/Smoke/Shell.hs b/src/windows/Test/Smoke/Shell.hs new file mode 100644 index 0000000..67795b7 --- /dev/null +++ b/src/windows/Test/Smoke/Shell.hs @@ -0,0 +1,15 @@ +module Test.Smoke.Shell where + +import Control.Monad.Trans.Except (ExceptT) +import qualified Data.Vector as Vector +import Test.Smoke.Paths +import Test.Smoke.Types + +defaultShellScriptName :: String +defaultShellScriptName = "smoke.bat" + +defaultShell :: ExceptT PathError IO Shell +defaultShell = do + cmd <- findExecutable $ parseFile "cmd" + let args = Args (Vector.fromList ["/q", "/c"]) + return $ Shell cmd args