]> granicus.if.org Git - check/commitdiff
report correct error if teardown after failure in no fork mode
authorbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Fri, 28 Nov 2014 05:50:22 +0000 (05:50 +0000)
committerbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Fri, 28 Nov 2014 05:50:22 +0000 (05:50 +0000)
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

AUTHORS
CMakeLists.txt
NEWS
src/check_msg.c
tests/CMakeLists.txt
tests/Makefile.am
tests/check_nofork_teardown.c [new file with mode: 0644]
tests/test_check_nofork_teardown.sh [new file with mode: 0755]

diff --git a/AUTHORS b/AUTHORS
index 316d84e47fdc92076202f6c8982dd190b7e0aa3c..4c2e50ee02bd2044734b56d8877c97a7b7213ce3 100644 (file)
--- 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 
index e0e3789a73c8fb0a87977b33f46cf004fdd7fe49..c45802c5bd3a7df22f67732e3ae317296383649b 100644 (file)
@@ -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 34e25e3db98c5a2ea0220ec0078ba51ee770056a..d47e6093314ff8824d7b81f0bb017adc2ec7a6c1 100644 (file)
--- 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)
index eeacaa0f07ec1f3bdcec5f22e5c077121b1efd98..d8f50acbd6e889da95cc3e8703a7609519ce56dc 100644 (file)
@@ -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);
index ad0de9b170e97c399d4cb1082fca41daa1e942bc..eb146a86c4ec44917bbf1518016ff44acafffa7f 100644 (file)
@@ -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)
index d46e60d48bc1bf8f9d9a85aeb17bc2a35a7ca9de..3a555d5182d8bb6177ed200c10d0004551800ec5 100644 (file)
@@ -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 (file)
index 0000000..5a772a7
--- /dev/null
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#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 (executable)
index 0000000..cad92f7
--- /dev/null
@@ -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