From de59c4bba2c26ad42efeae9cf6f38daa8e0082c0 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 7 Sep 2021 15:31:42 +0200 Subject: [PATCH] testutils: Introduce helper for stripping bulilddir/srcdir from test outputs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- tests/testutils.c | 30 ++++++++++++++++++++++++++++++ tests/testutils.h | 3 +++ 2 files changed, 33 insertions(+) diff --git a/tests/testutils.c b/tests/testutils.c index 5e9835ee89..185d281e96 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -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); +} diff --git a/tests/testutils.h b/tests/testutils.h index 48de864131..27d135fc02 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -170,3 +170,6 @@ int testCompareDomXML2XMLFiles(virCaps *caps, bool live, unsigned int parseFlags, testCompareDomXML2XMLResult expectResult); + +char * +virTestStablePath(const char *path);