2 * Copyright (C) 2012, 2013, 2014, 2015
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include <sys/types.h>
36 static const char *task_bins[] = {
49 static FILE **task_popen(const char *command, const char *mode) {
55 popen_t *data = (popen_t*)mem_a(sizeof(popen_t));
58 * Parse the command now into a list for execv, this is a pain
61 char *line = (char*)command;
65 while (*line != '\0') {
66 while (*line == ' ' || *line == '\t' || *line == '\n')
70 while (*line != '\0' && *line != ' ' &&
71 *line != '\t' && *line != '\n') line++;
73 vec_push(argv, (char *)0);
77 if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0;
78 if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1;
79 if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2;
81 if ((data->pid = fork()) > 0) {
86 data->pipes[0] = inhandle [1];
87 data->pipes[1] = outhandle[0];
88 data->pipes[2] = errhandle[0];
89 data->handles[0] = fdopen(inhandle [1], "w");
90 data->handles[1] = fdopen(outhandle[0], mode);
91 data->handles[2] = fdopen(errhandle[0], mode);
96 } else if (data->pid == 0) {
102 /* see piping documentation for this sillyness :P */
103 dup2(inhandle [0], 0);
104 dup2(outhandle[1], 1);
105 dup2(errhandle[1], 2);
111 goto task_popen_error_3;
114 task_popen_error_3: close(errhandle[0]), close(errhandle[1]);
115 task_popen_error_2: close(outhandle[0]), close(outhandle[1]);
116 task_popen_error_1: close(inhandle [0]), close(inhandle [1]);
123 static int task_pclose(FILE **handles) {
124 popen_t *data = (popen_t*)handles;
127 close(data->pipes[0]); /* stdin */
128 close(data->pipes[1]); /* stdout */
129 close(data->pipes[2]); /* stderr */
131 waitpid(data->pid, &status, 0);
138 #define TASK_COMPILE 0
139 #define TASK_EXECUTE 1
141 * Task template system:
142 * templates are rules for a specific test, used to create a "task" that
143 * is executed with those set of rules (arguments, and what not). Tests
144 * that don't have a template with them cannot become tasks, since without
145 * the information for that test there is no way to properly "test" them.
146 * Rules for these templates are described in a template file, using a
147 * task template language.
149 * The language is a basic finite statemachine, top-down single-line
150 * description language.
152 * The languge is composed entierly of "tags" which describe a string of
153 * text for a task. Think of it much like a configuration file. Except
154 * it's been designed to allow flexibility and future support for prodecual
157 * The following "tags" are suported by the language
160 * Used to set a description of the current test, this must be
161 * provided, this tag is NOT optional.
164 * Used to set the procedure for the given task, there are two
167 * This simply performs compilation only
169 * This will perform compilation and execution
171 * This will perform compilation, but requires
172 * the compilation to fail in order to succeed.
174 * This must be provided, this tag is NOT optional.
177 * Used to set the compilation flags for the given task, this
178 * must be provided, this tag is NOT optional.
180 * F: Used to set some test suite flags, currently the only option
181 * is -no-defs (to including of defs.qh)
184 * Used to set the execution flags for the given task. This tag
185 * must be provided if T == -execute, otherwise it's erroneous
186 * as compilation only takes place.
189 * Used to describe a string of text that should be matched from
190 * the output of executing the task. If this doesn't match the
191 * task fails. This tag must be provided if T == -execute, otherwise
192 * it's erroneous as compilation only takes place.
195 * Used to specify the INPUT source file to operate on, this must be
196 * provided, this tag is NOT optional
200 * These tags have one-time use, using them more than once will result
201 * in template compilation errors.
203 * Lines beginning with # or // in the template file are comments and
204 * are ignored by the template parser.
206 * Whitespace is optional, with exception to the colon ':' between the
207 * tag and it's assignment value/
209 * The template compiler will detect erronrous tags (optional tags
210 * that need not be set), as well as missing tags, and error accordingly
211 * this will result in the task failing.
226 * This is very much like a compiler code generator :-). This generates
227 * a value from some data observed from the compiler.
229 static bool task_template_generate(task_template_t *tmpl, char tag, const char *file, size_t line, char *value, size_t *pad) {
232 char **destval = NULL;
238 case 'D': destval = &tmpl->description; break;
239 case 'T': destval = &tmpl->proceduretype; break;
240 case 'C': destval = &tmpl->compileflags; break;
241 case 'E': destval = &tmpl->executeflags; break;
242 case 'I': destval = &tmpl->sourcefile; break;
243 case 'F': destval = &tmpl->testflags; break;
245 con_printmsg(LVL_ERROR, __FILE__, __LINE__, 0, "internal error",
246 "invalid tag `%c:` during code generation\n",
253 * Ensure if for the given tag, there already exists a
257 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "compile error",
258 "tag `%c:` already assigned value: %s\n",
265 * Strip any whitespace that might exist in the value for assignments
268 if (value && *value && (*value == ' ' || *value == '\t'))
274 * Value will contain a newline character at the end, we need to strip
275 * this otherwise kaboom, seriously, kaboom :P
277 if (strchr(value, '\n'))
278 *strrchr(value, '\n')='\0';
281 * Now allocate and set the actual value for the specific tag. Which
282 * was properly selected and can be accessed with *destval.
284 *destval = util_strdup(value);
287 if (*destval == tmpl->description) {
289 * Create some padding for the description to align the
290 * printing of the rules file.
292 if ((desclen = strlen(tmpl->description)) > pad[0])
296 if ((filelen = strlen(file)) > pad[2])
302 static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *fp, size_t *pad) {
311 /* top down parsing */
312 while (util_getline(&back, &size, fp) != EOF) {
313 /* skip whitespace */
315 if (*data && (*data == ' ' || *data == '\t'))
320 * Handle comments inside task tmpl files. We're strict
321 * about the language for fun :-)
324 if (data[1] != '/') {
325 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
326 "invalid character `/`, perhaps you meant `//` ?");
335 * Empty newlines are acceptable as well, so we handle that here
336 * despite being just odd since there should't be that many
337 * empty lines to begin with.
345 * Now begin the actual "tag" stuff. This works as you expect
354 if (data[1] != ':') {
355 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
356 "expected `:` after `%c`",
361 if (!task_template_generate(tmpl, *data, file, line, &data[3], pad)) {
362 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl compile error",
363 "failed to generate for given task\n"
370 * Match requires it's own system since we allow multiple M's
371 * for multi-line matching.
375 char *value = &data[3];
376 if (data[1] != ':') {
377 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
378 "expected `:` after `%c`",
385 * Value will contain a newline character at the end, we need to strip
386 * this otherwise kaboom, seriously, kaboom :P
388 if (strrchr(value, '\n'))
389 *strrchr(value, '\n')='\0';
390 else /* cppcheck: possible null pointer dereference */
393 vec_push(tmpl->comparematch, util_strdup(value));
399 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
400 "invalid tag `%c`", *data
403 /* no break required */
406 /* update line and free old sata */
421 * Nullifies the template data: used during initialization of a new
424 static void task_template_nullify(task_template_t *tmpl) {
428 tmpl->description = NULL;
429 tmpl->proceduretype = NULL;
430 tmpl->compileflags = NULL;
431 tmpl->executeflags = NULL;
432 tmpl->comparematch = NULL;
433 tmpl->sourcefile = NULL;
434 tmpl->tempfilename = NULL;
435 tmpl->rulesfile = NULL;
436 tmpl->testflags = NULL;
439 static task_template_t *task_template_compile(const char *file, const char *dir, size_t *pad) {
440 /* a page should be enough */
443 FILE *tempfile = NULL;
444 task_template_t *tmpl = NULL;
446 util_snprintf(fullfile, sizeof(fullfile), "%s/%s", dir, file);
448 tempfile = fopen(fullfile, "r");
449 tmpl = (task_template_t*)mem_a(sizeof(task_template_t));
450 task_template_nullify(tmpl);
453 * Create some padding for the printing to align the
454 * printing of the rules file to the console.
456 if ((filepadd = strlen(fullfile)) > pad[1])
459 tmpl->rulesfile = util_strdup(fullfile);
462 * Esnure the file even exists for the task, this is pretty useless
466 con_err("template file: %s does not exist or invalid permissions\n",
472 if (!task_template_parse(file, tmpl, tempfile, pad)) {
473 con_err("template parse error: error during parsing\n");
478 * Regardless procedure type, the following tags must exist:
484 if (!tmpl->description) {
485 con_err("template compile error: %s missing `D:` tag\n", file);
488 if (!tmpl->proceduretype) {
489 con_err("template compile error: %s missing `T:` tag\n", file);
492 if (!tmpl->compileflags) {
493 con_err("template compile error: %s missing `C:` tag\n", file);
496 if (!tmpl->sourcefile) {
497 con_err("template compile error: %s missing `I:` tag\n", file);
502 * Now lets compile the template, compilation is really just
503 * the process of validating the input.
505 if (!strcmp(tmpl->proceduretype, "-compile")) {
506 if (tmpl->executeflags)
507 con_err("template compile warning: %s erroneous tag `E:` when only compiling\n", file);
508 if (tmpl->comparematch)
509 con_err("template compile warning: %s erroneous tag `M:` when only compiling\n", file);
511 } else if (!strcmp(tmpl->proceduretype, "-execute")) {
512 if (!tmpl->executeflags) {
513 /* default to $null */
514 tmpl->executeflags = util_strdup("$null");
516 if (!tmpl->comparematch) {
517 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
520 } else if (!strcmp(tmpl->proceduretype, "-fail")) {
521 if (tmpl->executeflags)
522 con_err("template compile warning: %s erroneous tag `E:` when only failing\n", file);
523 if (tmpl->comparematch)
524 con_err("template compile warning: %s erroneous tag `M:` when only failing\n", file);
525 } else if (!strcmp(tmpl->proceduretype, "-diagnostic")) {
526 if (tmpl->executeflags)
527 con_err("template compile warning: %s erroneous tag `E:` when only diagnostic\n", file);
528 if (!tmpl->comparematch) {
529 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
532 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
533 if (tmpl->executeflags)
534 con_err("template compile warning: %s erroneous tag `E:` when only preprocessing\n", file);
535 if (!tmpl->comparematch) {
536 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
540 con_err("template compile error: %s invalid procedure type: %s\n", file, tmpl->proceduretype);
550 * The file might not exist and we jump here when that doesn't happen
551 * so the check to see if it's not null here is required.
560 static void task_template_destroy(task_template_t *tmpl) {
564 if (tmpl->description) mem_d(tmpl->description);
565 if (tmpl->proceduretype) mem_d(tmpl->proceduretype);
566 if (tmpl->compileflags) mem_d(tmpl->compileflags);
567 if (tmpl->executeflags) mem_d(tmpl->executeflags);
568 if (tmpl->sourcefile) mem_d(tmpl->sourcefile);
569 if (tmpl->rulesfile) mem_d(tmpl->rulesfile);
570 if (tmpl->testflags) mem_d(tmpl->testflags);
573 * Delete all allocated string for task tmpl then destroy the
578 for (; i < vec_size(tmpl->comparematch); i++)
579 mem_d(tmpl->comparematch[i]);
581 vec_free(tmpl->comparematch);
585 * Nullify all the template members otherwise NULL comparision
586 * checks will fail if tmpl pointer is reused.
588 mem_d(tmpl->tempfilename);
593 * Now comes the task manager, this system allows adding tasks in and out
594 * of a task list. This is the executor of the tasks essentially as well.
597 task_template_t *tmpl;
606 static task_t *task_tasks = NULL;
609 * Read a directory and searches for all template files in it
610 * which is later used to run all tests.
612 static bool task_propagate(const char *curdir, size_t *pad, const char *defs) {
615 struct dirent *files;
616 struct stat directory;
619 char **directories = NULL;
620 char *claim = util_strdup(curdir);
623 vec_push(directories, claim);
624 dir = opendir(claim);
627 * Generate a list of subdirectories since we'll be checking them too
630 while ((files = readdir(dir))) {
631 util_asprintf(&claim, "%s/%s", curdir, files->d_name);
632 if (stat(claim, &directory) == -1) {
638 if (S_ISDIR(directory.st_mode) && files->d_name[0] != '.') {
639 vec_push(directories, claim);
648 * Now do all the work, by touching all the directories inside
649 * test as well and compile the task templates into data we can
650 * use to run the tests.
652 for (i = 0; i < vec_size(directories); i++) {
653 dir = opendir(directories[i]);
655 while ((files = readdir(dir))) {
656 util_snprintf(buffer, sizeof(buffer), "%s/%s", directories[i], files->d_name);
657 if (stat(buffer, &directory) == -1) {
658 con_err("internal error: stat failed, aborting\n");
662 if (S_ISDIR(directory.st_mode))
666 * We made it here, which concludes the file/directory is not
667 * actually a directory, so it must be a file :)
669 if (strcmp(files->d_name + strlen(files->d_name) - 5, ".tmpl") == 0) {
670 task_template_t *tmpl = task_template_compile(files->d_name, directories[i], pad);
671 char buf[4096]; /* one page should be enough */
672 const char *qcflags = NULL;
677 con_err("error compiling task template: %s\n", files->d_name);
682 * Generate a temportary file name for the output binary
683 * so we don't trample over an existing one.
685 tmpl->tempfilename = NULL;
686 util_asprintf(&tmpl->tempfilename, "%s/TMPDAT.%s.dat", directories[i], files->d_name);
689 * Additional QCFLAGS enviroment variable may be used
690 * to test compile flags for all tests. This needs to be
691 * BEFORE other flags (so that the .tmpl can override them)
693 qcflags = getenv("QCFLAGS");
696 * Generate the command required to open a pipe to a process
697 * which will be refered to with a handle in the task for
698 * reading the data from the pipe.
700 if (strcmp(tmpl->proceduretype, "-pp")) {
702 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
703 util_snprintf(buf, sizeof(buf), "%s %s/%s %s %s -o %s",
704 task_bins[TASK_COMPILE],
712 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s %s -o %s",
713 task_bins[TASK_COMPILE],
724 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
725 util_snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s",
726 task_bins[TASK_COMPILE],
733 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s -o %s",
734 task_bins[TASK_COMPILE],
745 /* Preprocessing (qcflags mean shit all here we don't allow them) */
746 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
747 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s -o %s",
748 task_bins[TASK_COMPILE],
755 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s %s -o %s",
756 task_bins[TASK_COMPILE],
768 * The task template was compiled, now lets create a task from
769 * the template data which has now been propagated.
772 if (!(task.runhandles = task_popen(buf, "r"))) {
773 con_err("error opening pipe to process for test: %s\n", tmpl->description);
779 * Open up some file desciptors for logging the stdout/stderr
782 util_snprintf(buf, sizeof(buf), "%s.stdout", tmpl->tempfilename);
783 task.stdoutlogfile = util_strdup(buf);
784 if (!(task.stdoutlog = fopen(buf, "w"))) {
785 con_err("error opening %s for stdout\n", buf);
789 util_snprintf(buf, sizeof(buf), "%s.stderr", tmpl->tempfilename);
790 task.stderrlogfile = util_strdup(buf);
791 if (!(task.stderrlog = fopen(buf, "w"))) {
792 con_err("error opening %s for stderr\n", buf);
795 vec_push(task_tasks, task);
799 mem_d(directories[i]); /* free claimed memory */
801 vec_free(directories);
807 * Task precleanup removes any existing temporary files or log files
808 * left behind from a previous invoke of the test-suite.
810 static void task_precleanup(const char *curdir) {
812 struct dirent *files;
815 dir = opendir(curdir);
817 while ((files = readdir(dir))) {
818 if (strstr(files->d_name, "TMP") ||
819 strstr(files->d_name, ".stdout") ||
820 strstr(files->d_name, ".stderr") ||
821 strstr(files->d_name, ".dat"))
823 util_snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
825 con_err("error removing temporary file: %s\n", buffer);
832 static void task_destroy(void) {
834 * Free all the data in the task list and finally the list itself
835 * then proceed to cleanup anything else outside the program like
839 for (i = 0; i < vec_size(task_tasks); i++) {
841 * Close any open handles to files or processes here. It's mighty
842 * annoying to have to do all this cleanup work.
844 if (task_tasks[i].stdoutlog) fclose(task_tasks[i].stdoutlog);
845 if (task_tasks[i].stderrlog) fclose(task_tasks[i].stderrlog);
848 * Only remove the log files if the test actually compiled otherwise
849 * forget about it (or if it didn't compile, and the procedure type
850 * was set to -fail (meaning it shouldn't compile) .. stil remove)
852 if (task_tasks[i].compiled || !strcmp(task_tasks[i].tmpl->proceduretype, "-fail")) {
853 if (remove(task_tasks[i].stdoutlogfile))
854 con_err("error removing stdout log file: %s\n", task_tasks[i].stdoutlogfile);
855 if (remove(task_tasks[i].stderrlogfile))
856 con_err("error removing stderr log file: %s\n", task_tasks[i].stderrlogfile);
858 (void)!remove(task_tasks[i].tmpl->tempfilename);
861 /* free util_strdup data for log files */
862 mem_d(task_tasks[i].stdoutlogfile);
863 mem_d(task_tasks[i].stderrlogfile);
865 task_template_destroy(task_tasks[i].tmpl);
867 vec_free(task_tasks);
871 * This executes the QCVM task for a specificly compiled progs.dat
872 * using the template passed into it for call-flags and user defined
873 * messages IF the procedure type is -execute, otherwise it matches
874 * the preprocessor output.
876 static bool task_trymatch(size_t i, char ***line) {
879 int retval = EXIT_SUCCESS;
882 task_template_t *tmpl = task_tasks[i].tmpl;
884 memset (buffer,0,sizeof(buffer));
886 if (!strcmp(tmpl->proceduretype, "-execute")) {
888 * Drop the execution flags for the QCVM if none where
889 * actually specified.
891 if (!strcmp(tmpl->executeflags, "$null")) {
892 util_snprintf(buffer, sizeof(buffer), "%s %s",
893 task_bins[TASK_EXECUTE],
897 util_snprintf(buffer, sizeof(buffer), "%s %s %s",
898 task_bins[TASK_EXECUTE],
904 execute = popen(buffer, "r");
907 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
909 * we're preprocessing, which means we need to read int
910 * the produced file and do some really weird shit.
912 if (!(execute = fopen(tmpl->tempfilename, "r")))
917 * we're testing diagnostic output, which means it will be
918 * in runhandles[2] (stderr) since that is where the compiler
921 if (!(execute = fopen(task_tasks[i].stderrlogfile, "r")))
927 * Now lets read the lines and compare them to the matches we expect
928 * and handle accordingly.
935 while (util_getline(&data, &size, execute) != EOF) {
936 if (!strcmp(data, "No main function found\n")) {
937 con_err("test failure: `%s` (No main function found) [%s]\n",
944 pclose((FILE*)execute);
949 * Trim newlines from data since they will just break our
950 * ability to properly validate matches.
952 if (strrchr(data, '\n'))
953 *strrchr(data, '\n') = '\0';
956 * We remove the file/directory and stuff from the error
957 * match messages when testing diagnostics.
959 if(!strcmp(tmpl->proceduretype, "-diagnostic")) {
960 if (strstr(data, "there have been errors, bailing out"))
961 continue; /* ignore it */
962 if (strstr(data, ": error: ")) {
963 char *claim = util_strdup(data + (strstr(data, ": error: ") - data) + 9);
970 * We need to ignore null lines for when -pp is used (preprocessor), since
971 * the preprocessor is likely to create empty newlines in certain macro
972 * instantations, otherwise it's in the wrong nature to ignore empty newlines.
974 if (!strcmp(tmpl->proceduretype, "-pp") && !*data)
977 if (vec_size(tmpl->comparematch) > compare) {
978 if (strcmp(data, tmpl->comparematch[compare++])) {
986 * Copy to output vector for diagnostics if execution match
989 vec_push(*line, data);
996 if (compare != vec_size(tmpl->comparematch))
1004 retval = pclose((FILE*)execute);
1008 return success && retval == EXIT_SUCCESS;
1011 static const char *task_type(task_template_t *tmpl) {
1012 if (!strcmp(tmpl->proceduretype, "-pp"))
1013 return "type: preprocessor";
1014 if (!strcmp(tmpl->proceduretype, "-execute"))
1015 return "type: execution";
1016 if (!strcmp(tmpl->proceduretype, "-compile"))
1017 return "type: compile";
1018 if (!strcmp(tmpl->proceduretype, "-diagnostic"))
1019 return "type: diagnostic";
1020 return "type: fail";
1024 * This schedualizes all tasks and actually runs them individually
1025 * this is generally easy for just -compile variants. For compile and
1026 * execution this takes more work since a task needs to be generated
1027 * from thin air and executed INLINE.
1030 static size_t task_schedualize(size_t *pad) {
1032 bool execute = false;
1034 char **match = NULL;
1041 util_snprintf(space[0], sizeof(space[0]), "%d", (int)vec_size(task_tasks));
1043 for (; i < vec_size(task_tasks); i++) {
1044 memset(space[1], 0, sizeof(space[1]));
1045 util_snprintf(space[1], sizeof(space[1]), "%d", (int)(i + 1));
1047 con_out("test #%u %*s", i + 1, strlen(space[0]) - strlen(space[1]), "");
1050 * Generate a task from thin air if it requires execution in
1054 /* diagnostic is not executed, but compare tested instead, like preproessor */
1055 execute = !! (!strcmp(task_tasks[i].tmpl->proceduretype, "-execute")) ||
1056 (!strcmp(task_tasks[i].tmpl->proceduretype, "-pp")) ||
1057 (!strcmp(task_tasks[i].tmpl->proceduretype, "-diagnostic"));
1060 * We assume it compiled before we actually compiled :). On error
1061 * we change the value
1063 task_tasks[i].compiled = true;
1066 * Read data from stdout first and pipe that stuff into a log file
1067 * then we do the same for stderr.
1069 while (util_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) {
1070 fputs(data, task_tasks[i].stdoutlog);
1072 if (strstr(data, "failed to open file")) {
1073 task_tasks[i].compiled = false;
1077 while (util_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) {
1079 * If a string contains an error we just dissalow execution
1082 * TODO: make this more percise, e.g if we print a warning
1083 * that refers to a variable named error, or something like
1084 * that .. then this will blowup :P
1086 if (strstr(data, "error") && strcmp(task_tasks[i].tmpl->proceduretype, "-diagnostic")) {
1088 task_tasks[i].compiled = false;
1091 fputs(data, task_tasks[i].stderrlog);
1092 fflush(task_tasks[i].stderrlog); /* fast flush for read */
1095 if (!task_tasks[i].compiled && strcmp(task_tasks[i].tmpl->proceduretype, "-fail")) {
1096 con_out("failure: `%s` %*s %*s\n",
1097 task_tasks[i].tmpl->description,
1098 (pad[0] + pad[1] - strlen(task_tasks[i].tmpl->description)) + (strlen(task_tasks[i].tmpl->rulesfile) - pad[1]),
1099 task_tasks[i].tmpl->rulesfile,
1100 (pad[1] + pad[2] - strlen(task_tasks[i].tmpl->rulesfile)) + (strlen("(failed to compile)") - pad[2]),
1101 "(failed to compile)"
1107 status = task_pclose(task_tasks[i].runhandles);
1108 if ((!strcmp(task_tasks[i].tmpl->proceduretype, "-fail") && status == EXIT_SUCCESS)
1109 || ( strcmp(task_tasks[i].tmpl->proceduretype, "-fail") && status == EXIT_FAILURE)) {
1110 con_out("failure: `%s` %*s %*s\n",
1111 task_tasks[i].tmpl->description,
1112 (pad[0] + pad[1] - strlen(task_tasks[i].tmpl->description)) + (strlen(task_tasks[i].tmpl->rulesfile) - pad[1]),
1113 task_tasks[i].tmpl->rulesfile,
1114 (pad[1] + pad[2] - strlen(task_tasks[i].tmpl->rulesfile)) + (strlen("(compiler didn't return exit success)") - pad[2]),
1115 "(compiler didn't return exit success)"
1122 con_out("succeeded: `%s` %*s %*s\n",
1123 task_tasks[i].tmpl->description,
1124 (pad[0] + pad[1] - strlen(task_tasks[i].tmpl->description)) + (strlen(task_tasks[i].tmpl->rulesfile) - pad[1]),
1125 task_tasks[i].tmpl->rulesfile,
1126 (pad[1] + pad[2] - strlen(task_tasks[i].tmpl->rulesfile)) + (strlen(task_type(task_tasks[i].tmpl)) - pad[2]),
1127 task_type(task_tasks[i].tmpl)
1134 * If we made it here that concludes the task is to be executed
1135 * in the virtual machine (or the preprocessor output needs to
1138 if (!task_trymatch(i, &match)) {
1141 con_out("failure: `%s` %*s %*s\n",
1142 task_tasks[i].tmpl->description,
1143 (pad[0] + pad[1] - strlen(task_tasks[i].tmpl->description)) + (strlen(task_tasks[i].tmpl->rulesfile) - pad[1]),
1144 task_tasks[i].tmpl->rulesfile,
1145 (pad[1] + pad[2] - strlen(task_tasks[i].tmpl->rulesfile)) + (strlen(
1146 (strcmp(task_tasks[i].tmpl->proceduretype, "-pp"))
1147 ? "(invalid results from execution)"
1148 : (strcmp(task_tasks[i].tmpl->proceduretype, "-diagnostic"))
1149 ? "(invalid results from preprocessing)"
1150 : "(invalid results from compiler diagnsotics)"
1152 (strcmp(task_tasks[i].tmpl->proceduretype, "-pp"))
1153 ? "(invalid results from execution)"
1154 : (strcmp(task_tasks[i].tmpl->proceduretype, "-diagnostic"))
1155 ? "(invalid results from preprocessing)"
1156 : "(invalid results from compiler diagnsotics)"
1160 * Print nicely formatted expected match lists to console error
1161 * handler for the all the given matches in the template file and
1162 * what was actually returned from executing.
1164 con_out(" Expected From %u Matches: (got %u Matches)\n",
1165 vec_size(task_tasks[i].tmpl->comparematch),
1168 for (; d < vec_size(task_tasks[i].tmpl->comparematch); d++) {
1169 char *select = task_tasks[i].tmpl->comparematch[d];
1170 size_t length = 60 - strlen(select);
1172 con_out(" Expected: \"%s\"", select);
1175 con_out("| Got: \"%s\"\n", (d >= vec_size(match)) ? "<<nothing else to compare>>" : match[d]);
1179 * Print the non-expected out (since we are simply not expecting it)
1180 * This will help track down bugs in template files that fail to match
1183 if (vec_size(match) > vec_size(task_tasks[i].tmpl->comparematch)) {
1184 for (d = 0; d < vec_size(match) - vec_size(task_tasks[i].tmpl->comparematch); d++) {
1185 con_out(" Expected: Nothing | Got: \"%s\"\n",
1186 match[d + vec_size(task_tasks[i].tmpl->comparematch)]
1192 for (j = 0; j < vec_size(match); j++)
1199 for (j = 0; j < vec_size(match); j++)
1203 con_out("succeeded: `%s` %*s %*s\n",
1204 task_tasks[i].tmpl->description,
1205 (pad[0] + pad[1] - strlen(task_tasks[i].tmpl->description)) + (strlen(task_tasks[i].tmpl->rulesfile) - pad[1]),
1206 task_tasks[i].tmpl->rulesfile,
1207 (pad[1] + pad[2] - strlen(task_tasks[i].tmpl->rulesfile)) + (strlen(task_type(task_tasks[i].tmpl))- pad[2]),
1208 task_type(task_tasks[i].tmpl)
1217 * This is the heart of the whole test-suite process. This cleans up
1218 * any existing temporary files left behind as well as log files left
1219 * behind. Then it propagates a list of tests from `curdir` by scaning
1220 * it for template files and compiling them into tasks, in which it
1221 * schedualizes them (executes them) and actually reports errors and
1222 * what not. It then proceeds to destroy the tasks and return memory
1223 * it's the engine :)
1225 * It returns true of tests could be propagated, otherwise it returns
1228 * It expects con_init() was called before hand.
1230 static GMQCC_WARN bool test_perform(const char *curdir, const char *defs) {
1231 size_t failed = false;
1232 static const char *default_defs = "defs.qh";
1235 /* test ### [succeed/fail]: `description` [tests/template.tmpl] [type] */
1240 * If the default definition file isn't set to anything. We will
1241 * use the default_defs here, which is "defs.qc"
1244 defs = default_defs;
1248 task_precleanup(curdir);
1249 if (!task_propagate(curdir, pad, defs)) {
1250 con_err("error: failed to propagate tasks\n");
1255 * If we made it here all tasks where propagated from their resultant
1256 * template file. So we can start the FILO scheduler, this has been
1257 * designed in the most thread-safe way possible for future threading
1258 * it's designed to prevent lock contention, and possible syncronization
1261 failed = task_schedualize(pad);
1263 con_out("%u out of %u tests failed\n", failed, vec_size(task_tasks));
1266 return (failed) ? false : true;
1270 * Fancy GCC-like LONG parsing allows things like --opt=param with
1271 * assignment operator. This is used for redirecting stdout/stderr
1272 * console to specific files of your choice.
1274 static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
1276 char **argv = *argv_;
1278 size_t len = strlen(optname);
1280 if (strncmp(argv[0]+ds, optname, len))
1283 /* it's --optname, check how the parameter is supplied */
1284 if (argv[0][ds+len] == '=') {
1285 *out = argv[0]+ds+len+1;
1289 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
1292 /* using --opt param */
1299 int main(int argc, char **argv) {
1300 bool succeed = false;
1304 OPTS_OPTION_U16(OPTION_MEMDUMPCOLS) = 16;
1307 * Command line option parsing commences now We only need to support
1308 * a few things in the test suite.
1314 if (argv[0][0] == '-') {
1315 if (parsecmd("defs", &argc, &argv, &defs, 1, false))
1318 if (!strcmp(argv[0]+1, "debug")) {
1319 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
1322 if (!strcmp(argv[0]+1, "memchk")) {
1323 OPTS_OPTION_BOOL(OPTION_MEMCHK) = true;
1326 if (!strcmp(argv[0]+1, "nocolor")) {
1331 con_err("invalid argument %s\n", argv[0]+1);
1335 succeed = test_perform("tests", defs);
1337 return (succeed) ? EXIT_SUCCESS : EXIT_FAILURE;