Skip to content

Commit

Permalink
Fix for tests reading from stdin (sstephenson#227)
Browse files Browse the repository at this point in the history
Fix for tests reading from stdin
  • Loading branch information
sublimino authored Jul 4, 2019
2 parents e4be967 + bcce611 commit c92480b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
27 changes: 19 additions & 8 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ for filename in "$@"; do
fi
done

test_count="${#all_tests[@]}"

if [[ -n "$count_only_flag" ]]; then
printf '%d\n' "${#all_tests[@]}"
printf '%d\n' "${test_count}"
exit
fi

status=0
printf '1..%d\n' "${#all_tests[@]}"
printf '1..%d\n' "${test_count}"

# No point on continuing if there's no tests.
if [[ "${#all_tests[@]}" == 0 ]]; then
if [[ "${test_count}" == 0 ]]; then
exit
fi

Expand All @@ -92,10 +94,19 @@ if [[ "$num_jobs" != 1 ]]; then
parallel -qk -j "$num_jobs" --colsep="\t" -- bats-exec-test "${flags[@]}" '{1}' '{2}' '{#}' || status=1
else
# Just do it serially.
test_number=1
while IFS=$'\t' read -r filename test_name; do
bats-exec-test "${flags[@]}" "$filename" "$test_name" "$test_number" || status=1
((++test_number))
done < <(printf '%s\n' "${all_tests[@]}" | grep -v '^$')
test_number=0
for test_line in "${all_tests[@]}"; do
# Only handle non-empty lines
if [[ $test_line ]]; then
filename="${test_line%%$'\t'*}"
test_name="${test_line##*$'\t'}"
((++test_number))
bats-exec-test "${flags[@]}" "$filename" "$test_name" "$test_number" || status=1
fi
done
if [[ "${test_number}" != "${test_count}" ]]; then
printf '# bats warning: Only executed %s of %s tests\n' "$test_number" "$test_count"
status=1
fi
fi
exit "$status"
7 changes: 7 additions & 0 deletions libexec/bats-core/bats-format-tap-stream
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IFS= read -r header
if [[ "$header" =~ $header_pattern ]]; then
count="${header:3}"
index=0
passed=0
failures=0
skipped=0
name=
Expand Down Expand Up @@ -78,6 +79,11 @@ summary() {
buffer ', %d skipped' "$skipped"
fi

not_run=$((count - passed - failures - skipped))
if [[ "$not_run" -gt 0 ]]; then
buffer ', %d not run' "$not_run"
fi

buffer '\n'
}

Expand Down Expand Up @@ -158,6 +164,7 @@ while IFS= read -r line; do
((++skipped))
skip "${BASH_REMATCH[2]}"
else
((++passed))
pass
fi
;;
Expand Down
9 changes: 9 additions & 0 deletions test/bats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,12 @@ END_OF_ERR_MSG
# In theory it should take 3s, but let's give it bit of extra time instead.
[[ "$duration" -lt 20 ]]
}

@test "run tests which consume stdin (see #197)" {
run bats "$FIXTURE_ROOT/read_from_stdin.bats"
[ "$status" -eq 0 ]
[[ "${lines[0]}" == "1..3" ]]
[[ "${lines[1]}" == "ok 1 test 1" ]]
[[ "${lines[2]}" == "ok 2 test 2 with TAB in name" ]]
[[ "${lines[3]}" == "ok 3 test 3" ]]
}
19 changes: 19 additions & 0 deletions test/fixtures/bats/cmd_using_stdin.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

# Fractional timeout supported in bash 4+
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
timeout=1
else
timeout=0.01
fi

# Just reading from stdin
while read -r -t $timeout foo; do
if [ "$foo" == "EXIT" ]; then
echo "Found"
exit 0
fi
done

echo "Not found"
exit 1
20 changes: 20 additions & 0 deletions test/fixtures/bats/read_from_stdin.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bats

@test "test 1" {
# Don't print anything
run bash -c "$BATS_TEST_DIRNAME/cmd_using_stdin.bash"
[ "$status" -eq 1 ]
[ "$output" = "Not found" ]
}

@test "test 2 with TAB in name" {
run bash -c "echo EXIT | $BATS_TEST_DIRNAME/cmd_using_stdin.bash"
[ "$status" -eq 0 ]
[ "$output" = "Found" ]
}

@test "test 3" {
run bash -c "echo EXIT | $BATS_TEST_DIRNAME/cmd_using_stdin.bash"
[ "$status" -eq 0 ]
[ "$output" = "Found" ]
}

0 comments on commit c92480b

Please sign in to comment.