Fix XML escapes in JUnit output

This commit is contained in:
flysand7 2023-06-25 04:36:29 +11:00
parent b3d38c1a10
commit 805ed17f93
1 changed files with 30 additions and 2 deletions

View File

@ -51,6 +51,8 @@ unsigned long random_between(int lo, int hi) {
// FORMATTING AND IO // FORMATTING AND IO
bool fmt_xml_escapes = false;
static void fprintc(FILE *file, char c) { static void fprintc(FILE *file, char c) {
fputc(c, file); fputc(c, file);
} }
@ -61,6 +63,27 @@ static void fprints(FILE *file, char *str) {
} }
} }
static void fprintc_maybe_xml(FILE *file, char c) {
if(c == '"' && fmt_xml_escapes) {
fprints(file, """);
}
else if(c == '&' && fmt_xml_escapes) {
fprints(file, "&");
}
else if(c == '<' && fmt_xml_escapes) {
fprints(file, "&lt;");
}
else if(c == '>' && fmt_xml_escapes) {
fprints(file, "&gt;");
}
else if(c == '\'' && fmt_xml_escapes) {
fprints(file, "&apos;");
}
else {
fprintc(file, c);
}
}
static void fprintd(FILE *file, int32_t number, int width) { static void fprintd(FILE *file, int32_t number, int width) {
if(number < 0) { if(number < 0) {
fprintc(file, '-'); fprintc(file, '-');
@ -109,14 +132,17 @@ static void fvprint_fmt(FILE *file, char *fmt, va_list args) {
} }
if(*fmt == 'c') { if(*fmt == 'c') {
int ch = va_arg(args, int); int ch = va_arg(args, int);
fprintc(file, ch); fprintc_maybe_xml(file, ch);
} }
else if(*fmt == '%') { else if(*fmt == '%') {
fprintc(file, '%'); fprintc(file, '%');
} }
else if(*fmt == 's') { else if(*fmt == 's') {
char *str = va_arg(args, char*); char *str = va_arg(args, char*);
fprints(file, str); while(*str != 0) {
fprintc_maybe_xml(file, *str);
++str;
}
} }
else if(*fmt == 'd') { else if(*fmt == 'd') {
int32_t i = va_arg(args, int32_t); int32_t i = va_arg(args, int32_t);
@ -338,6 +364,7 @@ static void print_test_results(Test_Feature *features) {
// JUNIT OUTPUT // JUNIT OUTPUT
static void junit_write(char *path, Test_Feature *features) { static void junit_write(char *path, Test_Feature *features) {
fmt_xml_escapes = true;
FILE *xml = fopen(path, "wb"); FILE *xml = fopen(path, "wb");
// TODO: store tests and failures in an object instead of calculating it like that // TODO: store tests and failures in an object instead of calculating it like that
int total_test_count = 0; int total_test_count = 0;
@ -371,6 +398,7 @@ static void junit_write(char *path, Test_Feature *features) {
} }
fprint_fmt(xml, "</testsuites>\n"); fprint_fmt(xml, "</testsuites>\n");
fclose(xml); fclose(xml);
fmt_xml_escapes = false;
} }
// TEST MACROS // TEST MACROS