From 851299b967aa3565bdaf44ee7001dabbabf7c787 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Mon, 2 May 2016 23:57:34 -0400 Subject: [PATCH] Test fix: clean trace files left behind by test_ctf_ir_ref MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- tests/lib/Makefile.am | 6 ++++-- tests/lib/common.c | 34 +++++++++++++++++++++++++++++++ tests/lib/common.h | 2 ++ tests/lib/test_ctf_ir_ref.c | 2 ++ tests/lib/test_ctf_writer.c | 40 +++---------------------------------- 5 files changed, 45 insertions(+), 39 deletions(-) diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am index c9c27d07..a8c94488 100644 --- a/tests/lib/Makefile.am +++ b/tests/lib/Makefile.am @@ -18,14 +18,16 @@ test_bitfield_LDADD = $(LIBTAP) libtestcommon.a test_ctf_writer_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libbabeltrace.la \ - $(top_builddir)/formats/ctf/libbabeltrace-ctf.la + $(top_builddir)/formats/ctf/libbabeltrace-ctf.la \ + libtestcommon.a test_bt_values_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libbabeltrace.la test_ctf_ir_ref_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libbabeltrace.la \ - $(top_builddir)/formats/ctf/libbabeltrace-ctf.la + $(top_builddir)/formats/ctf/libbabeltrace-ctf.la \ + libtestcommon.a test_bt_ctf_field_type_validation_LDADD = $(LIBTAP) \ $(top_builddir)/lib/libbabeltrace.la \ diff --git a/tests/lib/common.c b/tests/lib/common.c index 60e2be04..d137cb30 100644 --- a/tests/lib/common.c +++ b/tests/lib/common.c @@ -21,6 +21,8 @@ #include #include +#include +#include struct bt_context *create_context_with_path(const char *path) { @@ -39,3 +41,35 @@ struct bt_context *create_context_with_path(const char *path) } return ctx; } + +void recursive_rmdir(const char *path) +{ + struct dirent *entry; + DIR *dir = opendir(path); + + if (!dir) { + perror("# opendir"); + return; + } + + while ((entry = readdir(dir))) { + struct stat st; + char filename[PATH_MAX]; + + if (snprintf(filename, sizeof(filename), "%s/%s", + path, entry->d_name) <= 0) { + continue; + } + + if (stat(filename, &st)) { + continue; + } + + if (S_ISREG(st.st_mode)) { + unlinkat(bt_dirfd(dir), entry->d_name, 0); + } + } + + rmdir(path); + closedir(dir); +} diff --git a/tests/lib/common.h b/tests/lib/common.h index 25a02ffa..06ea63b5 100644 --- a/tests/lib/common.h +++ b/tests/lib/common.h @@ -4,6 +4,7 @@ * Lib BabelTrace - Common function to all tests * * Copyright 2012 - Yannick Brosseau + * Copyright 2016 - Jérémie Galarneau * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +24,7 @@ struct bt_context; +void recursive_rmdir(const char *path); struct bt_context *create_context_with_path(const char *path); #endif /* _TESTS_COMMON_H */ diff --git a/tests/lib/test_ctf_ir_ref.c b/tests/lib/test_ctf_ir_ref.c index d30195c8..5d0816b1 100644 --- a/tests/lib/test_ctf_ir_ref.c +++ b/tests/lib/test_ctf_ir_ref.c @@ -32,6 +32,7 @@ #include #include #include +#include "common.h" #define NR_TESTS 41 @@ -551,6 +552,7 @@ static void create_user_full(struct user *user) BT_PUT(field); ret = bt_ctf_stream_append_event(user->stream, user->event); assert(!ret); + recursive_rmdir(trace_path); } static void test_put_order_swap(size_t *array, size_t a, size_t b) diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 370de0f7..321a4ac5 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -44,7 +44,7 @@ #include "tap/tap.h" #include #include -#include +#include "common.h" #define METADATA_LINE_SIZE 512 #define SEQUENCE_TEST_LENGTH 10 @@ -63,40 +63,6 @@ static int64_t current_time = 42; -static -void delete_trace(const char *trace_path) -{ - /* Remove all trace files and delete temporary trace directory */ - struct dirent *entry; - DIR *trace_dir = opendir(trace_path); - - if (!trace_dir) { - perror("# opendir"); - return; - } - - while ((entry = readdir(trace_dir))) { - struct stat st; - char filename[PATH_MAX]; - - if (snprintf(filename, sizeof(filename), "%s/%s", - trace_path, entry->d_name) <= 0) { - continue; - } - - if (stat(filename, &st)) { - continue; - } - - if (S_ISREG(st.st_mode)) { - unlinkat(bt_dirfd(trace_dir), entry->d_name, 0); - } - } - - rmdir(trace_path); - closedir(trace_dir); -} - /* Return 1 if uuids match, zero if different. */ static int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b) @@ -2919,7 +2885,7 @@ void test_create_writer_vs_non_writer_mode(void) bt_put(non_writer_clock); bt_put(packet); bt_put(packet2); - delete_trace(trace_path); + recursive_rmdir(trace_path); } static @@ -3637,6 +3603,6 @@ int main(int argc, char **argv) free(metadata_string); bt_put(stream_class); - delete_trace(trace_path); + recursive_rmdir(trace_path); return 0; } -- 2.34.1