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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user