Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/bats/assertions
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ assert_line_matches() {
# `output` should contain blank lines, call `split_bats_output_into_lines` from
# `lib/bats/helpers` before this.
#
# If you expect zero lines, then don't supply any arguments.
#
# Arguments:
# $@: Values to compare to each element of `${lines[@]}` for equality
assert_lines_equal() {
Expand All @@ -381,6 +383,10 @@ assert_lines_match() {

# Validates that a file contains exactly the specified output
#
# NOTE: If the file doesn't end with a newline, the last line will not be
# present. To check that a file is completely empty, supply only the `file_path`
# argument.
#
# Arguments:
# file_path: Path to file to evaluate
# ...: Exact lines expected to appear in the file
Expand Down Expand Up @@ -477,6 +483,9 @@ return_from_bats_assertion() {
# regard to the rest (such as the first or last lines), or search for several
# regular expressions in no particular order, this function may help.
#
# NOTE: If the file doesn't end with a newline, the last line will not be
# present. If the file is completely empty, `lines` will contain zero elements.
#
# Arguments:
# file_path: Path to file from which `output` and `lines` will be filled
set_bats_output_and_lines_from_file() {
Expand Down
20 changes: 20 additions & 0 deletions lib/bats/helpers
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
# variables are quoted properly in most places.
BATS_TEST_ROOTDIR="$BATS_TMPDIR/test rootdir"

# Created by `stub_program_in_path` and exported to `PATH`
BATS_TEST_BINDIR="$BATS_TEST_ROOTDIR/bin"

# Sets the global SUITE variable based on the path of the test file.
#
# To make Bats output easier to follow, call this function from your shared
Expand Down Expand Up @@ -231,3 +234,20 @@ split_bats_output_into_lines() {
lines+=("${line%$'\r'}")
done <<<"$output"
}

# Creates a stub program in PATH for testing purposes
#
# The script is written as `$BATS_TEST_BINDIR/$cmd_name`. `$BATS_TEST_BINDIR` is
# added to `PATH` and exported if it isn't already present.
#
# Arguments:
# cmd_name: Name of the command from PATH to stub
# ...: Lines comprising the stub script
stub_program_in_path() {
local bindir_pattern="^${BATS_TEST_BINDIR}:"

if [[ ! "$PATH" =~ $bindir_pattern ]]; then
export PATH="$BATS_TEST_BINDIR:$PATH"
fi
create_bats_test_script "${BATS_TEST_BINDIR#$BATS_TEST_ROOTDIR/}/$1" "${@:2}"
}
34 changes: 34 additions & 0 deletions tests/assertions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ teardown() {
"assert_lines_equal 'foo' 'bar' 'baz'"
}

@test "$SUITE: assert_lines_equal zero lines" {
expect_assertion_success "printf ''" \
"assert_lines_equal"
}

@test "$SUITE: assert_lines_equal failure" {
expect_assertion_failure "printf 'foo\nbar\nbaz\n'" \
"assert_lines_equal 'foo' 'quux' 'baz'" \
Expand Down Expand Up @@ -387,6 +392,21 @@ teardown() {
assert_lines_equal '' 'foo' '' 'bar' '' 'baz' ''
}

@test "$SUITE: set_bats_output_and_lines_from_file from empty file" {
printf_to_test_output_file '\n'
set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"
assert_lines_equal ''
}

@test "$SUITE: set_bats_output_and_lines_from_file from completely empty file" {
printf_to_test_output_file ''
set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"

# Note that because there wasn't even a newline, we don't even expect the
# empty string to be present in `lines`.
assert_lines_equal
}

@test "$SUITE: set_bats_output_and_lines_from_file fails if file is missing" {
run set_bats_output_and_lines_from_file "$TEST_OUTPUT_FILE"
assert_failure "'$TEST_OUTPUT_FILE' doesn't exist or isn't a regular file."
Expand All @@ -412,6 +432,20 @@ teardown() {
"assert_file_equals '$TEST_OUTPUT_FILE' '' 'foo' '' 'bar' '' 'baz' ''"
}

@test "$SUITE: assert_file_equals expect file containing empty string only" {
expect_assertion_success \
"printf_to_test_output_file '\n'" \
"assert_file_equals '$TEST_OUTPUT_FILE' ''"
}

@test "$SUITE: assert_file_equals expect file completely empty file" {
# Note that because there wasn't even a newline, we don't even supply the
# empty string to `assert_file_equals`.
expect_assertion_success \
"printf_to_test_output_file ''" \
"assert_file_equals '$TEST_OUTPUT_FILE'"
}

@test "$SUITE: assert_file_equals failure" {
expect_assertion_failure \
"printf_to_test_output_file '\nfoo\n\nbar\n\nbaz\n\n'" \
Expand Down
11 changes: 11 additions & 0 deletions tests/bats-helpers.bats
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,14 @@ teardown() {
split_bats_output_into_lines
assert_lines_equal '' '' 'foo' '' 'bar' '' 'baz'
}

@test "$SUITE: stub_program_in_path" {
local bats_bindir_pattern="^${BATS_TEST_BINDIR}:"
fail_if matches "$bats_bindir_pattern" "$PATH"

stub_program_in_path 'git' 'echo "$@"'
assert_matches "$bats_bindir_pattern" "$PATH"

run git Hello, World!
assert_success 'Hello, World!'
}
45 changes: 15 additions & 30 deletions tests/changes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,21 @@ teardown() {
remove_test_go_rootdir
}

create_fake_git() {
local fake_git_path="$TEST_GO_ROOTDIR/bin/git"
local fake_git_impl=('#! /usr/bin/env bash' "$@")
local IFS=$'\n'

echo "${fake_git_impl[*]}" >"$fake_git_path"
chmod 700 "$fake_git_path"
}

@test "$SUITE: tab completions" {
local versions=('v1.0.0' 'v1.1.0')
local IFS=$'\n'
local fake_git_impl=(
"if [[ \"\$1\" == 'tag' ]]; then"
" echo '${versions[*]}'"
' exit 0'
'fi'

stub_program_in_path 'git' \
"if [[ \"\$1\" == 'tag' ]]; then" \
" echo '${versions[*]}'" \
' exit 0' \
'fi' \
'exit 1'
)

create_fake_git "${fake_git_impl[@]}"
run "$TEST_GO_ROOTDIR/bin/git" 'tag'
run git tag
assert_success "${versions[*]}"

local PATH="$TEST_GO_ROOTDIR/bin:$PATH"
run ./go complete 1 changes ''
local IFS=$'\n'
assert_success "${versions[*]}"

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

@test "$SUITE: git log call is well-formed" {
local IFS=$'\n'
local fake_git_impl=(
"if [[ \"\$1\" == 'log' ]]; then"
' shift'
" IFS=\$'\\n'"
' echo "$*"'
' exit 0'
'fi'
stub_program_in_path 'git' \
"if [[ \"\$1\" == 'log' ]]; then" \
' shift' \
" IFS=\$'\\n'" \
' echo "$*"' \
' exit 0' \
'fi' \
'exit 1'
)

create_fake_git "${fake_git_impl[@]}"
local PATH="$TEST_GO_ROOTDIR/bin:$PATH"
run ./go changes v1.0.0 v1.1.0
assert_success
assert_line_matches 0 '^--pretty=format:'
Expand Down
9 changes: 6 additions & 3 deletions tests/file/fds-printf.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ create_fds_printf_test_script() {
'output_fds="$output_fd"'
run "$TEST_GO_SCRIPT"
assert_success
assert_equal "$MESSAGE" "$(< "$file_path")"
assert_file_equals "$file_path" "$MESSAGE"
}

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

@test "$SUITE: error if non-file descriptor argument given" {
Expand Down Expand Up @@ -79,5 +79,8 @@ create_fds_printf_test_script() {
assert_line_equals 0 "$MESSAGE"
assert_line_matches '-2' "Failed to print to fd [1-9][0-9]* at:"
assert_line_matches '-1' " $TEST_GO_SCRIPT:10 main"
assert_equal '' "$(< "$file_path")"

# Note that no "expected" argument means we expect the file to be completely
# empty, with not even a newline.
assert_file_equals "$file_path"
}
16 changes: 5 additions & 11 deletions tests/file/open-or-dup.bats
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ create_file_open_test_go_script() {
'echo "Goodbye, World!" >&"$write_fd"'
run "$TEST_GO_SCRIPT"
assert_success
assert_equal "Goodbye, World!" "$(< "$FILE_PATH")"
assert_file_equals "$FILE_PATH" "Goodbye, World!"
}

@test "$SUITE: duplicate descriptor for writing" {
Expand All @@ -64,7 +64,7 @@ create_file_open_test_go_script() {
'echo "Goodbye, World!" >&"$dup_write_fd"'
run "$TEST_GO_SCRIPT"
assert_success
assert_equal "Goodbye, World!" "$(< "$FILE_PATH")"
assert_file_equals "$FILE_PATH" "Goodbye, World!"
}

@test "$SUITE: open file for appending" {
Expand All @@ -77,9 +77,7 @@ create_file_open_test_go_script() {
'echo "Goodbye, World!" >&"$append_fd"'
run "$TEST_GO_SCRIPT"
assert_success

local IFS=$'\n'
assert_equal "${expected[*]}" "$(< "$FILE_PATH")"
assert_file_equals "$FILE_PATH" "${expected[@]}"
}

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

run "$TEST_GO_SCRIPT"

local IFS=$'\n'
local orig_content=("$first_line"
"$second_line")
assert_success "${orig_content[*]}"
assert_success "$first_line" "$second_line"

local updated_content=('You say goodbye,'
'and I say hello!')
assert_equal "${updated_content[*]}" "$(< "$FILE_PATH")"
assert_file_equals "$FILE_PATH" "${updated_content[@]}"
}

@test "$SUITE: error if _GO_MAX_FILE_DESCRIPTORS is not an integer" {
Expand Down
Loading