feat(spy): support optional exit code or custom implementation in bashunit::spy#615
Merged
Chemaclass merged 3 commits intoTypedDevs:mainfrom Mar 29, 2026
Merged
Conversation
…hunit::spy Closes TypedDevs#600 bashunit::spy now accepts an optional second argument: - Integer: the spy returns that exit code (e.g. `bashunit::spy thing 1`) - Function name: the spy delegates to that implementation after recording the call (e.g. `bashunit::spy thing mock_thing`) Default behaviour (no second argument) is unchanged: the spy is a no-op returning exit code 0.
…e test Using 'ps || actual_exit_code=$?' prevents set -e from aborting the test when the spy returns a non-zero exit code.
Chemaclass
approved these changes
Mar 29, 2026
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.
Issue
Closes #600
Background
bashunit::spyalways created a no-op that returns exit code 0, making it impossible to test code branches that depend on a command's exit code — or to capture real output while still recording calls.Goal
Allow
bashunit::spyto accept an optional second argument controlling the spy's behaviour, without changing any existing interface.Changes
src/test_doubles.shbashunit::spy command— unchanged, no-op returning 0bashunit::spy command <N>— spy returns exit code N (integer)bashunit::spy command <fn>— spy records the call then delegates tofn "$@", forwarding arguments and its return valuetests/unit/test_doubles_test.shtest_spy_with_exit_code_returns_specified_exit_codetest_spy_with_exit_code_zero_returns_zerotest_spy_with_impl_calls_custom_functiontest_spy_with_impl_records_calls_and_delegates