forked from TypedDevs/bashunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_doubles_test.sh
More file actions
218 lines (163 loc) · 5.01 KB
/
test_doubles_test.sh
File metadata and controls
218 lines (163 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env bash
# shellcheck disable=SC2009
# shellcheck disable=SC2317
# shellcheck disable=SC2329
function tear_down() {
unset code
unset ps
}
function set_up() {
function code() {
ps a | grep apache
}
}
function test_successful_mock() {
bashunit::mock ps <<EOF
PID TTY TIME CMD
13525 pts/7 00:00:01 bash
24162 pts/7 00:00:00 ps
8387 ? 00:00:00 /usr/sbin/apache2 -k start
EOF
assert_empty "$(assert_successful_code "$(code)")"
}
function test_successful_override_ps_with_echo_with_mock() {
bashunit::mock ps echo hello world
assert_same "hello world" "$(ps)"
}
function test_successful_spy() {
bashunit::spy ps
ps a_random_parameter_1 a_random_parameter_2
assert_have_been_called_with ps "a_random_parameter_1 a_random_parameter_2"
assert_have_been_called ps
}
function test_unsuccessful_spy_called() {
bashunit::spy ps
assert_same \
"$(bashunit::console_results::print_failed_test "Unsuccessful spy called" "ps" "to have been called" "once")" \
"$(assert_have_been_called ps)"
}
function test_successful_spy_called_times() {
bashunit::spy ps
ps
ps
assert_have_been_called_times 2 ps
}
function test_unsuccessful_spy_called_times() {
bashunit::spy ps
ps
ps
assert_same "$(bashunit::console_results::print_failed_test "Unsuccessful spy called times" "ps" \
"to have been called" "1 times" \
"actual" "2 times")" \
"$(assert_have_been_called_times 1 ps)"
}
function test_successful_spy_with_source_function() {
# shellcheck source=/dev/null
source "$(bashunit::current_dir)/fixtures/fake_function_to_spy.sh"
bashunit::spy function_to_be_spied_on
function_to_be_spied_on
assert_have_been_called function_to_be_spied_on
}
function test_unsuccessful_spy_with_source_function_have_been_called() {
# shellcheck source=/dev/null
source "$(bashunit::current_dir)/fixtures/fake_function_to_spy.sh"
bashunit::spy function_to_be_spied_on
function_to_be_spied_on
function_to_be_spied_on
assert_same "$(bashunit::console_results::print_failed_test \
"Unsuccessful spy with source function have been called" \
"function_to_be_spied_on" \
"to have been called" "1 times" \
"actual" "2 times")" \
"$(assert_have_been_called_times 1 function_to_be_spied_on)"
}
function test_successful_spy_called_times_with_source() {
# shellcheck source=/dev/null
source "$(bashunit::current_dir)/fixtures/fake_function_to_spy.sh"
bashunit::spy function_to_be_spied_on
function_to_be_spied_on
function_to_be_spied_on
assert_have_been_called_times 2 function_to_be_spied_on
}
function test_spy_called_in_subshell() {
bashunit::spy spy_called_in_subshell
function run() {
spy_called_in_subshell "$1"
spy_called_in_subshell "$1"
echo "done"
}
local result
result="$(run "2025-05-23")"
assert_same "done" "$result"
assert_have_been_called spy_called_in_subshell
assert_have_been_called_times 2 spy_called_in_subshell
assert_have_been_called_with spy_called_in_subshell "2025-05-23"
}
function test_mock_called_in_subshell() {
bashunit::mock date echo "2024-05-01"
function run() {
date
}
local result
result="$(run)"
assert_same "2024-05-01" "$result"
}
function test_spy_called_with_different_arguments() {
bashunit::spy ps
ps first_a first_b
ps second
assert_have_been_called_with ps "first_a first_b" 1
assert_have_been_called_with ps "second" 2
}
function test_spy_successful_not_called() {
bashunit::spy ps
assert_not_called ps
}
function test_spy_unsuccessful_not_called() {
bashunit::spy ps
ps
assert_same \
"$(bashunit::console_results::print_failed_test "Spy unsuccessful not called" "ps" \
"to have been called" "0 times" \
"actual" "1 times")" \
"$(assert_not_called ps)"
}
function test_spy_with_pipe_in_arguments() {
# Skip when coverage is enabled because coverage uses grep internally,
# which interferes with spying on grep
if bashunit::env::is_coverage_enabled; then
bashunit::skip "Cannot spy on grep when coverage is enabled"
return
fi
bashunit::spy grep
grep -E 'foo|bar'
assert_have_been_called_with grep '-E foo|bar'
}
function test_successful_spy_nth_called_with() {
bashunit::spy ps
ps first_a first_b
ps second
ps third
assert_have_been_called_nth_with 1 ps "first_a first_b"
assert_have_been_called_nth_with 2 ps "second"
assert_have_been_called_nth_with 3 ps "third"
}
function test_unsuccessful_spy_nth_called_with() {
bashunit::spy ps
ps first
ps second
assert_same \
"$(bashunit::console_results::print_failed_test \
"Unsuccessful spy nth called with" \
"wrong" "but got " "first")" \
"$(assert_have_been_called_nth_with 1 ps "wrong")"
}
function test_unsuccessful_spy_nth_called_with_invalid_index() {
bashunit::spy ps
ps first
assert_same \
"$(bashunit::console_results::print_failed_test \
"Unsuccessful spy nth called with invalid index" \
"expected call" "at index 5 but" "only called 1 times")" \
"$(assert_have_been_called_nth_with 5 ps "first")"
}