| 49 | namespace tests { |
| 50 | |
| 51 | void execute(const string& script) |
| 52 | { |
| 53 | // Create a temporary directory for the test. |
| 54 | Try<string> directory = environment->mkdtemp(); |
| 55 | |
| 56 | CHECK_SOME(directory) << "Failed to create temporary directory"; |
| 57 | |
| 58 | if (flags.verbose) { |
| 59 | std::cerr << "Using temporary directory '" |
| 60 | << directory.get() << "'" << std::endl; |
| 61 | } |
| 62 | |
| 63 | // Determine the path for the script. |
| 64 | Result<string> path = os::realpath(getTestScriptPath(script)); |
| 65 | |
| 66 | if (!path.isSome()) { |
| 67 | FAIL() << "Failed to locate script " |
| 68 | << script << ": " |
| 69 | << (path.isError() ? path.error() : "No such file or directory"); |
| 70 | } |
| 71 | |
| 72 | // Fork a process to change directory and run the test. |
| 73 | pid_t pid; |
| 74 | if ((pid = fork()) == -1) { |
| 75 | FAIL() << "Failed to fork to launch script"; |
| 76 | } |
| 77 | |
| 78 | if (pid > 0) { |
| 79 | // In parent process. |
| 80 | int status; |
| 81 | while (wait(&status) != pid || WIFSTOPPED(status)); |
| 82 | CHECK(WIFEXITED(status) || WIFSIGNALED(status)) |
| 83 | << "Unexpected wait status " << status; |
| 84 | |
| 85 | if (!WSUCCEEDED(status)) { |
| 86 | FAIL() << script << " " << WSTRINGIFY(status); |
| 87 | } |
| 88 | } else { |
| 89 | // In child process. DO NOT USE GLOG! |
| 90 | |
| 91 | // Start by cd'ing into the temporary directory. |
| 92 | Try<Nothing> chdir = os::chdir(directory.get()); |
| 93 | if (chdir.isError()) { |
| 94 | std::cerr << "Failed to chdir to '" << directory.get() << "': " |
| 95 | << chdir.error() << std::endl; |
| 96 | abort(); |
| 97 | } |
| 98 | |
| 99 | // Redirect output to /dev/null unless the test is verbose. |
| 100 | if (!flags.verbose) { |
| 101 | if (freopen(os::DEV_NULL, "w", stdout) == nullptr || |
| 102 | freopen(os::DEV_NULL, "w", stderr) == nullptr) { |
| 103 | std::cerr << "Failed to redirect stdout/stderr to /dev/null:" |
| 104 | << os::strerror(errno) << std::endl; |
| 105 | abort(); |
| 106 | } |
| 107 | } |
| 108 |
no test coverage detected