From 5505c781ea9482d4151bdb067a54efb3abf548c5 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 12 Jan 2017 18:44:06 -0500 Subject: [PATCH 1/2] bats-helpers: Add stub_program_in_path Much easier and more convenient than having to hand-roll `PATH` program stubs for every new test suite. --- lib/bats/helpers | 20 ++++++++++++++++++ tests/bats-helpers.bats | 11 ++++++++++ tests/changes.bats | 45 ++++++++++++++-------------------------- tests/kcov.bats | 45 ++++++++++++++++------------------------ tests/log/timestamp.bats | 12 ++++------- tests/test.bats | 9 ++------ 6 files changed, 70 insertions(+), 72 deletions(-) diff --git a/lib/bats/helpers b/lib/bats/helpers index dd1e68c..382d59b 100644 --- a/lib/bats/helpers +++ b/lib/bats/helpers @@ -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 @@ -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}" +} diff --git a/tests/bats-helpers.bats b/tests/bats-helpers.bats index f126c35..201ac86 100644 --- a/tests/bats-helpers.bats +++ b/tests/bats-helpers.bats @@ -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!' +} diff --git a/tests/changes.bats b/tests/changes.bats index 381e978..932ce9f 100644 --- a/tests/changes.bats +++ b/tests/changes.bats @@ -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' @@ -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:' diff --git a/tests/kcov.bats b/tests/kcov.bats index f4c724d..7b8f229 100644 --- a/tests/kcov.bats +++ b/tests/kcov.bats @@ -2,7 +2,6 @@ load environment -FAKE_BIN_DIR="$TEST_GO_ROOTDIR/fake-bin" KCOV_DIR='tests/kcov' KCOV_COVERAGE_DIR='tests/coverage' KCOV_INCLUDE_PATTERNS='include/,pattern' @@ -33,13 +32,10 @@ setup() { 'dpkg-query') local fake_binary - mkdir -p "$FAKE_BIN_DIR" - - for fake_binary in "${fake_binaries[@]/#/$FAKE_BIN_DIR/}"; do - echo '#! /usr/bin/env bash' >"$fake_binary" - echo 'echo "$@" >"$0.out" 2>&1' >>"$fake_binary" + for fake_binary in "${fake_binaries[@]}"; do + stub_program_in_path "$fake_binary" \ + 'echo "$@" >"$0.out" 2>&1' done - chmod 700 "$FAKE_BIN_DIR"/* } teardown() { @@ -49,7 +45,6 @@ teardown() { write_kcov_go_script() { create_test_go_script \ ". \"\$_GO_USE_MODULES\" 'kcov-ubuntu'" \ - "PATH=\"$FAKE_BIN_DIR:\$PATH\"" \ "$@" } @@ -65,21 +60,21 @@ write_kcov_dummy() { run "$TEST_GO_SCRIPT" assert_success '' - run cat "$FAKE_BIN_DIR/dpkg-query.out" . 'lib/kcov-ubuntu' - assert_success "-W -f=\${Package} \${Status}\\n ${__KCOV_DEV_PACKAGES[*]}" + assert_equal "-W -f=\${Package} \${Status}\\n ${__KCOV_DEV_PACKAGES[*]}" \ + "$(<"$BATS_TEST_BINDIR/dpkg-query.out")" } @test "$SUITE: check dev packages fails on dpkg-query error" { write_kcov_go_script '__check_kcov_dev_packages_installed' - echo 'exit 1' >>"$FAKE_BIN_DIR/dpkg-query" + echo 'exit 1' >>"$BATS_TEST_BINDIR/dpkg-query" run "$TEST_GO_SCRIPT" assert_failure '' } @test "$SUITE: check dev packages fails if a package deinstalled" { write_kcov_go_script '__check_kcov_dev_packages_installed' - echo 'echo deinstall' >>"$FAKE_BIN_DIR/dpkg-query" + echo 'echo deinstall' >>"$BATS_TEST_BINDIR/dpkg-query" run "$TEST_GO_SCRIPT" assert_failure '' } @@ -87,7 +82,7 @@ write_kcov_dummy() { @test "$SUITE: clone and build" { write_kcov_go_script '__check_kcov_dev_packages_installed() { return 1; }' \ '__clone_and_build_kcov tests/kcov' - echo 'mkdir -p "$3"' >> "$FAKE_BIN_DIR/git" + echo 'mkdir -p "$3"' >> "$BATS_TEST_BINDIR/git" run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT" . 'lib/kcov-ubuntu' @@ -98,23 +93,19 @@ write_kcov_dummy() { local IFS=$'\n' assert_success "${expected_output[*]}" - run cat "$FAKE_BIN_DIR/git.out" - assert_success "clone $__KCOV_URL tests/kcov" + assert_equal "clone $__KCOV_URL tests/kcov" "$(<"$BATS_TEST_BINDIR/git.out")" - run cat "$FAKE_BIN_DIR/sudo.out" IFS=' ' - assert_success "apt-get install -y ${__KCOV_DEV_PACKAGES[*]}" - - run cat "$FAKE_BIN_DIR/cmake.out" - assert_success '.' + assert_equal "apt-get install -y ${__KCOV_DEV_PACKAGES[*]}" \ + "$(<"$BATS_TEST_BINDIR/sudo.out")" - run cat "$FAKE_BIN_DIR/make.out" - assert_success '' + assert_equal '.' "$(<"$BATS_TEST_BINDIR/cmake.out")" + assert_equal '' "$(<"$BATS_TEST_BINDIR/make.out")" } @test "$SUITE: clone and build fails if clone fails" { write_kcov_go_script '__clone_and_build_kcov tests/kcov' - echo 'exit 1' >> "$FAKE_BIN_DIR/git" + echo 'exit 1' >> "$BATS_TEST_BINDIR/git" run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT" . 'lib/kcov-ubuntu' @@ -127,8 +118,8 @@ write_kcov_dummy() { @test "$SUITE: clone and build fails if install fails" { write_kcov_go_script '__clone_and_build_kcov tests/kcov' - echo 'exit 1' >>"$FAKE_BIN_DIR/dpkg-query" - echo 'exit 1' >> "$FAKE_BIN_DIR/sudo" + echo 'exit 1' >>"$BATS_TEST_BINDIR/dpkg-query" + echo 'exit 1' >> "$BATS_TEST_BINDIR/sudo" mkdir -p "$TEST_GO_ROOTDIR/tests/kcov" run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT" @@ -143,7 +134,7 @@ write_kcov_dummy() { @test "$SUITE: clone and build fails if cmake fails" { write_kcov_go_script '__clone_and_build_kcov tests/kcov' mkdir -p "$TEST_GO_ROOTDIR/tests/kcov" - echo 'exit 1' >> "$FAKE_BIN_DIR/cmake" + echo 'exit 1' >> "$BATS_TEST_BINDIR/cmake" run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT" . 'lib/kcov-ubuntu' @@ -157,7 +148,7 @@ write_kcov_dummy() { @test "$SUITE: clone and build fails if make fails" { write_kcov_go_script '__clone_and_build_kcov tests/kcov' mkdir -p "$TEST_GO_ROOTDIR/tests/kcov" - echo 'exit 1' >> "$FAKE_BIN_DIR/make" + echo 'exit 1' >> "$BATS_TEST_BINDIR/make" run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT" . 'lib/kcov-ubuntu' diff --git a/tests/log/timestamp.bats b/tests/log/timestamp.bats index b803ed1..fba1eda 100644 --- a/tests/log/timestamp.bats +++ b/tests/log/timestamp.bats @@ -5,7 +5,6 @@ load ../environment HAS_TIMESTAMP_BUILTIN= DATE_CMD= DATE_CMD_FILE= -TEST_PATH= setup() { if printf '%(%Y)T' &>/dev/null; then @@ -14,9 +13,8 @@ setup() { DATE_CMD="$(command -v date)" DATE_CMD_FILE="$TEST_GO_ROOTDIR/date-cmd-called" - TEST_PATH="$TEST_GO_ROOTDIR/bin:$PATH" - create_bats_test_script "${TEST_GO_ROOTDIR#$BATS_TEST_ROOTDIR}/bin/date" \ + stub_program_in_path date \ "declare -r DATE_CMD='$DATE_CMD'" \ "touch '$DATE_CMD_FILE'" \ "if [[ -n '$DATE_CMD' ]]; then" \ @@ -39,7 +37,7 @@ teardown() { } @test "$SUITE: return error if _GO_LOG_TIMESTAMP_FORMAT not set" { - _GO_LOG_TIMESTAMP_FORMAT= PATH="$TEST_PATH" run "$BASH" "$TEST_GO_SCRIPT" + _GO_LOG_TIMESTAMP_FORMAT= run "$BASH" "$TEST_GO_SCRIPT" assert_failure if [[ -f "$DATE_CMD_FILE" ]]; then @@ -52,8 +50,7 @@ teardown() { skip "Builtin format not available in bash version $BASH_VERSION" fi - _GO_LOG_TIMESTAMP_FORMAT='%M:%S' PATH="$TEST_PATH" \ - run "$BASH" "$TEST_GO_SCRIPT" + _GO_LOG_TIMESTAMP_FORMAT='%M:%S' run "$BASH" "$TEST_GO_SCRIPT" assert_success assert_output_matches '^[0-5][0-9]:[0-5][0-9]$' @@ -69,8 +66,7 @@ teardown() { skip "`date` command not found in \$PATH: $PATH" fi - _GO_LOG_TIMESTAMP_FORMAT='%M:%S' PATH="$TEST_PATH" \ - run "$BASH" "$TEST_GO_SCRIPT" + _GO_LOG_TIMESTAMP_FORMAT='%M:%S' run "$BASH" "$TEST_GO_SCRIPT" assert_success assert_output_matches '^[0-5][0-9]:[0-5][0-9]$' diff --git a/tests/test.bats b/tests/test.bats index f7e466c..15620ca 100644 --- a/tests/test.bats +++ b/tests/test.bats @@ -83,14 +83,9 @@ _trim_expected() { } @test "$SUITE: update bats submodule if not present" { - mkdir -p "$TEST_GO_SCRIPTS_DIR/bin" - - local git_dummy="$TEST_GO_SCRIPTS_DIR/bin/git" - printf '#! /usr/bin/env bash\necho "GIT ARGV: $*"\n' >"$git_dummy" - chmod 700 "$git_dummy" - + create_test_go_script '@go "$@"' cp "$_GO_ROOTDIR/scripts/test" "$TEST_GO_SCRIPTS_DIR" - create_test_go_script "PATH=\"$TEST_GO_SCRIPTS_DIR/bin:\$PATH\"; @go \"\$@\"" + stub_program_in_path 'git' 'echo "GIT ARGV: $*"' # This will fail because we didn't create the tests/ directory, but git should # have been called correctly. From a2fbfe19feddf8c919ad6ebdaec3f36a8494e073 Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Thu, 12 Jan 2017 22:11:08 -0500 Subject: [PATCH 2/2] tests: Apply `assert_file_*` everywhere Also documents how to check for completely empty output or files, both in assertion comments and with new test cases in `tests/assertions.bats`. --- lib/bats/assertions | 9 +++++++ tests/assertions.bats | 34 ++++++++++++++++++++++++ tests/file/fds-printf.bats | 9 ++++--- tests/file/open-or-dup.bats | 16 ++++-------- tests/kcov.bats | 15 +++++------ tests/log/add-output-file.bats | 48 ++++++++++++---------------------- tests/log/add-update.bats | 15 +++++------ 7 files changed, 83 insertions(+), 63 deletions(-) diff --git a/lib/bats/assertions b/lib/bats/assertions index a9df1c4..dbfbd3e 100644 --- a/lib/bats/assertions +++ b/lib/bats/assertions @@ -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() { @@ -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 @@ -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() { diff --git a/tests/assertions.bats b/tests/assertions.bats index 0ca00a2..6e44e71 100644 --- a/tests/assertions.bats +++ b/tests/assertions.bats @@ -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'" \ @@ -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." @@ -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'" \ diff --git a/tests/file/fds-printf.bats b/tests/file/fds-printf.bats index 7912094..1235fd9 100644 --- a/tests/file/fds-printf.bats +++ b/tests/file/fds-printf.bats @@ -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" { @@ -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" { @@ -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" } diff --git a/tests/file/open-or-dup.bats b/tests/file/open-or-dup.bats index e01fc45..cb73783 100644 --- a/tests/file/open-or-dup.bats +++ b/tests/file/open-or-dup.bats @@ -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" { @@ -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" { @@ -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" { @@ -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" { diff --git a/tests/kcov.bats b/tests/kcov.bats index 7b8f229..e73175b 100644 --- a/tests/kcov.bats +++ b/tests/kcov.bats @@ -61,8 +61,8 @@ write_kcov_dummy() { assert_success '' . 'lib/kcov-ubuntu' - assert_equal "-W -f=\${Package} \${Status}\\n ${__KCOV_DEV_PACKAGES[*]}" \ - "$(<"$BATS_TEST_BINDIR/dpkg-query.out")" + assert_file_equals "$BATS_TEST_BINDIR/dpkg-query.out" \ + "-W -f=\${Package} \${Status}\\n ${__KCOV_DEV_PACKAGES[*]}" } @test "$SUITE: check dev packages fails on dpkg-query error" { @@ -93,14 +93,13 @@ write_kcov_dummy() { local IFS=$'\n' assert_success "${expected_output[*]}" - assert_equal "clone $__KCOV_URL tests/kcov" "$(<"$BATS_TEST_BINDIR/git.out")" + assert_file_equals "$BATS_TEST_BINDIR/git.out" "clone $__KCOV_URL tests/kcov" IFS=' ' - assert_equal "apt-get install -y ${__KCOV_DEV_PACKAGES[*]}" \ - "$(<"$BATS_TEST_BINDIR/sudo.out")" - - assert_equal '.' "$(<"$BATS_TEST_BINDIR/cmake.out")" - assert_equal '' "$(<"$BATS_TEST_BINDIR/make.out")" + assert_file_equals "$BATS_TEST_BINDIR/sudo.out" \ + "apt-get install -y ${__KCOV_DEV_PACKAGES[*]}" + assert_file_equals "$BATS_TEST_BINDIR/cmake.out" '.' + assert_file_equals "$BATS_TEST_BINDIR/make.out" '' } @test "$SUITE: clone and build fails if clone fails" { diff --git a/tests/log/add-output-file.bats b/tests/log/add-output-file.bats index 929a530..f0155ab 100644 --- a/tests/log/add-output-file.bats +++ b/tests/log/add-output-file.bats @@ -52,13 +52,13 @@ run_log_script_and_assert_status_and_output() { @test "$SUITE: add an output file for all log levels" { run_log_script_and_assert_status_and_output \ "@go.log_add_output_file '$TEST_GO_ROOTDIR/all.log'" - assert_equal "$output" "$(< "$TEST_GO_ROOTDIR/all.log")" 'all.log' + assert_file_equals "$TEST_GO_ROOTDIR/all.log" "${lines[@]}" } @test "$SUITE: add an output file for an existing log level" { run_log_script_and_assert_status_and_output \ "@go.log_add_output_file '$TEST_GO_ROOTDIR/info.log' 'INFO'" - assert_matches "^INFO +FYI$" "$(< "$TEST_GO_ROOTDIR/info.log")" 'info.log' + assert_file_matches "$TEST_GO_ROOTDIR/info.log" "^INFO +FYI$" } @test "$SUITE: force formatted output in log file" { @@ -67,7 +67,7 @@ run_log_script_and_assert_status_and_output() { "@go.log INFO Hello, World!" assert_success assert_log_equals "$(format_label INFO)" 'Hello, World!' - assert_equal "$output" "$(< "$TEST_GO_ROOTDIR/info.log")" 'info.log' + assert_file_equals "$TEST_GO_ROOTDIR/info.log" "${lines[@]}" } @test "$SUITE: add an output file for a new log level" { @@ -82,25 +82,17 @@ run_log_script_and_assert_status_and_output() { "@go.log_add_output_file '$TEST_GO_ROOTDIR/foobar.log' 'FOOBAR'" \ "@go.log FOOBAR \"$msg\"" - assert_matches "^FOOBAR +$msg$" \ - "$(< "$TEST_GO_ROOTDIR/foobar.log")" 'foobar.log' + assert_file_matches "$TEST_GO_ROOTDIR/foobar.log" "^FOOBAR +$msg$" } @test "$SUITE: add output files for multiple log levels" { run_log_script_and_assert_status_and_output \ "@go.log_add_output_file '$TEST_GO_ROOTDIR/error.log' 'ERROR,FATAL'" - local error_log=() - local item - while IFS= read -r item; do - error_log+=("$item") - done <<<"$(< "$TEST_GO_ROOTDIR/error.log")" - - assert_equal '3' "${#error_log[@]}" 'Number of error log lines' - assert_matches '^ERROR +uh-oh$' "${error_log[0]}" 'ERROR log message' - assert_matches '^FATAL +oh noes!$' "${error_log[1]}" 'FATAL log message' - assert_equal "$(stack_trace_item_from_offset "$TEST_GO_SCRIPT")" \ - "${error_log[2]}" 'FATAL stack trace' + assert_file_lines_match "$TEST_GO_ROOTDIR/error.log" \ + '^ERROR +uh-oh$' \ + '^FATAL +oh noes!$' \ + "^$(stack_trace_item_from_offset "$TEST_GO_SCRIPT")\$" } @test "$SUITE: add output files for a mix of levels" { @@ -114,20 +106,12 @@ run_log_script_and_assert_status_and_output() { "@go.log_add_output_file '$TEST_GO_ROOTDIR/foobar.log' 'FOOBAR'" \ "@go.log FOOBAR \"$msg\"" - assert_equal "$output" "$(< "$TEST_GO_ROOTDIR/all.log")" 'all.log' - assert_matches "^INFO +FYI$" "$(< "$TEST_GO_ROOTDIR/info.log")" 'info.log' - assert_matches "^FOOBAR +$msg$" \ - "$(< "$TEST_GO_ROOTDIR/foobar.log")" 'foobar.log' - - local error_log=() - local item - while IFS= read -r item; do - error_log+=("$item") - done <<<"$(< "$TEST_GO_ROOTDIR/error.log")" - - assert_equal '3' "${#error_log[@]}" 'Number of error log lines' - assert_matches '^ERROR +uh-oh$' "${error_log[0]}" 'ERROR log message' - assert_matches '^FATAL +oh noes!$' "${error_log[1]}" 'FATAL log message' - assert_equal "$(stack_trace_item_from_offset "$TEST_GO_SCRIPT")" \ - "${error_log[2]}" 'FATAL stack trace' + assert_file_equals "$TEST_GO_ROOTDIR/all.log" "${lines[@]}" + assert_file_matches "$TEST_GO_ROOTDIR/info.log" "^INFO +FYI$" + assert_file_matches "$TEST_GO_ROOTDIR/foobar.log" "^FOOBAR +$msg$" + + assert_file_lines_match "$TEST_GO_ROOTDIR/error.log" \ + '^ERROR +uh-oh$' \ + '^FATAL +oh noes!$' \ + "^$(stack_trace_item_from_offset "$TEST_GO_SCRIPT")\$" } diff --git a/tests/log/add-update.bats b/tests/log/add-update.bats index 6917050..e3268dc 100644 --- a/tests/log/add-update.bats +++ b/tests/log/add-update.bats @@ -54,9 +54,8 @@ teardown() { "@go.add_or_update_log_level FOOBAR '$INFO_FORMAT' 27" \ '@go.log FOOBAR Hello, World!' assert_success '' - - local expected="$(printf "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m\n")" - assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output' + assert_file_equals "$TEST_GO_ROOTDIR/logfile" \ + "$(printf "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m\n")" } @test "$SUITE: add new log level defaulting to standard output" { @@ -79,9 +78,8 @@ teardown() { '@go.add_or_update_log_level INFO keep 27' \ '@go.log INFO Hello, World!' assert_success '' - - local expected="$(printf "${INFO_FORMAT}INFO\e[0m Hello, World!\e[0m\n")" - assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output' + assert_file_equals "$TEST_GO_ROOTDIR/logfile" \ + "$(printf "${INFO_FORMAT}INFO\e[0m Hello, World!\e[0m\n")" } @test "$SUITE: update format and file descriptor of existing log level" { @@ -90,7 +88,6 @@ teardown() { "@go.add_or_update_log_level INFO '$START_FORMAT' 27" \ '@go.log INFO Hello, World!' assert_success '' - - local expected="$(printf "${START_FORMAT}INFO\e[0m Hello, World!\e[0m\n")" - assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output' + assert_file_equals "$TEST_GO_ROOTDIR/logfile" \ + "$(printf "${START_FORMAT}INFO\e[0m Hello, World!\e[0m\n")" }