16 static const char *task_bins[] = {
29 static FILE **task_popen(const char *command, const char *mode) {
35 popen_t *data = (popen_t*)mem_a(sizeof(popen_t));
37 char *line = (char*)command;
38 std::vector<char *> argv;
41 while (*line != '\0') {
42 while (*line == ' ' || *line == '\t' || *line == '\n')
45 while (*line != '\0' && *line != ' ' &&
46 *line != '\t' && *line != '\n') line++;
48 argv.push_back((char *)0);
51 if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0;
52 if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1;
53 if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2;
55 if ((data->pid = fork()) > 0) {
60 data->pipes[0] = inhandle [1];
61 data->pipes[1] = outhandle[0];
62 data->pipes[2] = errhandle[0];
63 data->handles[0] = fdopen(inhandle [1], "w");
64 data->handles[1] = fdopen(outhandle[0], mode);
65 data->handles[2] = fdopen(errhandle[0], mode);
67 } else if (data->pid == 0) {
73 /* see piping documentation for this sillyness :P */
74 dup2(inhandle [0], 0);
75 dup2(outhandle[1], 1);
76 dup2(errhandle[1], 2);
78 execvp(argv[0], &argv[0]);
82 goto task_popen_error_3;
85 task_popen_error_3: close(errhandle[0]), close(errhandle[1]);
86 task_popen_error_2: close(outhandle[0]), close(outhandle[1]);
87 task_popen_error_1: close(inhandle [0]), close(inhandle [1]);
93 static int task_pclose(FILE **handles) {
94 popen_t *data = (popen_t*)handles;
97 close(data->pipes[0]); /* stdin */
98 close(data->pipes[1]); /* stdout */
99 close(data->pipes[2]); /* stderr */
101 waitpid(data->pid, &status, 0);
108 #define TASK_COMPILE 0
109 #define TASK_EXECUTE 1
111 * Task template system:
112 * templates are rules for a specific test, used to create a "task" that
113 * is executed with those set of rules (arguments, and what not). Tests
114 * that don't have a template with them cannot become tasks, since without
115 * the information for that test there is no way to properly "test" them.
116 * Rules for these templates are described in a template file, using a
117 * task template language.
119 * The language is a basic finite statemachine, top-down single-line
120 * description language.
122 * The languge is composed entierly of "tags" which describe a string of
123 * text for a task. Think of it much like a configuration file. Except
124 * it's been designed to allow flexibility and future support for prodecual
127 * The following "tags" are suported by the language
130 * Used to set a description of the current test, this must be
131 * provided, this tag is NOT optional.
134 * Used to set the procedure for the given task, there are two
137 * This simply performs compilation only
139 * This will perform compilation and execution
141 * This will perform compilation, but requires
142 * the compilation to fail in order to succeed.
144 * This must be provided, this tag is NOT optional.
147 * Used to set the compilation flags for the given task, this
148 * must be provided, this tag is NOT optional.
150 * F: Used to set some test suite flags, currently the only option
151 * is -no-defs (to including of defs.qh)
154 * Used to set the execution flags for the given task. This tag
155 * must be provided if T == -execute, otherwise it's erroneous
156 * as compilation only takes place.
159 * Used to describe a string of text that should be matched from
160 * the output of executing the task. If this doesn't match the
161 * task fails. This tag must be provided if T == -execute, otherwise
162 * it's erroneous as compilation only takes place.
165 * Used to specify the INPUT source file to operate on, this must be
166 * provided, this tag is NOT optional
170 * These tags have one-time use, using them more than once will result
171 * in template compilation errors.
173 * Lines beginning with # or // in the template file are comments and
174 * are ignored by the template parser.
176 * Whitespace is optional, with exception to the colon ':' between the
177 * tag and it's assignment value/
179 * The template compiler will detect erronrous tags (optional tags
180 * that need not be set), as well as missing tags, and error accordingly
181 * this will result in the task failing.
183 struct task_template_t {
190 std::vector<char *> comparematch;
196 * This is very much like a compiler code generator :-). This generates
197 * a value from some data observed from the compiler.
199 static bool task_template_generate(task_template_t *tmpl, char tag, const char *file, size_t line, char *value, size_t *pad) {
202 char **destval = NULL;
208 case 'D': destval = &tmpl->description; break;
209 case 'T': destval = &tmpl->proceduretype; break;
210 case 'C': destval = &tmpl->compileflags; break;
211 case 'E': destval = &tmpl->executeflags; break;
212 case 'I': destval = &tmpl->sourcefile; break;
213 case 'F': destval = &tmpl->testflags; break;
215 con_printmsg(LVL_ERROR, __FILE__, __LINE__, 0, "internal error",
216 "invalid tag `%c:` during code generation\n",
223 * Ensure if for the given tag, there already exists a
227 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "compile error",
228 "tag `%c:` already assigned value: %s\n",
235 * Strip any whitespace that might exist in the value for assignments
238 if (value && *value && (*value == ' ' || *value == '\t'))
244 * Value will contain a newline character at the end, we need to strip
245 * this otherwise kaboom, seriously, kaboom :P
247 if (strchr(value, '\n'))
248 *strrchr(value, '\n')='\0';
251 * Now allocate and set the actual value for the specific tag. Which
252 * was properly selected and can be accessed with *destval.
254 *destval = util_strdup(value);
257 if (*destval == tmpl->description) {
259 * Create some padding for the description to align the
260 * printing of the rules file.
262 if ((desclen = strlen(tmpl->description)) > pad[0])
266 if ((filelen = strlen(file)) > pad[2])
272 static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *fp, size_t *pad) {
281 /* top down parsing */
282 while (util_getline(&back, &size, fp) != EOF) {
283 /* skip whitespace */
285 if (*data && (*data == ' ' || *data == '\t'))
290 * Handle comments inside task tmpl files. We're strict
291 * about the language for fun :-)
294 if (data[1] != '/') {
295 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
296 "invalid character `/`, perhaps you meant `//` ?");
305 * Empty newlines are acceptable as well, so we handle that here
306 * despite being just odd since there should't be that many
307 * empty lines to begin with.
315 * Now begin the actual "tag" stuff. This works as you expect
324 if (data[1] != ':') {
325 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
326 "expected `:` after `%c`",
331 if (!task_template_generate(tmpl, *data, file, line, &data[3], pad)) {
332 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl compile error",
333 "failed to generate for given task\n"
340 * Match requires it's own system since we allow multiple M's
341 * for multi-line matching.
345 char *value = &data[3];
346 if (data[1] != ':') {
347 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
348 "expected `:` after `%c`",
355 * Value will contain a newline character at the end, we need to strip
356 * this otherwise kaboom, seriously, kaboom :P
358 if (strrchr(value, '\n'))
359 *strrchr(value, '\n')='\0';
360 else /* cppcheck: possible null pointer dereference */
363 tmpl->comparematch.push_back(util_strdup(value));
369 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
370 "invalid tag `%c`", *data
373 /* no break required */
376 /* update line and free old sata */
391 * Nullifies the template data: used during initialization of a new
394 static void task_template_nullify(task_template_t *tmpl) {
398 tmpl->description = NULL;
399 tmpl->proceduretype = NULL;
400 tmpl->compileflags = NULL;
401 tmpl->executeflags = NULL;
402 tmpl->sourcefile = NULL;
403 tmpl->tempfilename = NULL;
404 tmpl->rulesfile = NULL;
405 tmpl->testflags = NULL;
408 static task_template_t *task_template_compile(const char *file, const char *dir, size_t *pad) {
409 /* a page should be enough */
412 FILE *tempfile = NULL;
413 task_template_t *tmpl = NULL;
415 util_snprintf(fullfile, sizeof(fullfile), "%s/%s", dir, file);
417 tempfile = fopen(fullfile, "r");
418 tmpl = (task_template_t*)mem_a(sizeof(task_template_t));
419 new (tmpl) task_template_t();
420 task_template_nullify(tmpl);
423 * Create some padding for the printing to align the
424 * printing of the rules file to the console.
426 if ((filepadd = strlen(fullfile)) > pad[1])
429 tmpl->rulesfile = util_strdup(fullfile);
432 * Esnure the file even exists for the task, this is pretty useless
436 con_err("template file: %s does not exist or invalid permissions\n",
442 if (!task_template_parse(file, tmpl, tempfile, pad)) {
443 con_err("template parse error: error during parsing\n");
448 * Regardless procedure type, the following tags must exist:
454 if (!tmpl->description) {
455 con_err("template compile error: %s missing `D:` tag\n", file);
458 if (!tmpl->proceduretype) {
459 con_err("template compile error: %s missing `T:` tag\n", file);
462 if (!tmpl->compileflags) {
463 con_err("template compile error: %s missing `C:` tag\n", file);
466 if (!tmpl->sourcefile) {
467 con_err("template compile error: %s missing `I:` tag\n", file);
472 * Now lets compile the template, compilation is really just
473 * the process of validating the input.
475 if (!strcmp(tmpl->proceduretype, "-compile")) {
476 if (tmpl->executeflags)
477 con_err("template compile warning: %s erroneous tag `E:` when only compiling\n", file);
478 if (tmpl->comparematch.size())
479 con_err("template compile warning: %s erroneous tag `M:` when only compiling\n", file);
481 } else if (!strcmp(tmpl->proceduretype, "-execute")) {
482 if (!tmpl->executeflags) {
483 /* default to $null */
484 tmpl->executeflags = util_strdup("$null");
486 if (tmpl->comparematch.empty()) {
487 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
490 } else if (!strcmp(tmpl->proceduretype, "-fail")) {
491 if (tmpl->executeflags)
492 con_err("template compile warning: %s erroneous tag `E:` when only failing\n", file);
493 if (tmpl->comparematch.size())
494 con_err("template compile warning: %s erroneous tag `M:` when only failing\n", file);
495 } else if (!strcmp(tmpl->proceduretype, "-diagnostic")) {
496 if (tmpl->executeflags)
497 con_err("template compile warning: %s erroneous tag `E:` when only diagnostic\n", file);
498 if (tmpl->comparematch.empty()) {
499 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
502 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
503 if (tmpl->executeflags)
504 con_err("template compile warning: %s erroneous tag `E:` when only preprocessing\n", file);
505 if (tmpl->comparematch.empty()) {
506 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
510 con_err("template compile error: %s invalid procedure type: %s\n", file, tmpl->proceduretype);
520 * The file might not exist and we jump here when that doesn't happen
521 * so the check to see if it's not null here is required.
530 static void task_template_destroy(task_template_t *tmpl) {
534 if (tmpl->description) mem_d(tmpl->description);
535 if (tmpl->proceduretype) mem_d(tmpl->proceduretype);
536 if (tmpl->compileflags) mem_d(tmpl->compileflags);
537 if (tmpl->executeflags) mem_d(tmpl->executeflags);
538 if (tmpl->sourcefile) mem_d(tmpl->sourcefile);
539 if (tmpl->rulesfile) mem_d(tmpl->rulesfile);
540 if (tmpl->testflags) mem_d(tmpl->testflags);
543 for (auto &it : tmpl->comparematch)
547 * Nullify all the template members otherwise NULL comparision
548 * checks will fail if tmpl pointer is reused.
550 mem_d(tmpl->tempfilename);
555 * Now comes the task manager, this system allows adding tasks in and out
556 * of a task list. This is the executor of the tasks essentially as well.
559 task_template_t *tmpl;
568 static std::vector<task_t> task_tasks;
571 * Read a directory and searches for all template files in it
572 * which is later used to run all tests.
574 static bool task_propagate(const char *curdir, size_t *pad, const char *defs) {
577 struct dirent *files;
578 struct stat directory;
581 std::vector<char *> directories;
582 char *claim = util_strdup(curdir);
584 directories.push_back(claim);
585 dir = opendir(claim);
588 * Generate a list of subdirectories since we'll be checking them too
591 while ((files = readdir(dir))) {
592 util_asprintf(&claim, "%s/%s", curdir, files->d_name);
593 if (stat(claim, &directory) == -1) {
599 if (S_ISDIR(directory.st_mode) && files->d_name[0] != '.') {
600 directories.push_back(claim);
609 * Now do all the work, by touching all the directories inside
610 * test as well and compile the task templates into data we can
611 * use to run the tests.
613 for (auto &it : directories) {
615 while ((files = readdir(dir))) {
616 util_snprintf(buffer, sizeof(buffer), "%s/%s", it, files->d_name);
617 if (stat(buffer, &directory) == -1) {
618 con_err("internal error: stat failed, aborting\n");
622 if (S_ISDIR(directory.st_mode))
626 * We made it here, which concludes the file/directory is not
627 * actually a directory, so it must be a file :)
629 if (strcmp(files->d_name + strlen(files->d_name) - 5, ".tmpl") == 0) {
630 task_template_t *tmpl = task_template_compile(files->d_name, it, pad);
631 char buf[4096]; /* one page should be enough */
632 const char *qcflags = NULL;
637 con_err("error compiling task template: %s\n", files->d_name);
642 * Generate a temportary file name for the output binary
643 * so we don't trample over an existing one.
645 tmpl->tempfilename = NULL;
646 util_asprintf(&tmpl->tempfilename, "%s/TMPDAT.%s.dat", it, files->d_name);
649 * Additional QCFLAGS enviroment variable may be used
650 * to test compile flags for all tests. This needs to be
651 * BEFORE other flags (so that the .tmpl can override them)
653 qcflags = getenv("QCFLAGS");
656 * Generate the command required to open a pipe to a process
657 * which will be refered to with a handle in the task for
658 * reading the data from the pipe.
660 if (strcmp(tmpl->proceduretype, "-pp")) {
662 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
663 util_snprintf(buf, sizeof(buf), "%s %s/%s %s %s -o %s",
664 task_bins[TASK_COMPILE],
672 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s %s -o %s",
673 task_bins[TASK_COMPILE],
684 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
685 util_snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s",
686 task_bins[TASK_COMPILE],
693 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s -o %s",
694 task_bins[TASK_COMPILE],
705 /* Preprocessing (qcflags mean shit all here we don't allow them) */
706 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
707 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s -o %s",
708 task_bins[TASK_COMPILE],
715 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s %s -o %s",
716 task_bins[TASK_COMPILE],
728 * The task template was compiled, now lets create a task from
729 * the template data which has now been propagated.
732 if (!(task.runhandles = task_popen(buf, "r"))) {
733 con_err("error opening pipe to process for test: %s\n", tmpl->description);
739 * Open up some file desciptors for logging the stdout/stderr
742 util_snprintf(buf, sizeof(buf), "%s.stdout", tmpl->tempfilename);
743 task.stdoutlogfile = util_strdup(buf);
744 if (!(task.stdoutlog = fopen(buf, "w"))) {
745 con_err("error opening %s for stdout\n", buf);
749 util_snprintf(buf, sizeof(buf), "%s.stderr", tmpl->tempfilename);
750 task.stderrlogfile = util_strdup(buf);
751 if (!(task.stderrlog = fopen(buf, "w"))) {
752 con_err("error opening %s for stderr\n", buf);
755 task_tasks.push_back(task);
759 mem_d(it); /* free claimed memory */
765 * Task precleanup removes any existing temporary files or log files
766 * left behind from a previous invoke of the test-suite.
768 static void task_precleanup(const char *curdir) {
770 struct dirent *files;
773 dir = opendir(curdir);
775 while ((files = readdir(dir))) {
776 if (strstr(files->d_name, "TMP") ||
777 strstr(files->d_name, ".stdout") ||
778 strstr(files->d_name, ".stderr") ||
779 strstr(files->d_name, ".dat"))
781 util_snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
783 con_err("error removing temporary file: %s\n", buffer);
790 static void task_destroy(void) {
792 * Free all the data in the task list and finally the list itself
793 * then proceed to cleanup anything else outside the program like
796 for (auto &it : task_tasks) {
798 * Close any open handles to files or processes here. It's mighty
799 * annoying to have to do all this cleanup work.
801 if (it.stdoutlog) fclose(it.stdoutlog);
802 if (it.stderrlog) fclose(it.stderrlog);
805 * Only remove the log files if the test actually compiled otherwise
806 * forget about it (or if it didn't compile, and the procedure type
807 * was set to -fail (meaning it shouldn't compile) .. stil remove)
809 if (it.compiled || !strcmp(it.tmpl->proceduretype, "-fail")) {
810 if (remove(it.stdoutlogfile))
811 con_err("error removing stdout log file: %s\n", it.stdoutlogfile);
812 if (remove(it.stderrlogfile))
813 con_err("error removing stderr log file: %s\n", it.stderrlogfile);
815 (void)!remove(it.tmpl->tempfilename);
818 /* free util_strdup data for log files */
819 mem_d(it.stdoutlogfile);
820 mem_d(it.stderrlogfile);
822 task_template_destroy(it.tmpl);
827 * This executes the QCVM task for a specificly compiled progs.dat
828 * using the template passed into it for call-flags and user defined
829 * messages IF the procedure type is -execute, otherwise it matches
830 * the preprocessor output.
832 static bool task_trymatch(task_t &task, std::vector<char *> &line) {
835 int retval = EXIT_SUCCESS;
838 task_template_t *tmpl = task.tmpl;
840 memset(buffer,0,sizeof(buffer));
842 if (!strcmp(tmpl->proceduretype, "-execute")) {
844 * Drop the execution flags for the QCVM if none where
845 * actually specified.
847 if (!strcmp(tmpl->executeflags, "$null")) {
848 util_snprintf(buffer, sizeof(buffer), "%s %s",
849 task_bins[TASK_EXECUTE],
853 util_snprintf(buffer, sizeof(buffer), "%s %s %s",
854 task_bins[TASK_EXECUTE],
860 execute = popen(buffer, "r");
863 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
865 * we're preprocessing, which means we need to read int
866 * the produced file and do some really weird shit.
868 if (!(execute = fopen(tmpl->tempfilename, "r")))
873 * we're testing diagnostic output, which means it will be
874 * in runhandles[2] (stderr) since that is where the compiler
877 if (!(execute = fopen(task.stderrlogfile, "r")))
883 * Now lets read the lines and compare them to the matches we expect
884 * and handle accordingly.
891 while (util_getline(&data, &size, execute) != EOF) {
892 if (!strcmp(data, "No main function found\n")) {
893 con_err("test failure: `%s` (No main function found) [%s]\n",
900 pclose((FILE*)execute);
905 * Trim newlines from data since they will just break our
906 * ability to properly validate matches.
908 if (strrchr(data, '\n'))
909 *strrchr(data, '\n') = '\0';
912 * We remove the file/directory and stuff from the error
913 * match messages when testing diagnostics.
915 if(!strcmp(tmpl->proceduretype, "-diagnostic")) {
916 if (strstr(data, "there have been errors, bailing out"))
917 continue; /* ignore it */
918 if (strstr(data, ": error: ")) {
919 char *claim = util_strdup(data + (strstr(data, ": error: ") - data) + 9);
926 * We need to ignore null lines for when -pp is used (preprocessor), since
927 * the preprocessor is likely to create empty newlines in certain macro
928 * instantations, otherwise it's in the wrong nature to ignore empty newlines.
930 if (!strcmp(tmpl->proceduretype, "-pp") && !*data)
933 if (tmpl->comparematch.size() > compare) {
934 if (strcmp(data, tmpl->comparematch[compare++])) {
941 line.push_back(data);
948 if (compare != tmpl->comparematch.size())
956 retval = pclose((FILE*)execute);
960 return success && retval == EXIT_SUCCESS;
963 static const char *task_type(task_template_t *tmpl) {
964 if (!strcmp(tmpl->proceduretype, "-pp"))
965 return "type: preprocessor";
966 if (!strcmp(tmpl->proceduretype, "-execute"))
967 return "type: execution";
968 if (!strcmp(tmpl->proceduretype, "-compile"))
969 return "type: compile";
970 if (!strcmp(tmpl->proceduretype, "-diagnostic"))
971 return "type: diagnostic";
976 * This schedualizes all tasks and actually runs them individually
977 * this is generally easy for just -compile variants. For compile and
978 * execution this takes more work since a task needs to be generated
979 * from thin air and executed INLINE.
982 static size_t task_schedualize(size_t *pad) {
984 bool execute = false;
986 std::vector<char *> match;
992 util_snprintf(space[0], sizeof(space[0]), "%d", (int)task_tasks.size());
994 for (auto &it : task_tasks) {
996 memset(space[1], 0, sizeof(space[1]));
997 util_snprintf(space[1], sizeof(space[1]), "%d", (int)(i));
999 con_out("test #%u %*s", i, strlen(space[0]) - strlen(space[1]), "");
1002 * Generate a task from thin air if it requires execution in
1006 /* diagnostic is not executed, but compare tested instead, like preproessor */
1007 execute = !! (!strcmp(it.tmpl->proceduretype, "-execute")) ||
1008 (!strcmp(it.tmpl->proceduretype, "-pp")) ||
1009 (!strcmp(it.tmpl->proceduretype, "-diagnostic"));
1012 * We assume it compiled before we actually compiled :). On error
1013 * we change the value
1018 * Read data from stdout first and pipe that stuff into a log file
1019 * then we do the same for stderr.
1021 while (util_getline(&data, &size, it.runhandles[1]) != EOF) {
1022 fputs(data, it.stdoutlog);
1024 if (strstr(data, "failed to open file")) {
1025 it.compiled = false;
1029 while (util_getline(&data, &size, it.runhandles[2]) != EOF) {
1031 * If a string contains an error we just dissalow execution
1034 * TODO: make this more percise, e.g if we print a warning
1035 * that refers to a variable named error, or something like
1036 * that .. then this will blowup :P
1038 if (strstr(data, "error") && strcmp(it.tmpl->proceduretype, "-diagnostic")) {
1040 it.compiled = false;
1043 fputs(data, it.stderrlog);
1044 fflush(it.stderrlog); /* fast flush for read */
1047 if (!it.compiled && strcmp(it.tmpl->proceduretype, "-fail")) {
1048 con_out("failure: `%s` %*s %*s\n",
1049 it.tmpl->description,
1050 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1052 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen("(failed to compile)") - pad[2]),
1053 "(failed to compile)"
1059 status = task_pclose(it.runhandles);
1060 if ((!strcmp(it.tmpl->proceduretype, "-fail") && status == EXIT_SUCCESS)
1061 || ( strcmp(it.tmpl->proceduretype, "-fail") && status == EXIT_FAILURE)) {
1062 con_out("failure: `%s` %*s %*s\n",
1063 it.tmpl->description,
1064 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1066 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen("(compiler didn't return exit success)") - pad[2]),
1067 "(compiler didn't return exit success)"
1074 con_out("succeeded: `%s` %*s %*s\n",
1075 it.tmpl->description,
1076 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1078 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(task_type(it.tmpl)) - pad[2]),
1086 * If we made it here that concludes the task is to be executed
1087 * in the virtual machine (or the preprocessor output needs to
1090 if (!task_trymatch(it, match)) {
1093 con_out("failure: `%s` %*s %*s\n",
1094 it.tmpl->description,
1095 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1097 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(
1098 (strcmp(it.tmpl->proceduretype, "-pp"))
1099 ? "(invalid results from execution)"
1100 : (strcmp(it.tmpl->proceduretype, "-diagnostic"))
1101 ? "(invalid results from preprocessing)"
1102 : "(invalid results from compiler diagnsotics)"
1104 (strcmp(it.tmpl->proceduretype, "-pp"))
1105 ? "(invalid results from execution)"
1106 : (strcmp(it.tmpl->proceduretype, "-diagnostic"))
1107 ? "(invalid results from preprocessing)"
1108 : "(invalid results from compiler diagnsotics)"
1112 * Print nicely formatted expected match lists to console error
1113 * handler for the all the given matches in the template file and
1114 * what was actually returned from executing.
1116 con_out(" Expected From %u Matches: (got %u Matches)\n",
1117 it.tmpl->comparematch.size(),
1120 for (; d < it.tmpl->comparematch.size(); d++) {
1121 char *select = it.tmpl->comparematch[d];
1122 size_t length = 60 - strlen(select);
1123 con_out(" Expected: \"%s\"", select);
1126 con_out("| Got: \"%s\"\n", (d >= match.size()) ? "<<nothing else to compare>>" : match[d]);
1130 * Print the non-expected out (since we are simply not expecting it)
1131 * This will help track down bugs in template files that fail to match
1134 if (match.size() > it.tmpl->comparematch.size()) {
1135 for (d = 0; d < match.size() - it.tmpl->comparematch.size(); d++) {
1136 con_out(" Expected: Nothing | Got: \"%s\"\n",
1137 match[d + it.tmpl->comparematch.size()]
1142 for (auto &it : match)
1149 for (auto &it : match)
1153 con_out("succeeded: `%s` %*s %*s\n",
1154 it.tmpl->description,
1155 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1157 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(task_type(it.tmpl))- pad[2]),
1167 * This is the heart of the whole test-suite process. This cleans up
1168 * any existing temporary files left behind as well as log files left
1169 * behind. Then it propagates a list of tests from `curdir` by scaning
1170 * it for template files and compiling them into tasks, in which it
1171 * schedualizes them (executes them) and actually reports errors and
1172 * what not. It then proceeds to destroy the tasks and return memory
1173 * it's the engine :)
1175 * It returns true of tests could be propagated, otherwise it returns
1178 * It expects con_init() was called before hand.
1180 static GMQCC_WARN bool test_perform(const char *curdir, const char *defs) {
1181 size_t failed = false;
1182 static const char *default_defs = "defs.qh";
1185 /* test ### [succeed/fail]: `description` [tests/template.tmpl] [type] */
1190 * If the default definition file isn't set to anything. We will
1191 * use the default_defs here, which is "defs.qc"
1194 defs = default_defs;
1198 task_precleanup(curdir);
1199 if (!task_propagate(curdir, pad, defs)) {
1200 con_err("error: failed to propagate tasks\n");
1205 * If we made it here all tasks where propagated from their resultant
1206 * template file. So we can start the FILO scheduler, this has been
1207 * designed in the most thread-safe way possible for future threading
1208 * it's designed to prevent lock contention, and possible syncronization
1211 failed = task_schedualize(pad);
1213 con_out("%u out of %u tests failed\n", failed, task_tasks.size());
1216 return (failed) ? false : true;
1220 * Fancy GCC-like LONG parsing allows things like --opt=param with
1221 * assignment operator. This is used for redirecting stdout/stderr
1222 * console to specific files of your choice.
1224 static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
1226 char **argv = *argv_;
1228 size_t len = strlen(optname);
1230 if (strncmp(argv[0]+ds, optname, len))
1233 /* it's --optname, check how the parameter is supplied */
1234 if (argv[0][ds+len] == '=') {
1235 *out = argv[0]+ds+len+1;
1239 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
1242 /* using --opt param */
1249 int main(int argc, char **argv) {
1250 bool succeed = false;
1256 * Command line option parsing commences now We only need to support
1257 * a few things in the test suite.
1263 if (argv[0][0] == '-') {
1264 if (parsecmd("defs", &argc, &argv, &defs, 1, false))
1267 if (!strcmp(argv[0]+1, "debug")) {
1268 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
1271 if (!strcmp(argv[0]+1, "nocolor")) {
1276 con_err("invalid argument %s\n", argv[0]+1);
1280 succeed = test_perform("tests", defs);
1282 return (succeed) ? EXIT_SUCCESS : EXIT_FAILURE;