feat: All tests run in a child process now.

This commit is contained in:
2025-06-24 20:10:25 +02:00
parent c160e3a421
commit 8d45dc827a
6 changed files with 191 additions and 101 deletions

232
set.c
View File

@@ -3,9 +3,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "set_asserts.h"
#ifdef COLORIZED
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
@@ -14,113 +22,165 @@
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#else
#define ANSI_COLOR_RED
#define ANSI_COLOR_GREEN
#define ANSI_COLOR_YELLOW
#define ANSI_COLOR_BLUE
#define ANSI_COLOR_MAGENTA
#define ANSI_COLOR_CYAN
#define ANSI_COLOR_RESET
#endif
// clang-format off
#define BANNER \
"" \
" _____ _ _ _____ _ _____ _ \n"\
" / ___| | | | | ___| | | |_ _| | | \n"\
" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | | ___ ___| |_ ___ _ __ \n"\
" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ | |/ _ / __| __/ _ | '__|\n"\
" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | | __\\__ | || __| | \n"\
" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| |_| \\_/\\___|___/\\__\\___|_| \n"\
" __/ | \n"\
" |___/ \n"\
""
#define BANNER \
"" \
" _____ _ _ _____ _ " \
"_____ _ \n" \
" / ___| | | | | ___| | | |_ " \
"_| | | \n" \
" \\ `--. _ __ ___ __ _| | | | |__ _ __ ___ _ _ __ _| |__ | " \
"| ___ ___| |_ ___ _ __ \n" \
" `--. | '_ ` _ \\ / _` | | | | __| '_ \\ / _ \\| | | |/ _` | '_ \\ " \
" | |/ _ / __| __/ _ | '__|\n" \
" /\\__/ | | | | | | (_| | | | | |__| | | | (_) | |_| | (_| | | | | | " \
"| __\\__ | || __| | \n" \
" \\____/|_| |_| |_|\\__,_|_|_| \\____|_| |_|\\___/ \\__,_|\\__, |_| " \
"|_| \\_/\\___|___/\\__\\___|_| \n" \
" __/ | " \
" \n" \
" |___/ " \
" \n" \
""
//clang-format on
char* format_string(const char* fmt, ...)
char *format_string(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
size_t size = vsnprintf(NULL, 0, fmt, args);
char* out = (char*)malloc(size + 1);
va_list args;
va_start(args, fmt);
size_t size = vsnprintf(NULL, 0, fmt, args);
char *out = (char *)malloc(size + 1);
if (!out) {
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
}
if (!out)
{
fprintf(stderr, "%s", "Failed to allocate for format string.\n");
}
va_end(args);
va_end(args);
va_start(args, fmt);
vsnprintf(out, size + 1, fmt, args);
va_end(args);
va_start(args, fmt);
vsnprintf(out, size + 1, fmt, args);
va_end(args);
return out;
return out;
}
static void log_test_start(const char* name) { printf("Running test: %s", name); }
static void log_test_summary(struct SETest* test)
int create_shared_suit_space(size_t size)
{
if (test->passed) {
#ifdef COLORIZED
printf(ANSI_COLOR_GREEN " - passed.\n" ANSI_COLOR_RESET);
#else
printf(" - passed.\n");
#endif
} else {
#ifdef COLORIZED
printf(ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET " Reason: \n %s\n\n",
test->error_msg);
#else
printf(" - failed.\n Reason: \n %s\n\n", test->error_msg);
#endif
}
}
int suit_space_id = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
static void dispatch_tests(struct SETSuit* suit)
{
printf("\n\n==== Running suit: %s ====\n\n", suit->name);
for (int i = 0; i < suit->len; i++) {
struct SETest* test = suit->tests[i];
log_test_start(test->name);
test->function(test);
log_test_summary(test);
fflush(stdout);
}
if (suit_space_id == -1)
{
fprintf(stdout, "Couldn't create suite space of size: %lu\n", size);
perror("shmget");
exit(1);
}
return suit_space_id;
}
static void free_suit(struct SETSuit* suit)
static void log_test_summary(struct SETest *test)
{
for (int i = 0; i < suit->len; i++) {
struct SETest* test = suit->tests[i];
free((void*)test->error_msg);
free((void*)test);
}
free(suit->tests);
free(suit);
if (test->passed)
{
printf("Test: %s" ANSI_COLOR_GREEN " - passed." ANSI_COLOR_RESET"\n",
test->name);
}
else
{
printf("Test: %s" ANSI_COLOR_RED " - failed.\n" ANSI_COLOR_RESET
" Reason: \n %s\n\n",
test->name, test->error_msg);
}
}
static void dispatch_test_suits(struct SETSuit** suits, int counter)
static void log_abnormal_termination(int status)
{
for (int i = 0; i < counter; i++)
{
dispatch_tests(suits[i]);
free_suit(suits[i]);
}
fprintf(stderr,
ANSI_COLOR_RED "A test terminated with signal: %d" ANSI_COLOR_RESET,
WTERMSIG(status));
}
int main(int argc, char** argv)
static void dispatch_single_test(struct SETest *test)
{
#ifndef NO_BANNER
printf("\n\n%s", BANNER);
#endif /* ifndef NO_BANNER */
int counter = 0;
pid_t testPID = fork();
set_bundle_suits(NULL, &counter, true);
struct SETSuit ** suits = (struct SETSuit**)malloc(counter * sizeof(struct SETSuit*));
counter = 0;
set_bundle_suits(suits, &counter, false);
dispatch_test_suits(suits, counter);
free(suits);
if (testPID == 0)
{
test->function(test);
log_test_summary(test);
exit(0);
}
}
static void dispatch_tests(struct SETSuit *suit)
{
printf("\n\n==== Running suit: %s (%d) ====\n\n", suit->name, suit->len);
struct SETest *test = suit->tests;
for (int i = 0; i < suit->len; i++)
{
dispatch_single_test(&test[i]);
}
int status = 0;
// Wait for all tests.
while (wait(&status) > 0)
{
if (WTERMSIG(status) != 0)
log_abnormal_termination(status);
};
if (shmdt(suit->tests) < 0)
{
fprintf(stderr, "%s\n", "Couldn't detach suit space after dispatch.");
perror("shmat");
exit(1);
}
if (shmctl(suit->shm_key, IPC_RMID, 0) == -1)
{
fprintf(stderr, "%s\n", "Couldn't remove suit space after dispatch.");
perror("shctl");
exit(1);
}
}
static void dispatch_test_suits(struct SETSuit **suits, int counter)
{
for (int i = 0; i < counter; i++)
{
dispatch_tests(suits[i]);
munmap((void*)suits[i], sizeof(struct SETSuit));
}
}
int main(int argc, char **argv)
{
#ifndef NO_BANNER
printf("\n\n%s", BANNER);
#endif /* ifndef NO_BANNER */
int counter = 0;
set_bundle_suits(NULL, &counter, true);
struct SETSuit *suits[counter];
counter = 0;
set_bundle_suits(suits, &counter, false);
dispatch_test_suits(suits, counter);
}