[PATCH 9/9] tests: Use virTestCompareToString() more

Michal Privoznik mprivozn at redhat.com
Wed Nov 30 09:48:11 UTC 2022


Instead of using:

  if (STRNEQ(a, b)) {
      virTestDifference(stderr, a, b);
      ...
  }

we can use:

  if (virTestCompareToString(a, b) < ) {
      ...
  }

Generated by the following spatch:

  @@
  expression a, b;
  @@

  - if (STRNEQ(a, b)) {
  + if (virTestCompareToString(a, b) < 0) {
      ...
  -   virTestDifference(stderr, a, b);
      ...
      }

and its variations (STRNEQ_NULLABLE() instead of STRNEQ(), then
in some cases variables passed to STRNEQ() are in reversed order
when compared to virTestCompareToString()).

However, coccinelle failed to recognize the pattern in
testNWFilterEBIPTablesAllTeardown() so I had to fix it manually.
Also, I manually fixed testFormat() in tests/sockettest.c as I
didn't bother writing another spatch rule just for that.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tests/commandtest.c              | 33 +++++++++++---------------------
 tests/esxutilstest.c             | 10 +++-------
 tests/nwfilterebiptablestest.c   | 21 +++++++-------------
 tests/openvzutilstest.c          |  6 ++----
 tests/sockettest.c               |  3 +--
 tests/utiltest.c                 |  3 +--
 tests/vboxsnapshotxmltest.c      |  3 +--
 tests/virbuftest.c               | 21 +++++++-------------
 tests/virfirewalltest.c          | 30 ++++++++++-------------------
 tests/virjsontest.c              |  6 ++----
 tests/virkmodtest.c              |  3 +--
 tests/virnetdevbandwidthtest.c   |  3 +--
 tests/virnetdevopenvswitchtest.c |  6 ++----
 tests/virnetsockettest.c         |  3 +--
 tests/virshtest.c                |  3 +--
 15 files changed, 51 insertions(+), 103 deletions(-)

diff --git a/tests/commandtest.c b/tests/commandtest.c
index ffc4b24ef4..62275ba96d 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -436,8 +436,7 @@ static int test13(const void *unused G_GNUC_UNUSED)
 
     g_clear_pointer(&cmd, virCommandFree);
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
 
@@ -497,16 +496,13 @@ static int test14(const void *unused G_GNUC_UNUSED)
     if (!jointactual)
         goto cleanup;
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
-    if (STRNEQ(erractual, errexpect)) {
-        virTestDifference(stderr, errexpect, erractual);
+    if (virTestCompareToString(errexpect, erractual) < 0) {
         goto cleanup;
     }
-    if (STRNEQ(jointactual, jointexpect)) {
-        virTestDifference(stderr, jointexpect, jointactual);
+    if (virTestCompareToString(jointexpect, jointactual) < 0) {
         goto cleanup;
     }
 
@@ -569,8 +565,7 @@ static int test16(const void *unused G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
 
@@ -774,13 +769,11 @@ static int test21(const void *unused G_GNUC_UNUSED)
     if (virTestGetVerbose())
         printf("STDOUT:%s\nSTDERR:%s\n", NULLSTR(outbuf), NULLSTR(errbuf));
 
-    if (STRNEQ_NULLABLE(outbuf, outbufExpected)) {
-        virTestDifference(stderr, outbufExpected, outbuf);
+    if (virTestCompareToString(outbufExpected, outbuf) < 0) {
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(errbuf, errbufExpected)) {
-        virTestDifference(stderr, errbufExpected, errbuf);
+    if (virTestCompareToString(errbufExpected, errbuf) < 0) {
         return -1;
     }
 
@@ -1016,8 +1009,7 @@ static int test26(const void *unused G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
 
@@ -1086,12 +1078,10 @@ static int test27(const void *unused G_GNUC_UNUSED)
     if (!outactual || !erractual)
         return -1;
 
-    if (STRNEQ(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         return -1;
     }
-    if (STRNEQ(erractual, errexpect)) {
-        virTestDifference(stderr, errexpect, erractual);
+    if (virTestCompareToString(errexpect, erractual) < 0) {
         return -1;
     }
 
@@ -1225,8 +1215,7 @@ test29(const void *unused G_GNUC_UNUSED)
         }
     }
 
-    if (STRNEQ_NULLABLE(outactual, outexpect)) {
-        virTestDifference(stderr, outexpect, outactual);
+    if (virTestCompareToString(outexpect, outactual) < 0) {
         goto cleanup;
     }
 
diff --git a/tests/esxutilstest.c b/tests/esxutilstest.c
index 4591cf49db..5cebc10e44 100644
--- a/tests/esxutilstest.c
+++ b/tests/esxutilstest.c
@@ -50,19 +50,15 @@ testParseDatastorePath(const void *data G_GNUC_UNUSED)
         if (paths[i].result < 0)
             continue;
 
-        if (STRNEQ(paths[i].datastoreName, datastoreName)) {
-            virTestDifference(stderr, paths[i].datastoreName, datastoreName);
+        if (virTestCompareToString(paths[i].datastoreName, datastoreName) < 0) {
             return -1;
         }
 
-        if (STRNEQ(paths[i].directoryName, directoryName)) {
-            virTestDifference(stderr, paths[i].directoryName, directoryName);
+        if (virTestCompareToString(paths[i].directoryName, directoryName) < 0) {
             return -1;
         }
 
-        if (STRNEQ(paths[i].directoryAndFileName, directoryAndFileName)) {
-            virTestDifference(stderr, paths[i].directoryAndFileName,
-                              directoryAndFileName);
+        if (virTestCompareToString(paths[i].directoryAndFileName, directoryAndFileName) < 0) {
             return -1;
         }
     }
diff --git a/tests/nwfilterebiptablestest.c b/tests/nwfilterebiptablestest.c
index f76f13fd25..fe0d8e869c 100644
--- a/tests/nwfilterebiptablestest.c
+++ b/tests/nwfilterebiptablestest.c
@@ -107,8 +107,7 @@ testNWFilterEBIPTablesAllTeardown(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(actual, expected) < 0) {
         return -1;
     }
 
@@ -169,8 +168,7 @@ testNWFilterEBIPTablesTearOldRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -209,8 +207,7 @@ testNWFilterEBIPTablesRemoveBasicRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -234,8 +231,7 @@ testNWFilterEBIPTablesTearNewRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -297,8 +293,7 @@ testNWFilterEBIPTablesApplyBasicRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -378,8 +373,7 @@ testNWFilterEBIPTablesApplyDHCPOnlyRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
@@ -442,8 +436,7 @@ testNWFilterEBIPTablesApplyDropAllRules(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferContentAndReset(&buf);
 
-    if (STRNEQ_NULLABLE(actual, expected)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         return -1;
     }
 
diff --git a/tests/openvzutilstest.c b/tests/openvzutilstest.c
index 77e086e390..6ebfb0449f 100644
--- a/tests/openvzutilstest.c
+++ b/tests/openvzutilstest.c
@@ -49,8 +49,7 @@ testReadConfigParam(const void *data G_GNUC_UNUSED)
         if (configParams[i].ret != 1)
             continue;
 
-        if (STRNEQ(configParams[i].value, value)) {
-            virTestDifference(stderr, configParams[i].value, value);
+        if (virTestCompareToString(configParams[i].value, value) < 0) {
             return -1;
         }
     }
@@ -114,8 +113,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED)
         goto cleanup;
     }
 
-    if (STRNEQ(expected, actual)) {
-        virTestDifference(stderr, expected, actual);
+    if (virTestCompareToString(expected, actual) < 0) {
         goto cleanup;
     }
 
diff --git a/tests/sockettest.c b/tests/sockettest.c
index f2b75a2e53..6b9063e11d 100644
--- a/tests/sockettest.c
+++ b/tests/sockettest.c
@@ -48,8 +48,7 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
     if (!newaddrstr)
         return pass ? -1 : 0;
 
-    if (STRNEQ(newaddrstr, addrstr)) {
-        virTestDifference(stderr, addrstr, newaddrstr);
+    if (virTestCompareToString(newaddrstr, addrstr) < 0) {
         return pass ? -1 : 0;
     } else {
         return pass ? 0 : -1;
diff --git a/tests/utiltest.c b/tests/utiltest.c
index e7a79f808f..e90baca65f 100644
--- a/tests/utiltest.c
+++ b/tests/utiltest.c
@@ -47,8 +47,7 @@ testIndexToDiskName(const void *data G_GNUC_UNUSED)
 
         diskName = virIndexToDiskName(i, "sd");
 
-        if (STRNEQ(diskNames[i], diskName)) {
-            virTestDifference(stderr, diskNames[i], diskName);
+        if (virTestCompareToString(diskNames[i], diskName) < 0) {
             return -1;
         }
     }
diff --git a/tests/vboxsnapshotxmltest.c b/tests/vboxsnapshotxmltest.c
index 3ad8298895..ab65999df1 100644
--- a/tests/vboxsnapshotxmltest.c
+++ b/tests/vboxsnapshotxmltest.c
@@ -69,8 +69,7 @@ testCompareXMLtoXMLFiles(const char *xml)
     if (!(xmlData = testFilterXML(xmlData)))
         goto cleanup;
 
-    if (STRNEQ(actual, xmlData)) {
-        virTestDifference(stderr, xmlData, actual);
+    if (virTestCompareToString(xmlData, actual) < 0) {
         goto cleanup;
     }
 
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
index 144df6e66b..6b810381fb 100644
--- a/tests/virbuftest.c
+++ b/tests/virbuftest.c
@@ -92,8 +92,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
     virBufferAddChar(buf, '\n');
 
     result = virBufferContentAndReset(buf);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         ret = -1;
     }
     return ret;
@@ -122,8 +121,7 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
     virBufferTrim(buf, ",,");
 
     result = virBufferContentAndReset(buf);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         return -1;
     }
 
@@ -145,8 +143,7 @@ testBufTrimChars(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -250,8 +247,7 @@ static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
     }
 
     result = virBufferContentAndReset(&buf1);
-    if (STRNEQ_NULLABLE(result, expected)) {
-        virTestDifference(stderr, expected, result);
+    if (virTestCompareToString(expected, result) < 0) {
         return -1;
     }
 
@@ -276,8 +272,7 @@ testBufAddStr(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -303,8 +298,7 @@ testBufEscapeStr(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
@@ -326,8 +320,7 @@ testBufEscapeRegex(const void *opaque)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(actual, data->expect)) {
-        virTestDifference(stderr, data->expect, actual);
+    if (virTestCompareToString(data->expect, actual) < 0) {
         return -1;
     }
 
diff --git a/tests/virfirewalltest.c b/tests/virfirewalltest.c
index e3d15fb67b..51c8006331 100644
--- a/tests/virfirewalltest.c
+++ b/tests/virfirewalltest.c
@@ -89,9 +89,8 @@ testFirewallSingleGroup(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -136,9 +135,8 @@ testFirewallRemoveRule(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -190,9 +188,8 @@ testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -265,9 +262,8 @@ testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -318,9 +314,8 @@ testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -365,9 +360,8 @@ testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -431,9 +425,8 @@ testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -500,9 +493,8 @@ testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -599,9 +591,8 @@ testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)
 
     actual = virBufferCurrentContent(&cmdbuf);
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
@@ -763,9 +754,8 @@ testFirewallQuery(const void *opaque G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ_NULLABLE(expected, actual)) {
+    if (virTestCompareToString(expected, actual) < 0) {
         fprintf(stderr, "Unexpected command execution\n");
-        virTestDifference(stderr, expected, actual);
         return -1;
     }
 
diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 78283b632a..294889a795 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -93,8 +93,7 @@ testJSONFromString(const void *data)
         return -1;
     }
 
-    if (STRNEQ(expectstr, formatted)) {
-        virTestDifference(stderr, expectstr, formatted);
+    if (virTestCompareToString(expectstr, formatted) < 0) {
         return -1;
     }
 
@@ -424,8 +423,7 @@ testJSONEscapeObj(const void *data G_GNUC_UNUSED)
         return -1;
     }
 
-    if (STRNEQ(parsednestedstr, neststr)) {
-        virTestDifference(stderr, neststr, parsednestedstr);
+    if (virTestCompareToString(neststr, parsednestedstr) < 0) {
         return -1;
     }
 
diff --git a/tests/virkmodtest.c b/tests/virkmodtest.c
index 0e662878b2..ec28ef1282 100644
--- a/tests/virkmodtest.c
+++ b/tests/virkmodtest.c
@@ -40,8 +40,7 @@ checkOutput(virBuffer *buf, const char *exp_cmd)
         return -1;
     }
 
-    if (STRNEQ(exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, exp_cmd, actual_cmd);
+    if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
diff --git a/tests/virnetdevbandwidthtest.c b/tests/virnetdevbandwidthtest.c
index fced657811..f7c38faa2e 100644
--- a/tests/virnetdevbandwidthtest.c
+++ b/tests/virnetdevbandwidthtest.c
@@ -92,8 +92,7 @@ testVirNetDevBandwidthSet(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, exp_cmd, actual_cmd);
+    if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
diff --git a/tests/virnetdevopenvswitchtest.c b/tests/virnetdevopenvswitchtest.c
index e5883eb076..6e93f5e65a 100644
--- a/tests/virnetdevopenvswitchtest.c
+++ b/tests/virnetdevopenvswitchtest.c
@@ -175,8 +175,7 @@ testVirNetDevOpenvswitchInterfaceSetQos(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, info->exp_cmd, actual_cmd);
+    if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
@@ -204,8 +203,7 @@ testVirNetDevOpenvswitchInterfaceClearQos(const void *data)
          * Maybe that's expected, actually. */
     }
 
-    if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
-        virTestDifference(stderr, info->exp_cmd, actual_cmd);
+    if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
         return -1;
     }
 
diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 396005899a..2295e29777 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -488,8 +488,7 @@ static int testSocketSSH(const void *opaque)
         }
         buf[rv] = '\0';
 
-        if (STRNEQ(buf, data->expectOut)) {
-            virTestDifference(stderr, data->expectOut, buf);
+        if (virTestCompareToString(data->expectOut, buf) < 0) {
             goto cleanup;
         }
 
diff --git a/tests/virshtest.c b/tests/virshtest.c
index 3d297a1db2..cf834bb847 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -134,8 +134,7 @@ testCompareOutputLit(const char *expectData,
     if (filter && testFilterLine(actualData, filter) < 0)
         return -1;
 
-    if (STRNEQ(expectData, actualData)) {
-        virTestDifference(stderr, expectData, actualData);
+    if (virTestCompareToString(expectData, actualData) < 0) {
         return -1;
     }
 
-- 
2.37.4



More information about the libvir-list mailing list