From edb27a7048fa6de2c71dd5005cbed9c922c75b72 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 23 Apr 2015 14:02:57 -0400 Subject: [PATCH] tests: Add VIR_TEST_REGENERATE_OUTPUT 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 94d9d2cb45..fbe838ba78 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 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. diff --git a/docs/hacking.html.in b/docs/hacking.html.in index 53786b7d2a..408ea5043a 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -164,6 +164,18 @@
   ./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 diff --git a/tests/testutils.c b/tests/testutils.c index 8367b4b09e..acb2ef1064 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) {