]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: utils: Add virTestLoadFilePath helper
authorPeter Krempa <pkrempa@redhat.com>
Tue, 25 Jul 2017 13:11:31 +0000 (15:11 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 27 Jul 2017 07:34:49 +0000 (09:34 +0200)
This new helper loads and returns a file from 'abs_srcdir'. By using
variable arguments for the function, it's not necessary to format the
path separately in the test cases.

Reviewed-by: Eric Blake <eblake@redhat.com>
tests/testutils.c
tests/testutils.h

index 7f1c4672b37bc524a0943eb5d3e2b528d66d7632..53ef351ef7fdbdaae54f69085c286f1292d4231e 100644 (file)
@@ -356,6 +356,57 @@ virTestLoadFile(const char *file, char **buf)
     return 0;
 }
 
+
+static char *
+virTestLoadFileGetPath(const char *p,
+                       va_list ap)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *path = NULL;
+
+    virBufferAddLit(&buf, abs_srcdir "/");
+
+    if (p) {
+        virBufferAdd(&buf, p, -1);
+        virBufferStrcatVArgs(&buf, ap);
+    }
+
+    if (!(path = virBufferContentAndReset(&buf)))
+        VIR_TEST_VERBOSE("failed to format file path");
+
+    return path;
+}
+
+
+/**
+ * virTestLoadFilePath:
+ * @...: file name components terminated with a NULL
+ *
+ * Constructs the test file path from variable arguments and loads the file.
+ * 'abs_srcdir' is automatically prepended.
+ */
+char *
+virTestLoadFilePath(const char *p, ...)
+{
+    char *path = NULL;
+    char *ret = NULL;
+    va_list ap;
+
+    va_start(ap, p);
+
+    if (!(path = virTestLoadFileGetPath(p, ap)))
+        goto cleanup;
+
+    ignore_value(virTestLoadFile(path, &ret));
+
+ cleanup:
+    va_end(ap);
+    VIR_FREE(path);
+
+    return ret;
+}
+
+
 #ifndef WIN32
 static
 void virTestCaptureProgramExecChild(const char *const argv[],
index c7f02e468f08a1bedeb2bc35cfbca7b6791e1c98..98dfa990e6eb5b88aee6e67b00f2a5a1163dcd21 100644 (file)
@@ -52,6 +52,8 @@ int virTestRun(const char *title,
                int (*body)(const void *data),
                const void *data);
 int virTestLoadFile(const char *file, char **buf);
+char *virTestLoadFilePath(const char *p, ...)
+    ATTRIBUTE_SENTINEL;
 int virTestCaptureProgramOutput(const char *const argv[], char **buf, int maxlen);
 
 void virTestClearCommandPath(char *cmdset);