Introduce golden file mechanism for tests

This commit is contained in:
2026-04-24 21:09:47 +02:00
parent 9ca72ef5bf
commit a89e61eedd
9 changed files with 66 additions and 24 deletions
+38 -1
View File
@@ -29,6 +29,44 @@ void assert_log(const char* expected, const char* msg) {
assert_str(expected, s_logOutput, msg);
}
void assert_log_file(const char* filepath, const char* msg) {
const char* generate = getenv("GENERATE_GOLDEN");
if (generate && strcmp(generate, "1") == 0) {
FILE* f = fopen(filepath, "w");
if (!f) {
fail("could not open golden file for writing");
return;
}
fputs(s_logOutput ? s_logOutput : "", f);
fclose(f);
return;
}
FILE* f = fopen(filepath, "r");
if (!f) {
fail("could not open golden file for reading");
return;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
char* content = malloc(size + 1);
if (!content) {
fclose(f);
fail("could not allocate memory for golden file content");
return;
}
fread(content, 1, size, f);
content[size] = '\0';
fclose(f);
assert_str(content, s_logOutput, msg);
free(content);
}
static void log_append(const char* msg) {
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
size_t newLen = oldLen + strlen(msg) + 1;
@@ -75,7 +113,6 @@ static TestCase s_tests[] = {
{"parser_module_name", test_parser_module_name},
{"log_error", test_log_error},
{"log_on_line", test_log_on_line},
{"log_on_line_format", test_log_on_line_format},
};