Skip to content

feat(doubles): let bashunit::mock accept an exit code - #907

Merged
Chemaclass merged 1 commit into
mainfrom
feat/898-mock-exit-code
Jul 27, 2026
Merged

feat(doubles): let bashunit::mock accept an exit code#907
Chemaclass merged 1 commit into
mainfrom
feat/898-mock-exit-code

Conversation

@Chemaclass

Copy link
Copy Markdown
Member

🤔 Background

Related #898

bashunit::spy cmd 1 returns 1, but bashunit::mock had no equivalent: simulating a failing command needed a throwaway return 1 helper, or an inline body whose exit code was easy to get wrong.

💡 Changes

  • bashunit::mock cmd 1 now makes cmd exit 1 with no output; a numeric argument only counts when it is the single argument, so bashunit::mock cmd echo 1 still prints 1 and exits 0.
  • Extract the all-digits predicate the spy already used, so both doubles share one convention and one place where the interpolated value is proven numeric.
  • All existing forms (implementation, function, heredoc) are unchanged.
  • Documented in the test-doubles page and the agent skill.

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.

`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
@Chemaclass Chemaclass added the enhancement New feature or request label Jul 27, 2026
@Chemaclass Chemaclass self-assigned this Jul 27, 2026
@Chemaclass
Chemaclass merged commit 13a7fc5 into main Jul 27, 2026
37 checks passed
@Chemaclass
Chemaclass deleted the feat/898-mock-exit-code branch July 27, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant