Commit baa5c20d0815441cac2d2135d2b0190cb543e637

Edward Thomson 2018-08-26T15:31:14

clar: accept a value for the summary filename Accept an (optional) value for the summary filename. Continues to default to summary.xml.

diff --git a/tests/clar.c b/tests/clar.c
index fd5fcb6..3eecd78 100644
--- a/tests/clar.c
+++ b/tests/clar.c
@@ -139,6 +139,7 @@ static struct {
 	int exit_on_error;
 	int report_suite_names;
 	int write_summary;
+	const char *summary_file;
 
 	struct clar_explicit *explicit;
 	struct clar_explicit *last_explicit;
@@ -183,6 +184,10 @@ static void clar_print_onabort(const char *msg, ...);
 static void clar_unsandbox(void);
 static int clar_sandbox(void);
 
+/* From summary.h */
+static int clar_summary_init(const char *filename);
+static void clar_summary_shutdown(void);
+
 /* Load the declarations for the test suite */
 #include "clar.suite"
 
@@ -466,6 +471,8 @@ clar_parse_args(int argc, char **argv)
 
 		case 'r':
 			_clar.write_summary = 1;
+			_clar.summary_file = *(argument + 2) ? (argument + 2) :
+			    "summary.xml";
 			break;
 
 		default:
@@ -486,6 +493,11 @@ clar_test_init(int argc, char **argv)
 	if (argc > 1)
 		clar_parse_args(argc, argv);
 
+	if (_clar.write_summary && !clar_summary_init(_clar.summary_file)) {
+		clar_print_onabort("Failed to open the summary file: %s\n");
+		exit(-1);
+	}
+
 	if (clar_sandbox() < 0) {
 		clar_print_onabort("Failed to sandbox the test runner.\n");
 		exit(-1);
@@ -509,8 +521,6 @@ clar_test_run(void)
 	return _clar.total_errors;
 }
 
-static void clar_summary_write(void);
-
 void
 clar_test_shutdown(void)
 {
@@ -526,7 +536,7 @@ clar_test_shutdown(void)
 	clar_unsandbox();
 
 	if (_clar.write_summary)
-		clar_summary_write();
+		clar_summary_shutdown();
 
 	for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
 		explicit_next = explicit->next;
diff --git a/tests/clar/summary.h b/tests/clar/summary.h
index 452c58c..a6a475f 100644
--- a/tests/clar/summary.h
+++ b/tests/clar/summary.h
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <time.h>
 
+static const char *filename;
 static FILE *summary;
 
 int clar_summary_close_tag(const char *tag, int indent)
@@ -51,17 +52,19 @@ int clar_summary_failure(const char *type, const char *message, const char *desc
 	return fprintf(summary, "\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n", type, message, desc);
 }
 
-void clar_summary_write(void)
+int clar_summary_init(const char *fn)
+{
+	filename = fn;
+
+	summary = fopen(filename, "w");
+
+	return !!summary;
+}
+
+void clar_summary_shutdown(void)
 {
 	struct clar_report *report;
 	const char *last_suite = NULL;
-	char wd[1024];
-
-	summary = fopen("summary.xml", "w");
-	if (!summary) {
-		printf("failed to open summary.xml for writing\n");
-		return;
-	}
 
 	clar_summary_testsuites();
 
@@ -94,5 +97,5 @@ void clar_summary_write(void)
 
 	fclose(summary);
 
-	printf("written summary file to %s\n", getcwd((char *)&wd, sizeof(wd)));
+	printf("written summary file to %s\n", filename);
 }