From: brarcher Date: Tue, 17 Dec 2013 16:06:50 +0000 (+0000) Subject: add support for logging in TAP format X-Git-Tag: 0.10.0~343 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bfe918b279913c8341e89b4fa1e4290df4004558;p=check add support for logging in TAP format Logging in the Test Anything Protcol (TAP) format is useful in general, but is especially useful on platforms where fork() is unavailable. The TAP format requires that the planned number of tests to run be specified, either at the start or end of a test run. If the number of tests run does not match the plan, the test run is marked as failed. As running without fork() leaves the unit testing program open to failure if a test aborts/segfaults/etc, validating the TAP output is useful. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@873 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/src/check.c b/src/check.c index 94d9dbf..878a118 100644 --- a/src/check.c +++ b/src/check.c @@ -289,6 +289,7 @@ SRunner *srunner_create (Suite *s) sr->resultlst = check_list_create(); sr->log_fname = NULL; sr->xml_fname = NULL; + sr->tap_fname = NULL; sr->loglst = NULL; #if defined(HAVE_FORK) diff --git a/src/check.h.in b/src/check.h.in index f9407cd..2c18c07 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -487,6 +487,20 @@ int CK_EXPORT srunner_has_xml (SRunner *sr); /* Return the name of the XML file, or NULL if none */ const char * CK_EXPORT srunner_xml_fname (SRunner *sr); +/* Set a tap file to which to write during test running. + + Log file setting is an initialize only operation -- it should be + done immediatly after SRunner creation, and the log file can't be + changed after being set. +*/ +void CK_EXPORT srunner_set_tap (SRunner *sr, const char *fname); + +/* Does the SRunner have a tap file? */ +int CK_EXPORT srunner_has_tap (SRunner *sr); + +/* Return the name of the tap file, or NULL if none */ +const char * CK_EXPORT srunner_tap_fname (SRunner *sr); + /* Control forking */ enum fork_status { diff --git a/src/check_impl.h b/src/check_impl.h index 5402d69..45a48a0 100644 --- a/src/check_impl.h +++ b/src/check_impl.h @@ -109,6 +109,7 @@ struct SRunner { List *resultlst; /* List of unit test results */ const char *log_fname; /* name of log file */ const char *xml_fname; /* name of xml output file */ + const char *tap_fname; /* name of tap output file */ List *loglst; /* list of Log objects */ enum fork_status fstat; /* controls if suites are forked or not NOTE: Don't use this value directly, diff --git a/src/check_log.c b/src/check_log.c index 6b27fb2..6ff2b97 100644 --- a/src/check_log.c +++ b/src/check_log.c @@ -81,6 +81,28 @@ const char *srunner_xml_fname (SRunner *sr) return getenv("CK_XML_LOG_FILE_NAME"); } +void srunner_set_tap (SRunner *sr, const char *fname) +{ + if (sr->tap_fname) + return; + sr->tap_fname = fname; +} + +int srunner_has_tap (SRunner *sr) +{ + return srunner_tap_fname(sr) != NULL; +} + +const char *srunner_tap_fname (SRunner *sr) +{ + /* check if tap log filename have been set explicitly */ + if (sr->tap_fname != NULL) { + return sr->tap_fname; + } + + return getenv("CK_TAP_LOG_FILE_NAME"); +} + void srunner_register_lfun (SRunner *sr, FILE *lfile, int close, LFun lfun, enum print_output printmode) { @@ -290,6 +312,46 @@ void xml_lfun (SRunner *sr CK_ATTRIBUTE_UNUSED, FILE *file, enum print_output pr } +void tap_lfun (SRunner *sr, FILE *file, enum print_output printmode CK_ATTRIBUTE_UNUSED, + void *obj, enum cl_event evt) +{ + TestResult *tr; + Suite *s; + + static int num_tests_run = 0; + + switch (evt) { + case CLINITLOG_SR: + /* As this is a new log file, reset the number of tests executed */ + num_tests_run = 0; + break; + case CLENDLOG_SR: + /* Output the test plan as the last line */ + fprintf(file, "1..%d\n", num_tests_run); + fflush(file); + break; + case CLSTART_SR: + break; + case CLSTART_S: + break; + case CLEND_SR: + break; + case CLEND_S: + break; + case CLSTART_T: + break; + case CLEND_T: + /* Print the test result to the tap file */ + num_tests_run+=1; + tr = obj; + fprintf(file, "%s %d\n", tr->rtype == CK_PASS ? "ok" : "not ok", num_tests_run); + fflush(file); + break; + default: + eprintf("Bad event type received in tap_lfun", __FILE__, __LINE__); + } +} + #if ENABLE_SUBUNIT void subunit_lfun (SRunner *sr, FILE *file, enum print_output printmode, void *obj, enum cl_event evt) @@ -372,6 +434,18 @@ FILE *srunner_open_xmlfile (SRunner *sr) return f; } +FILE *srunner_open_tapfile (SRunner *sr) +{ + FILE *f = NULL; + if (srunner_has_tap (sr)) { + f = fopen(srunner_tap_fname(sr), "w"); + if (f == NULL) + eprintf ("Error in call to fopen while opening tap file %s:", __FILE__, __LINE__ - 2, + sr->tap_fname); + } + return f; +} + void srunner_init_logging (SRunner *sr, enum print_output print_mode) { FILE *f; @@ -392,6 +466,10 @@ void srunner_init_logging (SRunner *sr, enum print_output print_mode) if (f) { srunner_register_lfun (sr, f, 2, xml_lfun, print_mode); } + f = srunner_open_tapfile (sr); + if (f) { + srunner_register_lfun (sr, f, 2, tap_lfun, print_mode); + } srunner_send_evt (sr, NULL, CLINITLOG_SR); } diff --git a/src/check_log.h b/src/check_log.h index 3ed38ee..b4650fe 100644 --- a/src/check_log.h +++ b/src/check_log.h @@ -37,6 +37,9 @@ void lfile_lfun (SRunner *sr, FILE *file, enum print_output, void xml_lfun (SRunner *sr, FILE *file, enum print_output, void *obj, enum cl_event evt); +void tap_lfun (SRunner *sr, FILE *file, enum print_output, + void *obj, enum cl_event evt); + void subunit_lfun (SRunner *sr, FILE *file, enum print_output, void *obj, enum cl_event evt); @@ -45,6 +48,7 @@ void srunner_register_lfun (SRunner *sr, FILE *lfile, int close, FILE *srunner_open_lfile (SRunner *sr); FILE *srunner_open_xmlfile (SRunner *sr); +FILE *srunner_open_tapfile (SRunner *sr); void srunner_init_logging (SRunner *sr, enum print_output print_mode); void srunner_end_logging (SRunner *sr);