#!/usr/bin/env bats load test_helper fixtures bats @test "no arguments prints message and usage instructions" { run bats [ $status -eq 1 ] [ "${lines[0]}" == 'Error: Must specify at least one ' ] [ "${lines[1]%% *}" == 'Usage:' ] } @test "invalid option prints message and usage instructions" { run bats --invalid-option [ $status -eq 1 ] [ "${lines[0]}" == "Error: Bad command line option '--invalid-option'" ] [ "${lines[1]%% *}" == 'Usage:' ] } @test "-v and --version print version number" { run bats -v [ $status -eq 0 ] [ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ] } @test "-h and --help print help" { run bats -h [ $status -eq 0 ] [ "${#lines[@]}" -gt 3 ] } @test "invalid filename prints an error" { run bats nonexistent [ $status -eq 1 ] [ $(expr "$output" : ".*does not exist") -ne 0 ] } @test "empty test file runs zero tests" { run bats "$FIXTURE_ROOT/empty.bats" [ $status -eq 0 ] [ "$output" = "1..0" ] } @test "one passing test" { run bats "$FIXTURE_ROOT/passing.bats" [ $status -eq 0 ] [ "${lines[0]}" = "1..1" ] [ "${lines[1]}" = "ok 1 a passing test" ] } @test "summary passing tests" { run filter_control_sequences bats -p "$FIXTURE_ROOT/passing.bats" [ $status -eq 0 ] [ "${lines[1]}" = "1 test, 0 failures" ] } @test "summary passing and skipping tests" { run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_and_skipping.bats" [ $status -eq 0 ] [ "${lines[3]}" = "3 tests, 0 failures, 2 skipped" ] } @test "tap passing and skipping tests" { run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_and_skipping.bats" [ $status -eq 0 ] [ "${lines[0]}" = "1..3" ] [ "${lines[1]}" = "ok 1 a passing test" ] [ "${lines[2]}" = "ok 2 a skipped test with no reason # skip" ] [ "${lines[3]}" = "ok 3 a skipped test with a reason # skip for a really good reason" ] } @test "summary passing and failing tests" { run filter_control_sequences bats -p "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 0 ] [ "${lines[4]}" = "2 tests, 1 failure" ] } @test "summary passing, failing and skipping tests" { run filter_control_sequences bats -p "$FIXTURE_ROOT/passing_failing_and_skipping.bats" [ $status -eq 0 ] [ "${lines[5]}" = "3 tests, 1 failure, 1 skipped" ] } @test "tap passing, failing and skipping tests" { run filter_control_sequences bats --tap "$FIXTURE_ROOT/passing_failing_and_skipping.bats" [ $status -eq 0 ] [ "${lines[0]}" = "1..3" ] [ "${lines[1]}" = "ok 1 a passing test" ] [ "${lines[2]}" = "ok 2 a skipping test # skip" ] [ "${lines[3]}" = "not ok 3 a failing test" ] } @test "BATS_CWD is correctly set to PWD as validated by bats_trim_filename" { local trimmed bats_trim_filename "$PWD/foo/bar" 'trimmed' printf 'ACTUAL: %s\n' "$trimmed" >&2 [ "$trimmed" = 'foo/bar' ] } @test "one failing test" { run bats "$FIXTURE_ROOT/failing.bats" [ $status -eq 1 ] [ "${lines[0]}" = '1..1' ] [ "${lines[1]}" = 'not ok 1 a failing test' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing.bats, line 4)" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] } @test "one failing and one passing test" { run bats "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 1 ] [ "${lines[0]}" = '1..2' ] [ "${lines[1]}" = 'not ok 1 a failing test' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] [ "${lines[3]}" = "# \`false' failed" ] [ "${lines[4]}" = 'ok 2 a passing test' ] } @test "failing test with significant status" { STATUS=2 run bats "$FIXTURE_ROOT/failing.bats" [ $status -eq 1 ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ] } @test "failing helper function logs the test case's line number" { run bats "$FIXTURE_ROOT/failing_helper.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 failing helper function' ] [ "${lines[2]}" = "# (from function \`failing_helper' in file $RELATIVE_FIXTURE_ROOT/test_helper.bash, line 6," ] [ "${lines[3]}" = "# in test file $RELATIVE_FIXTURE_ROOT/failing_helper.bats, line 5)" ] [ "${lines[4]}" = "# \`failing_helper' failed" ] } @test "test environments are isolated" { run bats "$FIXTURE_ROOT/environment.bats" [ $status -eq 0 ] } @test "setup is run once before each test" { make_bats_test_suite_tmpdir run bats "$FIXTURE_ROOT/setup.bats" [ $status -eq 0 ] run cat "$BATS_TEST_SUITE_TMPDIR/setup.log" [ ${#lines[@]} -eq 3 ] } @test "teardown is run once after each test, even if it fails" { make_bats_test_suite_tmpdir run bats "$FIXTURE_ROOT/teardown.bats" [ $status -eq 1 ] run cat "$BATS_TEST_SUITE_TMPDIR/teardown.log" [ ${#lines[@]} -eq 3 ] } @test "setup failure" { run bats "$FIXTURE_ROOT/failing_setup.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 truth' ] [ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/failing_setup.bats, line 2)" ] [ "${lines[3]}" = "# \`false' failed" ] } @test "passing test with teardown failure" { PASS=1 run bats "$FIXTURE_ROOT/failing_teardown.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 truth' ] [ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 2)" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] } @test "failing test with teardown failure" { PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 truth' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 6)" ] [ "${lines[3]}" = $'# `[ "$PASS" = 1 ]\' failed' ] } @test "teardown failure with significant status" { PASS=1 STATUS=2 run bats "$FIXTURE_ROOT/failing_teardown.bats" [ $status -eq 1 ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ] } @test "failing test file outside of BATS_CWD" { make_bats_test_suite_tmpdir cd "$BATS_TEST_SUITE_TMPDIR" run bats "$FIXTURE_ROOT/failing.bats" [ $status -eq 1 ] [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ] } @test "load sources scripts relative to the current test file" { run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 0 ] } @test "load aborts if the specified script does not exist" { HELPER_NAME="nonexistent" run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 1 ] } @test "load sources scripts by absolute path" { HELPER_NAME="${FIXTURE_ROOT}/test_helper.bash" run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 0 ] } @test "load aborts if the script, specified by an absolute path, does not exist" { HELPER_NAME="${FIXTURE_ROOT}/nonexistent" run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 1 ] } @test "output is discarded for passing tests and printed for failing tests" { run bats "$FIXTURE_ROOT/output.bats" [ $status -eq 1 ] [ "${lines[6]}" = '# failure stdout 1' ] [ "${lines[7]}" = '# failure stdout 2' ] [ "${lines[11]}" = '# failure stderr' ] } @test "-c prints the number of tests" { run bats -c "$FIXTURE_ROOT/empty.bats" [ $status -eq 0 ] [ "$output" = 0 ] run bats -c "$FIXTURE_ROOT/output.bats" [ $status -eq 0 ] [ "$output" = 4 ] } @test "dash-e is not mangled on beginning of line" { run bats "$FIXTURE_ROOT/intact.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 dash-e on beginning of line" ] } @test "dos line endings are stripped before testing" { run bats "$FIXTURE_ROOT/dos_line.bats" [ $status -eq 0 ] } @test "test file without trailing newline" { run bats "$FIXTURE_ROOT/without_trailing_newline.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 truth" ] } @test "skipped tests" { run bats "$FIXTURE_ROOT/skipped.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 a skipped test # skip" ] [ "${lines[2]}" = "ok 2 a skipped test with a reason # skip a reason" ] } @test "skipped test with parens (pretty formatter)" { run bats --pretty "$FIXTURE_ROOT/skipped_with_parens.bats" [ $status -eq 0 ] # Some systems (Alpine, for example) seem to emit an extra whitespace into # entries in the 'lines' array when a carriage return is present from the # pretty formatter. This is why a '+' is used after the 'skipped' note. [[ "${lines[@]}" =~ "- a skipped test with parentheses in the reason (skipped: "+"a reason (with parentheses))" ]] } @test "extended syntax" { run bats-exec-test -x "$FIXTURE_ROOT/failing_and_passing.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'begin 1 a failing test' ] [ "${lines[2]}" = 'not ok 1 a failing test' ] [ "${lines[5]}" = 'begin 2 a passing test' ] [ "${lines[6]}" = 'ok 2 a passing test' ] } @test "pretty and tap formats" { run bats --tap "$FIXTURE_ROOT/passing.bats" tap_output="$output" [ $status -eq 0 ] run bats --pretty "$FIXTURE_ROOT/passing.bats" pretty_output="$output" [ $status -eq 0 ] [ "$tap_output" != "$pretty_output" ] } @test "pretty formatter bails on invalid tap" { run bats --tap "$FIXTURE_ROOT/invalid_tap.bats" [ $status -eq 1 ] [ "${lines[0]}" = "This isn't TAP!" ] [ "${lines[1]}" = "Good day to you" ] } @test "single-line tests" { run bats "$FIXTURE_ROOT/single_line.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'ok 1 empty' ] [ "${lines[2]}" = 'ok 2 passing' ] [ "${lines[3]}" = 'ok 3 input redirection' ] [ "${lines[4]}" = 'not ok 4 failing' ] [ "${lines[5]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/single_line.bats, line 9)" ] [ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ] } @test "testing IFS not modified by run" { run bats "$FIXTURE_ROOT/loop_keep_IFS.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 loop_func" ] } @test "expand variables in test name" { SUITE='test/suite' run bats "$FIXTURE_ROOT/expand_var_in_test_name.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 test/suite: test with variable in name" ] } @test "handle quoted and unquoted test names" { run bats "$FIXTURE_ROOT/quoted_and_unquoted_test_names.bats" [ $status -eq 0 ] [ "${lines[1]}" = "ok 1 single-quoted name" ] [ "${lines[2]}" = "ok 2 double-quoted name" ] [ "${lines[3]}" = "ok 3 unquoted name" ] } @test 'ensure compatibility with unofficial Bash strict mode' { local expected='ok 1 unofficial Bash strict mode conditions met' # Run Bats under `set -u` to catch as many unset variable accesses as # possible. run bash -u "${BATS_TEST_DIRNAME%/*}/bin/bats" \ "$FIXTURE_ROOT/unofficial_bash_strict_mode.bats" if [[ "$status" -ne 0 || "${lines[1]}" != "$expected" ]]; then cat <&2 printf 'actual: "%s"\n' "${lines[0]}" >&2 [ "${lines[0]}" = "$expected" ] printf 'num lines: %d\n' "${#lines[*]}" >&2 [ "${#lines[*]}" = "7" ] } @test "sourcing a nonexistent file in setup produces error output" { run bats "$FIXTURE_ROOT/source_nonexistent_file_in_setup.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails in setup' ] [ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file_in_setup.bats, line 2)" ] [ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ] } @test "referencing unset parameter in setup produces error output" { run bats "$FIXTURE_ROOT/reference_unset_parameter_in_setup.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 referencing unset parameter fails in setup' ] [ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter_in_setup.bats, line 3)" ] [ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ] } @test "sourcing a nonexistent file in test produces error output" { run bats "$FIXTURE_ROOT/source_nonexistent_file.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file.bats, line 2)" ] [ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ] } @test "referencing unset parameter in test produces error output" { run bats "$FIXTURE_ROOT/reference_unset_parameter.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 referencing unset parameter fails' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter.bats, line 3)" ] [ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ] } @test "sourcing a nonexistent file in teardown produces error output" { run bats "$FIXTURE_ROOT/source_nonexistent_file_in_teardown.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 sourcing nonexistent file fails in teardown' ] [ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/source_nonexistent_file_in_teardown.bats, line 2)" ] [ "${lines[3]}" = "# \`source \"nonexistent file\"' failed" ] } @test "referencing unset parameter in teardown produces error output" { run bats "$FIXTURE_ROOT/reference_unset_parameter_in_teardown.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 referencing unset parameter fails in teardown' ] [ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/reference_unset_parameter_in_teardown.bats, line 3)" ] [ "${lines[3]}" = "# \`echo \"\$unset_parameter\"' failed" ] } @test "execute exported function without breaking failing test output" { exported_function() { return 0; } export -f exported_function run bats "$FIXTURE_ROOT/exported_function.bats" [ $status -eq 1 ] [ "${lines[0]}" = "1..1" ] [ "${lines[1]}" = "not ok 1 failing test" ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/exported_function.bats, line 7)" ] [ "${lines[3]}" = "# \`false' failed" ] [ "${lines[4]}" = "# a='exported_function'" ] } @test "output printed even when no final newline" { run bats "$FIXTURE_ROOT/no-final-newline.bats" printf 'num lines: %d\n' "${#lines[@]}" >&2 printf 'LINE: %s\n' "${lines[@]}" >&2 [ "$status" -eq 1 ] [ "${#lines[@]}" -eq 7 ] [ "${lines[1]}" = 'not ok 1 no final newline' ] [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/no-final-newline.bats, line 2)" ] [ "${lines[3]}" = "# \`printf 'foo\nbar\nbaz' >&2 && return 1' failed" ] [ "${lines[4]}" = '# foo' ] [ "${lines[5]}" = '# bar' ] [ "${lines[6]}" = '# baz' ] }