Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 1 | //===-- TestRunner.cpp ----------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestRunner.h" |
| 10 | |
| 11 | using namespace llvm; |
| 12 | |
| 13 | /// Gets Current Working Directory and tries to create a Tmp Directory |
| 14 | static SmallString<128> initializeTmpDirectory() { |
| 15 | SmallString<128> CWD; |
| 16 | if (std::error_code EC = sys::fs::current_path(CWD)) { |
| 17 | errs() << "Error getting current directory: " << EC.message() << "!\n"; |
| 18 | exit(1); |
| 19 | } |
| 20 | |
| 21 | SmallString<128> TmpDirectory; |
| 22 | sys::path::append(TmpDirectory, CWD, "tmp"); |
| 23 | if (std::error_code EC = sys::fs::create_directory(TmpDirectory)) |
| 24 | errs() << "Error creating tmp directory: " << EC.message() << "!\n"; |
| 25 | |
| 26 | return TmpDirectory; |
| 27 | } |
| 28 | |
David Blaikie | aaef97a | 2019-09-12 00:31:57 +0000 | [diff] [blame^] | 29 | TestRunner::TestRunner(StringRef TestName, std::vector<std::string> TestArgs) |
| 30 | : TestName(TestName), TestArgs(std::move(TestArgs)) { |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 31 | TmpDirectory = initializeTmpDirectory(); |
| 32 | } |
| 33 | |
| 34 | /// Runs the interestingness test, passes file to be tested as first argument |
| 35 | /// and other specified test arguments after that. |
| 36 | int TestRunner::run(StringRef Filename) { |
| 37 | std::vector<StringRef> ProgramArgs; |
| 38 | ProgramArgs.push_back(TestName); |
| 39 | ProgramArgs.push_back(Filename); |
| 40 | |
| 41 | for (auto Arg : TestArgs) |
| 42 | ProgramArgs.push_back(Arg.c_str()); |
| 43 | |
| 44 | Optional<StringRef> Redirects[3]; // STDIN, STDOUT, STDERR |
Reid Kleckner | 6d5f002 | 2019-09-11 20:29:22 +0000 | [diff] [blame] | 45 | std::string ErrMsg; |
| 46 | int Result = |
| 47 | sys::ExecuteAndWait(TestName, ProgramArgs, None, Redirects, |
| 48 | /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 49 | |
| 50 | if (Result < 0) { |
Reid Kleckner | 6d5f002 | 2019-09-11 20:29:22 +0000 | [diff] [blame] | 51 | Error E = make_error<StringError>("Error running interesting-ness test: " + |
| 52 | ErrMsg, |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 53 | inconvertibleErrorCode()); |
Diego Trevino Ferrer | c268925 | 2019-08-15 22:39:43 +0000 | [diff] [blame] | 54 | errs() << toString(std::move(E)); |
Diego Trevino Ferrer | ddc64eb | 2019-08-08 22:16:33 +0000 | [diff] [blame] | 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | return !Result; |
| 59 | } |