From: Michael Tremer Date: Tue, 5 Oct 2021 16:09:08 +0000 (+0000) Subject: testsuite: Optionally return path to temporary file X-Git-Tag: 0.9.28~893 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79a0a5cd47db896871fd29e986bd63200c3b21cc;p=pakfire.git testsuite: Optionally return path to temporary file Signed-off-by: Michael Tremer --- diff --git a/tests/libpakfire/compress.c b/tests/libpakfire/compress.c index d9a46b1f6..e4afcfa9c 100644 --- a/tests/libpakfire/compress.c +++ b/tests/libpakfire/compress.c @@ -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"); diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index 73afb38d4..9a5b76418 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -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); diff --git a/tests/libpakfire/repo.c b/tests/libpakfire/repo.c index 8c88a593b..3db4eb839 100644 --- a/tests/libpakfire/repo.c +++ b/tests/libpakfire/repo.c @@ -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 diff --git a/tests/testsuite.c b/tests/testsuite.c index 6c41a32cb..cb9b395ce 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -28,6 +28,8 @@ #include #include +#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) diff --git a/tests/testsuite.h b/tests/testsuite.h index 7cadd4a1c..d2c06c00a 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -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 */