summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2016-09-21 14:22:52 +0200
committerAndreas Schneider <asn@cryptomilk.org>2016-09-21 14:48:54 +0200
commit7c5572f039530b45cb2e7a798c695a090160ea54 (patch)
tree8ca5a8ec92cd4e9fe895ba42bc0fd9ec12daee78
parent82a8748cd27e403b5a34f5eca571699de6f14403 (diff)
downloadcmocka-7c5572f039530b45cb2e7a798c695a090160ea54.tar.gz
cmocka-7c5572f039530b45cb2e7a798c695a090160ea54.tar.bz2
cmocka-7c5572f039530b45cb2e7a798c695a090160ea54.zip
cmocka: Do not add xml headers twice
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
-rw-r--r--doc/mainpage.dox7
-rw-r--r--src/cmocka.c33
2 files changed, 34 insertions, 6 deletions
diff --git a/doc/mainpage.dox b/doc/mainpage.dox
index 925bfd2..329b840 100644
--- a/doc/mainpage.dox
+++ b/doc/mainpage.dox
@@ -124,7 +124,10 @@ The case doesn't matter.
The XML output goes to stderr by default. If the environment variable
<tt>CMOCKA_XML_FILE</tt> exists and the file specified by this variable
-doesn't exist yet, then cmocka will put the output to this file.
-
+doesn't exist yet, then cmocka will put the output to this file. Note
+that if you are have several groups you should set <tt>CMOCKA_XML_FILE</tt>
+to <tt>CMOCKA_XML_FILE=cm_%g.xml</tt>. In this %g will be replaced by
+the group_name of the test and a file will be created for each group,
+othwerwise all groups will be printed into the same file.
*/
diff --git a/src/cmocka.c b/src/cmocka.c
index 7d4679e..3fd1af8 100644
--- a/src/cmocka.c
+++ b/src/cmocka.c
@@ -428,7 +428,8 @@ static void set_source_location(
static int c_strreplace(char *src,
size_t src_len,
const char *pattern,
- const char *repl)
+ const char *repl,
+ int *str_replaced)
{
char *p = NULL;
@@ -454,6 +455,9 @@ static int c_strreplace(char *src,
strncpy(src + of, repl, rl);
+ if (str_replaced != NULL) {
+ *str_replaced = 1;
+ }
p = strstr(src, pattern);
} while (p != NULL);
@@ -2147,6 +2151,9 @@ enum cm_printf_type {
PRINTF_TEST_SKIPPED,
};
+static int xml_printed;
+static int file_append;
+
static void cmprintf_group_finish_xml(const char *group_name,
size_t total_executed,
size_t total_failed,
@@ -2157,6 +2164,7 @@ static void cmprintf_group_finish_xml(const char *group_name,
{
FILE *fp = stdout;
int file_opened = 0;
+ int multiple_files = 0;
char *env;
size_t i;
@@ -2167,7 +2175,7 @@ static void cmprintf_group_finish_xml(const char *group_name,
snprintf(buf, sizeof(buf), "%s", env);
- rc = c_strreplace(buf, sizeof(buf), "%g", group_name);
+ rc = c_strreplace(buf, sizeof(buf), "%g", group_name, &multiple_files);
if (rc < 0) {
snprintf(buf, sizeof(buf), "%s", env);
}
@@ -2176,17 +2184,34 @@ static void cmprintf_group_finish_xml(const char *group_name,
if (fp == NULL) {
fp = fopen(buf, "w");
if (fp != NULL) {
+ file_append = 1;
file_opened = 1;
} else {
fp = stderr;
}
} else {
fclose(fp);
- fp = stderr;
+ if (file_append) {
+ fp = fopen(buf, "a");
+ if (fp != NULL) {
+ file_opened = 1;
+ xml_printed = 1;
+ } else {
+ fp = stderr;
+ }
+ } else {
+ fp = stderr;
+ }
+ }
+ }
+
+ if (!xml_printed || (file_opened && !file_append)) {
+ fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
+ if (!file_opened) {
+ xml_printed = 1;
}
}
- fprintf(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fprintf(fp, "<testsuites>\n");
fprintf(fp, " <testsuite name=\"%s\" time=\"%.3f\" "
"tests=\"%u\" failures=\"%u\" errors=\"%u\" skipped=\"%u\" >\n",