Skip to content

Commit

Permalink
Add a exec variant that doesn't fail upon receiving a non-zero error …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
smelc committed Jan 9, 2024
1 parent fdbed5a commit 675a11d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Hedgehog/Extras/Test/Process.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module Hedgehog.Extras.Test.Process
( createProcess
, exec
, execAny
, exec_
, execFlex
, execFlex'
Expand Down Expand Up @@ -194,20 +195,17 @@ exec_
-> m ()
exec_ execConfig bin arguments = void $ exec execConfig bin arguments

-- | Execute a process
-- | Execute a process, returning the stdout. Fail if the call returns
-- with a non-zero exit code. For a version that doesn't fail upon receiving
-- a non-zero exit code, see 'execAny'.
exec
:: (MonadTest m, MonadIO m, HasCallStack)
=> ExecConfig
-> String
-> [String]
-> m String
exec execConfig bin arguments = GHC.withFrozenCallStack $ do
let cp = (IO.proc bin arguments)
{ IO.env = getLast $ execConfigEnv execConfig
, IO.cwd = getLast $ execConfigCwd execConfig
}
H.annotate . ("Command: " <>) $ bin <> " " <> L.unwords arguments
(exitResult, stdout, stderr) <- H.evalIO $ IO.readCreateProcessWithExitCode cp ""
(exitResult, stdout, stderr) <- execAny execConfig bin arguments
case exitResult of
IO.ExitFailure exitCode -> H.failMessage GHC.callStack . L.unlines $
[ "Process exited with non-zero exit-code"
Expand All @@ -222,6 +220,21 @@ exec execConfig bin arguments = GHC.withFrozenCallStack $ do
]
IO.ExitSuccess -> return stdout

-- | Execute a process, returning the error code, the stdout, and the stderr.
execAny
:: (MonadTest m, MonadIO m, HasCallStack)
=> ExecConfig
-> String
-> [String]
-> m (ExitCode, String, String)
execAny execConfig bin arguments = GHC.withFrozenCallStack $ do
let cp = (IO.proc bin arguments)
{ IO.env = getLast $ execConfigEnv execConfig
, IO.cwd = getLast $ execConfigCwd execConfig
}
H.annotate . ("Command: " <>) $ bin <> " " <> L.unwords arguments
H.evalIO $ IO.readCreateProcessWithExitCode cp ""

-- | Wait for process to exit.
waitForProcess
:: (MonadTest m, MonadIO m, HasCallStack)
Expand Down

0 comments on commit 675a11d

Please sign in to comment.