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 TestRunner::TestRunner(StringRef TestName,
14                        const std::vector<std::string> &TestArgs,
15                        std::unique_ptr<ReducerWorkItem> Program)
16     : TestName(TestName), TestArgs(TestArgs), Program(std::move(Program)) {
17   assert(this->Program && "Initialized with null program?");
18 }
19 
20 /// Runs the interestingness test, passes file to be tested as first argument
21 /// and other specified test arguments after that.
22 int TestRunner::run(StringRef Filename) {
23   std::vector<StringRef> ProgramArgs;
24   ProgramArgs.push_back(TestName);
25 
26   for (const auto &Arg : TestArgs)
27     ProgramArgs.push_back(Arg);
28 
29   ProgramArgs.push_back(Filename);
30 
31   std::string ErrMsg;
32   int Result = sys::ExecuteAndWait(
33       TestName, ProgramArgs, /*Env=*/None, /*Redirects=*/None,
34       /*SecondsToWait=*/0, /*MemoryLimit=*/0, &ErrMsg);
35 
36   if (Result < 0) {
37     Error E = make_error<StringError>("Error running interesting-ness test: " +
38                                           ErrMsg,
39                                       inconvertibleErrorCode());
40     errs() << toString(std::move(E));
41     exit(1);
42   }
43 
44   return !Result;
45 }
46