[PATCH 03/24] testutils: Introduce helper for stripping bulilddir/srcdir from test outputs

Peter Krempa pkrempa at redhat.com
Thu Sep 9 08:49:03 UTC 2021


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 at redhat.com>
---
 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);
-- 
2.31.1




More information about the libvir-list mailing list