-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exec variant that allows a negative call #55
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
675a11d
Add a exec variant that doesn't fail upon receiving a non-zero error …
smelc 43a2598
exec*: shorten output (to enhance readibility)
smelc 03a8ed1
Add a execFlex variant that doesn't fail upon receiving a non-zero ex…
smelc d0b1bdc
New exec variants: add argument-level documentation
smelc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
module Hedgehog.Extras.Test.Process | ||
( createProcess | ||
, exec | ||
, execAny | ||
, exec_ | ||
, execFlex | ||
, execFlex' | ||
, execFlexAny' | ||
, procFlex | ||
, binFlex | ||
|
||
|
@@ -43,7 +45,7 @@ import Hedgehog (MonadTest) | |
import Hedgehog.Extras.Internal.Cli (argQuote) | ||
import Hedgehog.Extras.Internal.Plan (Component (..), Plan (..)) | ||
import Hedgehog.Extras.Stock.IO.Process (TimedOut (..)) | ||
import Prelude (error) | ||
import Prelude (error, (++)) | ||
import System.Exit (ExitCode) | ||
import System.FilePath (takeDirectory) | ||
import System.FilePath.Posix ((</>)) | ||
|
@@ -164,27 +166,36 @@ execFlex' | |
-> [String] | ||
-> m String | ||
execFlex' execConfig pkgBin envBin arguments = GHC.withFrozenCallStack $ do | ||
cp <- procFlex' execConfig pkgBin envBin arguments | ||
H.annotate . ("Command: " <>) $ case IO.cmdspec cp of | ||
IO.ShellCommand cmd -> cmd | ||
IO.RawCommand cmd args -> cmd <> " " <> L.unwords args | ||
(exitResult, stdout, stderr) <- H.evalIO $ IO.readCreateProcessWithExitCode cp "" | ||
(exitResult, stdout, stderr) <- execFlexAny' execConfig pkgBin envBin arguments | ||
case exitResult of | ||
IO.ExitFailure exitCode -> do | ||
H.annotate $ L.unlines $ | ||
[ "Process exited with non-zero exit-code" | ||
[ "Process exited with non-zero exit-code: " ++ show @Int exitCode | ||
, "━━━━ command ━━━━" | ||
, pkgBin <> " " <> L.unwords (fmap argQuote arguments) | ||
, "━━━━ stdout ━━━━" | ||
, stdout | ||
, "━━━━ stderr ━━━━" | ||
, stderr | ||
, "━━━━ exit code ━━━━" | ||
, show @Int exitCode | ||
] | ||
++ if L.null stdout then [] else ["━━━━ stdout ━━━━" , stdout] | ||
++ if L.null stderr then [] else ["━━━━ stderr ━━━━" , stderr] | ||
H.failMessage GHC.callStack "Execute process failed" | ||
IO.ExitSuccess -> return stdout | ||
|
||
-- | Run a process, returning its exit code, its stdout, and its stderr. | ||
-- Contrary to @execFlex'@, this function doesn't fail if the call fails. | ||
-- So, if you want to test something negative, this is the function to use. | ||
execFlexAny' | ||
:: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack) | ||
=> ExecConfig | ||
-> String | ||
-> String | ||
-> [String] | ||
-> m (ExitCode, String, String) | ||
execFlexAny' execConfig pkgBin envBin arguments = GHC.withFrozenCallStack $ do | ||
cp <- procFlex' execConfig pkgBin envBin arguments | ||
H.annotate . ("Command: " <>) $ case IO.cmdspec cp of | ||
IO.ShellCommand cmd -> cmd | ||
IO.RawCommand cmd args -> cmd <> " " <> L.unwords args | ||
H.evalIO $ IO.readCreateProcessWithExitCode cp "" | ||
|
||
-- | Execute a process, returning '()'. | ||
exec_ | ||
:: (MonadTest m, MonadIO m, HasCallStack) | ||
|
@@ -194,34 +205,42 @@ 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" | ||
[ "Process exited with non-zero exit-code: " ++ show @Int exitCode | ||
, "━━━━ command ━━━━" | ||
, bin <> " " <> L.unwords (fmap argQuote arguments) | ||
, "━━━━ stdout ━━━━" | ||
, stdout | ||
, "━━━━ stderr ━━━━" | ||
, stderr | ||
, "━━━━ exit code ━━━━" | ||
, show @Int exitCode | ||
] | ||
++ if L.null stdout then [] else ["━━━━ stdout ━━━━" , stdout] | ||
++ if L.null stderr then [] else ["━━━━ stderr ━━━━" , stderr] | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, a comment explaining There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment what's returned here? I guess stdout and stderr but in what order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍