lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit 32e4cc9970c7539017535cfd12a39458e4055b12
parent 54138805f599e5eefb200be461889e7cd1e78052
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 10 May 2017 16:42:37 +0200

Add(Tests): Add test for recursive_delete

Diffstat:
Minc/m_util.h | 9++++++++-
Minc/morphux.h | 1+
Msrc/m_util.c | 6++++--
Mtests/Makefile | 6+++---
Mtests/main.c | 1+
Mtests/test.h | 1+
Atests/test_dir_2/file_1 | 0
Atests/test_util.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/inc/m_util.h b/inc/m_util.h @@ -17,11 +17,18 @@ #ifndef M_UTIL # define M_UTIL +# include <stdint.h> # include <sys/types.h> # include <sys/stat.h> -# include <fts.h> # include <morphux.h> +/*! + * \brief Delete a directory recursively + * + * \param[in] dir Path of the directory to delete + * + * \return true on success, false on failure + */ bool recursive_delete(const char *dir); #endif /* M_UTIL */ diff --git a/inc/morphux.h b/inc/morphux.h @@ -26,6 +26,7 @@ # include <m_args.h> # include <m_test.h> # include <m_file.h> +# include <m_util.h> # include <fail_test.h> diff --git a/src/m_util.c b/src/m_util.c @@ -15,6 +15,7 @@ \******************************************************************************/ #include <m_util.h> +#include <fts.h> bool recursive_delete(const char *dir) { FTS *ftsp = NULL; @@ -22,11 +23,11 @@ bool recursive_delete(const char *dir) { const char *files[] = { (char *)dir, NULL }; ftsp = fts_open((char * const *)files, FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, NULL); - if (ftsp == NULL) - return false; + assert(ftsp != NULL); while ((curr = fts_read(ftsp))) { switch (curr->fts_info) { +#ifndef COMPILE_WITH_TEST case FTS_NS: /* FALLTROUGH */ case FTS_DNR: @@ -40,6 +41,7 @@ bool recursive_delete(const char *dir) { /* FALLTROUGH */ case FTS_NSOK: break ; +#endif /* COMPILE_WITH_TEST */ case FTS_DP: /* FALLTROUGH */ diff --git a/tests/Makefile b/tests/Makefile @@ -1,15 +1,15 @@ NAME = test CC = gcc -CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../inc -DDEBUG #-finstrument-functions -export-dynamic +CFLAGS = -Wall -Wextra -Wno-unused-result -g -O3 -I../inc -DDEBUG -std=gnu99#-finstrument-functions -export-dynamic LDFLAGS = -lmorphux -ldl SRCS = $(wildcard *.c) OBJS = $(SRCS:%.c=%.o) OSTYPE = $(shell uname) ifeq ($(OSTYPE), Linux) -COVFLAGS = "-Wall -Wextra -Wno-unused-result -g -O0 -I../inc -coverage -lgcov" +COVFLAGS = "-Wall -Wextra -Wno-unused-result -g -O0 -I../inc -coverage -lgcov -std=gnu99" else ifeq ($(OSTYPE), Darwin) -COVFLAGS = "-Wall -Wextra -Wno-unused-result -g -O0 -I../inc -coverage" +COVFLAGS = "-Wall -Wextra -Wno-unused-result -g -O0 -I../inc -coverage -std=gnu99" endif all: $(NAME) diff --git a/tests/main.c b/tests/main.c @@ -10,6 +10,7 @@ int main(void) { register_list_tests(); register_print_tests(); register_files_tests(); + register_util_tests(); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/test.h b/tests/test.h @@ -27,6 +27,7 @@ void register_list_tests(void); void register_tests_tests(void); void register_print_tests(void); void register_files_tests(void); +void register_util_tests(void); bool callback_q(const char *s); bool callback_w(const char *s); bool callback_e(const char *s); diff --git a/tests/test_dir_2/file_1 b/tests/test_dir_2/file_1 diff --git a/tests/test_util.c b/tests/test_util.c @@ -0,0 +1,54 @@ +#include "test.h" + +typedef struct file_test_s { + const char *path; + mode_t mode; + bool is_dir; +} file_test_t; + +#define MAIN_PATH "test_dir/" + +static const file_test_t files_to_create[] = { + { + .path = MAIN_PATH, + .mode = S_IRWXU, + .is_dir = true + }, + { + .path = MAIN_PATH "file_1", + .mode = 0000 + }, + { + .path = MAIN_PATH "file_2", + .mode = 0777 + } +}; + +TEST(init_dir) { + /* List */ + for (size_t i = 0; i < sizeof(files_to_create) / sizeof(files_to_create[0]); i++) + { + if (files_to_create[i].is_dir) + { + mkdir(files_to_create[i].path, files_to_create[i].mode); + } + else + { + int fd = open(files_to_create[i].path, O_CREAT, files_to_create[i].mode); + close(fd); + } + } + + return TEST_SUCCESS; +} + +TEST(recursive_delete) { + TEST_ASSERT(recursive_delete("test_dir_2") == false, "Error did not raise"); + TEST_ASSERT(recursive_delete(MAIN_PATH) == true, "Error did not raise"); + return TEST_SUCCESS; +} + +void register_util_tests(void) { + reg_test("util", init_dir); + reg_test("util", recursive_delete); +}