From: hugo303 Date: Mon, 15 Oct 2012 16:45:20 +0000 (+0000) Subject: Added support for setting log files via environment variables X-Git-Tag: 0.10.0~547 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=83d8535e54a8cd44adb36af44f876a35ce994469;p=check Added support for setting log files via environment variables git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@632 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/NEWS b/NEWS index 84ac3c6..73fa32c 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ In development. +* Added support for setting log files via environment variables. + Patch #3576713 on SF. + * Added better pkg-config and subunit support, patch #3417041 on SF. * Make tests/test_vars.in bourne shell compatible, bug #3472578 on SF. diff --git a/doc/check.texi b/doc/check.texi index a050e47..b1c0536 100644 --- a/doc/check.texi +++ b/doc/check.texi @@ -1318,6 +1318,13 @@ Results for all suites run: @end verbatim @end example +Another way to enable test logging is to use the @code{CK_LOG_FILE_NAME} +environment variable. When set tests will be logged to the specified file name. +If log file is specified with both @code{CK_LOG_FILE_NAME} and +@code{srunner_set_log()}, the name provided to @code{srunner_set_log()} will +be used. + + @menu * XML Logging:: @end menu @@ -1338,9 +1345,8 @@ const char *srunner_xml_fname (SRunner *sr); @end verbatim @end example -The only thing you need to do to get XML output is call -@code{srunner_set_xml()} before the tests are run. Here is an example -of the same log output as before but in XML: +XML output is enabled by a call to @code{srunner_set_xml()} before the tests +are run. Here is an example of the same log output as before but in XML: @example @verbatim @@ -1385,6 +1391,16 @@ of the same log output as before but in XML: @end verbatim @end example +XML logging can be enabled by an environment variable as well. If +@code{CK_XML_LOG_FILE_NAME} environment variable is set, the XML test log will +be written to specified file name. If XML log file is specified with both +@code{CK_XML_LOG_FILE_NAME} and @code{srunner_set_xml()}, the name provided +to @code{srunner_set_xml()} will be used. + +If both plain text and XML log files are specified, by any of above methods, +then check will log to both files. In other words logging in plain text and XML +format simultaneously is supported. + @node Subunit Support, , Test Logging, Advanced Features @section Subunit Support diff --git a/src/check_log.c b/src/check_log.c index c865d2c..7c1a1a4 100644 --- a/src/check_log.c +++ b/src/check_log.c @@ -46,12 +46,16 @@ void srunner_set_log (SRunner *sr, const char *fname) int srunner_has_log (SRunner *sr) { - return sr->log_fname != NULL; + return srunner_log_fname(sr) != NULL; } const char *srunner_log_fname (SRunner *sr) { - return sr->log_fname; + /* check if log filename have been set explicitly */ + if (sr->log_fname != NULL) + return sr->log_fname; + + return getenv("CK_LOG_FILE_NAME"); } @@ -64,12 +68,17 @@ void srunner_set_xml (SRunner *sr, const char *fname) int srunner_has_xml (SRunner *sr) { - return sr->xml_fname != NULL; + return srunner_xml_fname(sr) != NULL; } const char *srunner_xml_fname (SRunner *sr) { - return sr->xml_fname; + /* check if XML log filename have been set explicitly */ + if (sr->xml_fname != NULL) { + return sr->xml_fname; + } + + return getenv("CK_XML_LOG_FILE_NAME"); } void srunner_register_lfun (SRunner *sr, FILE *lfile, int close, @@ -344,7 +353,7 @@ FILE *srunner_open_lfile (SRunner *sr) { FILE *f = NULL; if (srunner_has_log (sr)) { - f = fopen(sr->log_fname, "w"); + f = fopen(srunner_log_fname(sr), "w"); if (f == NULL) eprintf ("Error in call to fopen while opening log file %s:", __FILE__, __LINE__ - 2, sr->log_fname); @@ -356,7 +365,7 @@ FILE *srunner_open_xmlfile (SRunner *sr) { FILE *f = NULL; if (srunner_has_xml (sr)) { - f = fopen(sr->xml_fname, "w"); + f = fopen(srunner_xml_fname(sr), "w"); if (f == NULL) eprintf ("Error in call to fopen while opening xml file %s:", __FILE__, __LINE__ - 2, sr->xml_fname); diff --git a/tests/check_check_log.c b/tests/check_check_log.c index 1a4a14f..96bf58c 100644 --- a/tests/check_check_log.c +++ b/tests/check_check_log.c @@ -7,6 +7,27 @@ #include "check_check.h" +/* save environment variable's value and set new value */ +static int save_set_env(const char *name, const char *value, + const char **old_value) +{ + *old_value = getenv(name); + return setenv(name, value, 1); +} + +/* restore environment variable's old value, handle cases where + variable must be unset (old value is NULL) */ +static int restore_env(const char *name, const char *old_value) +{ + int res; + if (old_value == NULL) { + res = unsetenv(name); + } else { + res = setenv(name, old_value, 1); + } + return res; +} + START_TEST(test_set_log) { Suite *s = suite_create("Suite"); @@ -20,6 +41,35 @@ START_TEST(test_set_log) } END_TEST +/* Test enabling logging via environment variable */ +START_TEST(test_set_log_env) +{ + const char *old_val; + Suite *s = suite_create("Suite"); + SRunner *sr = srunner_create(s); + + /* check that setting log file via environment variable works */ + fail_unless(save_set_env("CK_LOG_FILE_NAME", "test_log", &old_val) == 0, + "Failed to set environment variable"); + + fail_unless (srunner_has_log (sr), "SRunner not logging"); + fail_unless (strcmp(srunner_log_fname(sr), "test_log") == 0, + "Bad file name returned"); + + /* check that explicit call to srunner_set_log() + overrides environment variable */ + srunner_set_log (sr, "test2_log"); + + fail_unless (srunner_has_log (sr), "SRunner not logging"); + fail_unless (strcmp(srunner_log_fname(sr), "test2_log") == 0, + "Bad file name returned"); + + /* restore old environment */ + fail_unless(restore_env("CK_LOG_FILE_NAME", old_val) == 0, + "Failed to restore environment variable"); +} +END_TEST + START_TEST(test_no_set_log) { Suite *s = suite_create("Suite"); @@ -57,6 +107,35 @@ START_TEST(test_set_xml) } END_TEST +/* Test enabling XML logging via environment variable */ +START_TEST(test_set_xml_env) +{ + const char *old_val; + Suite *s = suite_create("Suite"); + SRunner *sr = srunner_create(s); + + /* check that setting XML log file via environment variable works */ + fail_unless(save_set_env("CK_XML_LOG_FILE_NAME", "test_log.xml", &old_val) == 0, + "Failed to set environment variable"); + + fail_unless (srunner_has_xml (sr), "SRunner not logging XML"); + fail_unless (strcmp(srunner_xml_fname(sr), "test_log.xml") == 0, + "Bad file name returned"); + + /* check that explicit call to srunner_set_xml() + overrides environment variable */ + srunner_set_xml (sr, "test2_log.xml"); + + fail_unless (srunner_has_xml (sr), "SRunner not logging XML"); + fail_unless (strcmp(srunner_xml_fname(sr), "test2_log.xml") == 0, + "Bad file name returned"); + + /* restore old environment */ + fail_unless(restore_env("CK_XML_LOG_FILE_NAME", old_val) == 0, + "Failed to restore environment variable"); +} +END_TEST + START_TEST(test_no_set_xml) { Suite *s = suite_create("Suite"); @@ -92,11 +171,13 @@ Suite *make_log_suite(void) suite_add_tcase(s, tc_core); tcase_add_test(tc_core, test_set_log); + tcase_add_test(tc_core, test_set_log_env); tcase_add_test(tc_core, test_no_set_log); tcase_add_test(tc_core, test_double_set_log); suite_add_tcase(s, tc_core_xml); tcase_add_test(tc_core_xml, test_set_xml); + tcase_add_test(tc_core_xml, test_set_xml_env); tcase_add_test(tc_core_xml, test_no_set_xml); tcase_add_test(tc_core_xml, test_double_set_xml);