@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
@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
<?xml version="1.0"?>
@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
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");
}
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,
{
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);
{
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);
#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");
}
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");
}
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");
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);