From b9e7cef0cd251561b545a7b7d78d3a55392031dc Mon Sep 17 00:00:00 2001 From: brarcher Date: Tue, 17 Dec 2013 16:06:53 +0000 Subject: [PATCH] Add tests for tap logging These tests follow closely with what the logging tests cover, with one small change. If fork() is disabled, one test case will invoked exit() and cause the unit testing program to terminate early. In that case, the tap output is expected to be incomplete. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@874 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- tests/Makefile.am | 9 ++- tests/ex_tap_output.c | 123 +++++++++++++++++++++++++++++++++++++++ tests/test_tap_output.sh | 52 +++++++++++++++++ 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 tests/ex_tap_output.c create mode 100755 tests/test_tap_output.sh diff --git a/tests/Makefile.am b/tests/Makefile.am index 52b0c24..009e8e6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -6,7 +6,8 @@ TESTS = \ test_output.sh \ test_check_nofork.sh \ test_xml_output.sh \ - test_log_output.sh + test_log_output.sh \ + test_tap_output.sh # check_thread_stress is kind of slow. # add this line back to TESTS to enable check_thread_stress @@ -24,7 +25,8 @@ noinst_PROGRAMS = \ check_nofork \ ex_output \ ex_xml_output \ - ex_log_output + ex_log_output \ + ex_tap_output EXTRA_DIST = test_output.sh test_check_nofork.sh test_log_output.sh test_vars.in test_xml_output.sh @@ -78,6 +80,9 @@ ex_log_output_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcom ex_xml_output_SOURCES = ex_xml_output.c ex_xml_output_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la +ex_tap_output_SOURCES = ex_tap_output.c +ex_tap_output_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la + AM_CPPFLAGS = -I$(top_builddir)/src -I$(top_srcdir)/src CLEANFILES = *~ *.log *.xml test_logfile diff --git a/tests/ex_tap_output.c b/tests/ex_tap_output.c new file mode 100644 index 0000000..c4f99ea --- /dev/null +++ b/tests/ex_tap_output.c @@ -0,0 +1,123 @@ +#include "../lib/libcompat.h" + +#include +#include +#include +#include + +START_TEST(test_pass) +{ + ck_assert_msg (1==1, "Shouldn't see this"); +} +END_TEST + +START_TEST(test_fail) +{ + ck_abort_msg("Failure"); +} +END_TEST + +/* + * This test will fail without fork, as it will result in the + * unit test runniner exiting early. + */ +#if defined(HAVE_FORK) +START_TEST(test_exit) +{ + exit(1); +} +END_TEST +#endif /* HAVE_FORK */ + +/* + * This test will intentionally mess up the unit testing program + * when fork is unavailable. The purpose of including it is to + * ensure that the tap output is correct when a test crashes. + */ +START_TEST(test_abort) +{ + exit(1); +} +END_TEST + +START_TEST(test_pass2) +{ + ck_assert_msg (1==1, "Shouldn't see this"); +} +END_TEST + +static Suite *make_s1_suite (void) +{ + Suite *s; + TCase *tc; + + s = suite_create("S1"); + tc = tcase_create ("Core"); + suite_add_tcase(s, tc); + tcase_add_test (tc, test_pass); + tcase_add_test (tc, test_fail); +#if defined(HAVE_FORK) + tcase_add_test (tc, test_exit); +#endif /* HAVE_FORK */ + + return s; +} + +static Suite *make_s2_suite (int include_abort_test) +{ + Suite *s; + TCase *tc; + + s = suite_create("S2"); + tc = tcase_create ("Core"); + suite_add_tcase(s, tc); + if(include_abort_test == 1) + { + tcase_add_test (tc, test_abort); + } + tcase_add_test (tc, test_pass2); + + return s; +} + +static void run_tests (int include_abort_test) +{ + SRunner *sr; + + sr = srunner_create(make_s1_suite()); + srunner_add_suite(sr, make_s2_suite(include_abort_test)); + srunner_set_tap(sr, "test.tap"); + srunner_run_all(sr, CK_SILENT); + srunner_free(sr); +} + +static void usage(void) +{ + printf ("Usage: ex_tap_output (NORMAL | TEST_ABORT)\n"); +} + +int main (int argc, char **argv) +{ + + if (argc != 2) + { + usage(); + return EXIT_FAILURE; + } + + if (strcmp (argv[1], "NORMAL") == 0) + { + run_tests(0 /* run only tests that will behave properly*/); + } + else if (strcmp (argv[1], "TEST_ABORT") == 0) + { + run_tests(1 /* Include a test that will fail without fork()*/); + } + else + { + usage(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/tests/test_tap_output.sh b/tests/test_tap_output.sh new file mode 100755 index 0000000..62b912f --- /dev/null +++ b/tests/test_tap_output.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +OUTPUT_FILE=test.tap + +. ./test_vars + +if [ $HAVE_FORK -eq 1 ]; then +expected_normal_tap="ok 1 +not ok 2 +not ok 3 +ok 4 +1..4" +expected_aborted_tap="ok 1 +not ok 2 +not ok 3 +not ok 4 +ok 5 +1..5" +else +expected_normal_tap="ok 1 +not ok 2 +ok 3 +1..3" +# When fork() is unavailable, one of the tests +# will invoke exit() which will terminate the +# unit testing program. In that case, the tap +# results will be incomplete, but the required +# test plan will be missing, signaling that +# something bad happened. +expected_aborted_tap="ok 1 +not ok 2" +fi + +test_tap_output ( ) { + ./ex_tap_output${EXEEXT} "${1}" > /dev/null + actual_tap=`cat ${OUTPUT_FILE} | tr -d "\r"` + expected_tap="${2}" + if [ x"${expected_tap}" != x"${actual_tap}" ]; then + echo "Problem with ex_tap_output${EXEEXT}"; + echo "Expected:"; + echo "${expected_tap}"; + echo + echo "Got:"; + echo "${actual_tap}"; + exit 1; + fi +} + +test_tap_output "NORMAL" "${expected_normal_tap}" +test_tap_output "TEST_ABORT" "${expected_aborted_tap}" + +exit 0 -- 2.40.0