From: brarcher Date: Fri, 28 Nov 2014 05:50:22 +0000 (+0000) Subject: report correct error if teardown after failure in no fork mode X-Git-Tag: 0.10.0~26 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e60390d54ab3a065f53d4e7150a79424b8a85fae;p=check report correct error if teardown after failure in no fork mode In nofork mode, the location of a failed assertion within a test case is lost if that test case has a checked teardown fixture (even if that fixture function is empty). The reason why this happens is this: the end of the message sequence coming down the pipe is CK_MSG_LOC (location of failing test), CK_MSG_FAIL, CK_MSG_CTX (TEARDOWN). It is this final message that confuses things, because rcvmsg_update_ctx() updates rmsg->lastctx (which I suspect is the right thing for it to do), which is the ctx value used by the first 'if' body in construct_test_result() in its call to tr_set_loc_by_ctx(). The solution is to initialize tr->ctx to rmsg->failctx if it is not CK_CTX_INVALID. Bug #99. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1193 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/AUTHORS b/AUTHORS index 316d84e..4c2e50e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -50,6 +50,7 @@ Contributors: Georg Sauthoff (Solaris support, misc autotools fixes) forest (AIX and Solaris support) Michael Piszczek (misc cleanup) + Stewart Brodie (bug fix: no fork mode failure reporting with teardowns) Anybody who has contributed code to Check or Check's build system is considered an author. Send patches to this file to diff --git a/CMakeLists.txt b/CMakeLists.txt index e0e3789..c45802c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -312,4 +312,5 @@ if(UNIX OR MINGW OR MSYS) add_test(NAME test_xml_output.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests COMMAND sh test_xml_output.sh) add_test(NAME test_tap_output.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests COMMAND sh test_tap_output.sh) add_test(NAME test_check_nofork.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests COMMAND sh test_check_nofork.sh) + add_test(NAME test_check_nofork_teardown.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests COMMAND sh test_check_nofork_teardown.sh) endif(UNIX OR MINGW OR MSYS) diff --git a/NEWS b/NEWS index 34e25e3..d47e609 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,11 @@ In Development: * Fix issue with string formatting in ck_assert(), where using the % operator would be interpreted as a string formatter. Bug #96. +* In nofork mode, the location of a failed assertion within a test + case was lost if that test case has a checked teardown fixture + (even if that fixture function is empty). This is now fixed. + Bug #99 + Sat July 26, 2014: Released Check 0.9.14 based on r1174 (2014-07-03 18:43:49 +0000) diff --git a/src/check_msg.c b/src/check_msg.c index eeacaa0..d8f50ac 100644 --- a/src/check_msg.c +++ b/src/check_msg.c @@ -175,7 +175,15 @@ static TestResult *construct_test_result(RcvMsg * rmsg, int waserror) if(rmsg->msg != NULL || waserror) { - tr->ctx = rmsg->lastctx; + if(rmsg->failctx != CK_CTX_INVALID) + { + tr->ctx = rmsg->failctx; + } + else + { + tr->ctx = rmsg->lastctx; + } + tr->msg = rmsg->msg; rmsg->msg = NULL; tr_set_loc_by_ctx(tr, tr->ctx, rmsg); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ad0de9b..eb146a8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -65,3 +65,7 @@ target_link_libraries(ex_output check compat) set(CHECK_NOFORK_SOURCES check_nofork.c) add_executable(check_nofork ${CHECK_NOFORK_SOURCES}) target_link_libraries(check_nofork check compat) + +set(CHECK_NOFORK_TEARDOWN_SOURCES check_nofork_teardown.c) +add_executable(check_nofork_teardown ${CHECK_NOFORK_TEARDOWN_SOURCES}) +target_link_libraries(check_nofork_teardown check compat) diff --git a/tests/Makefile.am b/tests/Makefile.am index d46e60d..3a555d5 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,6 +5,7 @@ TESTS = \ check_check \ test_output.sh \ test_check_nofork.sh \ + test_check_nofork_teardown.sh \ test_xml_output.sh \ test_log_output.sh \ test_tap_output.sh @@ -23,6 +24,7 @@ noinst_PROGRAMS = \ check_stress \ check_thread_stress \ check_nofork \ + check_nofork_teardown \ check_mem_leaks \ ex_output @@ -80,6 +82,9 @@ check_thread_stress_CFLAGS = @PTHREAD_CFLAGS@ check_nofork_SOURCES = check_nofork.c check_nofork_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la +check_nofork_teardown_SOURCES = check_nofork_teardown.c +check_nofork_teardown_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la + ex_output_SOURCES = ex_output.c ex_output_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la diff --git a/tests/check_nofork_teardown.c b/tests/check_nofork_teardown.c new file mode 100644 index 0000000..5a772a7 --- /dev/null +++ b/tests/check_nofork_teardown.c @@ -0,0 +1,60 @@ +#include +#include +#include "check.h" + +/** + * This test checks the result if in CK_NOFORK + * mode a unit test fails but a checked teardown + * runs after the failed test. + * + * Previously, the failure would be reported as: + * + * 0%: Checks: 1, Failures: 1, Errors: 0 + * (null):-1:S:tc:will_fail:0: Assertion '0' failed + * + * The reason why this happens is this: the end of the + * message sequence coming down the pipe is CK_MSG_LOC + * (location of failing test), CK_MSG_FAIL, CK_MSG_CTX + * (TEARDOWN). It is this final message that confuses + * things, because rcvmsg_update_ctx() updates + * rmsg->lastctx (which likely is the right thing for it + * to do), which is the ctx value used by the first 'if' + * body in construct_test_result() in its call to + * tr_set_loc_by_ctx(). + * + * After the fix, the test failure should be reported + * as: + * + * 0%: Checks: 1, Failures: 1, Errors: 0 + * check_nofork_teardown.c:33:F:tc:will_fail:0: Assertion '0' failed + */ + +START_TEST( will_fail ) +{ + ck_assert(0); +} +END_TEST + +static void empty_checked_teardown( void ) +{ +} + +int main( void ) +{ + SRunner *sr = srunner_create( NULL ); + Suite *s = suite_create( "bug-99" ); + TCase *tc = tcase_create( "tc" ); + int result; + + srunner_add_suite( sr, s ); + srunner_set_fork_status( sr, CK_NOFORK ); + suite_add_tcase( s, tc ); + tcase_add_checked_fixture( tc, NULL, empty_checked_teardown ); + tcase_add_test( tc, will_fail ); + + srunner_run_all( sr, CK_ENV ); + result = srunner_ntests_failed( sr ) ? EXIT_FAILURE : EXIT_SUCCESS; + srunner_free( sr ); + + return result; +} diff --git a/tests/test_check_nofork_teardown.sh b/tests/test_check_nofork_teardown.sh new file mode 100755 index 0000000..cad92f7 --- /dev/null +++ b/tests/test_check_nofork_teardown.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env sh + +. ./test_vars + +expected="Running suite(s): bug-99 +0%: Checks: 1, Failures: 1, Errors: 0 +check_nofork_teardown.c:34:F:tc:will_fail:0: Assertion '0' failed" + +actual=`./check_nofork_teardown${EXEEXT} | tr -d "\r"` +if [ x"${expected}" = x"${actual}" ]; then + exit 0 +else + echo "Problem with check_nofork_teardown${EXEEXT}" + echo "Expected: " + echo "${expected}" + echo "Got: " + echo "${actual}" + exit 1 +fi