]> git.ipfire.org Git - pakfire.git/commitdiff
testsuite: Optionally return path to temporary file
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 16:09:08 +0000 (16:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 5 Oct 2021 16:09:08 +0000 (16:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/compress.c
tests/libpakfire/packager.c
tests/libpakfire/repo.c
tests/testsuite.c
tests/testsuite.h

index d9a46b1f600950b26ad360e5121104c5dbbe4ae2..e4afcfa9c85350e6fd7c1d3e0fcfc1643fe8c1cc 100644 (file)
@@ -71,7 +71,7 @@ static int write_test(const struct test* t, FILE* (function)(FILE* f, const char
        FILE* f = NULL;
 
        // Create a backend storage file
-       ASSERT(f = test_mktemp());
+       ASSERT(f = test_mktemp(NULL));
 
        // Open compressed file for writing
        f = function(f, "w");
index 73afb38d4eb59b7bcc0a21749ad7811f216001b3..9a5b7641897aabcae7ac4ddca6440867233a3f77 100644 (file)
@@ -49,7 +49,7 @@ static int test_create(const struct test* t) {
        ASSERT_SUCCESS(pakfire_packager_add(packager, path, NULL));
 
        // Write archive
-       FILE* f = test_mktemp();
+       FILE* f = test_mktemp(NULL);
        r = pakfire_packager_finish(packager, f);
        ASSERT(r == 0);
 
index 8c88a593bb107925a453ef73363400737b0a20bf..3db4eb839561d4028c64e932996fa4342289a493 100644 (file)
@@ -46,7 +46,7 @@ static int test_scan(const struct test* t) {
        ASSERT(pakfire_repo_count(repo) == 1);
 
        // Allocate a temporary file
-       f = test_mktemp();
+       f = test_mktemp(NULL);
        ASSERT(f);
 
        // Write SOLV database
index 6c41a32cba62d51b718f7f154bf2f5d6b642e8ea..cb9b395ce246d7d13da83b99e138b022b21dda6b 100644 (file)
@@ -28,6 +28,8 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/util.h>
 
+#define TMP_TEMPLATE "/tmp/pakfire-test.XXXXXX"
+
 struct testsuite ts;
 
 static int test_run(int i, struct test* t) {
@@ -100,21 +102,36 @@ int testsuite_run() {
        return EXIT_SUCCESS;
 }
 
-FILE* test_mktemp() {
-       char path[] = "/tmp/.pakfire-test.XXXXXX";
+FILE* test_mktemp(char** path) {
+       char* p = NULL;
+
+       // Reset path
+       if (path)
+               *path = NULL;
+
+       int r = asprintf(&p, "%s", TMP_TEMPLATE);
+       if (r < 0)
+               return NULL;
 
-       int fd = mkstemp(path);
+       int fd = mkstemp(p);
        if (fd < 0)
                return NULL;
 
-       // Immediately delete the temporary file
-       unlink(path);
+       // If we want a named temporary file, we set path
+       if (path) {
+               *path = p;
+
+       // Otherwise we unlink the path and free p
+       } else {
+               unlink(p);
+               free(p);
+       }
 
        return fdopen(fd, "w+");
 }
 
 char* test_mkdtemp() {
-       char path[] = "/tmp/.pakfire-test.XXXXXX";
+       char path[] = TMP_TEMPLATE;
 
        char* p = mkdtemp(path);
        if (!p)
index 7cadd4a1c6be49075deb4b9679ea75e5fb197251..d2c06c00a5092a715689bfae5e2259023821f3d5 100644 (file)
@@ -152,7 +152,7 @@ int testsuite_run();
        } while (0)
 
 // Helper functions
-FILE* test_mktemp();
+FILE* test_mktemp(char** path);
 char* test_mkdtemp();
 
 #endif /* PAKFIRE_TESTSUITE_H */