From a715fff30a10d394e712928ffacc241702fefea7 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 12 Aug 2014 16:56:52 -0500 Subject: [PATCH] Print filenames relative to the working directory --- libexec/bats | 1 + libexec/bats-exec-test | 13 ++++++++++++- test/bats.bats | 23 +++++++++++++++-------- test/test_helper.bash | 1 + 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/libexec/bats b/libexec/bats index bdfbddc..c7c88bc 100755 --- a/libexec/bats +++ b/libexec/bats @@ -54,6 +54,7 @@ expand_path() { BATS_LIBEXEC="$(abs_dirname "$0")" export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")" +export BATS_CWD="$(abs_dirname .)" export PATH="$BATS_LIBEXEC:$PATH" options=() diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 225cdd8..49b8281 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -122,7 +122,7 @@ bats_print_stack_trace() { local count="${#@}" for frame in "$@"; do - local filename="$(bats_frame_filename "$frame")" + local filename="$(bats_trim_filename "$(bats_frame_filename "$frame")")" local lineno="$(bats_frame_lineno "$frame")" if [ $index -eq 1 ]; then @@ -199,6 +199,17 @@ bats_strip_string() { printf "%s" "$string" | sed -e "s/^[ "$'\t'"]*//" -e "s/[ "$'\t'"]*$//" } +bats_trim_filename() { + local filename="$1" + local length="${#BATS_CWD}" + + if [ "${filename:0:length+1}" = "${BATS_CWD}/" ]; then + echo "${filename:length+1}" + else + echo "$filename" + fi +} + bats_debug_trap() { if [ "$BASH_SOURCE" != "$1" ]; then bats_capture_stack_trace diff --git a/test/bats.bats b/test/bats.bats index f867922..acc435b 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -45,7 +45,7 @@ fixtures bats [ $status -eq 1 ] [ "${lines[0]}" = '1..1' ] [ "${lines[1]}" = 'not ok 1 a failing test' ] - [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing.bats, line 4)" ] + [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing.bats, line 4)" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] } @@ -54,7 +54,7 @@ fixtures bats [ $status -eq 1 ] [ "${lines[0]}" = '1..2' ] [ "${lines[1]}" = 'not ok 1 a failing test' ] - [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_and_passing.bats, line 2)" ] + [ "${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' ] } @@ -69,8 +69,8 @@ fixtures bats 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 $FIXTURE_ROOT/test_helper.bash, line 6," ] - [ "${lines[3]}" = "# in test file $FIXTURE_ROOT/failing_helper.bats, line 5)" ] + [ "${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]}" = "# \`false' failed" ] } @@ -99,7 +99,7 @@ fixtures bats run bats "$FIXTURE_ROOT/failing_setup.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 truth' ] - [ "${lines[2]}" = "# (from function \`setup' in test file $FIXTURE_ROOT/failing_setup.bats, line 2)" ] + [ "${lines[2]}" = "# (from function \`setup' in test file $RELATIVE_FIXTURE_ROOT/failing_setup.bats, line 2)" ] [ "${lines[3]}" = "# \`false' failed" ] } @@ -107,7 +107,7 @@ fixtures bats 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 $FIXTURE_ROOT/failing_teardown.bats, line 2)" ] + [ "${lines[2]}" = "# (from function \`teardown' in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 2)" ] [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed" ] } @@ -115,7 +115,7 @@ fixtures bats PASS=0 run bats "$FIXTURE_ROOT/failing_teardown.bats" [ $status -eq 1 ] [ "${lines[1]}" = 'not ok 1 truth' ] - [ "${lines[2]}" = "# (in test file $FIXTURE_ROOT/failing_teardown.bats, line 6)" ] + [ "${lines[2]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/failing_teardown.bats, line 6)" ] [ "${lines[3]}" = $'# `[ "$PASS" = "1" ]\' failed' ] } @@ -125,6 +125,13 @@ fixtures bats [ "${lines[3]}" = "# \`eval \"( exit \${STATUS:-1} )\"' failed with status 2" ] } +@test "failing test file outside of BATS_CWD" { + cd "$TMP" + 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 ] @@ -222,6 +229,6 @@ fixtures bats [ "${lines[2]}" = 'ok 2 passing' ] [ "${lines[3]}" = 'ok 3 input redirection' ] [ "${lines[4]}" = 'not ok 4 failing' ] - [ "${lines[5]}" = "# (in test file $FIXTURE_ROOT/single_line.bats, line 9)" ] + [ "${lines[5]}" = "# (in test file $RELATIVE_FIXTURE_ROOT/single_line.bats, line 9)" ] [ "${lines[6]}" = $'# `@test "failing" { false; }\' failed' ] } diff --git a/test/test_helper.bash b/test/test_helper.bash index 643b3d7..a01b4b0 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -1,5 +1,6 @@ fixtures() { FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/$1" + RELATIVE_FIXTURE_ROOT="$(bats_trim_filename "$FIXTURE_ROOT")" } setup() {