file_test_base.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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)
  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. llvm::StringRef 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.data(), nullptr, test_name.data(),
  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. auto FileTestEventListener::OnTestProgramStart(
  290. const testing::UnitTest& /*unit_test*/) -> void {
  291. llvm::CrashRecoveryContext::Enable();
  292. llvm::DefaultThreadPool pool(
  293. {.ThreadsRequested = absl::GetFlag(FLAGS_dump_output)
  294. ? 1
  295. : absl::GetFlag(FLAGS_threads)});
  296. if (!absl::GetFlag(FLAGS_dump_output)) {
  297. llvm::errs() << "Running tests with " << pool.getMaxConcurrency()
  298. << " thread(s)\n";
  299. }
  300. // Guard access to both `llvm::errs` and `crashed`.
  301. bool crashed = false;
  302. std::mutex output_mutex;
  303. for (auto& test : tests_) {
  304. if (!test.registered_test->should_run()) {
  305. continue;
  306. }
  307. pool.async([&output_mutex, &crashed, &test] {
  308. // If any thread crashed, don't try running more.
  309. {
  310. std::unique_lock<std::mutex> lock(output_mutex);
  311. if (crashed) {
  312. return;
  313. }
  314. }
  315. // Use a crash recovery context to try to get a stack trace when
  316. // multiple threads may crash in parallel, which otherwise leads to the
  317. // program aborting without printing a stack trace.
  318. llvm::CrashRecoveryContext crc;
  319. crc.DumpStackAndCleanupOnFailure = true;
  320. bool thread_crashed = !crc.RunSafely([&] {
  321. std::unique_ptr<FileTestBase> test_instance(test.factory_fn());
  322. // Add a crash trace entry with the single-file test command.
  323. std::string test_command =
  324. GetBazelCommand(BazelMode::Test, test.test_name);
  325. llvm::PrettyStackTraceString stack_trace_entry(test_command.c_str());
  326. if (absl::GetFlag(FLAGS_dump_output)) {
  327. std::unique_lock<std::mutex> lock(output_mutex);
  328. llvm::errs() << "\n--- Dumping: " << test.test_name << "\n\n";
  329. }
  330. test.test_result = ProcessTestFileAndRun(
  331. test_instance.get(), &output_mutex,
  332. absl::GetFlag(FLAGS_dump_output), absl::GetFlag(FLAGS_autoupdate));
  333. if (!test.test_result->ok()) {
  334. std::unique_lock<std::mutex> lock(output_mutex);
  335. llvm::errs() << "\n" << test.test_result->error().message() << "\n";
  336. return;
  337. }
  338. test.autoupdate_differs =
  339. RunAutoupdater(test_instance.get(), **test.test_result,
  340. /*dry_run=*/!absl::GetFlag(FLAGS_autoupdate));
  341. std::unique_lock<std::mutex> lock(output_mutex);
  342. if (absl::GetFlag(FLAGS_dump_output)) {
  343. llvm::outs().flush();
  344. const TestFile& test_file = **test.test_result;
  345. llvm::errs() << "\n--- Exit with success: "
  346. << (test_file.run_result.success ? "true" : "false")
  347. << "\n--- Autoupdate differs: "
  348. << (test.autoupdate_differs ? "true" : "false") << "\n";
  349. } else {
  350. llvm::errs() << (test.autoupdate_differs ? "!" : ".");
  351. }
  352. });
  353. if (thread_crashed) {
  354. std::unique_lock<std::mutex> lock(output_mutex);
  355. crashed = true;
  356. }
  357. });
  358. }
  359. pool.wait();
  360. if (crashed) {
  361. // Abort rather than returning so that we don't get a LeakSanitizer report.
  362. // We expect to have leaked memory if one or more of our tests crashed.
  363. std::abort();
  364. }
  365. llvm::errs() << "\nDone!\n";
  366. }
  367. // Implements main() within the Carbon::Testing namespace for convenience.
  368. static auto Main(int argc, char** argv) -> ErrorOr<int> {
  369. Carbon::InitLLVM init_llvm(argc, argv);
  370. testing::InitGoogleTest(&argc, argv);
  371. auto args = absl::ParseCommandLine(argc, argv);
  372. if (args.size() > 1) {
  373. ErrorBuilder b;
  374. b << "Unexpected arguments:";
  375. for (char* arg : llvm::ArrayRef(args).drop_front()) {
  376. b << " " << FormatEscaped(arg);
  377. }
  378. return b;
  379. }
  380. std::string exe_path = FindExecutablePath(argv[0]);
  381. // Tests might try to read from stdin. Ensure those reads fail by closing
  382. // stdin and reopening it as /dev/null. Note that STDIN_FILENO doesn't exist
  383. // on Windows, but POSIX requires it to be 0.
  384. if (std::error_code error =
  385. llvm::sys::Process::SafelyCloseFileDescriptor(0)) {
  386. return Error("Unable to close standard input: " + error.message());
  387. }
  388. if (std::error_code error =
  389. llvm::sys::Process::FixupStandardFileDescriptors()) {
  390. return Error("Unable to correct standard file descriptors: " +
  391. error.message());
  392. }
  393. if (absl::GetFlag(FLAGS_autoupdate) && absl::GetFlag(FLAGS_dump_output)) {
  394. return Error("--autoupdate and --dump_output are mutually exclusive.");
  395. }
  396. auto test_factory = GetFileTestFactory();
  397. MaybeApplyFileTestsFlag(test_factory.name);
  398. // Inline 0 entries because it will always be too large to store on the stack.
  399. llvm::SmallVector<FileTestInfo, 0> tests;
  400. CARBON_RETURN_IF_ERROR(RegisterTests(&test_factory, exe_path, tests));
  401. testing::TestEventListeners& listeners =
  402. testing::UnitTest::GetInstance()->listeners();
  403. if (absl::GetFlag(FLAGS_autoupdate) || absl::GetFlag(FLAGS_dump_output)) {
  404. // Suppress all of the default output.
  405. delete listeners.Release(listeners.default_result_printer());
  406. }
  407. // Use a listener to run tests in parallel.
  408. listeners.Append(new FileTestEventListener(tests));
  409. return RUN_ALL_TESTS();
  410. }
  411. } // namespace Carbon::Testing
  412. auto main(int argc, char** argv) -> int {
  413. if (auto result = Carbon::Testing::Main(argc, argv); result.ok()) {
  414. return *result;
  415. } else {
  416. llvm::errs() << result.error() << "\n";
  417. return EXIT_FAILURE;
  418. }
  419. }