[libvirt] [PATCH 4/4] tests: Add VIR_TEST_REGENERATE_OUTPUT

Cole Robinson crobinso at redhat.com
Thu Apr 23 18:20:00 UTC 2015


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              |  7 +++++++
 docs/hacking.html.in | 12 ++++++++++++
 tests/testutils.c    | 45 +++++++++++++++++++++++++++------------------
 3 files changed, 46 insertions(+), 18 deletions(-)

diff --git a/HACKING b/HACKING
index 94d9d2c..cfc507a 100644
--- 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 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.
diff --git a/docs/hacking.html.in b/docs/hacking.html.in
index 53786b7..8f25bd4 100644
--- a/docs/hacking.html.in
+++ b/docs/hacking.html.in
@@ -164,6 +164,18 @@
 <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 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
diff --git a/tests/testutils.c b/tests/testutils.c
index 8367b4b..acb2ef1 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -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)
 {
-- 
2.3.5




More information about the libvir-list mailing list