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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
#!/usr/bin/env bash
# shellcheck disable=2317 # Unreachable code
test_fail_fails() {
with_bash_unit_muted fail && \
(
echo "FAILURE: fail must fail !!!"
exit 1
) || \
echo "OK" > /dev/null
}
#fail can now be used in the following tests
test_assert_fails_succeeds() {
(assert_fails false) || fail 'assert_fails should succeed'
}
test_assert_fails_fails() {
# shellcheck disable=2015 # Note about A && B || C - C may run if A is true
with_bash_unit_muted assert_fails true && fail 'assert_fails should fail' || true
}
#assert_fails can now be used in the following tests
test_assert_succeeds() {
assert true || fail 'assert should succeed'
}
test_assert_fails() {
assert_fails "with_bash_unit_muted assert false" "assert should fail"
}
#assert can now be used in the following tests
test_assert_equals_fails_when_not_equal() {
assert_fails \
"with_bash_unit_muted assert_equals toto tutu" \
"assert_equals should fail"
}
test_assert_equals_succeed_when_equal() {
assert \
"assert_equals 'toto tata' 'toto tata'"\
'assert_equals should succeed'
}
#assert_equals can now be used in the following tests
test_assert_matches_fails_when_not_matching() {
assert_fails \
"with_bash_unit_muted assert_matches to.*to tutu" \
"assert_matches should fail"
}
test_assert_matches_succeed_when_matching() {
assert \
"assert_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'"\
'assert_matches should succeed'
}
test_assert_not_matches_fails_when_matching() {
assert_fails \
"with_bash_unit_muted assert_not_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'" \
"assert_not_matches should fail"
}
test_assert_not_matches_succeed_when_not_matching() {
assert \
"assert_not_matches 'toto' 'tata'"\
'assert_not_matches should succeed'
}
test_assert_not_equals_fails_when_equal() {
assert_fails \
"with_bash_unit_muted assert_not_equals toto toto" \
"assert_not_equals should fail"
}
test_assert_not_equals_succeeds_when_not_equal() {
assert \
"assert_not_equals 'toto tata' 'toto tutu'"\
'assert_not_equals should succeed'
}
test_assert_within_delta_succeeds() {
assert \
"assert_within_delta 12 10 3"\
'assert_within_delta should succeed'
assert \
"assert_within_delta 10 12 3"\
'assert_within_delta should succeed'
assert \
"assert_within_delta 10 12 2"\
'assert_within_delta should succeed'
assert \
"assert_within_delta 10 10 0"\
'assert_within_delta should succeed'
}
test_assert_within_delta_fails() {
assert_fails \
"assert_within_delta 13 10 2"\
'assert_within_delta should fail'
assert_fails \
"assert_within_delta 10 13 2"\
'assert_within_delta should fail'
assert_fails \
"assert_within_delta 11 10 0"\
'assert_within_delta should fail'
assert_fails \
"assert_within_delta eleven 10 0"\
'assert_within_delta should fail'
}
test_assert_no_diff_succeeds_when_no_diff() {
assert \
"assert_no_diff <(echo foo) <(echo foo)" \
"assert_no_diff should succeed"
}
test_assert_no_diff_fails_when_diff() {
assert_fails \
"assert_no_diff <(echo foo) <(echo bar)" \
"assert_no_diff should fail"
}
test_fail_prints_failure_message() {
message=$(with_bash_unit_log fail 'failure message' | line 2)
assert_equals 'failure message' "$message" \
"unexpected error message"
}
test_fail_prints_where_is_error() {
assert_equals "${BASH_SOURCE[0]}:${LINENO}:${FUNCNAME[0]}()" \
"$(with_bash_unit_stack fail | last_line)"
}
test_assert_status_code_succeeds() {
assert "assert_status_code 3 'exit 3'" \
"assert_status_code should succeed"
}
test_assert_status_code_fails() {
assert_fails "with_bash_unit_muted assert_status_code 3 true" \
"assert_status_code should fail"
}
test_assert_shows_stderr_on_failure() {
message="$(with_bash_unit_err \
assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2'
)"
assert_equals "\
some error message" \
"$message"
}
test_assert_shows_stdout_on_failure() {
message="$(with_bash_unit_out \
assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2'
)"
assert_equals "\
some ok message
another ok message" \
"$message"
}
test_fake_actually_fakes_the_command() {
fake ps echo expected
assert_equals "expected" "$(ps)"
}
test_fake_can_fake_inline() {
assert_equals \
"expected" \
"$(fake ps echo expected ; ps)"
}
test_fake_exports_faked_in_subshells() {
fake ps echo expected
assert_equals \
expected \
"$( bash -c ps )"
}
test_fake_transmits_params_to_fake_code() {
function _ps() {
assert_equals "aux" "$FAKE_PARAMS"
}
export -f _ps
fake ps _ps
ps aux
}
test_fake_transmits_params_to_fake_code_as_array() {
function _ps() {
assert_equals "1" "${#FAKE_PARAMS[@]}"
}
export -f _ps
fake ps _ps
ps "hello world"
}
test_fake_echo_stdin_when_no_params() {
fake ps << EOF
PID TTY TIME CMD
7801 pts/9 00:00:00 bash
7818 pts/9 00:00:00 ps
EOF
assert_equals 2 "$(ps | "$GREP" pts | wc -l| tr -d ' ')"
}
test_should_pretty_format_even_when_LANG_is_unset() {
# See https://github.com/pgrange/bash_unit/pull/81
unset LANG
assert "echo foo | pretty_format GREEN I"
}
if [[ "${STICK_TO_CWD:-}" != true ]]
then
# do not test for cwd if STICK_TO_CWD is true
test_bash_unit_changes_cwd_to_current_test_file_directory() {
assert "ls ../tests/$(basename "${BASH_SOURCE[0]}")" \
"bash_unit should change current working directory to match the directory of the currently running test"
}
#the following assertion is out of any test on purpose
assert "ls ../tests/$(basename "${BASH_SOURCE[0]}")" \
"bash_unit should change current working directory to match the directory of the currently running test before sourcing test file"
fi
setup() {
# enforce bash variable controls during core tests
# this way we know that people using this enforcement
# in their own code can still rely on bash_unit
set -u
# fake basic unix commands bash_unit relies on so that
# we ensure bash_unit keeps working when people fake
# this commands in their tests
fake cat :
fake sed :
fake grep :
fake printf :
}
line() {
line_nb=$1
tail -n +"$line_nb" | head -1
}
last_line() {
tail -1
}
with_bash_unit_muted() {
with_bash_unit_notifications_muted "$@"
}
with_bash_unit_err() {
with_bash_unit_notifications_muted -e "$@"
}
with_bash_unit_out() {
with_bash_unit_notifications_muted -o "$@"
}
with_bash_unit_log() {
with_bash_unit_notifications_muted -l "$@"
}
with_bash_unit_stack() {
with_bash_unit_notifications_muted -s "$@"
}
with_bash_unit_notifications_muted() {
(
mute
unset OPTIND
while getopts "lsoe" option
do
case "$option" in
l)
unmute_logs
;;
s)
unmute_stack
;;
o)
unmute_out
;;
e)
unmute_err
;;
*)
# Ignore invalid flags
;;
esac
done
shift $((OPTIND-1))
"$@"
)
}
unmute_logs() {
notify_suite_starting() { echo "Running tests in $1" ; }
notify_test_starting () { echo -e -n "\tRunning $1... " ; }
notify_test_succeeded() { echo "SUCCESS" ; }
notify_test_failed () { echo "FAILURE" ; }
notify_message () { echo "$1" ; }
}
unmute_stack() {
notify_stack() { "$CAT" ; }
}
unmute_out() {
notify_stdout() { "$CAT" ; }
}
unmute_err() {
notify_stderr() { "$CAT" ; }
}
mute() {
notify_suite_starting() { : ; }
notify_test_starting () { : ; }
notify_test_succeeded() { : ; }
notify_test_failed () { : ; }
notify_message () { : ; }
notify_stack () { $CAT >/dev/null ; }
notify_stdout () { : ; }
notify_stderr () { : ; }
notify_suites_succeded () { : ; }
notify_suites_failed () { : ; }
}
|