feat(doubles): let bashunit::mock accept an exit code - #907
Merged
Conversation
`bashunit::spy cmd 1` returns 1, but `bashunit::mock` had no equivalent: making a mocked command fail meant a throwaway `return 1` helper, or an inline body that exits with the status of whatever it ran rather than the simulated one. Read a lone all-digits argument as an exit code, using the same predicate the spy uses — now extracted so both doubles share one convention and one place where the interpolated value is proven numeric. Every existing form is unchanged: a numeric argument only counts when it is the single argument, so `bashunit::mock cmd echo 1` still prints 1 and exits 0. Note the issue's "command named 42" case is not reachable: bash rejects an all-digits function name, so such a command cannot be mocked at all. The argument-position guard is covered by the multi-argument test instead. Closes #898
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤔 Background
Related #898
bashunit::spy cmd 1returns 1, butbashunit::mockhad no equivalent: simulating a failing command needed a throwawayreturn 1helper, or an inline body whose exit code was easy to get wrong.💡 Changes
bashunit::mock cmd 1now makescmdexit 1 with no output; a numeric argument only counts when it is the single argument, sobashunit::mock cmd echo 1still prints 1 and exits 0.The issue's "command named 42" case turned out to be unreachable — bash rejects an all-digits function name, so such a command cannot be mocked at all; the argument-position guard is covered by the multi-argument test instead.