]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
tests: Add VIR_TEST_REGENERATE_OUTPUT
authorCole Robinson <crobinso@redhat.com>
Thu, 23 Apr 2015 18:02:57 +0000 (14:02 -0400)
committerCole Robinson <crobinso@redhat.com>
Thu, 23 Apr 2015 21:08:48 +0000 (17:08 -0400)
If this enviroment variable is set, the virTestCompareToFile helper
will overwrite the file content we are comparing against, if the
file doesn't exist or it doesn't match the expected input.

This is useful when adding new test cases, or making changes that
generate a lot of output churn.

HACKING
docs/hacking.html.in
tests/testutils.c

diff --git a/HACKING b/HACKING
index 94d9d2cb45ac2ddc231bce79c289d50b6bfa413e..fbe838ba7886c868f1052eb3614ee7d96a929b7c 100644 (file)
--- a/HACKING
+++ b/HACKING
@@ -136,6 +136,13 @@ Also, individual tests can be run from inside the "tests/" directory, like:
 
   ./qemuxml2xmltest
 
+If you are adding new test cases, or making changes that alter existing test
+output, you can use the environment variable VIR_TEST_REGENERATE_OUTPUT to
+quickly update the saved test data. Of course you still need to review the
+changes VERY CAREFULLY to ensure they are correct.
+
+  VIR_TEST_REGENERATE_OUTPUT=1 ./qemuxml2argvtest
+
 There is also a "./run" script at the top level, to make it easier to run
 programs that have not yet been installed, as well as to wrap invocations of
 various tests under gdb or Valgrind.
index 53786b7d2afd9ee725dab90472d1ce1fd11abf41..408ea5043a762da924b81937b7dcee1ac65332bb 100644 (file)
 <pre>
   ./qemuxml2xmltest
 </pre>
+
+        <p>
+          If you are adding new test cases, or making changes that alter
+          existing test output, you can use the environment variable
+          VIR_TEST_REGENERATE_OUTPUT to quickly update the saved test data.
+          Of course you still need to review the changes VERY CAREFULLY to
+          ensure they are correct.
+        </p>
+<pre>
+  VIR_TEST_REGENERATE_OUTPUT=1 ./qemuxml2argvtest
+</pre>
+
         <p>There is also a <code>./run</code> script at the top level,
           to make it easier to run programs that have not yet been
           installed, as well as to wrap invocations of various tests
index 8367b4b09e2c1b505281b8e0846497b6c4f6c7dc..acb2ef106497771259e7abf6a1ece33779797390 100644 (file)
@@ -91,6 +91,21 @@ bool virtTestOOMActive(void)
     return testOOMActive;
 }
 
+static unsigned int
+virTestGetFlag(const char *name)
+{
+    char *flagStr;
+    unsigned int flag;
+
+    if ((flagStr = getenv(name)) == NULL)
+        return 0;
+
+    if (virStrToLong_ui(flagStr, NULL, 10, &flag) < 0)
+        return 0;
+
+    return flag;
+}
+
 #ifdef TEST_OOM_TRACE
 static void virTestAllocHook(int nalloc ATTRIBUTE_UNUSED,
                              void *opaque ATTRIBUTE_UNUSED)
@@ -607,21 +622,30 @@ virtTestCompareToFile(const char *strcontent,
     int ret = -1;
     char *filecontent = NULL;
     char *fixedcontent = NULL;
+    bool regenerate = !!virTestGetFlag("VIR_TEST_REGENERATE_OUTPUT");
 
-    if (virtTestLoadFile(filename, &filecontent) < 0)
+    if (virtTestLoadFile(filename, &filecontent) < 0 && !regenerate)
         goto failure;
 
-    if (filecontent[strlen(filecontent) - 1] == '\n' &&
+    if (filecontent &&
+        filecontent[strlen(filecontent) - 1] == '\n' &&
         strcontent[strlen(strcontent) - 1] != '\n') {
         if (virAsprintf(&fixedcontent, "%s\n", strcontent) < 0)
             goto failure;
     }
 
-    if (STRNEQ(fixedcontent ? fixedcontent : strcontent, filecontent)) {
+    if (STRNEQ_NULLABLE(fixedcontent ? fixedcontent : strcontent,
+                        filecontent)) {
+        if (regenerate) {
+            if (virFileWriteStr(filename, strcontent, 0666) < 0)
+                goto failure;
+            goto out;
+        }
         virtTestDifference(stderr, strcontent, filecontent);
         goto failure;
     }
 
+ out:
     ret = 0;
  failure:
     VIR_FREE(fixedcontent);
@@ -692,21 +716,6 @@ virtTestLogContentAndReset(void)
 }
 
 
-static unsigned int
-virTestGetFlag(const char *name)
-{
-    char *flagStr;
-    unsigned int flag;
-
-    if ((flagStr = getenv(name)) == NULL)
-        return 0;
-
-    if (virStrToLong_ui(flagStr, NULL, 10, &flag) < 0)
-        return 0;
-
-    return flag;
-}
-
 unsigned int
 virTestGetDebug(void)
 {