cmocka: Do not add xml headers twice
authorAndreas Schneider <asn@cryptomilk.org>
Wed, 21 Sep 2016 12:22:52 +0000 (14:22 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 21 Sep 2016 12:48:54 +0000 (14:48 +0200)
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
doc/mainpage.dox
src/cmocka.c

index 925bfd210ef4b134d216d79c482ed67622037511..329b840f217d9515a63a61eb7bc30d7b203ca86e 100644 (file)
@@ -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.
 
 */
index 7d4679eba0e47fc3d97e8e961e4264b3d1f8ddee..3fd1af8a3c67fd01d7e088eab179cc98383b4a0f 100644 (file)
@@ -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",