feat/stdout (#3)
* feat: disable standard output for tests * fuck les fds * patch some complation issues * style: norm * feat: timeout * fix: patc more stuff * fix: patch merge * feat: more tests
This commit is contained in:
@@ -6,94 +6,82 @@
|
||||
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */
|
||||
/* Updated: 2026/01/24 16:50:51 by elagouch ### ########.fr */
|
||||
/* Updated: 2026/01/25 15:15:05 by elagouch ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "libunit.h"
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void print_wtermsig(int signal)
|
||||
int parent_helper(int ffd, struct s_helper *h, const char *fn_name,
|
||||
t_unit_test *test_list)
|
||||
{
|
||||
if (signal == 11)
|
||||
ft_putstr_fd("[\x1B[33mSIGSEGV\x1B[0m]\n", 1);
|
||||
else if (signal == 7)
|
||||
ft_putstr_fd("[\x1B[33mSIGBUS\x1B[0m]\n", 1);
|
||||
else if (signal == 6)
|
||||
ft_putstr_fd("[\x1B[33mSIGABRT\x1B[0m]\n", 1);
|
||||
else if (signal == 8)
|
||||
ft_putstr_fd("[\x1B[33mSIGFPE\x1B[0m]\n", 1);
|
||||
else if (signal == 13)
|
||||
ft_putstr_fd("[\x1B[33mSIGPIPE\x1B[0m]\n", 1);
|
||||
else if (signal == 4)
|
||||
ft_putstr_fd("[\x1B[33mSIGILL\x1B[0m]\n", 1);
|
||||
}
|
||||
int status;
|
||||
|
||||
static size_t interpret_status(int status, const char *fn_name,
|
||||
char *test_name)
|
||||
{
|
||||
ft_putstr_fd((char *)fn_name, 1);
|
||||
ft_putstr_fd(" : ", 1);
|
||||
ft_putstr_fd(test_name, 1);
|
||||
ft_putstr_fd(" : ", 1);
|
||||
if (WIFEXITED(status))
|
||||
{
|
||||
if (WEXITSTATUS(status))
|
||||
ft_putstr_fd("[\x1B[31m\x1B[1mKO\x1B[0m]\n", 1);
|
||||
else
|
||||
{
|
||||
ft_putstr_fd("[\x1B[32mOK\x1B[0m]\n", 1);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
else if (WIFSIGNALED(status))
|
||||
print_wtermsig(WTERMSIG(status));
|
||||
h->wpid = waitpid(h->wpid, &status, WCONTINUED);
|
||||
if (h->wpid < 0)
|
||||
return (-1);
|
||||
h->ok_tests += interpret_status(ffd, status, fn_name, get_test_at(test_list,
|
||||
h->i)->title);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void print_passed_test(size_t ok_tests, t_unit_test *test_list)
|
||||
int child_helper(t_unit_test *test_list, t_h h)
|
||||
{
|
||||
size_t total;
|
||||
int fd;
|
||||
int res;
|
||||
t_unit_test *test;
|
||||
|
||||
total = count_tests(test_list);
|
||||
if (ok_tests == total)
|
||||
ft_putstr_fd("\x1B[32m", 1);
|
||||
else
|
||||
ft_putstr_fd("\x1B[31m", 1);
|
||||
ft_putnbr_fd(ok_tests, 1);
|
||||
ft_putchar_fd('/', 1);
|
||||
ft_putnbr_fd((int)total, 1);
|
||||
ft_putstr_fd("\x1B[0m tests succeeded\n\n", 1);
|
||||
fd = open("/dev/null", O_WRONLY);
|
||||
if (!fd)
|
||||
return (-1);
|
||||
if (dup2(fd, STDOUT_FILENO) < 0)
|
||||
return (-1);
|
||||
test = get_test_at(test_list, h.i);
|
||||
if (test->timeout)
|
||||
alarm(test->timeout);
|
||||
res = test->func();
|
||||
close(fd);
|
||||
exit(res);
|
||||
}
|
||||
|
||||
int handle_out(const char *fn_name)
|
||||
{
|
||||
char *tmp;
|
||||
int fd;
|
||||
|
||||
tmp = ft_strjoin(fn_name, ".log");
|
||||
if (!tmp)
|
||||
return (-1);
|
||||
fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
free(tmp);
|
||||
return (fd);
|
||||
}
|
||||
|
||||
int launch_tests(t_unit_test *test_list, const char *fn_name)
|
||||
{
|
||||
size_t ok_tests;
|
||||
pid_t wpid;
|
||||
int status;
|
||||
int i;
|
||||
int ffd;
|
||||
t_h h;
|
||||
|
||||
ok_tests = 0;
|
||||
i = -1;
|
||||
while (++i < (int)count_tests(test_list))
|
||||
h.ok_tests = 0;
|
||||
h.i = -1;
|
||||
ffd = handle_out(fn_name);
|
||||
if (ffd < 0)
|
||||
return (1);
|
||||
while (++h.i < (int)count_tests(test_list))
|
||||
{
|
||||
wpid = fork();
|
||||
if (wpid < 0)
|
||||
h.wpid = fork();
|
||||
if (h.wpid < 0)
|
||||
return (1);
|
||||
else if (h.wpid == 0 && child_helper(test_list, h) < 0)
|
||||
return (1);
|
||||
else if (parent_helper(ffd, &h, fn_name, test_list))
|
||||
return (1);
|
||||
else if (wpid == 0)
|
||||
exit(!get_test_at(test_list, i)->func());
|
||||
else
|
||||
{
|
||||
wpid = wait(&status);
|
||||
if (wpid < 0)
|
||||
return (1);
|
||||
ok_tests += interpret_status(status, fn_name, get_test_at(test_list,
|
||||
i)->title);
|
||||
}
|
||||
}
|
||||
print_passed_test(ok_tests, test_list);
|
||||
print_passed_test(ffd, h.ok_tests, test_list);
|
||||
close(ffd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/24 14:38:40 by elagouch #+# #+# */
|
||||
/* Updated: 2026/01/24 18:56:03 by elagouch ### ########.fr */
|
||||
/* Updated: 2026/01/25 15:09:41 by elagouch ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LIBUNIT_H
|
||||
# define LIBUNIT_H
|
||||
|
||||
# include "libft.h"
|
||||
# include "../libft/libft.h"
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
|
||||
@@ -25,6 +25,7 @@ typedef struct s_unit_test
|
||||
char *title;
|
||||
int (*func)(void);
|
||||
struct s_unit_test *next;
|
||||
unsigned int timeout;
|
||||
} t_unit_test;
|
||||
|
||||
/**
|
||||
@@ -53,7 +54,7 @@ size_t count_tests(t_unit_test *head);
|
||||
* @return -1 on error, 0 on success
|
||||
*/
|
||||
size_t load_test(t_unit_test **head_ptr, const char *title,
|
||||
int (*test_func)(void));
|
||||
int (*test_func)(void), unsigned int timeout);
|
||||
|
||||
/**
|
||||
* @brief clears the tests memory
|
||||
@@ -66,4 +67,17 @@ void clear_tests(t_unit_test **head_ptr);
|
||||
int launch_tests(t_unit_test *test_list,
|
||||
const char *fn_name);
|
||||
|
||||
typedef struct s_helper
|
||||
{
|
||||
pid_t wpid;
|
||||
size_t ok_tests;
|
||||
int i;
|
||||
} t_h;
|
||||
|
||||
size_t interpret_status(int ffd, int status,
|
||||
const char *fn_name, char *test_name);
|
||||
|
||||
void print_passed_test(int ffd, size_t ok_tests,
|
||||
t_unit_test *test_list);
|
||||
|
||||
#endif // !LIBUNIT_H
|
||||
|
||||
86
framework/libunit_logger.c
Normal file
86
framework/libunit_logger.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* libunit_logger.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/25 14:26:54 by elagouch #+# #+# */
|
||||
/* Updated: 2026/01/25 14:56:06 by elagouch ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libunit.h"
|
||||
|
||||
static void print_wtermsig(int ffd, int signal)
|
||||
{
|
||||
static char *sigs[15];
|
||||
char *msg;
|
||||
|
||||
sigs[4] = "SIGKILL";
|
||||
sigs[6] = "SIGABRT";
|
||||
sigs[7] = "SIGBUS";
|
||||
sigs[8] = "SIGFPE";
|
||||
sigs[11] = "SIGSEGV";
|
||||
sigs[13] = "SIGPIPE";
|
||||
sigs[14] = "TIMEOUT";
|
||||
ft_putstr_fd("[\x1B[33m", 1);
|
||||
ft_putstr_fd("[", ffd);
|
||||
if ((signal >= 6 && signal <=8) || signal == 4 || signal == 11 || (signal >= 13 && signal <= 14))
|
||||
msg = sigs[signal];
|
||||
else
|
||||
msg = "UNKNOWN";
|
||||
ft_putstr_fd(msg, 1);
|
||||
ft_putstr_fd(msg, ffd);
|
||||
ft_putstr_fd("\x1B[0m]\n", 1);
|
||||
ft_putstr_fd("]\n", ffd);
|
||||
}
|
||||
|
||||
size_t interpret_status(int ffd, int status, const char *fn_name,
|
||||
char *test_name)
|
||||
{
|
||||
ft_putstr_fd((char *)fn_name, 1);
|
||||
ft_putstr_fd((char *)fn_name, ffd);
|
||||
ft_putstr_fd(" : ", 1);
|
||||
ft_putstr_fd(" : ", ffd);
|
||||
ft_putstr_fd(test_name, 1);
|
||||
ft_putstr_fd(test_name, ffd);
|
||||
ft_putstr_fd(" : ", 1);
|
||||
ft_putstr_fd(" : ", ffd);
|
||||
if (WIFEXITED(status))
|
||||
{
|
||||
if (WEXITSTATUS(status))
|
||||
{
|
||||
ft_putstr_fd("[\x1B[31m\x1B[1mKO\x1B[0m]\n", 1);
|
||||
ft_putstr_fd("[KO]\n", ffd);
|
||||
}
|
||||
else
|
||||
{
|
||||
ft_putstr_fd("[\x1B[32mOK\x1B[0m]\n", 1);
|
||||
ft_putstr_fd("[OK]\n", ffd);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
else if (WIFSIGNALED(status))
|
||||
print_wtermsig(ffd, WTERMSIG(status));
|
||||
return (0);
|
||||
}
|
||||
|
||||
void print_passed_test(int ffd, size_t ok_tests, t_unit_test *test_list)
|
||||
{
|
||||
size_t total;
|
||||
|
||||
total = count_tests(test_list);
|
||||
if (ok_tests == total)
|
||||
ft_putstr_fd("\x1B[32m", 1);
|
||||
else
|
||||
ft_putstr_fd("\x1B[31m", 1);
|
||||
ft_putnbr_fd(ok_tests, 1);
|
||||
ft_putnbr_fd(ok_tests, ffd);
|
||||
ft_putchar_fd('/', 1);
|
||||
ft_putchar_fd('/', ffd);
|
||||
ft_putnbr_fd((int)total, 1);
|
||||
ft_putnbr_fd((int)total, ffd);
|
||||
ft_putstr_fd("\x1B[0m tests succeeded\n\n", 1);
|
||||
ft_putstr_fd(" tests succeeded\n\n", ffd);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: elagouch <elagouch@student.42lyon.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/01/24 14:35:57 by elagouch #+# #+# */
|
||||
/* Updated: 2026/01/24 16:15:35 by elagouch ### ########.fr */
|
||||
/* Updated: 2026/01/25 15:11:24 by elagouch ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -39,7 +39,7 @@ t_unit_test *get_last(t_unit_test *head)
|
||||
/**
|
||||
* @brief util function to allocate a new test
|
||||
*/
|
||||
t_unit_test *alloc_test(const char *title, int (*test_func)(void))
|
||||
static t_unit_test *alloc_test(const char *title, int (*test_func)(void), unsigned int timeout)
|
||||
{
|
||||
t_unit_test *p;
|
||||
|
||||
@@ -47,11 +47,12 @@ t_unit_test *alloc_test(const char *title, int (*test_func)(void))
|
||||
p->title = ft_strdup(title);
|
||||
p->func = test_func;
|
||||
p->next = NULL;
|
||||
p->timeout = timeout;
|
||||
return (p);
|
||||
}
|
||||
|
||||
size_t load_test(t_unit_test **head_ptr, const char *title,
|
||||
int (*test_func)(void))
|
||||
int (*test_func)(void), unsigned int timeout)
|
||||
{
|
||||
t_unit_test *p;
|
||||
t_unit_test *last;
|
||||
@@ -60,14 +61,14 @@ size_t load_test(t_unit_test **head_ptr, const char *title,
|
||||
return (-1);
|
||||
if (!*head_ptr)
|
||||
{
|
||||
p = alloc_test(title, test_func);
|
||||
p = alloc_test(title, test_func, timeout);
|
||||
if (!p)
|
||||
return (-1);
|
||||
*head_ptr = p;
|
||||
return (0);
|
||||
}
|
||||
last = get_last(*head_ptr);
|
||||
last->next = alloc_test(title, test_func);
|
||||
last->next = alloc_test(title, test_func, timeout);
|
||||
if (!last->next)
|
||||
return (-1);
|
||||
return (0);
|
||||
|
||||
Reference in New Issue
Block a user