file_test_base.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // Implementation-wise, this:
  5. //
  6. // - Uses the registered `FileTestFactory` to construct `FileTestBase`
  7. // instances.
  8. // - Constructs a `FileTestCase` that wraps each `FileTestBase` instance to
  9. // register with googletest, and to provide the actual `TestBody`.
  10. // - Using `FileTestEventListener`, runs tests in parallel prior to normal
  11. // googletest execution.
  12. // - This is required to support `--gtest_filter` and access `should_run`.
  13. // - Runs each `FileTestBase` instance to cache the `TestFile` on
  14. // `FileTestInfo`.
  15. // - Determines whether autoupdate would make changes, autoupdating if
  16. // requested.
  17. // - When googletest would normally execute the test, `FileTestCase::TestBody`
  18. // instead uses the cached state on `FileTestInfo`.
  19. // - This only occurs when neither autoupdating nor dumping output.
  20. #include "testing/file_test/file_test_base.h"
  21. #include <atomic>
  22. #include <chrono>
  23. #include <cstdlib>
  24. #include <filesystem>
  25. #include <functional>
  26. #include <memory>
  27. #include <mutex>
  28. #include <optional>
  29. #include <string>
  30. #include <string_view>
  31. #include <system_error>
  32. #include <utility>
  33. #include "absl/flags/flag.h"
  34. #include "absl/flags/parse.h"
  35. #include "absl/strings/str_join.h"
  36. #include "common/check.h"
  37. #include "common/error.h"
  38. #include "common/exe_path.h"
  39. #include "common/init_llvm.h"
  40. #include "common/raw_string_ostream.h"
  41. #include "llvm/ADT/StringExtras.h"
  42. #include "llvm/Support/CrashRecoveryContext.h"
  43. #include "llvm/Support/FormatVariadic.h"
  44. #include "llvm/Support/MemoryBuffer.h"
  45. #include "llvm/Support/PrettyStackTrace.h"
  46. #include "llvm/Support/Process.h"
  47. #include "llvm/Support/ThreadPool.h"
  48. #include "testing/file_test/autoupdate.h"
  49. #include "testing/file_test/run_test.h"
  50. #include "testing/file_test/test_file.h"
  51. ABSL_FLAG(std::vector<std::string>, file_tests, {},
  52. "A comma-separated list of repo-relative names of test files. "
  53. "Similar to and overrides `--gtest_filter`, but doesn't require the "
  54. "test class name to be known.");
  55. ABSL_FLAG(bool, autoupdate, false,
  56. "Instead of verifying files match test output, autoupdate files "
  57. "based on test output.");
  58. ABSL_FLAG(unsigned int, threads, 0,
  59. "Number of threads to use when autoupdating tests, or 0 to "
  60. "automatically determine a thread count.");
  61. ABSL_FLAG(bool, dump_output, false,
  62. "Instead of verifying files match test output, directly dump output "
  63. "to stderr.");
  64. ABSL_FLAG(int, print_slowest_tests, 5,
  65. "The number of tests to print when showing slowest tests. Set to 0 "
  66. "to disabling printing. Set to -1 to print all tests.");
  67. namespace Carbon::Testing {
  68. // Information for a test case.
  69. struct FileTestInfo {
  70. // The name.
  71. std::string test_name;
  72. // A factory function for creating the test object.
  73. std::function<auto()->FileTestBase*> factory_fn;
  74. // gtest's information about the test.
  75. ::testing::TestInfo* registered_test;
  76. // The test result, set after running.
  77. std::optional<ErrorOr<TestFile>> test_result;
  78. // Whether running autoupdate would change (or when autoupdating, already
  79. // changed) the test file. This may be true even if output passes test
  80. // expectations.
  81. bool autoupdate_differs = false;
  82. // Time spent in the test total, including processing and autoupdate.
  83. std::chrono::milliseconds elapsed_ms = std::chrono::milliseconds(0);
  84. };
  85. // Adapts a `FileTestBase` instance to gtest for outputting results.
  86. class FileTestCase : public testing::Test {
  87. public:
  88. explicit FileTestCase(FileTestInfo* test_info) : test_info_(test_info) {}
  89. // Runs a test and compares output. This keeps output split by line so that
  90. // issues are a little easier to identify by the different line.
  91. auto TestBody() -> void final;
  92. private:
  93. FileTestInfo* test_info_;
  94. };
  95. // Splits outputs to string_view because gtest handles string_view by default.
  96. static auto SplitOutput(llvm::StringRef output)
  97. -> llvm::SmallVector<std::string_view> {
  98. if (output.empty()) {
  99. return {};
  100. }
  101. llvm::SmallVector<llvm::StringRef> lines;
  102. llvm::StringRef(output).split(lines, "\n");
  103. return llvm::SmallVector<std::string_view>(lines.begin(), lines.end());
  104. }
  105. // Verify that the success and `fail_` prefix use correspond. Separately handle
  106. // both cases for clearer test failures.
  107. static auto CompareFailPrefix(llvm::StringRef filename, bool success) -> void {
  108. if (success) {
  109. EXPECT_FALSE(filename.starts_with("fail_"))
  110. << "`" << filename
  111. << "` succeeded; if success is expected, remove the `fail_` "
  112. "prefix.";
  113. } else {
  114. EXPECT_TRUE(filename.starts_with("fail_"))
  115. << "`" << filename
  116. << "` failed; if failure is expected, add the `fail_` prefix.";
  117. }
  118. }
  119. // Modes for GetBazelCommand.
  120. enum class BazelMode : uint8_t {
  121. Autoupdate,
  122. Dump,
  123. Test,
  124. };
  125. // Returns the requested bazel command string for the given execution mode.
  126. static auto GetBazelCommand(BazelMode mode, llvm::StringRef test_name)
  127. -> std::string {
  128. RawStringOstream args;
  129. const char* target = getenv("TEST_TARGET");
  130. args << "bazel " << ((mode == BazelMode::Test) ? "test" : "run") << " "
  131. << (target ? target : "<target>") << " ";
  132. switch (mode) {
  133. case BazelMode::Autoupdate:
  134. args << "-- --autoupdate ";
  135. break;
  136. case BazelMode::Dump:
  137. args << "-- --dump_output ";
  138. break;
  139. case BazelMode::Test:
  140. args << "--test_arg=";
  141. break;
  142. }
  143. args << "--file_tests=";
  144. args << test_name;
  145. return args.TakeStr();
  146. }
  147. // Runs the FileTestAutoupdater, returning the result.
  148. static auto RunAutoupdater(FileTestBase* test_base, const TestFile& test_file,
  149. bool dry_run) -> bool {
  150. if (!test_file.autoupdate_line_number) {
  151. return false;
  152. }
  153. llvm::SmallVector<llvm::StringRef> filenames;
  154. filenames.reserve(test_file.non_check_lines.size());
  155. if (test_file.has_splits) {
  156. // There are splits, so we provide an empty name for the first file.
  157. filenames.push_back({});
  158. }
  159. for (const auto& file : test_file.file_splits) {
  160. filenames.push_back(file.filename);
  161. }
  162. llvm::ArrayRef expected_filenames = filenames;
  163. if (filenames.size() > 1) {
  164. expected_filenames = expected_filenames.drop_front();
  165. }
  166. return FileTestAutoupdater(
  167. std::filesystem::absolute(test_base->test_name().str()),
  168. GetBazelCommand(BazelMode::Test, test_base->test_name()),
  169. GetBazelCommand(BazelMode::Dump, test_base->test_name()),
  170. test_file.input_content, filenames,
  171. *test_file.autoupdate_line_number, test_file.autoupdate_split,
  172. test_file.non_check_lines, test_file.actual_stdout,
  173. test_file.actual_stderr,
  174. test_base->GetDefaultFileRE(expected_filenames),
  175. test_base->GetLineNumberReplacements(expected_filenames),
  176. [&](std::string& line) {
  177. test_base->DoExtraCheckReplacements(line);
  178. })
  179. .Run(dry_run);
  180. }
  181. auto FileTestCase::TestBody() -> void {
  182. if (absl::GetFlag(FLAGS_autoupdate) || absl::GetFlag(FLAGS_dump_output)) {
  183. return;
  184. }
  185. CARBON_CHECK(test_info_->test_result,
  186. "Expected test to be run prior to TestBody: {0}",
  187. test_info_->test_name);
  188. ASSERT_TRUE(test_info_->test_result->ok())
  189. << test_info_->test_result->error();
  190. auto test_filename = std::filesystem::path(test_info_->test_name).filename();
  191. // Check success/failure against `fail_` prefixes.
  192. TestFile& test_file = **(test_info_->test_result);
  193. if (test_file.run_result.per_file_success.empty()) {
  194. CompareFailPrefix(test_filename.string(), test_file.run_result.success);
  195. } else {
  196. bool require_overall_failure = false;
  197. for (const auto& [filename, success] :
  198. test_file.run_result.per_file_success) {
  199. CompareFailPrefix(filename, success);
  200. if (!success) {
  201. require_overall_failure = true;
  202. }
  203. }
  204. if (require_overall_failure) {
  205. EXPECT_FALSE(test_file.run_result.success)
  206. << "There is a per-file failure expectation, so the overall result "
  207. "should have been a failure.";
  208. } else {
  209. // Individual files all succeeded, so the prefix is enforced on the main
  210. // test file.
  211. CompareFailPrefix(test_filename.string(), test_file.run_result.success);
  212. }
  213. }
  214. // Check results. Include a reminder for NOAUTOUPDATE tests.
  215. std::unique_ptr<testing::ScopedTrace> scoped_trace;
  216. if (!test_file.autoupdate_line_number) {
  217. scoped_trace = std::make_unique<testing::ScopedTrace>(
  218. __FILE__, __LINE__,
  219. "This file is NOAUTOUPDATE, so expected differences require manual "
  220. "updates.");
  221. }
  222. if (test_file.check_subset) {
  223. EXPECT_THAT(SplitOutput(test_file.actual_stdout),
  224. IsSupersetOf(test_file.expected_stdout));
  225. EXPECT_THAT(SplitOutput(test_file.actual_stderr),
  226. IsSupersetOf(test_file.expected_stderr));
  227. } else {
  228. EXPECT_THAT(SplitOutput(test_file.actual_stdout),
  229. ElementsAreArray(test_file.expected_stdout));
  230. EXPECT_THAT(SplitOutput(test_file.actual_stderr),
  231. ElementsAreArray(test_file.expected_stderr));
  232. }
  233. if (HasFailure()) {
  234. llvm::errs() << "\nTo test this file alone, run:\n "
  235. << GetBazelCommand(BazelMode::Test, test_info_->test_name)
  236. << "\n\n";
  237. if (!test_file.autoupdate_line_number) {
  238. llvm::errs() << "\nThis test is NOAUTOUPDATE.\n\n";
  239. }
  240. }
  241. if (test_info_->autoupdate_differs) {
  242. ADD_FAILURE() << "Autoupdate would make changes to the file content. Run:\n"
  243. << GetBazelCommand(BazelMode::Autoupdate,
  244. test_info_->test_name);
  245. }
  246. }
  247. auto FileTestBase::GetLineNumberReplacements(
  248. llvm::ArrayRef<llvm::StringRef> filenames) const
  249. -> llvm::SmallVector<LineNumberReplacement> {
  250. return {{.has_file = true,
  251. .re = std::make_shared<RE2>(
  252. llvm::formatv(R"(({0}):(\d+)?)", llvm::join(filenames, "|"))),
  253. .line_formatv = R"({0})"}};
  254. }
  255. // If `--file_tests` is set, transform it into a `--gtest_filter`.
  256. static auto MaybeApplyFileTestsFlag(llvm::StringRef factory_name) -> void {
  257. if (absl::GetFlag(FLAGS_file_tests).empty()) {
  258. return;
  259. }
  260. RawStringOstream filter;
  261. llvm::ListSeparator sep(":");
  262. for (const auto& file : absl::GetFlag(FLAGS_file_tests)) {
  263. filter << sep << factory_name << "." << file;
  264. }
  265. absl::SetFlag(&FLAGS_gtest_filter, filter.TakeStr());
  266. }
  267. // Loads tests from the manifest file, and registers them for execution. The
  268. // vector is taken as an output parameter so that the address of entries is
  269. // stable for the factory.
  270. static auto RegisterTests(FileTestFactory* test_factory,
  271. llvm::StringRef exe_path,
  272. llvm::SmallVectorImpl<FileTestInfo>& tests)
  273. -> ErrorOr<Success> {
  274. // Prepare the vector first, so that the location of entries won't change.
  275. for (auto& test_name : GetFileTestManifest()) {
  276. tests.push_back({.test_name = test_name});
  277. }
  278. // Amend entries with factory functions.
  279. for (auto& test : tests) {
  280. const std::string& test_name = test.test_name;
  281. test.factory_fn = [test_factory, exe_path, &test_name]() {
  282. return test_factory->factory_fn(exe_path, test_name);
  283. };
  284. test.registered_test = testing::RegisterTest(
  285. test_factory->name, test_name.c_str(), nullptr, test_name.c_str(),
  286. __FILE__, __LINE__, [&test]() { return new FileTestCase(&test); });
  287. }
  288. return Success();
  289. }
  290. // Implements the parallel test execution through gtest's listener support.
  291. class FileTestEventListener : public testing::EmptyTestEventListener {
  292. public:
  293. explicit FileTestEventListener(llvm::MutableArrayRef<FileTestInfo> tests)
  294. : tests_(tests) {}
  295. // Runs test during start, after `should_run` is initialized. This is
  296. // multi-threaded to get extra speed.
  297. auto OnTestProgramStart(const testing::UnitTest& /*unit_test*/)
  298. -> void override;
  299. private:
  300. llvm::MutableArrayRef<FileTestInfo> tests_;
  301. };
  302. // Returns true if the main thread should be used to run tests. This is if
  303. // either --dump_output is specified, or only 1 thread is needed to run tests.
  304. static auto SingleThreaded(llvm::ArrayRef<FileTestInfo> tests) -> bool {
  305. if (absl::GetFlag(FLAGS_dump_output) || absl::GetFlag(FLAGS_threads) == 1) {
  306. return true;
  307. }
  308. bool found_test_to_run = false;
  309. for (const auto& test : tests) {
  310. if (!test.registered_test->should_run()) {
  311. continue;
  312. }
  313. if (found_test_to_run) {
  314. // At least two tests will run, so multi-threaded.
  315. return false;
  316. }
  317. // Found the first test to run.
  318. found_test_to_run = true;
  319. }
  320. // 0 or 1 test will be run, so single-threaded.
  321. return true;
  322. }
  323. // Runs the test in the section that would be inside a lock, possibly inside a
  324. // CrashRecoveryContext.
  325. static auto RunSingleTestHelper(FileTestInfo& test, FileTestBase& test_instance)
  326. -> void {
  327. Timer timer;
  328. // Add a crash trace entry with the single-file test command.
  329. std::string test_command = GetBazelCommand(BazelMode::Test, test.test_name);
  330. llvm::PrettyStackTraceString stack_trace_entry(test_command.c_str());
  331. if (auto err = RunTestFile(test_instance, absl::GetFlag(FLAGS_dump_output),
  332. **test.test_result);
  333. !err.ok()) {
  334. test.test_result = std::move(err).error();
  335. }
  336. test.elapsed_ms += timer.elapsed_ms();
  337. }
  338. // Runs a single test. Uses a CrashRecoveryContext, and returns false on a
  339. // crash. For test_elapsed_ms, try to exclude time spent waiting on
  340. // output_mutex.
  341. static auto RunSingleTest(FileTestInfo& test, bool single_threaded,
  342. std::mutex& output_mutex) -> bool {
  343. std::unique_ptr<FileTestBase> test_instance(test.factory_fn());
  344. if (absl::GetFlag(FLAGS_dump_output)) {
  345. std::unique_lock<std::mutex> lock(output_mutex);
  346. llvm::errs() << "\n--- Dumping: " << test.test_name << "\n\n";
  347. }
  348. // Load expected output.
  349. Timer process_timer;
  350. test.test_result = ProcessTestFile(test_instance->test_name(),
  351. absl::GetFlag(FLAGS_autoupdate));
  352. test.elapsed_ms = process_timer.elapsed_ms();
  353. if (test.test_result->ok()) {
  354. // Execution must be serialized for either serial tests or console
  355. // output.
  356. std::unique_lock<std::mutex> output_lock;
  357. if ((*test.test_result)->capture_console_output ||
  358. !test_instance->AllowParallelRun()) {
  359. output_lock = std::unique_lock<std::mutex>(output_mutex);
  360. }
  361. if (single_threaded) {
  362. RunSingleTestHelper(test, *test_instance);
  363. } else {
  364. // Use a crash recovery context to try to get a stack trace when
  365. // multiple threads may crash in parallel, which otherwise leads to the
  366. // program aborting without printing a stack trace.
  367. llvm::CrashRecoveryContext crc;
  368. crc.DumpStackAndCleanupOnFailure = true;
  369. if (!crc.RunSafely([&] { RunSingleTestHelper(test, *test_instance); })) {
  370. return false;
  371. }
  372. }
  373. }
  374. if (!test.test_result->ok()) {
  375. std::unique_lock<std::mutex> lock(output_mutex);
  376. llvm::errs() << "\n" << test.test_result->error().message() << "\n";
  377. return true;
  378. }
  379. Timer autoupdate_timer;
  380. test.autoupdate_differs =
  381. RunAutoupdater(test_instance.get(), **test.test_result,
  382. /*dry_run=*/!absl::GetFlag(FLAGS_autoupdate));
  383. test.elapsed_ms += autoupdate_timer.elapsed_ms();
  384. std::unique_lock<std::mutex> lock(output_mutex);
  385. if (absl::GetFlag(FLAGS_dump_output)) {
  386. llvm::outs().flush();
  387. const TestFile& test_file = **test.test_result;
  388. llvm::errs() << "\n--- Exit with success: "
  389. << (test_file.run_result.success ? "true" : "false")
  390. << "\n--- Autoupdate differs: "
  391. << (test.autoupdate_differs ? "true" : "false") << "\n";
  392. } else {
  393. llvm::errs() << (test.autoupdate_differs ? "!" : ".");
  394. }
  395. return true;
  396. }
  397. auto FileTestEventListener::OnTestProgramStart(
  398. const testing::UnitTest& /*unit_test*/) -> void {
  399. bool single_threaded = SingleThreaded(tests_);
  400. std::unique_ptr<llvm::ThreadPoolInterface> pool;
  401. if (single_threaded) {
  402. pool = std::make_unique<llvm::SingleThreadExecutor>();
  403. } else {
  404. // Enable the CRC for use in `RunSingleTest`.
  405. llvm::CrashRecoveryContext::Enable();
  406. llvm::ThreadPoolStrategy thread_strategy = {
  407. .ThreadsRequested = absl::GetFlag(FLAGS_threads),
  408. // Disable hyper threads to reduce contention.
  409. .UseHyperThreads = false};
  410. pool = std::make_unique<llvm::DefaultThreadPool>(thread_strategy);
  411. }
  412. if (!absl::GetFlag(FLAGS_dump_output)) {
  413. llvm::errs() << "Running tests with " << pool->getMaxConcurrency()
  414. << " thread(s)\n";
  415. }
  416. // Guard access to output (stdout and stderr).
  417. std::mutex output_mutex;
  418. std::atomic<bool> crashed = false;
  419. Timer all_timer;
  420. int run_count = 0;
  421. for (auto& test : tests_) {
  422. if (!test.registered_test->should_run()) {
  423. continue;
  424. }
  425. ++run_count;
  426. pool->async([&] {
  427. // If any thread crashed, don't try running more.
  428. if (crashed) {
  429. return;
  430. }
  431. if (!RunSingleTest(test, single_threaded, output_mutex)) {
  432. crashed = true;
  433. }
  434. });
  435. }
  436. pool->wait();
  437. if (crashed) {
  438. // Abort rather than returning so that we don't get a LeakSanitizer report.
  439. // We expect to have leaked memory if one or more of our tests crashed.
  440. std::abort();
  441. }
  442. // Calculate the total test time.
  443. auto all_elapsed_ms = all_timer.elapsed_ms();
  444. auto total_elapsed_ms = std::chrono::milliseconds(0);
  445. for (auto& test : tests_) {
  446. total_elapsed_ms += test.elapsed_ms;
  447. }
  448. llvm::errs() << "\nRan " << run_count << " tests in "
  449. << all_elapsed_ms.count() << " ms wall time, "
  450. << total_elapsed_ms.count() << " ms across threads\n";
  451. // When there are multiple tests, give additional timing details, particularly
  452. // slowest tests.
  453. auto print_slowest_tests = absl::GetFlag(FLAGS_print_slowest_tests);
  454. if (run_count > 1 && print_slowest_tests != 0) {
  455. llvm::errs() << " Slowest tests:\n";
  456. llvm::sort(tests_, [](const FileTestInfo& lhs, const FileTestInfo& rhs) {
  457. return lhs.elapsed_ms > rhs.elapsed_ms;
  458. });
  459. int count = print_slowest_tests > 0 ? print_slowest_tests : run_count;
  460. for (const auto& test : tests_.take_front(count)) {
  461. std::chrono::milliseconds run_ms(0);
  462. if (test.test_result && test.test_result->ok()) {
  463. run_ms = test.test_result.value()->run_elapsed_ms;
  464. }
  465. llvm::errs() << " - " << test.test_name << ": "
  466. << test.elapsed_ms.count() << " ms, " << run_ms.count()
  467. << " ms in Run\n";
  468. }
  469. }
  470. }
  471. // Implements main() within the Carbon::Testing namespace for convenience.
  472. static auto Main(int argc, char** argv) -> ErrorOr<int> {
  473. Carbon::InitLLVM init_llvm(argc, argv);
  474. testing::InitGoogleTest(&argc, argv);
  475. auto args = absl::ParseCommandLine(argc, argv);
  476. if (args.size() > 1) {
  477. ErrorBuilder b;
  478. b << "Unexpected arguments:";
  479. for (char* arg : llvm::ArrayRef(args).drop_front()) {
  480. b << " " << FormatEscaped(arg);
  481. }
  482. return b;
  483. }
  484. std::string exe_path = FindExecutablePath(argv[0]);
  485. // Tests might try to read from stdin. Ensure those reads fail by closing
  486. // stdin and reopening it as /dev/null. Note that STDIN_FILENO doesn't exist
  487. // on Windows, but POSIX requires it to be 0.
  488. if (std::error_code error =
  489. llvm::sys::Process::SafelyCloseFileDescriptor(0)) {
  490. return Error("Unable to close standard input: " + error.message());
  491. }
  492. if (std::error_code error =
  493. llvm::sys::Process::FixupStandardFileDescriptors()) {
  494. return Error("Unable to correct standard file descriptors: " +
  495. error.message());
  496. }
  497. if (absl::GetFlag(FLAGS_autoupdate) && absl::GetFlag(FLAGS_dump_output)) {
  498. return Error("--autoupdate and --dump_output are mutually exclusive.");
  499. }
  500. auto test_factory = GetFileTestFactory();
  501. MaybeApplyFileTestsFlag(test_factory.name);
  502. // Inline 0 entries because it will always be too large to store on the stack.
  503. llvm::SmallVector<FileTestInfo, 0> tests;
  504. CARBON_RETURN_IF_ERROR(RegisterTests(&test_factory, exe_path, tests));
  505. testing::TestEventListeners& listeners =
  506. testing::UnitTest::GetInstance()->listeners();
  507. if (absl::GetFlag(FLAGS_autoupdate) || absl::GetFlag(FLAGS_dump_output)) {
  508. // Suppress all of the default output.
  509. delete listeners.Release(listeners.default_result_printer());
  510. }
  511. // Use a listener to run tests in parallel.
  512. listeners.Append(new FileTestEventListener(tests));
  513. return RUN_ALL_TESTS();
  514. }
  515. } // namespace Carbon::Testing
  516. auto main(int argc, char** argv) -> int {
  517. if (auto result = Carbon::Testing::Main(argc, argv); result.ok()) {
  518. return *result;
  519. } else {
  520. llvm::errs() << result.error() << "\n";
  521. return EXIT_FAILURE;
  522. }
  523. }