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
24 #include <sys/types.h>
35 * TODO: Windows version
36 * this implements a unique bi-directional popen-like function that
37 * allows reading data from both stdout and stderr. And writing to
41 * FILE *handles[3] = task_popen("ls", "-l", "r");
42 * if (!handles) { perror("failed to open stdin/stdout/stderr to ls");
43 * // handles[0] = stdin
44 * // handles[1] = stdout
45 * // handles[2] = stderr
47 * task_pclose(handles); // to close
50 #include <sys/types.h>
63 FILE ** task_popen(const char *command, const char *mode) {
69 popen_t *data = mem_a(sizeof(popen_t));
72 * Parse the command now into a list for execv, this is a pain
75 char *line = (char*)command;
79 while (*line != '\0') {
80 while (*line == ' ' || *line == '\t' || *line == '\n')
84 while (*line != '\0' && *line != ' ' &&
85 *line != '\t' && *line != '\n') line++;
91 if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0;
92 if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1;
93 if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2;
95 if ((data->pid = fork()) > 0) {
101 data->pipes [0] = inhandle [1];
102 data->pipes [1] = outhandle[0];
103 data->pipes [2] = errhandle[0];
104 data->handles[0] = fdopen(inhandle [1], "w");
105 data->handles[1] = fdopen(outhandle[0], mode);
106 data->handles[2] = fdopen(errhandle[0], mode);
111 return data->handles;
112 } else if (data->pid == 0) {
118 /* see piping documentation for this sillyness :P */
119 close(0), dup(inhandle [0]);
120 close(1), dup(outhandle[1]);
121 close(2), dup(errhandle[1]);
127 goto task_popen_error_3;
131 * clang is stupid, it doesn't understand that yes, this code
132 * is actually reachable.
135 # pragma clang diagnostic push
136 # pragma clang diagnostic ignored "-Wunreachable-code"
142 # pragma clang diagnostic pop
145 return data->handles;
147 task_popen_error_3: close(errhandle[0]), close(errhandle[1]);
148 task_popen_error_2: close(outhandle[0]), close(outhandle[1]);
149 task_popen_error_1: close(inhandle [0]), close(inhandle [1]);
157 int task_pclose(FILE **handles) {
158 popen_t *data = (popen_t*)handles;
161 close(data->pipes[0]); /* stdin */
162 close(data->pipes[1]); /* stdout */
163 close(data->pipes[2]); /* stderr */
165 waitpid(data->pid, &status, 0);
172 # define _WIN32_LEAN_AND_MEAN
173 # define popen _popen
174 # define pclose _pclose
175 # include <windows.h>
179 * Bidirectional piping implementation for windows using CreatePipe and DuplicateHandle +
185 /* TODO: implement */
188 FILE **task_popen(const char *command, const char *mode) {
192 /* TODO: implement */
196 void task_pclose(FILE **files) {
197 /* TODO: implement */
203 /* mingw32 has dirent.h */
205 # elif defined (_MSC_VER)
207 * visual studio lacks dirent.h it's a posix thing
208 * so we emulate it with the WinAPI.
213 unsigned short d_reclen;
214 unsigned short d_namlen;
215 char d_name[FILENAME_MAX];
219 struct _finddata_t dd_dta;
220 struct dirent dd_dir;
226 DIR *opendir(const char *name) {
227 DIR *dir = (DIR*)mem_a(sizeof(DIR) + strlen(name));
231 strcpy(dir->dd_name, name);
235 int closedir(DIR *dir) {
236 FindClose((HANDLE)dir->dd_handle);
241 struct dirent *readdir(DIR *dir) {
242 WIN32_FIND_DATA info;
246 if (!dir->dd_handle) {
249 size_t n = strlen(dir->dd_name);
250 if ((dirname = (char*)mem_a(n + 5) /* 4 + 1 */)) {
251 strcpy(dirname, dir->dd_name);
252 strcpy(dirname + n, "\\*.*"); /* 4 + 1 */
255 if (!(dirname = util_strdup("\\*.*")))
259 dir->dd_handle = (long)FindFirstFile(dirname, &info);
261 rets = !(!dir->dd_handle);
262 } else if (dir->dd_handle != -11) {
263 rets = FindNextFile ((HANDLE)dir->dd_handle, &info);
271 if ((data = (struct dirent*)mem_a(sizeof(struct dirent)))) {
272 strncpy(data->d_name, info.cFileName, FILENAME_MAX - 1);
273 data->d_name[FILENAME_MAX - 1] = '\0'; /* terminate */
274 data->d_namlen = strlen(data->d_name);
280 * Visual studio also lacks S_ISDIR for sys/stat.h, so we emulate this as well
281 * which is not hard at all.
283 # undef S_ISDIR /* undef just incase */
284 # define S_ISDIR(X) ((X)&_S_IFDIR)
288 #define TASK_COMPILE 0
289 #define TASK_EXECUTE 1
292 * Task template system:
293 * templates are rules for a specific test, used to create a "task" that
294 * is executed with those set of rules (arguments, and what not). Tests
295 * that don't have a template with them cannot become tasks, since without
296 * the information for that test there is no way to properly "test" them.
297 * Rules for these templates are described in a template file, using a
298 * task template language.
300 * The language is a basic finite statemachine, top-down single-line
301 * description language.
303 * The languge is composed entierly of "tags" which describe a string of
304 * text for a task. Think of it much like a configuration file. Except
305 * it's been designed to allow flexibility and future support for prodecual
308 * The following "tags" are suported by the language
311 * Used to set a description of the current test, this must be
312 * provided, this tag is NOT optional.
315 * Used to set a failure message, this message will be displayed
316 * if the test fails, this tag is optional
319 * Used to set a success message, this message will be displayed
320 * if the test succeeds, this tag is optional.
323 * Used to set the procedure for the given task, there are two
326 * This simply performs compilation only
328 * This will perform compilation and execution
330 * This will perform compilation, but requires
331 * the compilation to fail in order to succeed.
333 * This must be provided, this tag is NOT optional.
336 * Used to set the compilation flags for the given task, this
337 * must be provided, this tag is NOT optional.
340 * Used to set the execution flags for the given task. This tag
341 * must be provided if T == -execute, otherwise it's erroneous
342 * as compilation only takes place.
345 * Used to describe a string of text that should be matched from
346 * the output of executing the task. If this doesn't match the
347 * task fails. This tag must be provided if T == -execute, otherwise
348 * it's erroneous as compilation only takes place.
351 * Used to specify the INPUT source file to operate on, this must be
352 * provided, this tag is NOT optional
356 * These tags have one-time use, using them more than once will result
357 * in template compilation errors.
359 * Lines beginning with # or // in the template file are comments and
360 * are ignored by the template parser.
362 * Whitespace is optional, with exception to the colon ':' between the
363 * tag and it's assignment value/
365 * The template compiler will detect erronrous tags (optional tags
366 * that need not be set), as well as missing tags, and error accordingly
367 * this will result in the task failing.
371 char *failuremessage;
372 char *successmessage;
382 * This is very much like a compiler code generator :-). This generates
383 * a value from some data observed from the compiler.
385 bool task_template_generate(task_template_t *template, char tag, const char *file, size_t line, const char *value) {
386 char **destval = NULL;
392 case 'D': destval = &template->description; break;
393 case 'F': destval = &template->failuremessage; break;
394 case 'S': destval = &template->successmessage; break;
395 case 'T': destval = &template->proceduretype; break;
396 case 'C': destval = &template->compileflags; break;
397 case 'E': destval = &template->executeflags; break;
398 case 'I': destval = &template->sourcefile; break;
400 con_printmsg(LVL_ERROR, __FILE__, __LINE__, "internal error",
401 "invalid tag `%c:` during code generation\n",
408 * Ensure if for the given tag, there already exists a
412 con_printmsg(LVL_ERROR, file, line, "compile error",
413 "tag `%c:` already assigned value: %s\n",
420 * Strip any whitespace that might exist in the value for assignments
423 if (value && *value && (*value == ' ' || *value == '\t'))
427 * Value will contain a newline character at the end, we need to strip
428 * this otherwise kaboom, seriously, kaboom :P
430 if (strchr(value, '\n'))
431 *strrchr(value, '\n')='\0';
432 else /* cppcheck: possible nullpointer dereference */
436 * Now allocate and set the actual value for the specific tag. Which
437 * was properly selected and can be accessed with *destval.
439 *destval = util_strdup(value);
444 bool task_template_parse(const char *file, task_template_t *template, FILE *fp) {
453 /* top down parsing */
454 while (util_getline(&back, &size, fp) != EOF) {
455 /* skip whitespace */
457 if (*data && (*data == ' ' || *data == '\t'))
462 * Handle comments inside task template files. We're strict
463 * about the language for fun :-)
466 if (data[1] != '/') {
467 con_printmsg(LVL_ERROR, file, line, "template parse error",
468 "invalid character `/`, perhaps you meant `//` ?");
477 * Empty newlines are acceptable as well, so we handle that here
478 * despite being just odd since there should't be that many
479 * empty lines to begin with.
487 * Now begin the actual "tag" stuff. This works as you expect
497 if (data[1] != ':') {
498 con_printmsg(LVL_ERROR, file, line, "template parse error",
499 "expected `:` after `%c`",
504 if (!task_template_generate(template, *data, file, line, &data[3])) {
505 con_printmsg(LVL_ERROR, file, line, "template compile error",
506 "failed to generate for given task\n"
513 * Match requires it's own system since we allow multiple M's
514 * for multi-line matching.
518 char *value = &data[3];
519 if (data[1] != ':') {
520 con_printmsg(LVL_ERROR, file, line, "template parse error",
521 "expected `:` after `%c`",
527 if (value && *value && (*value == ' ' || *value == '\t'))
531 * Value will contain a newline character at the end, we need to strip
532 * this otherwise kaboom, seriously, kaboom :P
534 if (strrchr(value, '\n'))
535 *strrchr(value, '\n')='\0';
536 else /* cppcheck: possible null pointer dereference */
539 vec_push(template->comparematch, util_strdup(value));
545 con_printmsg(LVL_ERROR, file, line, "template parse error",
546 "invalid tag `%c`", *data
549 /* no break required */
552 /* update line and free old sata */
568 * Nullifies the template data: used during initialization of a new
571 void task_template_nullify(task_template_t *template) {
575 template->description = NULL;
576 template->failuremessage = NULL;
577 template->successmessage = NULL;
578 template->proceduretype = NULL;
579 template->compileflags = NULL;
580 template->executeflags = NULL;
581 template->comparematch = NULL;
582 template->sourcefile = NULL;
583 template->tempfilename = NULL;
586 task_template_t *task_template_compile(const char *file, const char *dir) {
587 /* a page should be enough */
589 FILE *tempfile = NULL;
590 task_template_t *template = NULL;
592 memset (fullfile, 0, sizeof(fullfile));
593 snprintf(fullfile, sizeof(fullfile), "%s/%s", dir, file);
595 tempfile = fopen(fullfile, "r");
596 template = mem_a(sizeof(task_template_t));
597 task_template_nullify(template);
600 * Esnure the file even exists for the task, this is pretty useless
604 con_err("template file: %s does not exist or invalid permissions\n",
610 if (!task_template_parse(file, template, tempfile)) {
611 con_err("template parse error: error during parsing\n");
616 * Regardless procedure type, the following tags must exist:
622 if (!template->description) {
623 con_err("template compile error: %s missing `D:` tag\n", file);
626 if (!template->proceduretype) {
627 con_err("template compile error: %s missing `T:` tag\n", file);
630 if (!template->compileflags) {
631 con_err("template compile error: %s missing `C:` tag\n", file);
634 if (!template->sourcefile) {
635 con_err("template compile error: %s missing `I:` tag\n", file);
640 * Now lets compile the template, compilation is really just
641 * the process of validating the input.
643 if (!strcmp(template->proceduretype, "-compile")) {
644 if (template->executeflags)
645 con_err("template compile warning: %s erroneous tag `E:` when only compiling\n", file);
646 if (template->comparematch)
647 con_err("template compile warning: %s erroneous tag `M:` when only compiling\n", file);
649 } else if (!strcmp(template->proceduretype, "-execute")) {
650 if (!template->executeflags) {
651 /* default to $null */
652 template->executeflags = util_strdup("$null");
654 if (!template->comparematch) {
655 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
658 } else if (!strcmp(template->proceduretype, "-fail")) {
659 if (template->executeflags)
660 con_err("template compile warning: %s erroneous tag `E:` when only failing\n", file);
661 if (template->comparematch)
662 con_err("template compile warning: %s erroneous tag `M:` when only failing\n", file);
665 con_err("template compile error: %s invalid procedure type: %s\n", file, template->proceduretype);
675 * The file might not exist and we jump here when that doesn't happen
676 * so the check to see if it's not null here is required.
685 void task_template_destroy(task_template_t **template) {
689 if ((*template)->description) mem_d((*template)->description);
690 if ((*template)->failuremessage) mem_d((*template)->failuremessage);
691 if ((*template)->successmessage) mem_d((*template)->successmessage);
692 if ((*template)->proceduretype) mem_d((*template)->proceduretype);
693 if ((*template)->compileflags) mem_d((*template)->compileflags);
694 if ((*template)->executeflags) mem_d((*template)->executeflags);
695 if ((*template)->sourcefile) mem_d((*template)->sourcefile);
698 * Delete all allocated string for task template then destroy the
703 for (; i < vec_size((*template)->comparematch); i++)
704 mem_d((*template)->comparematch[i]);
706 vec_free((*template)->comparematch);
710 * Nullify all the template members otherwise NULL comparision
711 * checks will fail if template pointer is reused.
717 * Now comes the task manager, this system allows adding tasks in and out
718 * of a task list. This is the executor of the tasks essentially as well.
721 task_template_t *template;
730 task_t *task_tasks = NULL;
733 * Read a directory and searches for all template files in it
734 * which is later used to run all tests.
736 bool task_propagate(const char *curdir) {
739 struct dirent *files;
740 struct stat directory;
744 dir = opendir(curdir);
746 while ((files = readdir(dir))) {
747 memset (buffer, 0,sizeof(buffer));
748 snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
750 if (stat(buffer, &directory) == -1) {
751 con_err("internal error: stat failed, aborting\n");
755 /* skip directories */
756 if (S_ISDIR(directory.st_mode))
760 * We made it here, which concludes the file/directory is not
761 * actually a directory, so it must be a file :)
763 if (strcmp(files->d_name + strlen(files->d_name) - 5, ".tmpl") == 0) {
764 task_template_t *template = task_template_compile(files->d_name, curdir);
765 char buf[4096]; /* one page should be enough */
768 util_debug("TEST", "compiling task template: %s/%s\n", curdir, files->d_name);
771 con_err("error compiling task template: %s\n", files->d_name);
776 * Generate a temportary file name for the output binary
777 * so we don't trample over an existing one.
779 template->tempfilename = tempnam(curdir, "TMPDAT");
782 * Generate the command required to open a pipe to a process
783 * which will be refered to with a handle in the task for
784 * reading the data from the pipe.
786 memset (buf,0,sizeof(buf));
787 snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s",
788 task_bins[TASK_COMPILE],
790 template->sourcefile,
791 template->compileflags,
792 template->tempfilename
796 * The task template was compiled, now lets create a task from
797 * the template data which has now been propagated.
799 task.template = template;
800 if (!(task.runhandles = task_popen(buf, "r"))) {
801 con_err("error opening pipe to process for test: %s\n", template->description);
806 util_debug("TEST", "executing test: `%s` [%s]\n", template->description, buf);
809 * Open up some file desciptors for logging the stdout/stderr
812 memset (buf,0,sizeof(buf));
813 snprintf(buf, sizeof(buf), "%s.stdout", template->tempfilename);
814 task.stdoutlogfile = util_strdup(buf);
815 if (!(task.stdoutlog = fopen(buf, "w"))) {
816 con_err("error opening %s for stdout\n", buf);
820 memset (buf,0,sizeof(buf));
821 snprintf(buf, sizeof(buf), "%s.stderr", template->tempfilename);
822 task.stderrlogfile = util_strdup(buf);
823 if (!(task.stderrlog = fopen(buf, "w"))) {
824 con_err("error opening %s for stderr\n", buf);
828 vec_push(task_tasks, task);
832 util_debug("TEST", "compiled %d task template files out of %d\n",
833 vec_size(task_tasks),
842 * Removes all temporary 'progs.dat' files created during compilation
845 void task_cleanup(const char *curdir) {
847 struct dirent *files;
850 dir = opendir(curdir);
852 while ((files = readdir(dir))) {
853 memset(buffer, 0, sizeof(buffer));
854 if (strstr(files->d_name, "TMP")) {
855 snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
857 con_err("error removing temporary file: %s\n", buffer);
859 util_debug("TEST", "removed temporary file: %s\n", buffer);
867 * Task precleanup removes any existing temporary files or log files
868 * left behind from a previous invoke of the test-suite.
870 void task_precleanup(const char *curdir) {
872 struct dirent *files;
875 dir = opendir(curdir);
877 while ((files = readdir(dir))) {
878 memset(buffer, 0, sizeof(buffer));
879 if (strstr(files->d_name, "TMP") ||
880 strstr(files->d_name, ".stdout") ||
881 strstr(files->d_name, ".stderr"))
883 snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
885 con_err("error removing temporary file: %s\n", buffer);
887 util_debug("TEST", "removed temporary file: %s\n", buffer);
894 void task_destroy(const char *curdir) {
896 * Free all the data in the task list and finally the list itself
897 * then proceed to cleanup anything else outside the program like
901 for (i = 0; i < vec_size(task_tasks); i++) {
903 * Close any open handles to files or processes here. It's mighty
904 * annoying to have to do all this cleanup work.
906 if (task_tasks[i].runhandles) task_pclose(task_tasks[i].runhandles);
907 if (task_tasks[i].stdoutlog) fclose (task_tasks[i].stdoutlog);
908 if (task_tasks[i].stderrlog) fclose (task_tasks[i].stderrlog);
911 * Only remove the log files if the test actually compiled otherwise
912 * forget about it (or if it didn't compile, and the procedure type
913 * was set to -fail (meaning it shouldn't compile) .. stil remove)
915 if (task_tasks[i].compiled || !strcmp(task_tasks[i].template->proceduretype, "-fail")) {
916 if (remove(task_tasks[i].stdoutlogfile))
917 con_err("error removing stdout log file: %s\n", task_tasks[i].stdoutlogfile);
919 util_debug("TEST", "removed stdout log file: %s\n", task_tasks[i].stdoutlogfile);
921 if (remove(task_tasks[i].stderrlogfile))
922 con_err("error removing stderr log file: %s\n", task_tasks[i].stderrlogfile);
924 util_debug("TEST", "removed stderr log file: %s\n", task_tasks[i].stderrlogfile);
927 /* free util_strdup data for log files */
928 mem_d(task_tasks[i].stdoutlogfile);
929 mem_d(task_tasks[i].stderrlogfile);
931 task_template_destroy(&task_tasks[i].template);
933 vec_free(task_tasks);
936 * Cleanup outside stuff like temporary files.
938 task_cleanup(curdir);
942 * This executes the QCVM task for a specificly compiled progs.dat
943 * using the template passed into it for call-flags and user defined
946 bool task_execute(task_template_t *template, char ***line) {
950 memset (buffer,0,sizeof(buffer));
953 * Drop the execution flags for the QCVM if none where
954 * actually specified.
956 if (!strcmp(template->executeflags, "$null")) {
957 snprintf(buffer, sizeof(buffer), "%s %s",
958 task_bins[TASK_EXECUTE],
959 template->tempfilename
962 snprintf(buffer, sizeof(buffer), "%s %s %s",
963 task_bins[TASK_EXECUTE],
964 template->executeflags,
965 template->tempfilename
969 util_debug("TEST", "executing qcvm: `%s` [%s]\n",
970 template->description,
974 execute = popen(buffer, "r");
979 * Now lets read the lines and compare them to the matches we expect
980 * and handle accordingly.
986 while (util_getline(&data, &size, execute) != EOF) {
987 if (!strcmp(data, "No main function found\n")) {
988 con_err("test failure: `%s` [%s] (No main function found)\n",
989 template->description,
990 (template->failuremessage) ?
991 template->failuremessage : "unknown"
998 * Trim newlines from data since they will just break our
999 * ability to properly validate matches.
1001 if (strrchr(data, '\n'))
1002 *strrchr(data, '\n') = '\0';
1004 if (vec_size(template->comparematch) > compare) {
1005 if (strcmp(data, template->comparematch[compare++]))
1012 * Copy to output vector for diagnostics if execution match
1015 vec_push(*line, data);
1029 * This schedualizes all tasks and actually runs them individually
1030 * this is generally easy for just -compile variants. For compile and
1031 * execution this takes more work since a task needs to be generated
1032 * from thin air and executed INLINE.
1034 void task_schedualize() {
1035 bool execute = false;
1037 char **match = NULL;
1042 util_debug("TEST", "found %d tasks, preparing to execute\n", vec_size(task_tasks));
1044 for (i = 0; i < vec_size(task_tasks); i++) {
1045 util_debug("TEST", "executing task: %d: %s\n", i, task_tasks[i].template->description);
1047 * Generate a task from thin air if it requires execution in
1050 execute = !!(!strcmp(task_tasks[i].template->proceduretype, "-execute"));
1053 * We assume it compiled before we actually compiled :). On error
1054 * we change the value
1056 task_tasks[i].compiled = true;
1059 * Read data from stdout first and pipe that stuff into a log file
1060 * then we do the same for stderr.
1062 while (util_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) {
1063 fputs(data, task_tasks[i].stdoutlog);
1065 if (strstr(data, "failed to open file")) {
1066 task_tasks[i].compiled = false;
1070 fflush(task_tasks[i].stdoutlog);
1072 while (util_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) {
1074 * If a string contains an error we just dissalow execution
1077 * TODO: make this more percise, e.g if we print a warning
1078 * that refers to a variable named error, or something like
1079 * that .. then this will blowup :P
1081 if (strstr(data, "error")) {
1083 task_tasks[i].compiled = false;
1086 fputs(data, task_tasks[i].stderrlog);
1087 fflush(task_tasks[i].stdoutlog);
1090 if (!task_tasks[i].compiled && strcmp(task_tasks[i].template->proceduretype, "-fail")) {
1091 con_err("test failure: `%s` [%s] (failed to compile) see %s.stdout and %s.stderr\n",
1092 task_tasks[i].template->description,
1093 (task_tasks[i].template->failuremessage) ?
1094 task_tasks[i].template->failuremessage : "unknown",
1095 task_tasks[i].template->tempfilename,
1096 task_tasks[i].template->tempfilename
1102 con_out("test succeeded: `%s` [%s]\n",
1103 task_tasks[i].template->description,
1104 (task_tasks[i].template->successmessage) ?
1105 task_tasks[i].template->successmessage : "unknown"
1111 * If we made it here that concludes the task is to be executed
1112 * in the virtual machine.
1114 if (!task_execute(task_tasks[i].template, &match)) {
1117 con_err("test failure: `%s` [%s] (invalid results from execution)\n",
1118 task_tasks[i].template->description,
1119 (task_tasks[i].template->failuremessage) ?
1120 task_tasks[i].template->failuremessage : "unknown"
1124 * Print nicely formatted expected match lists to console error
1125 * handler for the all the given matches in the template file and
1126 * what was actually returned from executing.
1128 con_err(" Expected From %u Matches: (got %u Matches)\n",
1129 vec_size(task_tasks[i].template->comparematch),
1132 for (; d < vec_size(task_tasks[i].template->comparematch); d++) {
1133 char *select = task_tasks[i].template->comparematch[d];
1134 size_t length = 40 - strlen(select);
1136 con_err(" Expected: \"%s\"", select);
1139 con_err("| Got: \"%s\"\n", (d >= vec_size(match)) ? "<<nothing else to compare>>" : match[d]);
1143 * Print the non-expected out (since we are simply not expecting it)
1144 * This will help track down bugs in template files that fail to match
1147 if (vec_size(match) > vec_size(task_tasks[i].template->comparematch)) {
1148 for (d = 0; d < vec_size(match) - vec_size(task_tasks[i].template->comparematch); d++) {
1149 con_err(" Expected: Nothing | Got: \"%s\"\n",
1150 match[d + vec_size(task_tasks[i].template->comparematch)]
1156 for (j = 0; j < vec_size(match); j++)
1161 for (j = 0; j < vec_size(match); j++)
1165 con_out("test succeeded: `%s` [%s]\n",
1166 task_tasks[i].template->description,
1167 (task_tasks[i].template->successmessage) ?
1168 task_tasks[i].template->successmessage : "unknown"
1175 * This is the heart of the whole test-suite process. This cleans up
1176 * any existing temporary files left behind as well as log files left
1177 * behind. Then it propagates a list of tests from `curdir` by scaning
1178 * it for template files and compiling them into tasks, in which it
1179 * schedualizes them (executes them) and actually reports errors and
1180 * what not. It then proceeds to destroy the tasks and return memory
1181 * it's the engine :)
1183 * It returns true of tests could be propagated, otherwise it returns
1186 * It expects con_init() was called before hand.
1188 bool test_perform(const char *curdir) {
1189 task_precleanup(curdir);
1190 if (!task_propagate(curdir)) {
1191 con_err("error: failed to propagate tasks\n");
1192 task_destroy(curdir);
1196 * If we made it here all tasks where propagated from their resultant
1197 * template file. So we can start the FILO scheduler, this has been
1198 * designed in the most thread-safe way possible for future threading
1199 * it's designed to prevent lock contention, and possible syncronization
1203 task_destroy(curdir);
1209 * Fancy GCC-like LONG parsing allows things like --opt=param with
1210 * assignment operator. This is used for redirecting stdout/stderr
1211 * console to specific files of your choice.
1213 static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
1215 char **argv = *argv_;
1217 size_t len = strlen(optname);
1219 if (strncmp(argv[0]+ds, optname, len))
1222 /* it's --optname, check how the parameter is supplied */
1223 if (argv[0][ds+len] == '=') {
1224 *out = argv[0]+ds+len+1;
1228 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
1231 /* using --opt param */
1238 int main(int argc, char **argv) {
1239 char *redirout = (char*)stdout;
1240 char *redirerr = (char*)stderr;
1245 * Command line option parsing commences now We only need to support
1246 * a few things in the test suite.
1252 if (argv[0][0] == '-') {
1253 if (parsecmd("redirout", &argc, &argv, &redirout, 1, false))
1255 if (parsecmd("redirerr", &argc, &argv, &redirerr, 1, false))
1258 con_change(redirout, redirerr);
1260 if (!strcmp(argv[0]+1, "debug")) {
1264 if (!strcmp(argv[0]+1, "memchk")) {
1268 if (!strcmp(argv[0]+1, "nocolor")) {
1273 con_err("invalid argument %s\n", argv[0]+1);
1277 con_change(redirout, redirerr);
1278 test_perform("tests");