/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* libunit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/24 12:43:35 by dgaillet #+# #+# */ /* Updated: 2026/01/25 15:15:05 by elagouch ### ########.fr */ /* */ /* ************************************************************************** */ #include "libunit.h" #include #include #include #include int parent_helper(int ffd, struct s_helper *h, const char *fn_name, t_unit_test *test_list) { int 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); } int child_helper(t_unit_test *test_list, t_h h) { int fd; int res; t_unit_test *test; 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) { int ffd; t_h h; 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)) { 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); } print_passed_test(ffd, h.ok_tests, test_list); close(ffd); return (0); }