diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-08-22 22:10:51 +0200 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-08-22 22:10:51 +0200 |
| commit | 0ba7a3adcc5e539a31b5893bc956a39105e6a9bf (patch) | |
| tree | 9bb1cbf98c3ee4e65367d73fa0c272a962a1b826 | |
| parent | b7638754c0856b1bb5f911985190419ecb5e267b (diff) | |
Shell: track exit code of the last command
| -rw-r--r-- | src/Script/Shell.hs | 12 | ||||
| -rw-r--r-- | test/asset/shell/error-exit.et | 10 | ||||
| -rw-r--r-- | test/script/shell.et | 11 |
3 files changed, 24 insertions, 9 deletions
diff --git a/src/Script/Shell.hs b/src/Script/Shell.hs index 29e2324..f070de5 100644 --- a/src/Script/Shell.hs +++ b/src/Script/Shell.hs @@ -47,6 +47,7 @@ data ShellState = ShellState { shellWorkingDirectory :: FilePath , shellOldWorkingDirectory :: FilePath , shellExitOnError :: Bool + , shellLastExitCode :: ExitCode } data ShellStatement = ShellStatement @@ -134,17 +135,17 @@ executeCommand sei@ShellExecInfo {..} st pstdin pstdout pstderr scmd@ShellComman _ -> do return cur - ( getExitStatus, state' ) <- executeCommandProcess sei st (handledHandle pstdin') (handledHandle pstdout') (handledHandle pstderr') args cmdCommand + ( getExitStatus, st' ) <- executeCommandProcess sei st (handledHandle pstdin') (handledHandle pstdout') (handledHandle pstderr') args cmdCommand let failedWithStatus status = do when (shellExitOnError st) $ do liftIO $ putMVar seiStatusVar status throwError Failed - return state' + return st' { shellLastExitCode = status } mapM_ closeIfRequested [ pstdin', pstdout', pstderr' ] getExitStatus >>= \case Exited ExitSuccess -> do - return state' + return st' { shellLastExitCode = ExitSuccess } Exited status -> do outLine OutputChildFail (Just $ textProcName seiProcName) $ "failed at: " <> textSourceLine cmdSourceLine failedWithStatus status @@ -243,11 +244,12 @@ executeScript sei@ShellExecInfo {..} pstdin pstdout pstderr (ShellScript stateme { shellWorkingDirectory = nodeDir seiNode , shellOldWorkingDirectory = nodeDir seiNode , shellExitOnError = True + , shellLastExitCode = ExitSuccess } - _ <- (\f -> foldM f initialState statements) $ \st ShellStatement {..} -> do + finalState <- (\f -> foldM f initialState statements) $ \st ShellStatement {..} -> do executePipeline sei st (KeepHandle pstdin) (KeepHandle pstdout) (KeepHandle pstderr) shellPipeline - liftIO $ putMVar seiStatusVar ExitSuccess + liftIO $ putMVar seiStatusVar (shellLastExitCode finalState) spawnShell :: Node -> ProcName -> ShellScript -> TestRun Process spawnShell procNode procName script = do diff --git a/test/asset/shell/error-exit.et b/test/asset/shell/error-exit.et index feddf64..ecc2cd4 100644 --- a/test/asset/shell/error-exit.et +++ b/test/asset/shell/error-exit.et @@ -3,7 +3,14 @@ test Default: shell on n as sh: false -test Disabled: +test DisabledSuccess: + node n + shell on n as sh: + set +e + false + true + +test DisabledFailure: node n shell on n as sh: set +e @@ -16,3 +23,4 @@ test Enabled: false set -e false + true diff --git a/test/script/shell.et b/test/script/shell.et index e63093b..e58dfea 100644 --- a/test/script/shell.et +++ b/test/script/shell.et @@ -139,13 +139,18 @@ test ShellExitOnError: expect /run-done/ flush - send "run Disabled" - expect /run-test-result Disabled done/ + send "run DisabledSuccess" + expect /run-test-result DisabledSuccess done/ + expect /run-done/ + flush + + send "run DisabledFailure" + expect /run-test-result DisabledFailure failed/ expect /run-done/ flush send "run Enabled" - expect /child-fail sh failed at: .*error-exit.et:18:9: false/ + expect /child-fail sh failed at: .*error-exit.et:25:9: false/ expect /run-test-result Enabled failed/ expect /run-done/ flush |