Skip to content

Commit

Permalink
Don't print an error message about cat if a test exits abnormally
Browse files Browse the repository at this point in the history
On calls to fail, the failure message is written to $TEST_TMPDIR/__fail. This
is cat-ed to get the message, but if the test exited without calling fail then
an annoying 'cat: blah/blah/blah/__fail: No such file or directory' message is
printed.  This throws out the error message if the cat fails.

--
MOS_MIGRATED_REVID=109896051
  • Loading branch information
kchodorow committed Dec 10, 2015
1 parent 20262fd commit 02269df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/test/shell/testenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ if [ -z "${TEST_TMPDIR:-}" ]; then
fi
if [ ! -e "${TEST_TMPDIR}" ]; then
mkdir -p -m 0700 "${TEST_TMPDIR}"
# Clean TEST_TMPDIR on exit
atexit "rm -fr ${TEST_TMPDIR}"
fi
# Clean TEST_TMPDIR on exit
atexit "rm -fr ${TEST_TMPDIR}"

# Functions to compare the actual output of a test to the expected
# (golden) output.
Expand Down
4 changes: 2 additions & 2 deletions src/test/shell/unittest.bash
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ function run_suite() {
timestamp >$TEST_TMPDIR/__ts_start
set_up
eval $TEST_name
timestamp >$TEST_TMPDIR/__ts_end
tear_down
timestamp >$TEST_TMPDIR/__ts_end
test $TEST_passed == true
) 2>&1 | tee $TEST_TMPDIR/__log
# Note that tee will prevent the control flow continuing if the test
Expand Down Expand Up @@ -570,7 +570,7 @@ function run_suite() {
echo "FAILED: $TEST_name" >&2
# end marker in CDATA cannot be escaped, we need to split the CDATA sections
log=$(cat $TEST_TMPDIR/__log | sed 's/]]>/]]>]]&gt;<![CDATA[/g')
fail_msg=$(cat $TEST_TMPDIR/__fail || echo "No failure message")
fail_msg=$(cat $TEST_TMPDIR/__fail 2> /dev/null || echo "No failure message")
testcase_tag="<testcase name=\"$TEST_name\" status=\"run\" time=\"$run_time\" classname=\"\"><error message=\"$fail_msg\"><![CDATA[$log]]></error></testcase>"
fi
$TEST_verbose && echo >&2
Expand Down
46 changes: 46 additions & 0 deletions src/test/shell/unittest_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source ${DIR}/unittest.bash || { echo "Could not source unittest.sh" >&2; exit 1; }

function set_up() {
tmp_TEST_TMPDIR=$TEST_TMPDIR
TEST_TMPDIR=$TEST_TMPDIR/$TEST_name
mkdir -p $TEST_TMPDIR
}

function tear_down() {
TEST_TMPDIR=$tmp_TEST_TMPDIR
}

function test_1() {
echo "Everything is okay in test_1"
}
Expand All @@ -36,4 +46,40 @@ function test_timestamp() {
assert_equals $time_diff 123.456
}

function test_failure_message() {
cd $TEST_TMPDIR
cat > thing.sh <<EOF
#!/bin/bash
source ${DIR}/unittest.bash
function test_thing() {
fail "I'm a failure"
}
run_suite "thing tests"
EOF
chmod +x thing.sh
./thing.sh &> $TEST_log || echo "thing.sh should fail"
expect_not_log "__fail: No such file or directory"
assert_contains "I'm a failure." $XML_OUTPUT_FILE
}

function test_no_failure_message() {
cd $TEST_TMPDIR
cat > thing.sh <<EOF
#!/bin/bash
source ${DIR}/unittest.bash
function test_thing() {
TEST_passed=blorp
}
run_suite "thing tests"
EOF
chmod +x thing.sh
./thing.sh &> $TEST_log || echo "thing.sh should fail"
expect_not_log "__fail: No such file or directory"
assert_contains "No failure message" $XML_OUTPUT_FILE
}

run_suite "unittests Tests"

0 comments on commit 02269df

Please sign in to comment.