From 98b52fb257ce1ad36f127ad615c970fc600ddddd Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 13 Dec 2025 23:58:09 -0800 Subject: [PATCH] tests: rework to allow parallelization --- inc/add.h | 2 +- inc/postpone.h | 2 +- test/unit/add.tests.c | 3 ++- test/unit/file.tests.c | 3 +++ test/unit/ls.tests.c | 4 ++++ test/unit/test_utils.c | 27 ++++++++++++++------------- test/unit/test_utils.h | 1 + 7 files changed, 26 insertions(+), 16 deletions(-) diff --git a/inc/add.h b/inc/add.h index 41148fd..afe6f6b 100644 --- a/inc/add.h +++ b/inc/add.h @@ -13,7 +13,7 @@ #include #include -int add(); +int add(int, char**); int add_to_file(struct event*); #endif diff --git a/inc/postpone.h b/inc/postpone.h index daa5129..f6d80c2 100644 --- a/inc/postpone.h +++ b/inc/postpone.h @@ -9,6 +9,6 @@ #include #include -int postpone(); +int postpone(int, char**); #endif diff --git a/test/unit/add.tests.c b/test/unit/add.tests.c index 14db545..20b93ba 100644 --- a/test/unit/add.tests.c +++ b/test/unit/add.tests.c @@ -203,7 +203,8 @@ static void handle_args_basic_test() { assert(handle_args(5,args_with_place,&ev)==1); assert(strcmp(ev.name,"event")==0); assert(strcmp(ev.place,"place")==0); - assert(mktime(&(ev.datetime))==1641024000); + time_t got = mktime(&(ev.datetime)); + assert(1641024000==got); char *args[] = { "ev", diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index aafb687..5b41a05 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -29,6 +29,9 @@ int main() { static void file_open_basic_test() { FILE *fp; + /* This already exists because of setup_env() */ + assert(remove(global_options.file)==0); + char tempfile[] = "/tmp/XXXXXX"; fp = file_temp(tempfile); assert(fp!=NULL); diff --git a/test/unit/ls.tests.c b/test/unit/ls.tests.c index 951947a..ce01cfb 100644 --- a/test/unit/ls.tests.c +++ b/test/unit/ls.tests.c @@ -9,9 +9,13 @@ static void event_print_basic_test(); static void event_print_timezone_test(); int main() { + setup_env(); + event_print_basic_test(); event_print_timezone_test(); + clean_env(); + return EXIT_SUCCESS; } diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index 4f188a1..3389d39 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -2,33 +2,34 @@ struct options global_options; -static const char default_file[] = "/tmp/events.test"; - void clean_env() { - reset_env(); + if(access(global_options.file, F_OK)==0) { + assert(remove(global_options.file)==0); + } - free(global_options.file); + if(global_options.file!=NULL) { free(global_options.file); } } void reset_env() { - if(access(default_file, F_OK)==0) { - assert(remove(default_file)==0); - } - - if(global_options.file!=NULL) { free(global_options.file); } + clean_env(); opt_global_init(); - global_options.file = strdup(default_file); - assert(global_options.file!=NULL); + setup_env(); } void setup_env() { - assert(setenv("TZ",":America/Los_Angeles",1)==0); + char file_template[] = "/tmp/eventsXXXXXX"; + + assert(setenv("TZ","America/Los_Angeles",1)==0); tzset(); srand(time(NULL)); - global_options.file = strdup(default_file); + int fd = mkstemp(file_template); + assert(fd>=0); + assert(0==close(fd)); + + global_options.file = strdup(file_template); assert(global_options.file!=NULL); } diff --git a/test/unit/test_utils.h b/test/unit/test_utils.h index c15d177..e05e64d 100644 --- a/test/unit/test_utils.h +++ b/test/unit/test_utils.h @@ -1,6 +1,7 @@ #ifndef __TEST_UTILS_H_ #define __TEST_UTILS_H_ +#include #include #include -- 2.52.0