Skip to content

Commit d25e5d3

Browse files
authored
Merge pull request #106 from mbland/stub-program-assert-file-#40
Add `stub_program_in_path` Bats helper, apply `assert_file_*` everywhere
2 parents e7c0ba3 + a2fbfe1 commit d25e5d3

File tree

12 files changed

+146
-128
lines changed

12 files changed

+146
-128
lines changed

lib/bats/assertions

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ assert_line_matches() {
357357
# `output` should contain blank lines, call `split_bats_output_into_lines` from
358358
# `lib/bats/helpers` before this.
359359
#
360+
# If you expect zero lines, then don't supply any arguments.
361+
#
360362
# Arguments:
361363
# $@: Values to compare to each element of `${lines[@]}` for equality
362364
assert_lines_equal() {
@@ -381,6 +383,10 @@ assert_lines_match() {
381383

382384
# Validates that a file contains exactly the specified output
383385
#
386+
# NOTE: If the file doesn't end with a newline, the last line will not be
387+
# present. To check that a file is completely empty, supply only the `file_path`
388+
# argument.
389+
#
384390
# Arguments:
385391
# file_path: Path to file to evaluate
386392
# ...: Exact lines expected to appear in the file
@@ -477,6 +483,9 @@ return_from_bats_assertion() {
477483
# regard to the rest (such as the first or last lines), or search for several
478484
# regular expressions in no particular order, this function may help.
479485
#
486+
# NOTE: If the file doesn't end with a newline, the last line will not be
487+
# present. If the file is completely empty, `lines` will contain zero elements.
488+
#
480489
# Arguments:
481490
# file_path: Path to file from which `output` and `lines` will be filled
482491
set_bats_output_and_lines_from_file() {

lib/bats/helpers

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
# variables are quoted properly in most places.
4848
BATS_TEST_ROOTDIR="$BATS_TMPDIR/test rootdir"
4949

50+
# Created by `stub_program_in_path` and exported to `PATH`
51+
BATS_TEST_BINDIR="$BATS_TEST_ROOTDIR/bin"
52+
5053
# Sets the global SUITE variable based on the path of the test file.
5154
#
5255
# To make Bats output easier to follow, call this function from your shared
@@ -231,3 +234,20 @@ split_bats_output_into_lines() {
231234
lines+=("${line%$'\r'}")
232235
done <<<"$output"
233236
}
237+
238+
# Creates a stub program in PATH for testing purposes
239+
#
240+
# The script is written as `$BATS_TEST_BINDIR/$cmd_name`. `$BATS_TEST_BINDIR` is
241+
# added to `PATH` and exported if it isn't already present.
242+
#
243+
# Arguments:
244+
# cmd_name: Name of the command from PATH to stub
245+
# ...: Lines comprising the stub script
246+
stub_program_in_path() {
247+
local bindir_pattern="^${BATS_TEST_BINDIR}:"
248+
249+
if [[ ! "$PATH" =~ $bindir_pattern ]]; then
250+
export PATH="$BATS_TEST_BINDIR:$PATH"
251+
fi
252+
create_bats_test_script "${BATS_TEST_BINDIR#$BATS_TEST_ROOTDIR/}/$1" "${@:2}"
253+
}

tests/assertions.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ teardown() {
267267
"assert_lines_equal 'foo' 'bar' 'baz'"
268268
}
269269

270+
@test "$SUITE: assert_lines_equal zero lines" {
271+
expect_assertion_success "printf ''" \
272+
"assert_lines_equal"
273+
}
274+
270275
@test "$SUITE: assert_lines_equal failure" {
271276
expect_assertion_failure "printf 'foo\nbar\nbaz\n'" \
272277
"assert_lines_equal 'foo' 'quux' 'baz'" \
@@ -387,6 +392,21 @@ teardown() {
387392
assert_lines_equal '' 'foo' '' 'bar' '' 'baz' ''
388393
}
389394

395+
@test "$SUITE: set_bats_output_and_lines_from_file from empty file" {
396+
printf_to_test_output_file '\n'
397+
set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"
398+
assert_lines_equal ''
399+
}
400+
401+
@test "$SUITE: set_bats_output_and_lines_from_file from completely empty file" {
402+
printf_to_test_output_file ''
403+
set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"
404+
405+
# Note that because there wasn't even a newline, we don't even expect the
406+
# empty string to be present in `lines`.
407+
assert_lines_equal
408+
}
409+
390410
@test "$SUITE: set_bats_output_and_lines_from_file fails if file is missing" {
391411
run set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"
392412
assert_failure "'$TEST_OUTPUT_FILE' doesn't exist or isn't a regular file."
@@ -412,6 +432,20 @@ teardown() {
412432
"assert_file_equals '$TEST_OUTPUT_FILE' '' 'foo' '' 'bar' '' 'baz' ''"
413433
}
414434

435+
@test "$SUITE: assert_file_equals expect file containing empty string only" {
436+
expect_assertion_success \
437+
"printf_to_test_output_file '\n'" \
438+
"assert_file_equals '$TEST_OUTPUT_FILE' ''"
439+
}
440+
441+
@test "$SUITE: assert_file_equals expect file completely empty file" {
442+
# Note that because there wasn't even a newline, we don't even supply the
443+
# empty string to `assert_file_equals`.
444+
expect_assertion_success \
445+
"printf_to_test_output_file ''" \
446+
"assert_file_equals '$TEST_OUTPUT_FILE'"
447+
}
448+
415449
@test "$SUITE: assert_file_equals failure" {
416450
expect_assertion_failure \
417451
"printf_to_test_output_file '\nfoo\n\nbar\n\nbaz\n\n'" \

tests/bats-helpers.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,14 @@ teardown() {
191191
split_bats_output_into_lines
192192
assert_lines_equal '' '' 'foo' '' 'bar' '' 'baz'
193193
}
194+
195+
@test "$SUITE: stub_program_in_path" {
196+
local bats_bindir_pattern="^${BATS_TEST_BINDIR}:"
197+
fail_if matches "$bats_bindir_pattern" "$PATH"
198+
199+
stub_program_in_path 'git' 'echo "$@"'
200+
assert_matches "$bats_bindir_pattern" "$PATH"
201+
202+
run git Hello, World!
203+
assert_success 'Hello, World!'
204+
}

tests/changes.bats

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,21 @@ teardown() {
1010
remove_test_go_rootdir
1111
}
1212

13-
create_fake_git() {
14-
local fake_git_path="$TEST_GO_ROOTDIR/bin/git"
15-
local fake_git_impl=('#! /usr/bin/env bash' "$@")
16-
local IFS=$'\n'
17-
18-
echo "${fake_git_impl[*]}" >"$fake_git_path"
19-
chmod 700 "$fake_git_path"
20-
}
21-
2213
@test "$SUITE: tab completions" {
2314
local versions=('v1.0.0' 'v1.1.0')
24-
local IFS=$'\n'
25-
local fake_git_impl=(
26-
"if [[ \"\$1\" == 'tag' ]]; then"
27-
" echo '${versions[*]}'"
28-
' exit 0'
29-
'fi'
15+
16+
stub_program_in_path 'git' \
17+
"if [[ \"\$1\" == 'tag' ]]; then" \
18+
" echo '${versions[*]}'" \
19+
' exit 0' \
20+
'fi' \
3021
'exit 1'
31-
)
3222

33-
create_fake_git "${fake_git_impl[@]}"
34-
run "$TEST_GO_ROOTDIR/bin/git" 'tag'
23+
run git tag
3524
assert_success "${versions[*]}"
3625

37-
local PATH="$TEST_GO_ROOTDIR/bin:$PATH"
3826
run ./go complete 1 changes ''
27+
local IFS=$'\n'
3928
assert_success "${versions[*]}"
4029

4130
run ./go complete 1 changes 'v1.0'
@@ -59,19 +48,15 @@ create_fake_git() {
5948
}
6049

6150
@test "$SUITE: git log call is well-formed" {
62-
local IFS=$'\n'
63-
local fake_git_impl=(
64-
"if [[ \"\$1\" == 'log' ]]; then"
65-
' shift'
66-
" IFS=\$'\\n'"
67-
' echo "$*"'
68-
' exit 0'
69-
'fi'
51+
stub_program_in_path 'git' \
52+
"if [[ \"\$1\" == 'log' ]]; then" \
53+
' shift' \
54+
" IFS=\$'\\n'" \
55+
' echo "$*"' \
56+
' exit 0' \
57+
'fi' \
7058
'exit 1'
71-
)
7259

73-
create_fake_git "${fake_git_impl[@]}"
74-
local PATH="$TEST_GO_ROOTDIR/bin:$PATH"
7560
run ./go changes v1.0.0 v1.1.0
7661
assert_success
7762
assert_line_matches 0 '^--pretty=format:'

tests/file/fds-printf.bats

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ create_fds_printf_test_script() {
3333
'output_fds="$output_fd"'
3434
run "$TEST_GO_SCRIPT"
3535
assert_success
36-
assert_equal "$MESSAGE" "$(< "$file_path")"
36+
assert_file_equals "$file_path" "$MESSAGE"
3737
}
3838

3939
@test "$SUITE: print to standard output and to a file" {
@@ -46,7 +46,7 @@ create_fds_printf_test_script() {
4646
'output_fds="1,$output_fd"'
4747
run "$TEST_GO_SCRIPT"
4848
assert_success "$MESSAGE"
49-
assert_equal "$MESSAGE" "$(< "$file_path")"
49+
assert_file_equals "$file_path" "$MESSAGE"
5050
}
5151

5252
@test "$SUITE: error if non-file descriptor argument given" {
@@ -79,5 +79,8 @@ create_fds_printf_test_script() {
7979
assert_line_equals 0 "$MESSAGE"
8080
assert_line_matches '-2' "Failed to print to fd [1-9][0-9]* at:"
8181
assert_line_matches '-1' " $TEST_GO_SCRIPT:10 main"
82-
assert_equal '' "$(< "$file_path")"
82+
83+
# Note that no "expected" argument means we expect the file to be completely
84+
# empty, with not even a newline.
85+
assert_file_equals "$file_path"
8386
}

tests/file/open-or-dup.bats

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ create_file_open_test_go_script() {
5252
'echo "Goodbye, World!" >&"$write_fd"'
5353
run "$TEST_GO_SCRIPT"
5454
assert_success
55-
assert_equal "Goodbye, World!" "$(< "$FILE_PATH")"
55+
assert_file_equals "$FILE_PATH" "Goodbye, World!"
5656
}
5757

5858
@test "$SUITE: duplicate descriptor for writing" {
@@ -64,7 +64,7 @@ create_file_open_test_go_script() {
6464
'echo "Goodbye, World!" >&"$dup_write_fd"'
6565
run "$TEST_GO_SCRIPT"
6666
assert_success
67-
assert_equal "Goodbye, World!" "$(< "$FILE_PATH")"
67+
assert_file_equals "$FILE_PATH" "Goodbye, World!"
6868
}
6969

7070
@test "$SUITE: open file for appending" {
@@ -77,9 +77,7 @@ create_file_open_test_go_script() {
7777
'echo "Goodbye, World!" >&"$append_fd"'
7878
run "$TEST_GO_SCRIPT"
7979
assert_success
80-
81-
local IFS=$'\n'
82-
assert_equal "${expected[*]}" "$(< "$FILE_PATH")"
80+
assert_file_equals "$FILE_PATH" "${expected[@]}"
8381
}
8482

8583
@test "$SUITE: open file for read and write" {
@@ -104,15 +102,11 @@ create_file_open_test_go_script() {
104102
'echo " hello!" >&"$read_write_fd"' \
105103

106104
run "$TEST_GO_SCRIPT"
107-
108-
local IFS=$'\n'
109-
local orig_content=("$first_line"
110-
"$second_line")
111-
assert_success "${orig_content[*]}"
105+
assert_success "$first_line" "$second_line"
112106

113107
local updated_content=('You say goodbye,'
114108
'and I say hello!')
115-
assert_equal "${updated_content[*]}" "$(< "$FILE_PATH")"
109+
assert_file_equals "$FILE_PATH" "${updated_content[@]}"
116110
}
117111

118112
@test "$SUITE: error if _GO_MAX_FILE_DESCRIPTORS is not an integer" {

0 commit comments

Comments
 (0)