X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=test.c;h=b59826a4139e64c0a6370b9111322ae73040b2e9;hb=5778b6ffb60296c5081fda4aa62bd566add98d0e;hp=ad871836addb815973790a155026602deb21a33f;hpb=bf3011a28d8f97be961b1e06aeca56cca7bf01fc;p=xonotic%2Fgmqcc.git diff --git a/test.c b/test.c index ad87183..b59826a 100644 --- a/test.c +++ b/test.c @@ -25,13 +25,141 @@ #include #include -bool opts_memchk = true; +bool opts_memchk = false; bool opts_debug = false; char *task_bins[] = { "./gmqcc", "./qcvm" }; +/* + * TODO: Windows version + * this implements a unique bi-directional popen-like function that + * allows reading data from both stdout and stderr. And writing to + * stdin :) + * + * Example of use: + * FILE *handles[3] = task_popen("ls", "-l", "r"); + * if (!handles) { perror("failed to open stdin/stdout/stderr to ls"); + * // handles[0] = stdin + * // handles[1] = stdout + * // handles[2] = stderr + * + * task_pclose(handles); // to close + */ +#ifndef _WIN32 +#include +#include + +#include +typedef struct { + FILE *handles[3]; + int pipes [3]; + + int stderr_fd; + int stdout_fd; + int pid; +} popen_t; + +FILE ** task_popen(const char *command, const char *mode) { + int inhandle [2]; + int outhandle [2]; + int errhandle [2]; + int trypipe; + + popen_t *data = mem_a(sizeof(popen_t)); + + /* + * Parse the command now into a list for execv, this is a pain + * in the ass. + */ + char *line = (char*)command; + char **argv = NULL; + { + + while (*line != '\0') { + while (*line == ' ' || *line == '\t' || *line == '\n') + *line++ = '\0'; + vec_push(argv, line); + + while (*line != '\0' && *line != ' ' && + *line != '\t' && *line != '\n') line++; + } + vec_push(argv, '\0'); + } + + + if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0; + if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1; + if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2; + + if ((data->pid = fork()) > 0) { + /* parent */ + close(inhandle [0]); + close(outhandle [1]); + close(errhandle [1]); + + data->pipes [0] = inhandle [1]; + data->pipes [1] = outhandle[0]; + data->pipes [2] = errhandle[0]; + data->handles[0] = fdopen(inhandle [1], "w"); + data->handles[1] = fdopen(outhandle[0], mode); + data->handles[2] = fdopen(errhandle[0], mode); + + /* sigh */ + if (argv) + vec_free(argv); + return data->handles; + } else if (data->pid == 0) { + /* child */ + close(inhandle [1]); + close(outhandle[0]); + close(errhandle[0]); + + /* see piping documentation for this sillyness :P */ + close(0), dup(inhandle [0]); + close(1), dup(outhandle[1]); + close(2), dup(errhandle[1]); + + execvp(*argv, argv); + exit(1); + } else { + /* fork failed */ + goto task_popen_error_3; + } + + if (argv) + vec_free(argv); + return data->handles; + +task_popen_error_3: close(errhandle[0]), close(errhandle[1]); +task_popen_error_2: close(outhandle[0]), close(outhandle[1]); +task_popen_error_1: close(inhandle [0]), close(inhandle [1]); +task_popen_error_0: + + if (argv) + vec_free(argv); + return NULL; +} + +int task_pclose(FILE **handles) { + popen_t *data = (popen_t*)handles; + int status = 0; + + close(data->pipes[0]); /* stdin */ + close(data->pipes[1]); /* stdout */ + close(data->pipes[2]); /* stderr */ + + waitpid(data->pid, &status, 0); + + mem_d(data); + + return status; +} +#else + +#endif + #define TASK_COMPILE 0 #define TASK_EXECUTE 1 @@ -93,7 +221,8 @@ char *task_bins[] = { * * I: * Used to specify the INPUT source file to operate on, this must be - * provided, this tag is NOT optional. + * provided, this tag is NOT optional + * * * Notes: * These tags have one-time use, using them more than once will result @@ -118,6 +247,7 @@ typedef struct { char *comparematch; char *proceduretype; char *sourcefile; + char *tempfilename; } task_template_t; /* @@ -289,6 +419,7 @@ void task_template_nullify(task_template_t *template) { template->executeflags = NULL; template->comparematch = NULL; template->sourcefile = NULL; + template->tempfilename = NULL; } task_template_t *task_template_compile(const char *file, const char *dir) { @@ -412,7 +543,12 @@ void task_template_destroy(task_template_t **template) { */ typedef struct { task_template_t *template; - FILE *handle; + FILE **runhandles; + FILE *stderrlog; + FILE *stdoutlog; + char *stdoutlogfile; + char *stderrlogfile; + bool compiled; } task_t; task_t *task_tasks = NULL; @@ -426,11 +562,18 @@ bool task_propogate(const char *curdir) { DIR *dir; struct dirent *files; struct stat directory; + char buffer[4096]; dir = opendir(curdir); while ((files = readdir(dir))) { - stat(files->d_name, &directory); + memset (buffer, 0,sizeof(buffer)); + snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name); + + if (stat(buffer, &directory) == -1) { + con_err("internal error: stat failed, aborting\n"); + abort(); + } /* skip directories */ if (S_ISDIR(directory.st_mode)) @@ -448,6 +591,11 @@ bool task_propogate(const char *curdir) { success = false; continue; } + /* + * Generate a temportary file name for the output binary + * so we don't trample over an existing one. + */ + template->tempfilename = tempnam(curdir, "TMPDAT"); /* * Generate the command required to open a pipe to a process @@ -456,11 +604,12 @@ bool task_propogate(const char *curdir) { */ char buf[4096]; /* one page should be enough */ memset (buf,0,sizeof(buf)); - snprintf(buf, sizeof(buf), "%s %s/%s %s", + snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s", task_bins[TASK_COMPILE], curdir, template->sourcefile, - template->compileflags + template->compileflags, + template->tempfilename ); /* @@ -469,24 +618,133 @@ bool task_propogate(const char *curdir) { */ task_t task; task.template = template; - if (!(task.handle = popen(buf, "r"))) { + if (!(task.runhandles = task_popen(buf, "r"))) { con_err("error opening pipe to process for test: %s\n", template->description); success = false; continue; } + con_out("executing test: `%s` [%s]\n", template->description, buf); + /* + * Open up some file desciptors for logging the stdout/stderr + * to our own. + */ + memset (buf,0,sizeof(buf)); + snprintf(buf, sizeof(buf), "%s/%s.stdout", curdir, template->sourcefile); + task.stdoutlogfile = util_strdup(buf); + task.stdoutlog = fopen(buf, "w"); + + memset (buf,0,sizeof(buf)); + snprintf(buf, sizeof(buf), "%s/%s.stderr", curdir, template->sourcefile); + task.stderrlogfile = util_strdup(buf); + task.stderrlog = fopen(buf, "w"); + vec_push(task_tasks, task); } } + + closedir(dir); return success; } -void task_destroy() { +/* + * Removes all temporary 'progs.dat' files created during compilation + * of all tests' + */ +void task_cleanup(const char *curdir) { + DIR *dir; + struct dirent *files; + char buffer[4096]; + + dir = opendir(curdir); + + while ((files = readdir(dir))) { + memset(buffer, 0, sizeof(buffer)); + if (strstr(files->d_name, "TMP")) { + snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name); + if (remove(buffer)) + con_err("error removing temporary file: %s\n", buffer); + else + con_out("removed temporary file: %s\n", buffer); + } + } + + closedir(dir); +} + +/* + * Task precleanup removes any existing temporary files or log files + * left behind from a previous invoke of the test-suite. + */ +void task_precleanup(const char *curdir) { + DIR *dir; + struct dirent *files; + char buffer[4096]; + + dir = opendir(curdir); + + while ((files = readdir(dir))) { + memset(buffer, 0, sizeof(buffer)); + if (strstr(files->d_name, "TMP") || + strstr(files->d_name, ".stdout") || + strstr(files->d_name, ".stderr")) + { + snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name); + if (remove(buffer)) + con_err("error removing temporary file: %s\n", buffer); + else + con_out("removed temporary file: %s\n", buffer); + } + } + + closedir(dir); +} + +void task_destroy(const char *curdir) { + /* + * Free all the data in the task list and finally the list itself + * then proceed to cleanup anything else outside the program like + * temporary files. + */ size_t i; - for (i = 0; i < vec_size(task_tasks); i++) + for (i = 0; i < vec_size(task_tasks); i++) { + /* + * Close any open handles to files or processes here. It's mighty + * annoying to have to do all this cleanup work. + */ + if (task_tasks[i].runhandles) task_pclose(task_tasks[i].runhandles); + if (task_tasks[i].stdoutlog) fclose (task_tasks[i].stdoutlog); + if (task_tasks[i].stderrlog) fclose (task_tasks[i].stderrlog); + + /* + * Only remove the log files if the test actually compiled otherwise + * forget about it. + */ + if (task_tasks[i].compiled) { + if (remove(task_tasks[i].stdoutlogfile)) + con_err("error removing stdout log file: %s\n", task_tasks[i].stdoutlogfile); + else + con_out("removed stdout log file: %s\n", task_tasks[i].stdoutlogfile); + + if (remove(task_tasks[i].stderrlogfile)) + con_err("error removing stderr log file: %s\n", task_tasks[i].stderrlogfile); + else + con_out("removed stderr log file: %s\n", task_tasks[i].stderrlogfile); + } + + /* free util_strdup data for log files */ + mem_d(task_tasks[i].stdoutlogfile); + mem_d(task_tasks[i].stderrlogfile); + task_template_destroy(&task_tasks[i].template); + } vec_free(task_tasks); + + /* + * Cleanup outside stuff like temporary files. + */ + task_cleanup(curdir); } /* @@ -505,15 +763,15 @@ bool task_execute(task_template_t *template) { * actually specified. */ if (!strcmp(template->executeflags, "$null")) { - snprintf(buffer, sizeof(buffer), "%s %s %s", + snprintf(buffer, sizeof(buffer), "%s %s", task_bins[TASK_EXECUTE], - template->executeflags, - "progs.dat" + template->tempfilename ); } else { - snprintf(buffer, sizeof(buffer), "%s %s", + snprintf(buffer, sizeof(buffer), "%s %s %s", task_bins[TASK_EXECUTE], - "progs.dat" + template->executeflags, + template->tempfilename ); } @@ -545,6 +803,13 @@ bool task_execute(task_template_t *template) { return false; } + /* + * Trim newlines from data since they will just break our + * ability to properly validate matches. + */ + if (strrchr(data, '\n')) + *strrchr(data, '\n') = '\0'; + /* null list */ if (!strcmp(template->comparematch, "$null")) success = true; @@ -566,11 +831,11 @@ bool task_execute(task_template_t *template) { * execution this takes more work since a task needs to be generated * from thin air and executed INLINE. */ -void task_schedualize() { - bool execute = false; - char *back = NULL; - char *data = NULL; - size_t size = 0; +void task_schedualize(const char *curdir) { + bool execute = false; + char *back = NULL; + char *data = NULL; + size_t size = 0; size_t i; for (i = 0; i < vec_size(task_tasks); i++) { @@ -581,23 +846,40 @@ void task_schedualize() { if (!strcmp(task_tasks[i].template->proceduretype, "-execute")) execute = true; - while (util_getline(&data, &size, task_tasks[i].handle) != EOF) { + /* + * We assume it compiled before we actually compiled :). On error + * we change the value + */ + task_tasks[i].compiled = true; + + /* + * Read data from stdout first and pipe that stuff into a log file + * then we do the same for stderr. + */ + while (util_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) { + back = data; + fputs(data, task_tasks[i].stdoutlog); + fflush(task_tasks[i].stdoutlog); + } + while (util_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) { back = data; - /* chances are we want to print errors */ + /* + * If a string contains an error we just dissalow execution + * of it in the vm. + * + * TODO: make this more percise, e.g if we print a warning + * that refers to a variable named error, or something like + * that .. then this will blowup :P + */ if (strstr(data, "error")) { - con_out("compile failed: %s\n", - /* strip the newline from the end */ - (*strrchr(data, '\n')='\0') - ); - - /* - * The compilation failed which means it cannot be executed - * as the file simply will not exist. - */ - execute = false; - break; + execute = false; + task_tasks[i].compiled = false; } + + fputs(data, task_tasks[i].stderrlog); + fflush(task_tasks[i].stdoutlog); } + if (back) mem_d(back); @@ -626,23 +908,31 @@ void task_schedualize() { (task_tasks[i].template->successmessage) ? task_tasks[i].template->successmessage : "unknown" ); - - /* - * We need to unlink the progs.dat file that is sitting around - * collecting dust. - */ - unlink("progs.dat"); } if (back) mem_d(back); } -int main(int argc, char **argv) { - con_init(); - if (!task_propogate("tests")) { +/* + * This is the heart of the whole test-suite process. This cleans up + * any existing temporary files left behind as well as log files left + * behind. Then it propogates a list of tests from `curdir` by scaning + * it for template files and compiling them into tasks, in which it + * schedualizes them (executes them) and actually reports errors and + * what not. It then proceeds to destroy the tasks and return memory + * it's the engine :) + * + * It returns true of tests could be propogated, otherwise it returns + * false. + * + * It expects con_init() was called before hand. + */ +bool test_perform(const char *curdir) { + task_precleanup(curdir); + if (!task_propogate(curdir)) { con_err("error: failed to propogate tasks\n"); - task_destroy(); - return -1; + task_destroy(curdir); + return false; } /* * If we made it here all tasks where propogated from their resultant @@ -651,9 +941,82 @@ int main(int argc, char **argv) { * it's designed to prevent lock contention, and possible syncronization * issues. */ - task_schedualize(); + task_schedualize(curdir); + task_destroy(curdir); + + return true; +} + +/* + * Fancy GCC-like LONG parsing allows things like --opt=param with + * assignment operator. This is used for redirecting stdout/stderr + * console to specific files of your choice. + */ +static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) { + int argc = *argc_; + char **argv = *argv_; + + size_t len = strlen(optname); + + if (strncmp(argv[0]+ds, optname, len)) + return false; + + /* it's --optname, check how the parameter is supplied */ + if (argv[0][ds+len] == '=') { + *out = argv[0]+ds+len+1; + return true; + } + + if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */ + return false; + + /* using --opt param */ + *out = argv[1]; + --*argc_; + ++*argv_; + return true; +} + +int main(int argc, char **argv) { + char *redirout = (char*)stdout; + char *redirerr = (char*)stderr; + con_init(); - task_destroy(); + /* + * Command line option parsing commences now We only need to support + * a few things in the test suite. + */ + while (argc > 1) { + ++argv; + --argc; + + if (argv[0][0] == '-') { + if (parsecmd("redirout", &argc, &argv, &redirout, 1, false)) + continue; + if (parsecmd("redirerr", &argc, &argv, &redirerr, 1, false)) + continue; + + con_change(redirout, redirerr); + + if (!strcmp(argv[0]+1, "debug")) { + opts_debug = true; + continue; + } + if (!strcmp(argv[0]+1, "memchk")) { + opts_memchk = true; + continue; + } + if (!strcmp(argv[0]+1, "nocolor")) { + con_color(0); + continue; + } + + con_err("invalid argument %s\n", argv[0]+1); + return -1; + } + } + con_change(redirout, redirerr); + test_perform("tests"); util_meminfo(); return 0; }