From 627d2bac2477986401400127fb31af33ca0f69b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 16 May 2018 11:35:41 +0200 Subject: fs-util,test: add helper to remove tempfiles This simplifies the use of tempfiles in tests and fixes "leaked" temporary files in test-fileio, test-catalog, test-conf-parser. Not the whole tree is converted. --- src/journal/test-catalog.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'src/journal/test-catalog.c') diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c index d5640c549c..67466cf326 100644 --- a/src/journal/test-catalog.c +++ b/src/journal/test-catalog.c @@ -16,6 +16,7 @@ #include "alloc-util.h" #include "catalog.h" #include "fd-util.h" +#include "fs-util.h" #include "fileio.h" #include "log.h" #include "macro.h" @@ -32,9 +33,8 @@ static const char *no_catalog_dirs[] = { NULL }; -static Hashmap * test_import(const char* contents, ssize_t size, int code) { - int r; - char name[] = "/tmp/test-catalog.XXXXXX"; +static Hashmap* test_import(const char* contents, ssize_t size, int code) { + _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-catalog.XXXXXX"; _cleanup_close_ int fd; Hashmap *h; @@ -47,10 +47,7 @@ static Hashmap * test_import(const char* contents, ssize_t size, int code) { assert_se(fd >= 0); assert_se(write(fd, contents, size) == size); - r = catalog_import_file(h, name); - assert_se(r == code); - - unlink(name); + assert_se(catalog_import_file(h, name) == code); return h; } @@ -164,17 +161,9 @@ static void test_catalog_import_merge_no_body(void) { } } -static const char* database = NULL; - -static void test_catalog_update(void) { - static char name[] = "/tmp/test-catalog.XXXXXX"; +static void test_catalog_update(const char *database) { int r; - r = mkostemp_safe(name); - assert_se(r >= 0); - - database = name; - /* Test what happens if there are no files. */ r = catalog_update(database, NULL, NULL); assert_se(r == 0); @@ -218,6 +207,7 @@ static void test_catalog_file_lang(void) { } int main(int argc, char *argv[]) { + _cleanup_(unlink_tempfilep) char database[] = "/tmp/test-catalog.XXXXXX"; _cleanup_free_ char *text = NULL; int r; @@ -234,7 +224,9 @@ int main(int argc, char *argv[]) { test_catalog_import_merge(); test_catalog_import_merge_no_body(); - test_catalog_update(); + assert_se(mkostemp_safe(database) >= 0); + + test_catalog_update(database); r = catalog_list(stdout, database, true); assert_se(r >= 0); @@ -245,8 +237,5 @@ int main(int argc, char *argv[]) { assert_se(catalog_get(database, SD_MESSAGE_COREDUMP, &text) >= 0); printf(">>>%s<<<\n", text); - if (database) - unlink(database); - return 0; } -- cgit v1.2.1