file_test_base.cpp 19 KB

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