From ca3ea5ae920dc2e47565018ce6585ee11f2bb089 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 6 Jun 2016 15:14:52 +0200 Subject: [PATCH] Add unit test for mkpath() and makepath() funcs Signed-off-by: Joachim Nilsson --- makepath.c | 77 ++++++------------------------------------- makepath.mk | 4 --- tests/Makefile.am | 1 + tests/check.h | 4 +++ tests/makepath_func.c | 60 +++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 71 deletions(-) delete mode 100644 makepath.mk create mode 100644 tests/makepath_func.c diff --git a/makepath.c b/makepath.c index b008612..7c57796 100644 --- a/makepath.c +++ b/makepath.c @@ -1,6 +1,6 @@ /* mkpath() -- Create all components leading up to a given directory * - * Copyright (c) 2013 Joachim Nilsson + * Copyright (c) 2013-2016 Joachim Nilsson * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,12 +17,18 @@ #include #include -#include -#include -#include #include #include +#include "lite.h" +/** + * mkpath - Like makepath() but takes a mode_t argument + * @dir: Directory to created, relative or absolute + * @mode: A &mode_t mode to create @dir with + * + * Returns: + * POSIX OK(0) on success, otherwise -1 with @errno set. + */ int mkpath(char *dir, mode_t mode) { struct stat sb; @@ -55,71 +61,8 @@ int makepath(char *dir) return mkpath(dir, 0777); } -/********************************* UNIT TESTS ************************************/ -#ifdef UNITTEST -#include "lite.h" - -int checkpath(char *dir) -{ - char tmp[256]; - struct stat sb; - - snprintf(tmp, sizeof(tmp), "ls -ld %s", dir); - if (system(tmp)) - perror("system"); - - if (!stat(dir, &sb) && S_ISDIR(sb.st_mode)) - return 0; - - errno = ENOTDIR; - return 1; -} - -int test_makepath(char *dir) -{ - int ret = makepath(dir); - - if (!ret) - ret = checkpath(dir); - if (ret) - perror("Failed"); - - return ret; -} - -int main(void) -{ - int i, ret = 0; - char *list[] = { - "/tmp/tok/", - "/tmp/tok2", - "/tmp/ab", - "/tmp/b", - "/tmp/a/", - "/tmp/a/b", - "/tmp/a/c/", - "../../sometestdir/arpa/inet", - NULL - }; - - for (i = 0; list[i]; i++) - rmdir(list[i]); - - printf("Testing makepath() ...\n"); - for (i = 0; list[i] && !ret; i++) - ret = test_makepath(list[i]); - - printf("\nCleaning up ...\n"); - for (i = 0; list[i]; i++) - rmdir(list[i]); - - return ret; -} -#endif /* UNITTEST */ - /** * Local Variables: - * compile-command: "make V=1 -f makepath.mk" * indent-tabs-mode: t * c-file-style: "linux" * End: diff --git a/makepath.mk b/makepath.mk deleted file mode 100644 index 26d7b47..0000000 --- a/makepath.mk +++ /dev/null @@ -1,4 +0,0 @@ -OBJS = -TARGET = makepath.test clean - -include rules.mk diff --git a/tests/Makefile.am b/tests/Makefile.am index 465c249..41b0b64 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,6 +3,7 @@ CLEANFILES = *~ TESTS = TESTS += pidfile_func +TESTS += makepath_func check_PROGRAMS = $(TESTS) diff --git a/tests/check.h b/tests/check.h index b61b625..1a1c120 100644 --- a/tests/check.h +++ b/tests/check.h @@ -1,6 +1,10 @@ #ifndef LITE_TESTS_CHECK_H_ #define LITE_TESTS_CHECK_H_ +#include +#include +#include + #include "../lite.h" #define fail_unless(test) \ diff --git a/tests/makepath_func.c b/tests/makepath_func.c new file mode 100644 index 0000000..58bbcf2 --- /dev/null +++ b/tests/makepath_func.c @@ -0,0 +1,60 @@ +#include +#include +#include "check.h" + +int checkpath(char *dir) +{ + char tmp[256]; + struct stat sb; + + snprintf(tmp, sizeof(tmp), "ls -ld %s", dir); + if (system(tmp)) + perror("system"); + + if (!stat(dir, &sb) && S_ISDIR(sb.st_mode)) + return 0; + + errno = ENOTDIR; + return 1; +} + +int test_makepath(char *dir) +{ + int ret = makepath(dir); + + if (!ret) + ret = checkpath(dir); + if (ret) + perror("Failed"); + + return ret; +} + +int main(void) +{ + int i, ret = 0; + char *list[] = { + "/tmp/tok/", + "/tmp/tok2", + "/tmp/ab", + "/tmp/b", + "/tmp/a/", + "/tmp/a/b", + "/tmp/a/c/", + "../../sometestdir/arpa/inet", + NULL + }; + + for (i = 0; list[i]; i++) + rmdir(list[i]); + + printf("Testing makepath() ...\n"); + for (i = 0; list[i] && !ret; i++) + ret |= test_makepath(list[i]); + + printf("\nCleaning up ...\n"); + for (i = 0; list[i]; i++) + rmdir(list[i]); + + return ret; +}