calog/tests/testRunner.c
2026-10-15 17:45:12 -05:00

70 lines
2.3 KiB
C

// testRunner.c -- the bin/calog runner end to end, as a user runs it: calogScript() names each
// script's own file (with the extension the runner found for an extensionless name) and is "" in a
// task, which no file started; calogArgs() hands every script the arguments after "--". The scripts
// are in tests/runner; whoami.lua ends the run once the others have printed.
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#define CHECK(cond, msg) checkImpl((cond), (msg), __FILE__, __LINE__)
#define OUTPUT_MAX 4096
// timeout(1) bounds a runner that never ends, so a regression fails here instead of hanging make test
#define RUN_COMMAND "timeout 60 ./bin/calog tests/runner/whoami tests/runner/whoami.js -- one two 2>&1"
static int32_t testsRun = 0;
static int32_t testsFailed = 0;
static void checkImpl(bool condition, const char *message, const char *file, int32_t line);
static void testScriptAndArgs(void);
static void checkImpl(bool condition, const char *message, const char *file, int32_t line) {
testsRun++;
if (!condition) {
testsFailed++;
printf("FAIL %s:%d %s\n", file, line, message);
}
}
static void testScriptAndArgs(void) {
char output[OUTPUT_MAX];
size_t length;
FILE *run;
int status;
run = popen(RUN_COMMAND, "r");
CHECK(run != NULL, "the runner starts");
if (run == NULL) {
return;
}
length = fread(output, 1, sizeof(output) - 1, run);
output[length] = '\0';
status = pclose(run);
CHECK(WIFEXITED(status) && WEXITSTATUS(status) == 0, "the run ends with the code calogExit gave it");
CHECK(strstr(output, "lua tests/runner/whoami.lua one,two\n") != NULL, "an extensionless script's calogScript() is the file the runner found, and calogArgs() the arguments after --");
CHECK(strstr(output, "js tests/runner/whoami.js one,two\n") != NULL, "each script of a run has its own calogScript()");
CHECK(strstr(output, "task []\n") != NULL, "calogScript() is empty in a task, which no file started");
if (testsFailed != 0) {
printf("runner output:\n%s", output);
}
}
int main(void) {
testScriptAndArgs();
printf("\n%d checks, %d failed\n", testsRun, testsFailed);
fflush(stdout);
if (testsFailed != 0) {
return 1;
}
return 0;
}