libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit ffa2585d08f91c0922c9f0a95b38c8fe0f30d6bf
parent 03052d23ba28cb260f5bd1a8dc9c7dba9ee07b28
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 30 Jan 2017 11:08:28 +0100

Add(Tests): Clean database function

Diffstat:
Mtests/test.h | 9+++++++++
Mtests/test_database.c | 14++++++++++++++
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/tests/test.h b/tests/test.h @@ -2,8 +2,17 @@ # define TEST_H # include <libmpm.h> +# include <sys/types.h> +# include <sys/wait.h> +# include <errno.h> + +# define DUP_ALL_OUTPUTS(name) dup2(name[1], 1); dup2(name[1], 2); close(name[1]); +# define WAIT_AND_CLOSE(pid, status, fd) pid = waitpid(pid, &status, 0); close(fd[1]); + void register_flags_test(void); void register_test_database(void); +void clean_db(const char *name); + #endif /* TEST_H */ diff --git a/tests/test_database.c b/tests/test_database.c @@ -78,9 +78,23 @@ TEST(database_exec_2) { ret = mpm_database_exec(NULL, NULL, &exec_callback, ptr, &err); TEST_ASSERT((ret == 1), "NULL not handled correctly."); mpm_database_close(ptr); + clean_db("test.db"); return TEST_SUCCESS; } +void clean_db(const char *name) { + int st, fd[2]; + pid_t pid; + + pipe(fd); + if ((pid = fork()) == 0) { + DUP_ALL_OUTPUTS(fd); + execl("/bin/rm", "/bin/rm", name, NULL); + } else { + WAIT_AND_CLOSE(pid, st, fd); + } +} + void register_test_database(void) { reg_test("database", database_open_1); reg_test("database", database_open_2);