From 4e1492f0fa3a48e5fa88d8623e47e50bb8527c87 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 14 Mar 2024 15:52:54 -0400 Subject: Pretty-print pytest xml results (if xmllint is present) * Adds Delivery.storage.results_dir member * Exposes storage.results_dir to templates * This is to make the test results human-readable * xmllint is optional. If it isn't installed it isn't a big deal. --- src/utils.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 62bf58a..047a266 100644 --- a/src/utils.c +++ b/src/utils.c @@ -532,4 +532,62 @@ int path_store(char **destptr, size_t maxlen, const char *base, const char *path l_path_setup_error: guard_free(path_tmp); return -1; +} + +int xml_pretty_print_in_place(const char *filename, const char *pretty_print_prog, const char *pretty_print_args) { + int status = 0; + char *tempfile = NULL; + char *result = NULL; + FILE *fp = NULL; + FILE *tmpfp = NULL; + char cmd[PATH_MAX]; + if (!find_program(pretty_print_prog)) { + // Pretty printing is optional. 99% chance the XML data will + // be passed to a report generator; not inspected by a human. + return 0; + } + memset(cmd, 0, sizeof(cmd)); + snprintf(cmd, sizeof(cmd) - 1, "%s %s %s", pretty_print_prog, pretty_print_args, filename); + result = shell_output(cmd, &status); + if (status || !result) { + goto pretty_print_failed; + } + + tempfile = xmkstemp(&tmpfp, "w+"); + if (!tmpfp || !tempfile) { + goto pretty_print_failed; + } + + fprintf(tmpfp, "%s", result); + fflush(tmpfp); + fclose(tmpfp); + + fp = fopen(filename, "w+"); + if (!fp) { + goto pretty_print_failed; + } + + if (copy2(tempfile, filename, CT_PERM)) { + goto pretty_print_failed; + } + + if (remove(tempfile)) { + goto pretty_print_failed; + } + + guard_free(tempfile); + guard_free(result); + return 0; + + pretty_print_failed: + fprintf(SYSERROR); + if (fp) { + fclose(fp); + } + if (tmpfp) { + fclose(tmpfp); + } + guard_free(tempfile); + guard_free(result); + return -1; } \ No newline at end of file -- cgit