Skip to content

feat(spy): support optional exit code or custom implementation in bashunit::spy#615

Merged
Chemaclass merged 3 commits intoTypedDevs:mainfrom
SauronBot:feat/spy-exit-code-and-impl
Mar 29, 2026
Merged

feat(spy): support optional exit code or custom implementation in bashunit::spy#615
Chemaclass merged 3 commits intoTypedDevs:mainfrom
SauronBot:feat/spy-exit-code-and-impl

Conversation

@SauronBot
Copy link
Copy Markdown
Contributor

Issue

Closes #600

Background

bashunit::spy always 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::spy to accept an optional second argument controlling the spy's behaviour, without changing any existing interface.

Changes

src/test_doubles.sh

  • bashunit::spy command — unchanged, no-op returning 0
  • bashunit::spy command <N> — spy returns exit code N (integer)
  • bashunit::spy command <fn> — spy records the call then delegates to fn "$@", forwarding arguments and its return value

tests/unit/test_doubles_test.sh

  • test_spy_with_exit_code_returns_specified_exit_code
  • test_spy_with_exit_code_zero_returns_zero
  • test_spy_with_impl_calls_custom_function
  • test_spy_with_impl_records_calls_and_delegates

…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 Chemaclass added the enhancement New feature or request label Mar 29, 2026
@Chemaclass Chemaclass changed the title feat(spy): support optional exit code or custom implementation in bashunit::spy (#600) feat(spy): support optional exit code or custom implementation in bashunit::spy Mar 29, 2026
@Chemaclass Chemaclass merged commit 720e5b9 into TypedDevs:main Mar 29, 2026
26 checks passed
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.

Spy exit code

2 participants