int testcase_run_one(const struct testgroup_t *group, const struct testcase_t *testcase) { enum outcome outcome; if (testcase->flags & TT_SKIP) { if (opt_verbosity>0) printf("%s%s: SKIPPED\n", group->prefix, testcase->name); ++n_skipped; return SKIP; } if (opt_verbosity>0 && !opt_forked) { printf("%s%s: ", group->prefix, testcase->name); } else { if (opt_verbosity==0) printf("."); cur_test_prefix = group->prefix; cur_test_name = testcase->name; } if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) { outcome = testcase_run_forked_(group, testcase); } else { outcome = testcase_run_bare_(testcase); } if (outcome == OK) { ++n_ok; if (opt_verbosity>0 && !opt_forked) puts(opt_verbosity==1?"OK":""); } else if (outcome == SKIP) { ++n_skipped; if (opt_verbosity>0 && !opt_forked) puts("SKIPPED"); } else { ++n_bad; if (!opt_forked) printf("\n [%s FAILED]\n", testcase->name); } if (opt_forked) { exit(outcome==OK ? 0 : (outcome==SKIP?MAGIC_EXITCODE : 1)); return 1; /* unreachable */ } else { return (int)outcome; } }
static enum outcome testcase_run_forked_(const struct testgroup_t *group, const struct testcase_t *testcase) { #ifdef _WIN32 /* Fork? On Win32? How primitive! We'll do what the smart kids do: we'll invoke our own exe (whose name we recall from the command line) with a command line that tells it to run just the test we want, and this time without forking. (No, threads aren't an option. The whole point of forking is to share no state between tests.) */ int ok; char buffer[LONGEST_TEST_NAME+256]; STARTUPINFOA si; PROCESS_INFORMATION info; DWORD exitcode; if (!in_tinytest_main) { printf("\nERROR. On Windows, testcase_run_forked_ must be" " called from within tinytest_main.\n"); abort(); } if (opt_verbosity>0) printf("[forking] "); snprintf(buffer, sizeof(buffer), "%s --RUNNING-FORKED %s %s%s", commandname, verbosity_flag, group->prefix, testcase->name); memset(&si, 0, sizeof(si)); memset(&info, 0, sizeof(info)); si.cb = sizeof(si); ok = CreateProcessA(commandname, buffer, NULL, NULL, 0, 0, NULL, NULL, &si, &info); if (!ok) { printf("CreateProcess failed!\n"); return 0; } WaitForSingleObject(info.hProcess, INFINITE); GetExitCodeProcess(info.hProcess, &exitcode); CloseHandle(info.hProcess); CloseHandle(info.hThread); if (exitcode == 0) return OK; else if (exitcode == MAGIC_EXITCODE) return SKIP; else return FAIL; #else int outcome_pipe[2]; pid_t pid; (void)group; if (pipe(outcome_pipe)) perror("opening pipe"); if (opt_verbosity>0) printf("[forking] "); pid = fork(); #ifdef FORK_BREAKS_GCOV vproc_transaction_begin(0); #endif if (!pid) { /* child. */ int test_r, write_r; char b[1]; close(outcome_pipe[0]); test_r = testcase_run_bare_(testcase); assert(0<=(int)test_r && (int)test_r<=2); b[0] = "NYS"[test_r]; write_r = (int)write(outcome_pipe[1], b, 1); if (write_r != 1) { perror("write outcome to pipe"); exit(1); } exit(0); return FAIL; /* unreachable */ } else { /* parent */ int status, r; char b[1]; /* Close this now, so that if the other side closes it, * our read fails. */ close(outcome_pipe[1]); r = (int)read(outcome_pipe[0], b, 1); if (r == 0) { printf("[Lost connection!] "); return 0; } else if (r != 1) { perror("read outcome from pipe"); } waitpid(pid, &status, 0); close(outcome_pipe[0]); return b[0]=='Y' ? OK : (b[0]=='S' ? SKIP : FAIL); } #endif }
int testcase_run_one(const struct testgroup_t *group, const struct testcase_t *testcase) { enum outcome outcome; if (testcase->flags & (TT_SKIP|TT_OFF_BY_DEFAULT)) { if (opt_verbosity>0) printf("%s%s: %s\n", group->prefix, testcase->name, (testcase->flags & TT_SKIP) ? "SKIPPED" : "DISABLED"); ++n_skipped; return SKIP; } if (opt_verbosity>0 && !opt_forked) { printf("%s%s: ", group->prefix, testcase->name); } else { if (opt_verbosity==0) printf("."); cur_test_prefix = group->prefix; cur_test_name = testcase->name; } #ifndef NO_FORKING if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) { outcome = testcase_run_forked_(group, testcase); } else { #else { #endif outcome = testcase_run_bare_(testcase); } if (outcome == OK) { ++n_ok; if (opt_verbosity>0 && !opt_forked) puts(opt_verbosity==1?"OK":""); } else if (outcome == SKIP) { ++n_skipped; if (opt_verbosity>0 && !opt_forked) puts("SKIPPED"); } else { ++n_bad; if (!opt_forked) printf("\n [%s FAILED]\n", testcase->name); } if (opt_forked) { exit(outcome==OK ? 0 : (outcome==SKIP?MAGIC_EXITCODE : 1)); return 1; /* unreachable */ } else { return (int)outcome; } } int tinytest_set_flag_(struct testgroup_t *groups, const char *arg, int set, unsigned long flag) { int i, j; size_t length = LONGEST_TEST_NAME; char fullname[LONGEST_TEST_NAME]; int found=0; if (strstr(arg, "..")) length = strstr(arg,"..")-arg; for (i=0; groups[i].prefix; ++i) { for (j=0; groups[i].cases[j].name; ++j) { struct testcase_t *testcase = &groups[i].cases[j]; snprintf(fullname, sizeof(fullname), "%s%s", groups[i].prefix, testcase->name); if (!flag) { /* Hack! */ printf(" %s", fullname); if (testcase->flags & TT_OFF_BY_DEFAULT) puts(" (Off by default)"); else if (testcase->flags & TT_SKIP) puts(" (DISABLED)"); else puts(""); } if (!strncmp(fullname, arg, length)) { if (set) testcase->flags |= flag; else testcase->flags &= ~flag; ++found; } } } return found; }