tests: Ensure test files are newline-terminated

Currently we only append a newline to 'actual' if 'expected'
(as loaded from file) already ends in a newline, but that
results in inconsistent behavior.

For example, some of the test files used by virhostcputest are
newline-terminated and some aren't. If we were to remove
existing newlines from those files or add them where they
aren't present, the test would still pass, and even using
VIR_TEST_REGENERATE_OUTPUT=1 wouldn't change them back.

Make things consistent by ensuring that 'actual' is always
newline-terminated. The only exception is when 'actual' is
completely empty: in that case, we want the file to be actually
empty, not contain a single empty line. query-jobs-empty.result
in qemumonitorjsondata/ is an example of this being used.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Andrea Bolognani 2024-01-05 12:02:43 +01:00
parent 2439e7135c
commit 8a743a598b
22 changed files with 27 additions and 30 deletions

View File

@ -582,6 +582,7 @@ virTestCompareToFileFull(const char *actual,
g_autofree char *filecontent = NULL;
g_autofree char *fixedcontent = NULL;
const char *cmpcontent = actual;
size_t cmpcontentLen;
if (!cmpcontent)
cmpcontent = "";
@ -594,16 +595,12 @@ virTestCompareToFileFull(const char *actual,
return -1;
}
if (filecontent) {
size_t filecontentLen = strlen(filecontent);
size_t cmpcontentLen = strlen(cmpcontent);
cmpcontentLen = strlen(cmpcontent);
if (filecontentLen > 0 &&
filecontent[filecontentLen - 1] == '\n' &&
(cmpcontentLen == 0 || cmpcontent[cmpcontentLen - 1] != '\n')) {
fixedcontent = g_strdup_printf("%s\n", cmpcontent);
cmpcontent = fixedcontent;
}
if (cmpcontentLen > 0 &&
cmpcontent[cmpcontentLen - 1] != '\n') {
fixedcontent = g_strdup_printf("%s\n", cmpcontent);
cmpcontent = fixedcontent;
}
if (STRNEQ_NULLABLE(cmpcontent, filecontent)) {