clar: remove globals; error-check fprintf/fclose Remove the global summary filename and file pointer; pass them in to the summary functions as needed. Error check the results of buffered I/O calls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
diff --git a/tests/clar.c b/tests/clar.c
index 3eecd78..025f2f3 100644
--- a/tests/clar.c
+++ b/tests/clar.c
@@ -123,6 +123,11 @@ struct clar_report {
struct clar_report *next;
};
+struct clar_summary {
+ const char *filename;
+ FILE *fp;
+};
+
static struct {
enum cl_test_status test_status;
@@ -138,8 +143,10 @@ static struct {
int report_errors_only;
int exit_on_error;
int report_suite_names;
+
int write_summary;
- const char *summary_file;
+ const char *summary_filename;
+ struct clar_summary *summary;
struct clar_explicit *explicit;
struct clar_explicit *last_explicit;
@@ -185,8 +192,8 @@ 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);
+static struct clar_summary *clar_summary_init(const char *filename);
+static int clar_summary_shutdown(struct clar_summary *fp);
/* Load the declarations for the test suite */
#include "clar.suite"
@@ -352,14 +359,14 @@ clar_usage(const char *arg)
{
printf("Usage: %s [options]\n\n", arg);
printf("Options:\n");
- printf(" -sname\tRun only the suite with `name` (can go to individual test name)\n");
- printf(" -iname\tInclude the suite with `name`\n");
- printf(" -xname\tExclude the suite with `name`\n");
- printf(" -v \tIncrease verbosity (show suite names)\n");
- printf(" -q \tOnly report tests that had an error\n");
- printf(" -Q \tQuit as soon as a test fails\n");
- printf(" -l \tPrint suite names\n");
- printf(" -r \tWrite summary file\n");
+ printf(" -sname Run only the suite with `name` (can go to individual test name)\n");
+ printf(" -iname Include the suite with `name`\n");
+ printf(" -xname Exclude the suite with `name`\n");
+ printf(" -v Increase verbosity (show suite names)\n");
+ printf(" -q Only report tests that had an error\n");
+ printf(" -Q Quit as soon as a test fails\n");
+ printf(" -l Print suite names\n");
+ printf(" -r[filename] Write summary file (to the optional filename)\n");
exit(-1);
}
@@ -471,7 +478,7 @@ clar_parse_args(int argc, char **argv)
case 'r':
_clar.write_summary = 1;
- _clar.summary_file = *(argument + 2) ? (argument + 2) :
+ _clar.summary_filename = *(argument + 2) ? (argument + 2) :
"summary.xml";
break;
@@ -493,8 +500,9 @@ 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");
+ if (_clar.write_summary &&
+ !(_clar.summary = clar_summary_init(_clar.summary_filename))) {
+ clar_print_onabort("Failed to open the summary file\n");
exit(-1);
}
@@ -535,8 +543,10 @@ clar_test_shutdown(void)
clar_unsandbox();
- if (_clar.write_summary)
- clar_summary_shutdown();
+ if (_clar.write_summary && clar_summary_shutdown(_clar.summary) < 0) {
+ clar_print_onabort("Failed to write the summary file\n");
+ exit(-1);
+ }
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 a6a475f..491f345 100644
--- a/tests/clar/summary.h
+++ b/tests/clar/summary.h
@@ -2,10 +2,8 @@
#include <stdio.h>
#include <time.h>
-static const char *filename;
-static FILE *summary;
-
-int clar_summary_close_tag(const char *tag, int indent)
+int clar_summary_close_tag(
+ struct clar_summary *summary, const char *tag, int indent)
{
const char *indt;
@@ -13,15 +11,17 @@ int clar_summary_close_tag(const char *tag, int indent)
else if (indent == 1) indt = "\t";
else indt = "\t\t";
- return fprintf(summary, "%s</%s>\n", indt, tag);
+ return fprintf(summary->fp, "%s</%s>\n", indt, tag);
}
-int clar_summary_testsuites(void)
+int clar_summary_testsuites(struct clar_summary *summary)
{
- return fprintf(summary, "<testsuites>\n");
+ return fprintf(summary->fp, "<testsuites>\n");
}
-int clar_summary_testsuite(int idn, const char *name, const char *pkg, time_t timestamp, double elapsed, int test_count, int fail_count, int error_count)
+int clar_summary_testsuite(struct clar_summary *summary,
+ int idn, const char *name, const char *pkg, time_t timestamp,
+ double elapsed, int test_count, int fail_count, int error_count)
{
struct tm *tm = localtime(×tamp);
char iso_dt[20];
@@ -29,7 +29,7 @@ int clar_summary_testsuite(int idn, const char *name, const char *pkg, time_t ti
if (strftime(iso_dt, sizeof(iso_dt), "%FT%T", tm) == 0)
return -1;
- return fprintf(summary, "\t<testsuite "
+ return fprintf(summary->fp, "\t<testsuite "
" id=\"%d\""
" name=\"%s\""
" package=\"%s\""
@@ -42,60 +42,93 @@ int clar_summary_testsuite(int idn, const char *name, const char *pkg, time_t ti
idn, name, pkg, iso_dt, elapsed, test_count, fail_count, error_count);
}
-int clar_summary_testcase(const char *name, const char *classname, double elapsed)
+int clar_summary_testcase(struct clar_summary *summary,
+ const char *name, const char *classname, double elapsed)
{
- return fprintf(summary, "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n", name, classname, elapsed);
+ return fprintf(summary->fp,
+ "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n",
+ name, classname, elapsed);
}
-int clar_summary_failure(const char *type, const char *message, const char *desc)
+int clar_summary_failure(struct clar_summary *summary,
+ 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);
+ return fprintf(summary->fp,
+ "\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n",
+ type, message, desc);
}
-int clar_summary_init(const char *fn)
+struct clar_summary *clar_summary_init(const char *filename)
{
- filename = fn;
+ struct clar_summary *summary;
+ FILE *fp;
+
+ if ((fp = fopen(filename, "w")) == NULL)
+ return NULL;
+
+ if ((summary = malloc(sizeof(struct clar_summary))) == NULL) {
+ fclose(fp);
+ return NULL;
+ }
- summary = fopen(filename, "w");
+ summary->filename = filename;
+ summary->fp = fp;
- return !!summary;
+ return summary;
}
-void clar_summary_shutdown(void)
+int clar_summary_shutdown(struct clar_summary *summary)
{
struct clar_report *report;
const char *last_suite = NULL;
- clar_summary_testsuites();
+ if (clar_summary_testsuites(summary) < 0)
+ goto on_error;
report = _clar.reports;
while (report != NULL) {
struct clar_error *error = report->errors;
if (last_suite == NULL || strcmp(last_suite, report->suite) != 0) {
- clar_summary_testsuite(0, report->suite, "", time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0);
+ if (clar_summary_testsuite(summary, 0, report->suite, "",
+ time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0) < 0)
+ goto on_error;
}
last_suite = report->suite;
- clar_summary_testcase(report->test, "what", 0);
+ clar_summary_testcase(summary, report->test, "what", 0);
while (error != NULL) {
- clar_summary_failure("assert", error->error_msg, error->description);
+ if (clar_summary_failure(summary, "assert",
+ error->error_msg, error->description) < 0)
+ goto on_error;
+
error = error->next;
}
- clar_summary_close_tag("testcase", 2);
+ if (clar_summary_close_tag(summary, "testcase", 2) < 0)
+ goto on_error;
report = report->next;
- if (!report || strcmp(last_suite, report->suite) != 0)
- clar_summary_close_tag("testsuite", 1);
+ if (!report || strcmp(last_suite, report->suite) != 0) {
+ if (clar_summary_close_tag(summary, "testsuite", 1) < 0)
+ goto on_error;
+ }
}
- clar_summary_close_tag("testsuites", 0);
+ if (clar_summary_close_tag(summary, "testsuites", 0) < 0 ||
+ fclose(summary->fp) != 0)
+ goto on_error;
+
+ printf("written summary file to %s\n", summary->filename);
- fclose(summary);
+ free(summary);
+ return 0;
- printf("written summary file to %s\n", filename);
+on_error:
+ fclose(summary->fp);
+ free(summary);
+ return -1;
}