Skip to content

Commit 92cacd6

Browse files
authored
Merge pull request #511 from carlfriedrich/feat/pass-args-to-mocked-functions
Pass arguments to mocked functions
2 parents c063e7b + 0b42fdb commit 92cacd6

6 files changed

Lines changed: 48 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Pass arguments to mocked functions
56
- Fix lifecycle hooks not catching failing commands (exit code errors)
67

78
## [0.26.0](https://github.com/TypedDevs/bashunit/compare/0.25.0...0.26.0) - 2025-11-02

docs/test-doubles.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ function test_example() {
7575
```
7676
:::
7777

78+
All arguments passed to the original call are forwarded to the mocked function, so you can mock different behavior depending on the arguments.
79+
80+
::: code-group
81+
```bash [Example]
82+
mockTool() {
83+
if [[ "$1" == "--version" ]]; then
84+
echo "1.2.3"
85+
return 0
86+
else
87+
echo "tool: '$1' is not a valid command."
88+
return 1
89+
fi
90+
}
91+
92+
test_example() {
93+
local output
94+
mock tool mockTool
95+
96+
output="$(tool --version)"
97+
assert_successful_code
98+
assert_same "1.2.3" "${output}"
99+
100+
output="$(tool foo)"
101+
assert_general_error
102+
assert_contains "is not a valid command" "${output}"
103+
}
104+
105+
```
106+
:::
107+
78108
## spy
79109
> `spy "function"`
80110

src/test_doubles.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function mock() {
2727
shift
2828

2929
if [[ $# -gt 0 ]]; then
30-
eval "function $command() { $* ; }"
30+
eval "function $command() { $* \"\$@\"; }"
3131
else
3232
eval "function $command() { echo \"$($CAT)\" ; }"
3333
fi

tests/acceptance/install_test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ function test_install_downloads_the_non_stable_beta_version() {
138138
skip "curl or wget not installed" && return
139139
fi
140140

141-
mock date echo "2023-11-13"
142-
mock tput echo ""
141+
mock date <<< "2023-11-13"
142+
mock tput <<< ""
143143
local installed_bashunit="./deps/bashunit"
144144
local output
145145

tests/unit/clock_test.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function mock_date_seconds() {
2424

2525
function test_now_with_perl() {
2626
mock clock::shell_time mock_non_existing_fn
27-
mock perl echo "1720705883457"
27+
mock perl <<< "1720705883457"
2828
mock dependencies::has_python mock_false
2929
mock dependencies::has_node mock_false
3030

@@ -35,7 +35,7 @@ function test_now_on_linux_unknown() {
3535
mock_unknown_linux_os
3636
mock clock::shell_time mock_non_existing_fn
3737
mock perl mock_non_existing_fn
38-
mock date echo "1720705883457"
38+
mock date <<< "1720705883457"
3939
mock dependencies::has_python mock_false
4040
mock dependencies::has_node mock_false
4141

@@ -45,7 +45,7 @@ function test_now_on_linux_unknown() {
4545
function test_now_on_linux_alpine() {
4646
mock_alpine_os
4747
mock clock::shell_time mock_non_existing_fn
48-
mock perl echo "1720705883457"
48+
mock perl <<< "1720705883457"
4949
mock dependencies::has_python mock_false
5050
mock dependencies::has_node mock_false
5151

@@ -56,7 +56,7 @@ function test_now_on_windows_without_with_powershell() {
5656
mock_windows_os
5757
mock dependencies::has_perl mock_false
5858
mock dependencies::has_powershell mock_true
59-
mock powershell echo "1727768183281580800"
59+
mock powershell <<< "1727768183281580800"
6060
mock clock::shell_time mock_non_existing_fn
6161
mock dependencies::has_python mock_false
6262
mock dependencies::has_node mock_false
@@ -68,7 +68,7 @@ function test_now_on_windows_without_without_powershell() {
6868
mock_windows_os
6969
mock dependencies::has_perl mock_false
7070
mock dependencies::has_powershell mock_false
71-
mock date echo "1727768951"
71+
mock date <<< "1727768951"
7272
mock clock::shell_time mock_non_existing_fn
7373
mock dependencies::has_python mock_false
7474
mock dependencies::has_node mock_false
@@ -94,24 +94,24 @@ function test_now_on_osx_without_perl() {
9494

9595
mock_macos
9696
mock dependencies::has_perl mock_false
97-
mock clock::shell_time echo "1727708708.326957"
97+
mock clock::shell_time <<< "1727708708.326957"
9898
mock dependencies::has_python mock_false
9999
mock dependencies::has_node mock_false
100100

101101
assert_same "1727708708326957000" "$(clock::now)"
102102
}
103103

104104
function test_runtime_in_milliseconds_when_not_empty_time() {
105-
mock perl echo "1720705883457"
105+
mock perl <<< "1720705883457"
106106
mock dependencies::has_python mock_false
107107
mock dependencies::has_node mock_false
108108

109109
assert_not_empty "$(clock::total_runtime_in_milliseconds)"
110110
}
111111

112112
function test_now_prefers_perl_over_shell_time() {
113-
mock clock::shell_time echo "1234.0"
114-
mock perl echo "999999999999"
113+
mock clock::shell_time <<< "1234.0"
114+
mock perl <<< "999999999999"
115115
mock dependencies::has_python mock_false
116116
mock dependencies::has_node mock_false
117117

@@ -121,9 +121,9 @@ function test_now_prefers_perl_over_shell_time() {
121121
function test_now_prefers_python_over_node() {
122122
mock perl mock_non_existing_fn
123123
mock dependencies::has_python mock_true
124-
mock python echo "777777777777"
124+
mock python <<< "777777777777"
125125
mock dependencies::has_node mock_true
126-
mock node echo "888888888888"
126+
mock node <<< "888888888888"
127127

128128
assert_same "777777777777" "$(clock::now)"
129129
}

tests/unit/console_results_test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ function test_render_execution_time_on_osx_with_perl() {
315315
mock dependencies::has_adjtimex mock_false
316316
mock dependencies::has_perl mock_true
317317
_START_TIME="1726393394574382186"
318-
mock perl echo "1726393394574372186"
319-
mock uname echo "Darwin"
318+
mock perl <<< "1726393394574372186"
319+
mock uname <<< "Darwin"
320320
render_result=$(
321-
mock perl echo "1726393394574372186";
321+
mock perl <<< "1726393394574372186";
322322
323323
console_results::render_result
324324
)

0 commit comments

Comments
 (0)