diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-05-16 11:35:41 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-05-18 20:11:31 +0200 |
commit | 627d2bac2477986401400127fb31af33ca0f69b0 (patch) | |
tree | c2e0593b9a4c2c895fe2dcaa03e240e0513bfbee /src/journal/test-catalog.c | |
parent | c55b280158a5a349adb091bf6c3600d4cf7f8020 (diff) | |
download | systemd-627d2bac2477986401400127fb31af33ca0f69b0.tar.gz |
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.
Diffstat (limited to 'src/journal/test-catalog.c')
-rw-r--r-- | src/journal/test-catalog.c | 29 |
1 files changed, 9 insertions, 20 deletions
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; } |