From 1735a4fcd26bf34d5e3118185ff3b84f4bcd4258 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 29 Jan 2015 20:51:49 +0100 Subject: [PATCH 1/2] saving $IFS in run() not altered for code using it IFS was modified by run() becoming '\n' and so relying to its bash default was failing tests. Also some wrong tests corrected because was relying on this behavior to pass. Fix #89 --- libexec/bats-exec-test | 4 +++- test/bats.bats | 8 +++++++- test/fixtures/bats/loop_keep_IFS.bats | 16 ++++++++++++++++ test/suite.bats | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/bats/loop_keep_IFS.bats diff --git a/libexec/bats-exec-test b/libexec/bats-exec-test index 5a076650..8f3bd510 100755 --- a/libexec/bats-exec-test +++ b/libexec/bats-exec-test @@ -48,7 +48,7 @@ load() { } run() { - local e E T + local e E T oldIFS [[ ! "$-" =~ e ]] || e=1 [[ ! "$-" =~ E ]] || E=1 [[ ! "$-" =~ T ]] || T=1 @@ -57,10 +57,12 @@ run() { set +T output="$("$@" 2>&1)" status="$?" + oldIFS=$IFS IFS=$'\n' lines=($output) [ -z "$e" ] || set -e [ -z "$E" ] || set -E [ -z "$T" ] || set -T + IFS=$oldIFS } setup() { diff --git a/test/bats.bats b/test/bats.bats index 280515d2..0df38100 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -37,7 +37,7 @@ fixtures bats run bats "$FIXTURE_ROOT/passing.bats" [ $status -eq 0 ] [ ${lines[0]} = "1..1" ] - [ ${lines[1]} = "ok 1 a passing test" ] + [ "${lines[1]}" = "ok 1 a passing test" ] } @test "summary passing tests" { @@ -256,3 +256,9 @@ fixtures bats [ "${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" ] +} diff --git a/test/fixtures/bats/loop_keep_IFS.bats b/test/fixtures/bats/loop_keep_IFS.bats new file mode 100644 index 00000000..f30613ae --- /dev/null +++ b/test/fixtures/bats/loop_keep_IFS.bats @@ -0,0 +1,16 @@ +# see issue #89 +loop_func() { + local search="none one two tree" + local d + + for d in $search ; do + echo $d + done +} + +@test "loop_func" { + run loop_func + [[ "${lines[3]}" == 'tree' ]] + run loop_func + [[ "${lines[2]}" == 'two' ]] +} diff --git a/test/suite.bats b/test/suite.bats index 14f5008e..48f75933 100755 --- a/test/suite.bats +++ b/test/suite.bats @@ -13,7 +13,7 @@ fixtures suite run bats "$FIXTURE_ROOT/single" [ $status -eq 0 ] [ ${lines[0]} = "1..1" ] - [ ${lines[1]} = "ok 1 a passing test" ] + [ "${lines[1]}" = "ok 1 a passing test" ] } @test "counting tests in a suite" { From 5fe46a0893b3586e931603e663cd13db8dfeae77 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Fri, 30 Jan 2015 13:08:27 +0100 Subject: [PATCH 2/2] pull #90 quote every string compare test are all successful. --- test/bats.bats | 4 ++-- test/suite.bats | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/bats.bats b/test/bats.bats index 0df38100..f1aff293 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -30,13 +30,13 @@ fixtures bats @test "empty test file runs zero tests" { run bats "$FIXTURE_ROOT/empty.bats" [ $status -eq 0 ] - [ $output = "1..0" ] + [ "$output" = "1..0" ] } @test "one passing test" { run bats "$FIXTURE_ROOT/passing.bats" [ $status -eq 0 ] - [ ${lines[0]} = "1..1" ] + [ "${lines[0]}" = "1..1" ] [ "${lines[1]}" = "ok 1 a passing test" ] } diff --git a/test/suite.bats b/test/suite.bats index 48f75933..53716863 100755 --- a/test/suite.bats +++ b/test/suite.bats @@ -6,30 +6,30 @@ fixtures suite @test "running a suite with no test files" { run bats "$FIXTURE_ROOT/empty" [ $status -eq 0 ] - [ $output = "1..0" ] + [ "$output" = "1..0" ] } @test "running a suite with one test file" { run bats "$FIXTURE_ROOT/single" [ $status -eq 0 ] - [ ${lines[0]} = "1..1" ] + [ "${lines[0]}" = "1..1" ] [ "${lines[1]}" = "ok 1 a passing test" ] } @test "counting tests in a suite" { run bats -c "$FIXTURE_ROOT/single" [ $status -eq 0 ] - [ $output -eq 1 ] + [ "$output" -eq 1 ] run bats -c "$FIXTURE_ROOT/multiple" [ $status -eq 0 ] - [ $output -eq 3 ] + [ "$output" -eq 3 ] } @test "aggregated output of multiple tests in a suite" { run bats "$FIXTURE_ROOT/multiple" [ $status -eq 0 ] - [ ${lines[0]} = "1..3" ] + [ "${lines[0]}" = "1..3" ] echo "$output" | grep "^ok . truth" echo "$output" | grep "^ok . more truth" echo "$output" | grep "^ok . quasi-truth" @@ -38,14 +38,14 @@ fixtures suite @test "a failing test in a suite results in an error exit code" { FLUNK=1 run bats "$FIXTURE_ROOT/multiple" [ $status -eq 1 ] - [ ${lines[0]} = "1..3" ] + [ "${lines[0]}" = "1..3" ] echo "$output" | grep "^not ok . quasi-truth" } @test "running an ad-hoc suite by specifying multiple test files" { run bats "$FIXTURE_ROOT/multiple/a.bats" "$FIXTURE_ROOT/multiple/b.bats" [ $status -eq 0 ] - [ ${lines[0]} = "1..3" ] + [ "${lines[0]}" = "1..3" ] echo "$output" | grep "^ok . truth" echo "$output" | grep "^ok . more truth" echo "$output" | grep "^ok . quasi-truth"