|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include <time.h> |
| 4 | + |
| 5 | +static FILE *summary; |
| 6 | + |
| 7 | +int clar_summary_close_tag(const char *tag, int indent) |
| 8 | +{ |
| 9 | + const char *indt; |
| 10 | + |
| 11 | + if (indent == 0) indt = ""; |
| 12 | + else if (indent == 1) indt = "\t"; |
| 13 | + else indt = "\t\t"; |
| 14 | + |
| 15 | + return fprintf(summary, "%s</%s>\n", indt, tag); |
| 16 | +} |
| 17 | + |
| 18 | +int clar_summary_testsuites(void) |
| 19 | +{ |
| 20 | + return fprintf(summary, "<testsuites>\n"); |
| 21 | +} |
| 22 | + |
| 23 | +int clar_summary_testsuite(int idn, const char *name, const char *pkg, time_t timestamp, double time, int test_count, int fail_count, int error_count) |
| 24 | +{ |
| 25 | + struct tm *tm = localtime(×tamp); |
| 26 | + char iso_dt[20]; |
| 27 | + |
| 28 | + if (strftime(iso_dt, sizeof(iso_dt), "%FT%T", tm) == 0) |
| 29 | + return -1; |
| 30 | + |
| 31 | + return fprintf(summary, "\t<testsuite " |
| 32 | + " id=\"%d\"" |
| 33 | + " name=\"%s\"" |
| 34 | + " package=\"%s\"" |
| 35 | + " hostname=\"localhost\"" |
| 36 | + " timestamp=\"%s\"" |
| 37 | + " time=\"%.2f\"" |
| 38 | + " tests=\"%d\"" |
| 39 | + " failures=\"%d\"" |
| 40 | + " errors=\"%d\">\n", |
| 41 | + idn, name, pkg, iso_dt, time, test_count, fail_count, error_count); |
| 42 | +} |
| 43 | + |
| 44 | +int clar_summary_testcase(const char *name, const char *classname, double time) |
| 45 | +{ |
| 46 | + return fprintf(summary, "\t\t<testcase name=\"%s\" classname=\"%s\" time=\"%.2f\">\n", name, classname, time); |
| 47 | +} |
| 48 | + |
| 49 | +int clar_summary_failure(const char *type, const char *message, const char *desc) |
| 50 | +{ |
| 51 | + return fprintf(summary, "\t\t\t<failure type=\"%s\"><![CDATA[%s\n%s]]></failure>\n", type, message, desc); |
| 52 | +} |
| 53 | + |
| 54 | +void clar_summary_write(void) |
| 55 | +{ |
| 56 | + struct clar_report *report; |
| 57 | + const char *last_suite = NULL; |
| 58 | + char wd[1024]; |
| 59 | + |
| 60 | + summary = fopen("summary.xml", "w"); |
| 61 | + if (!summary) { |
| 62 | + printf("failed to open summary.xml for writing\n"); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + clar_summary_testsuites(); |
| 67 | + |
| 68 | + report = _clar.reports; |
| 69 | + while (report != NULL) { |
| 70 | + struct clar_error *error = report->errors; |
| 71 | + |
| 72 | + if (last_suite == NULL || strcmp(last_suite, report->suite) != 0) { |
| 73 | + clar_summary_testsuite(0, report->suite, "", time(NULL), 0, _clar.tests_ran, _clar.total_errors, 0); |
| 74 | + } |
| 75 | + |
| 76 | + last_suite = report->suite; |
| 77 | + |
| 78 | + clar_summary_testcase(report->test, "what", 0); |
| 79 | + |
| 80 | + while (error != NULL) { |
| 81 | + clar_summary_failure("assert", error->error_msg, error->description); |
| 82 | + error = error->next; |
| 83 | + } |
| 84 | + |
| 85 | + clar_summary_close_tag("testcase", 2); |
| 86 | + |
| 87 | + report = report->next; |
| 88 | + |
| 89 | + if (!report || strcmp(last_suite, report->suite) != 0) |
| 90 | + clar_summary_close_tag("testsuite", 1); |
| 91 | + } |
| 92 | + |
| 93 | + clar_summary_close_tag("testsuites", 0); |
| 94 | + |
| 95 | + fclose(summary); |
| 96 | + |
| 97 | + printf("written summary file to %s\n", getcwd((char *)&wd, sizeof(wd))); |
| 98 | +} |
0 commit comments