testutils: Introduce helper for stripping bulilddir/srcdir from test outputs

In certain cases we want to be able to compare test output containing
real paths against a static output file and thus we need a helper which
strips srcdir/builddir from given path.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-09-07 15:31:42 +02:00
parent aea559fa74
commit de59c4bba2
2 changed files with 33 additions and 0 deletions

View File

@ -1117,3 +1117,33 @@ const char
return virtTestCounterStr;
}
/**
* virTestStablePath:
* @path: path to make stable
*
* If @path starts with the absolute source directory path, the prefix
* is replaced with the string "ABS_SRCDIR" and similarly the build directory
* is replaced by "ABS_BUILDDIR". This is useful when paths e.g. in output
* test files need to be made stable.
*
* If @path is NULL the equivalent to NULLSTR(path) is returned.
*
* The caller is responsible for freeing the returned buffer.
*/
char *
virTestStablePath(const char *path)
{
const char *tmp;
path = NULLSTR(path);
if ((tmp = STRSKIP(path, abs_srcdir)))
return g_strdup_printf("ABS_SRCDIR%s", tmp);
if ((tmp = STRSKIP(path, abs_builddir)))
return g_strdup_printf("ABS_BUILDDIR%s", tmp);
return g_strdup(path);
}

View File

@ -170,3 +170,6 @@ int testCompareDomXML2XMLFiles(virCaps *caps,
bool live,
unsigned int parseFlags,
testCompareDomXML2XMLResult expectResult);
char *
virTestStablePath(const char *path);