Skip to content

Commit 720e5b9

Browse files
authored
feat(spy): support optional exit code or custom implementation in bashunit::spy (#615)
Co-authored-by: SauronBot <sauronbot@users.noreply.github.com>
1 parent 52368d3 commit 720e5b9

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
### Added
6+
- Allow `bashunit::spy` to accept an optional exit code (e.g. `bashunit::spy thing 1`) or custom implementation function (e.g. `bashunit::spy thing mock_thing`) (#600)
7+
58
### Fixed
69
- Fix spying on `echo` or `printf` causing bashunit to hang due to infinite recursion (#607)
710

src/test_doubles.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function bashunit::mock() {
4444

4545
function bashunit::spy() {
4646
local command=$1
47+
local exit_code_or_impl="${2:-}"
4748
local variable
4849
variable="$(bashunit::helper::normalize_variable_name "$command")"
4950

@@ -56,6 +57,13 @@ function bashunit::spy() {
5657
export "${variable}_times_file"="$times_file"
5758
export "${variable}_params_file"="$params_file"
5859

60+
local body_suffix=""
61+
if [[ "$exit_code_or_impl" =~ ^[0-9]+$ ]]; then
62+
body_suffix="return $exit_code_or_impl"
63+
elif [ -n "$exit_code_or_impl" ]; then
64+
body_suffix="$exit_code_or_impl \"\$@\""
65+
fi
66+
5967
eval "function $command() {
6068
local raw=\"\$*\"
6169
local serialized=\"\"
@@ -69,6 +77,7 @@ function bashunit::spy() {
6977
_c=\$(cat '$times_file' 2>/dev/null || builtin echo 0)
7078
_c=\$((_c+1))
7179
builtin echo \"\$_c\" > '$times_file'
80+
$body_suffix
7281
}"
7382

7483
export -f "${command?}"

tests/unit/test_doubles_test.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,54 @@ function test_unsuccessful_spy_nth_called_with_invalid_index() {
216216
}
217217

218218

219+
220+
function test_spy_with_exit_code_returns_specified_exit_code() {
221+
bashunit::spy ps 1
222+
223+
local actual_exit_code=0
224+
ps || actual_exit_code=$?
225+
226+
assert_have_been_called ps
227+
assert_same "1" "$actual_exit_code"
228+
}
229+
230+
function test_spy_with_exit_code_zero_returns_zero() {
231+
bashunit::spy ps 0
232+
233+
ps
234+
local actual_exit_code=$?
235+
236+
assert_have_been_called ps
237+
assert_same "0" "$actual_exit_code"
238+
}
239+
240+
function test_spy_with_impl_calls_custom_function() {
241+
custom_ps_impl() {
242+
builtin echo "custom output"
243+
}
244+
export -f custom_ps_impl
245+
246+
bashunit::spy ps custom_ps_impl
247+
248+
local output
249+
output=$(ps)
250+
251+
assert_have_been_called ps
252+
assert_same "custom output" "$output"
253+
}
254+
255+
function test_spy_with_impl_records_calls_and_delegates() {
256+
custom_ps_impl() {
257+
builtin echo "delegated"
258+
}
259+
export -f custom_ps_impl
260+
261+
bashunit::spy ps custom_ps_impl
262+
263+
ps first
264+
ps second
265+
266+
assert_have_been_called_times 2 ps
267+
assert_have_been_called_nth_with 1 ps "first"
268+
assert_have_been_called_nth_with 2 ps "second"
269+
}

0 commit comments

Comments
 (0)