tests: Use virTestCompareToString() more

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@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Michal Privoznik 2022-11-30 09:57:49 +01:00
parent 1fffd1605d
commit a156b69c35
15 changed files with 51 additions and 103 deletions

View File

@ -436,8 +436,7 @@ static int test13(const void *unused G_GNUC_UNUSED)
g_clear_pointer(&cmd, virCommandFree); g_clear_pointer(&cmd, virCommandFree);
if (STRNEQ(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
goto cleanup; goto cleanup;
} }
@ -497,16 +496,13 @@ static int test14(const void *unused G_GNUC_UNUSED)
if (!jointactual) if (!jointactual)
goto cleanup; goto cleanup;
if (STRNEQ(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
goto cleanup; goto cleanup;
} }
if (STRNEQ(erractual, errexpect)) { if (virTestCompareToString(errexpect, erractual) < 0) {
virTestDifference(stderr, errexpect, erractual);
goto cleanup; goto cleanup;
} }
if (STRNEQ(jointactual, jointexpect)) { if (virTestCompareToString(jointexpect, jointactual) < 0) {
virTestDifference(stderr, jointexpect, jointactual);
goto cleanup; goto cleanup;
} }
@ -569,8 +565,7 @@ static int test16(const void *unused G_GNUC_UNUSED)
return -1; return -1;
} }
if (STRNEQ(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
return -1; return -1;
} }
@ -774,13 +769,11 @@ static int test21(const void *unused G_GNUC_UNUSED)
if (virTestGetVerbose()) if (virTestGetVerbose())
printf("STDOUT:%s\nSTDERR:%s\n", NULLSTR(outbuf), NULLSTR(errbuf)); printf("STDOUT:%s\nSTDERR:%s\n", NULLSTR(outbuf), NULLSTR(errbuf));
if (STRNEQ_NULLABLE(outbuf, outbufExpected)) { if (virTestCompareToString(outbufExpected, outbuf) < 0) {
virTestDifference(stderr, outbufExpected, outbuf);
return -1; return -1;
} }
if (STRNEQ_NULLABLE(errbuf, errbufExpected)) { if (virTestCompareToString(errbufExpected, errbuf) < 0) {
virTestDifference(stderr, errbufExpected, errbuf);
return -1; return -1;
} }
@ -1016,8 +1009,7 @@ static int test26(const void *unused G_GNUC_UNUSED)
return -1; return -1;
} }
if (STRNEQ(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
return -1; return -1;
} }
@ -1086,12 +1078,10 @@ static int test27(const void *unused G_GNUC_UNUSED)
if (!outactual || !erractual) if (!outactual || !erractual)
return -1; return -1;
if (STRNEQ(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
return -1; return -1;
} }
if (STRNEQ(erractual, errexpect)) { if (virTestCompareToString(errexpect, erractual) < 0) {
virTestDifference(stderr, errexpect, erractual);
return -1; return -1;
} }
@ -1225,8 +1215,7 @@ test29(const void *unused G_GNUC_UNUSED)
} }
} }
if (STRNEQ_NULLABLE(outactual, outexpect)) { if (virTestCompareToString(outexpect, outactual) < 0) {
virTestDifference(stderr, outexpect, outactual);
goto cleanup; goto cleanup;
} }

View File

@ -50,19 +50,15 @@ testParseDatastorePath(const void *data G_GNUC_UNUSED)
if (paths[i].result < 0) if (paths[i].result < 0)
continue; continue;
if (STRNEQ(paths[i].datastoreName, datastoreName)) { if (virTestCompareToString(paths[i].datastoreName, datastoreName) < 0) {
virTestDifference(stderr, paths[i].datastoreName, datastoreName);
return -1; return -1;
} }
if (STRNEQ(paths[i].directoryName, directoryName)) { if (virTestCompareToString(paths[i].directoryName, directoryName) < 0) {
virTestDifference(stderr, paths[i].directoryName, directoryName);
return -1; return -1;
} }
if (STRNEQ(paths[i].directoryAndFileName, directoryAndFileName)) { if (virTestCompareToString(paths[i].directoryAndFileName, directoryAndFileName) < 0) {
virTestDifference(stderr, paths[i].directoryAndFileName,
directoryAndFileName);
return -1; return -1;
} }
} }

View File

@ -107,8 +107,7 @@ testNWFilterEBIPTablesAllTeardown(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(actual, expected) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -169,8 +168,7 @@ testNWFilterEBIPTablesTearOldRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -209,8 +207,7 @@ testNWFilterEBIPTablesRemoveBasicRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -234,8 +231,7 @@ testNWFilterEBIPTablesTearNewRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -297,8 +293,7 @@ testNWFilterEBIPTablesApplyBasicRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -378,8 +373,7 @@ testNWFilterEBIPTablesApplyDHCPOnlyRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -442,8 +436,7 @@ testNWFilterEBIPTablesApplyDropAllRules(const void *opaque G_GNUC_UNUSED)
actual = virBufferContentAndReset(&buf); actual = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(actual, expected)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }

View File

@ -49,8 +49,7 @@ testReadConfigParam(const void *data G_GNUC_UNUSED)
if (configParams[i].ret != 1) if (configParams[i].ret != 1)
continue; continue;
if (STRNEQ(configParams[i].value, value)) { if (virTestCompareToString(configParams[i].value, value) < 0) {
virTestDifference(stderr, configParams[i].value, value);
return -1; return -1;
} }
} }
@ -114,8 +113,7 @@ testReadNetworkConf(const void *data G_GNUC_UNUSED)
goto cleanup; goto cleanup;
} }
if (STRNEQ(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
virTestDifference(stderr, expected, actual);
goto cleanup; goto cleanup;
} }

View File

@ -48,8 +48,7 @@ static int testFormat(virSocketAddr *addr, const char *addrstr, bool pass)
if (!newaddrstr) if (!newaddrstr)
return pass ? -1 : 0; return pass ? -1 : 0;
if (STRNEQ(newaddrstr, addrstr)) { if (virTestCompareToString(newaddrstr, addrstr) < 0) {
virTestDifference(stderr, addrstr, newaddrstr);
return pass ? -1 : 0; return pass ? -1 : 0;
} else { } else {
return pass ? 0 : -1; return pass ? 0 : -1;

View File

@ -47,8 +47,7 @@ testIndexToDiskName(const void *data G_GNUC_UNUSED)
diskName = virIndexToDiskName(i, "sd"); diskName = virIndexToDiskName(i, "sd");
if (STRNEQ(diskNames[i], diskName)) { if (virTestCompareToString(diskNames[i], diskName) < 0) {
virTestDifference(stderr, diskNames[i], diskName);
return -1; return -1;
} }
} }

View File

@ -69,8 +69,7 @@ testCompareXMLtoXMLFiles(const char *xml)
if (!(xmlData = testFilterXML(xmlData))) if (!(xmlData = testFilterXML(xmlData)))
goto cleanup; goto cleanup;
if (STRNEQ(actual, xmlData)) { if (virTestCompareToString(xmlData, actual) < 0) {
virTestDifference(stderr, xmlData, actual);
goto cleanup; goto cleanup;
} }

View File

@ -92,8 +92,7 @@ static int testBufAutoIndent(const void *data G_GNUC_UNUSED)
virBufferAddChar(buf, '\n'); virBufferAddChar(buf, '\n');
result = virBufferContentAndReset(buf); result = virBufferContentAndReset(buf);
if (STRNEQ_NULLABLE(result, expected)) { if (virTestCompareToString(expected, result) < 0) {
virTestDifference(stderr, expected, result);
ret = -1; ret = -1;
} }
return ret; return ret;
@ -122,8 +121,7 @@ static int testBufTrim(const void *data G_GNUC_UNUSED)
virBufferTrim(buf, ",,"); virBufferTrim(buf, ",,");
result = virBufferContentAndReset(buf); result = virBufferContentAndReset(buf);
if (STRNEQ_NULLABLE(result, expected)) { if (virTestCompareToString(expected, result) < 0) {
virTestDifference(stderr, expected, result);
return -1; return -1;
} }
@ -145,8 +143,7 @@ testBufTrimChars(const void *opaque)
return -1; return -1;
} }
if (STRNEQ_NULLABLE(actual, data->expect)) { if (virTestCompareToString(data->expect, actual) < 0) {
virTestDifference(stderr, data->expect, actual);
return -1; return -1;
} }
@ -250,8 +247,7 @@ static int testBufAddBuffer(const void *data G_GNUC_UNUSED)
} }
result = virBufferContentAndReset(&buf1); result = virBufferContentAndReset(&buf1);
if (STRNEQ_NULLABLE(result, expected)) { if (virTestCompareToString(expected, result) < 0) {
virTestDifference(stderr, expected, result);
return -1; return -1;
} }
@ -276,8 +272,7 @@ testBufAddStr(const void *opaque)
return -1; return -1;
} }
if (STRNEQ_NULLABLE(actual, data->expect)) { if (virTestCompareToString(data->expect, actual) < 0) {
virTestDifference(stderr, data->expect, actual);
return -1; return -1;
} }
@ -303,8 +298,7 @@ testBufEscapeStr(const void *opaque)
return -1; return -1;
} }
if (STRNEQ_NULLABLE(actual, data->expect)) { if (virTestCompareToString(data->expect, actual) < 0) {
virTestDifference(stderr, data->expect, actual);
return -1; return -1;
} }
@ -326,8 +320,7 @@ testBufEscapeRegex(const void *opaque)
return -1; return -1;
} }
if (STRNEQ_NULLABLE(actual, data->expect)) { if (virTestCompareToString(data->expect, actual) < 0) {
virTestDifference(stderr, data->expect, actual);
return -1; return -1;
} }

View File

@ -89,9 +89,8 @@ testFirewallSingleGroup(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -136,9 +135,8 @@ testFirewallRemoveRule(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -190,9 +188,8 @@ testFirewallManyGroups(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -265,9 +262,8 @@ testFirewallIgnoreFailGroup(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -318,9 +314,8 @@ testFirewallIgnoreFailRule(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -365,9 +360,8 @@ testFirewallNoRollback(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -431,9 +425,8 @@ testFirewallSingleRollback(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -500,9 +493,8 @@ testFirewallManyRollback(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -599,9 +591,8 @@ testFirewallChainedRollback(const void *opaque G_GNUC_UNUSED)
actual = virBufferCurrentContent(&cmdbuf); actual = virBufferCurrentContent(&cmdbuf);
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }
@ -763,9 +754,8 @@ testFirewallQuery(const void *opaque G_GNUC_UNUSED)
return -1; return -1;
} }
if (STRNEQ_NULLABLE(expected, actual)) { if (virTestCompareToString(expected, actual) < 0) {
fprintf(stderr, "Unexpected command execution\n"); fprintf(stderr, "Unexpected command execution\n");
virTestDifference(stderr, expected, actual);
return -1; return -1;
} }

View File

@ -93,8 +93,7 @@ testJSONFromString(const void *data)
return -1; return -1;
} }
if (STRNEQ(expectstr, formatted)) { if (virTestCompareToString(expectstr, formatted) < 0) {
virTestDifference(stderr, expectstr, formatted);
return -1; return -1;
} }
@ -424,8 +423,7 @@ testJSONEscapeObj(const void *data G_GNUC_UNUSED)
return -1; return -1;
} }
if (STRNEQ(parsednestedstr, neststr)) { if (virTestCompareToString(neststr, parsednestedstr) < 0) {
virTestDifference(stderr, neststr, parsednestedstr);
return -1; return -1;
} }

View File

@ -40,8 +40,7 @@ checkOutput(virBuffer *buf, const char *exp_cmd)
return -1; return -1;
} }
if (STRNEQ(exp_cmd, actual_cmd)) { if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
virTestDifference(stderr, exp_cmd, actual_cmd);
return -1; return -1;
} }

View File

@ -92,8 +92,7 @@ testVirNetDevBandwidthSet(const void *data)
* Maybe that's expected, actually. */ * Maybe that's expected, actually. */
} }
if (STRNEQ_NULLABLE(exp_cmd, actual_cmd)) { if (virTestCompareToString(exp_cmd, actual_cmd) < 0) {
virTestDifference(stderr, exp_cmd, actual_cmd);
return -1; return -1;
} }

View File

@ -175,8 +175,7 @@ testVirNetDevOpenvswitchInterfaceSetQos(const void *data)
* Maybe that's expected, actually. */ * Maybe that's expected, actually. */
} }
if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) { if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
virTestDifference(stderr, info->exp_cmd, actual_cmd);
return -1; return -1;
} }
@ -204,8 +203,7 @@ testVirNetDevOpenvswitchInterfaceClearQos(const void *data)
* Maybe that's expected, actually. */ * Maybe that's expected, actually. */
} }
if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) { if (virTestCompareToString(info->exp_cmd, actual_cmd) < 0) {
virTestDifference(stderr, info->exp_cmd, actual_cmd);
return -1; return -1;
} }

View File

@ -488,8 +488,7 @@ static int testSocketSSH(const void *opaque)
} }
buf[rv] = '\0'; buf[rv] = '\0';
if (STRNEQ(buf, data->expectOut)) { if (virTestCompareToString(data->expectOut, buf) < 0) {
virTestDifference(stderr, data->expectOut, buf);
goto cleanup; goto cleanup;
} }

View File

@ -134,8 +134,7 @@ testCompareOutputLit(const char *expectData,
if (filter && testFilterLine(actualData, filter) < 0) if (filter && testFilterLine(actualData, filter) < 0)
return -1; return -1;
if (STRNEQ(expectData, actualData)) { if (virTestCompareToString(expectData, actualData) < 0) {
virTestDifference(stderr, expectData, actualData);
return -1; return -1;
} }