file_test_base.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #include "testing/file_test/file_test_base.h"
  5. #include <filesystem>
  6. #include <fstream>
  7. #include <optional>
  8. #include <string>
  9. #include <utility>
  10. #include "absl/flags/flag.h"
  11. #include "absl/flags/parse.h"
  12. #include "common/check.h"
  13. #include "llvm/ADT/StringExtras.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/Support/FormatVariadic.h"
  16. #include "llvm/Support/InitLLVM.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/PrettyStackTrace.h"
  19. #include "testing/file_test/autoupdate.h"
  20. ABSL_FLAG(std::vector<std::string>, file_tests, {},
  21. "A comma-separated list of repo-relative names of test files. "
  22. "Overrides test_targets_file.");
  23. ABSL_FLAG(std::string, test_targets_file, "",
  24. "A path to a file containing repo-relative names of test files.");
  25. ABSL_FLAG(bool, autoupdate, false,
  26. "Instead of verifying files match test output, autoupdate files "
  27. "based on test output.");
  28. namespace Carbon::Testing {
  29. using ::testing::Eq;
  30. using ::testing::Matcher;
  31. using ::testing::MatchesRegex;
  32. using ::testing::StrEq;
  33. // Reads a file to string.
  34. static auto ReadFile(std::string_view path) -> std::string {
  35. std::ifstream proto_file(path);
  36. std::stringstream buffer;
  37. buffer << proto_file.rdbuf();
  38. proto_file.close();
  39. return buffer.str();
  40. }
  41. // Splits outputs to string_view because gtest handles string_view by default.
  42. static auto SplitOutput(llvm::StringRef output)
  43. -> llvm::SmallVector<std::string_view> {
  44. if (output.empty()) {
  45. return {};
  46. }
  47. llvm::SmallVector<llvm::StringRef> lines;
  48. llvm::StringRef(output).split(lines, "\n");
  49. return llvm::SmallVector<std::string_view>(lines.begin(), lines.end());
  50. }
  51. // Runs a test and compares output. This keeps output split by line so that
  52. // issues are a little easier to identify by the different line.
  53. auto FileTestBase::TestBody() -> void {
  54. std::optional<llvm::PrettyStackTraceFormat> stack_trace_entry;
  55. // If we're being run from bazel, provide some assistance for understanding
  56. // and reproducing failures.
  57. const char* target = getenv("TEST_TARGET");
  58. if (target) {
  59. // This advice overrides the --file_tests flag provided by the file_test
  60. // rule.
  61. llvm::errs() << "\nTo test this file alone, run:\n bazel test " << target
  62. << " --test_arg=--file_tests=" << test_name_ << "\n\n";
  63. // Add a crash trace entry with a command that runs this test in isolation.
  64. stack_trace_entry.emplace("bazel test %s --test_arg=--file_tests=%s",
  65. target, test_name_);
  66. }
  67. TestContext context;
  68. auto run_result = ProcessTestFileAndRun(context);
  69. ASSERT_TRUE(run_result.ok()) << run_result.error();
  70. ValidateRun();
  71. auto test_filename = std::filesystem::path(test_name_.str()).filename();
  72. EXPECT_THAT(!llvm::StringRef(test_filename).starts_with("fail_"),
  73. Eq(context.exit_with_success))
  74. << "Tests should be prefixed with `fail_` if and only if running them "
  75. "is expected to fail.";
  76. // Check results. Include a reminder of the autoupdate command for any
  77. // stdout/stderr differences.
  78. std::string update_message;
  79. if (target && context.autoupdate_line_number) {
  80. update_message = llvm::formatv(
  81. "If these differences are expected, try the autoupdater:\n"
  82. "\tbazel run {0} -- --autoupdate --file_tests={1}",
  83. target, test_name_);
  84. } else {
  85. update_message =
  86. "If these differences are expected, content must be updated manually.";
  87. }
  88. SCOPED_TRACE(update_message);
  89. if (context.check_subset) {
  90. EXPECT_THAT(SplitOutput(context.stdout),
  91. IsSupersetOf(context.expected_stdout));
  92. EXPECT_THAT(SplitOutput(context.stderr),
  93. IsSupersetOf(context.expected_stderr));
  94. } else {
  95. EXPECT_THAT(SplitOutput(context.stdout),
  96. ElementsAreArray(context.expected_stdout));
  97. EXPECT_THAT(SplitOutput(context.stderr),
  98. ElementsAreArray(context.expected_stderr));
  99. }
  100. // If there are no other test failures, check if autoupdate would make
  101. // changes. We don't do this when there _are_ failures because the
  102. // SCOPED_TRACE already contains the autoupdate reminder.
  103. if (!HasFailure() && RunAutoupdater(context, /*dry_run=*/true)) {
  104. ADD_FAILURE() << "Autoupdate would make changes to the file content.";
  105. }
  106. }
  107. auto FileTestBase::RunAutoupdater(const TestContext& context, bool dry_run)
  108. -> bool {
  109. if (!context.autoupdate_line_number) {
  110. return false;
  111. }
  112. llvm::SmallVector<llvm::StringRef> filenames;
  113. filenames.reserve(context.non_check_lines.size());
  114. if (context.has_splits) {
  115. // There are splits, so we provide an empty name for the first file.
  116. filenames.push_back({});
  117. }
  118. for (const auto& file : context.test_files) {
  119. filenames.push_back(file.filename);
  120. }
  121. llvm::ArrayRef expected_filenames = filenames;
  122. if (filenames.size() > 1) {
  123. expected_filenames = expected_filenames.drop_front();
  124. }
  125. return FileTestAutoupdater(
  126. std::filesystem::absolute(test_name_.str()), context.input_content,
  127. filenames, *context.autoupdate_line_number,
  128. context.non_check_lines, context.stdout, context.stderr,
  129. GetDefaultFileRE(expected_filenames),
  130. GetLineNumberReplacements(expected_filenames),
  131. [&](std::string& line) { DoExtraCheckReplacements(line); })
  132. .Run(dry_run);
  133. }
  134. auto FileTestBase::Autoupdate() -> ErrorOr<bool> {
  135. // Add a crash trace entry mentioning which file we're updating.
  136. llvm::PrettyStackTraceFormat stack_trace_entry("performing autoupdate for %s",
  137. test_name_);
  138. TestContext context;
  139. auto run_result = ProcessTestFileAndRun(context);
  140. if (!run_result.ok()) {
  141. return ErrorBuilder() << "Error updating " << test_name_ << ": "
  142. << run_result.error();
  143. }
  144. return RunAutoupdater(context, /*dry_run=*/false);
  145. }
  146. auto FileTestBase::GetLineNumberReplacements(
  147. llvm::ArrayRef<llvm::StringRef> filenames)
  148. -> llvm::SmallVector<LineNumberReplacement> {
  149. return {{.has_file = true,
  150. .re = std::make_shared<RE2>(
  151. llvm::formatv(R"(({0}):(\d+))", llvm::join(filenames, "|"))),
  152. .line_formatv = R"({0})"}};
  153. }
  154. auto FileTestBase::ProcessTestFileAndRun(TestContext& context)
  155. -> ErrorOr<Success> {
  156. // Store the file so that test_files can use references to content.
  157. context.input_content = ReadFile(test_name_);
  158. // Load expected output.
  159. CARBON_RETURN_IF_ERROR(ProcessTestFile(context));
  160. // Process arguments.
  161. if (context.test_args.empty()) {
  162. context.test_args = GetDefaultArgs();
  163. }
  164. CARBON_RETURN_IF_ERROR(
  165. DoArgReplacements(context.test_args, context.test_files));
  166. // Create the files in-memory.
  167. llvm::vfs::InMemoryFileSystem fs;
  168. for (const auto& test_file : context.test_files) {
  169. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  170. llvm::MemoryBuffer::getMemBuffer(
  171. test_file.content, test_file.filename,
  172. /*RequiresNullTerminator=*/false))) {
  173. return ErrorBuilder() << "File is repeated: " << test_file.filename;
  174. }
  175. }
  176. // Convert the arguments to StringRef and const char* to match the
  177. // expectations of PrettyStackTraceProgram and Run.
  178. llvm::SmallVector<llvm::StringRef> test_args_ref;
  179. llvm::SmallVector<const char*> test_argv_for_stack_trace;
  180. test_args_ref.reserve(context.test_args.size());
  181. test_argv_for_stack_trace.reserve(context.test_args.size() + 1);
  182. for (const auto& arg : context.test_args) {
  183. test_args_ref.push_back(arg);
  184. test_argv_for_stack_trace.push_back(arg.c_str());
  185. }
  186. // Add a trailing null so that this is a proper argv.
  187. test_argv_for_stack_trace.push_back(nullptr);
  188. // Add a stack trace entry for the test invocation.
  189. llvm::PrettyStackTraceProgram stack_trace_entry(
  190. test_argv_for_stack_trace.size(), test_argv_for_stack_trace.data());
  191. // Capture trace streaming, but only when in debug mode.
  192. llvm::raw_svector_ostream stdout(context.stdout);
  193. llvm::raw_svector_ostream stderr(context.stderr);
  194. CARBON_ASSIGN_OR_RETURN(context.exit_with_success,
  195. Run(test_args_ref, fs, stdout, stderr));
  196. return Success();
  197. }
  198. auto FileTestBase::DoArgReplacements(
  199. llvm::SmallVector<std::string>& test_args,
  200. const llvm::SmallVector<TestFile>& test_files) -> ErrorOr<Success> {
  201. for (auto* it = test_args.begin(); it != test_args.end(); ++it) {
  202. auto percent = it->find("%");
  203. if (percent == std::string::npos) {
  204. continue;
  205. }
  206. if (percent + 1 >= it->size()) {
  207. return ErrorBuilder() << "% is not allowed on its own: " << *it;
  208. }
  209. char c = (*it)[percent + 1];
  210. switch (c) {
  211. case 's': {
  212. if (*it != "%s") {
  213. return ErrorBuilder() << "%s must be the full argument: " << *it;
  214. }
  215. it = test_args.erase(it);
  216. for (const auto& file : test_files) {
  217. it = test_args.insert(it, file.filename);
  218. ++it;
  219. }
  220. // Back up once because the for loop will advance.
  221. --it;
  222. break;
  223. }
  224. case 't': {
  225. char* tmpdir = getenv("TEST_TMPDIR");
  226. CARBON_CHECK(tmpdir != nullptr);
  227. it->replace(percent, 2, llvm::formatv("{0}/temp_file", tmpdir));
  228. break;
  229. }
  230. default:
  231. return ErrorBuilder() << "%" << c << " is not supported: " << *it;
  232. }
  233. }
  234. return Success();
  235. }
  236. auto FileTestBase::ProcessTestFile(TestContext& context) -> ErrorOr<Success> {
  237. // Original file content, and a cursor for walking through it.
  238. llvm::StringRef file_content = context.input_content;
  239. llvm::StringRef cursor = file_content;
  240. // Whether content has been found, only updated before a file split is found
  241. // (which may be never).
  242. bool found_content_pre_split = false;
  243. // Whether either AUTOUDPATE or NOAUTOUPDATE was found.
  244. bool found_autoupdate = false;
  245. // The index in the current test file. Will be reset on splits.
  246. int line_index = 0;
  247. // The current file name, considering splits. Not set for the default file.
  248. llvm::StringRef current_file_name;
  249. // The current file's start.
  250. const char* current_file_start = nullptr;
  251. int file_number = 0;
  252. while (!cursor.empty()) {
  253. auto [line, next_cursor] = cursor.split("\n");
  254. cursor = next_cursor;
  255. auto line_trimmed = line.ltrim();
  256. static constexpr llvm::StringLiteral SplitPrefix = "// ---";
  257. if (line_trimmed.consume_front(SplitPrefix)) {
  258. if (!found_autoupdate) {
  259. // If there's a split, all output is appended at the end of each file
  260. // before AUTOUPDATE. We may want to change that, but it's not necessary
  261. // to handle right now.
  262. return ErrorBuilder()
  263. << "AUTOUPDATE/NOAUTOUPDATE setting must be in the first file.";
  264. }
  265. context.has_splits = true;
  266. ++file_number;
  267. context.non_check_lines.push_back(FileTestLine(file_number, 0, line));
  268. // On a file split, add the previous file, then start a new one.
  269. if (current_file_start) {
  270. context.test_files.push_back(TestFile(
  271. current_file_name.str(),
  272. llvm::StringRef(current_file_start, line_trimmed.begin() -
  273. current_file_start -
  274. SplitPrefix.size())));
  275. } else if (found_content_pre_split) {
  276. // For the first split, we make sure there was no content prior.
  277. return ErrorBuilder()
  278. << "When using split files, there must be no content before the "
  279. "first split file.";
  280. }
  281. current_file_name = line_trimmed.trim();
  282. current_file_start = cursor.begin();
  283. line_index = 0;
  284. continue;
  285. } else if (!current_file_start && !line_trimmed.starts_with("//") &&
  286. !line_trimmed.empty()) {
  287. found_content_pre_split = true;
  288. }
  289. ++line_index;
  290. // Process expectations when found.
  291. if (line_trimmed.consume_front("// CHECK")) {
  292. // Don't build expectations when doing an autoupdate. We don't want to
  293. // break the autoupdate on an invalid CHECK line.
  294. if (!absl::GetFlag(FLAGS_autoupdate)) {
  295. llvm::SmallVector<Matcher<std::string>>* expected = nullptr;
  296. if (line_trimmed.consume_front(":STDOUT:")) {
  297. expected = &context.expected_stdout;
  298. } else if (line_trimmed.consume_front(":STDERR:")) {
  299. expected = &context.expected_stderr;
  300. } else {
  301. return ErrorBuilder() << "Unexpected CHECK in input: " << line.str();
  302. }
  303. CARBON_ASSIGN_OR_RETURN(Matcher<std::string> check_matcher,
  304. TransformExpectation(line_index, line_trimmed));
  305. expected->push_back(check_matcher);
  306. }
  307. } else {
  308. context.non_check_lines.push_back(
  309. FileTestLine(file_number, line_index, line));
  310. if (line_trimmed.consume_front("// ARGS: ")) {
  311. if (context.test_args.empty()) {
  312. // Split the line into arguments.
  313. std::pair<llvm::StringRef, llvm::StringRef> cursor =
  314. llvm::getToken(line_trimmed);
  315. while (!cursor.first.empty()) {
  316. context.test_args.push_back(std::string(cursor.first));
  317. cursor = llvm::getToken(cursor.second);
  318. }
  319. } else {
  320. return ErrorBuilder()
  321. << "ARGS was specified multiple times: " << line.str();
  322. }
  323. } else if (line_trimmed == "// AUTOUPDATE" ||
  324. line_trimmed == "// NOAUTOUPDATE") {
  325. if (found_autoupdate) {
  326. return ErrorBuilder()
  327. << "Multiple AUTOUPDATE/NOAUTOUPDATE settings found";
  328. }
  329. found_autoupdate = true;
  330. if (line_trimmed == "// AUTOUPDATE") {
  331. context.autoupdate_line_number = line_index;
  332. }
  333. } else if (line_trimmed == "// SET-CHECK-SUBSET") {
  334. if (!context.check_subset) {
  335. context.check_subset = true;
  336. } else {
  337. return ErrorBuilder()
  338. << "SET-CHECK-SUBSET was specified multiple times";
  339. }
  340. }
  341. }
  342. }
  343. if (!found_autoupdate) {
  344. return ErrorBuilder() << "Missing AUTOUPDATE/NOAUTOUPDATE setting";
  345. }
  346. if (current_file_start) {
  347. context.test_files.push_back(
  348. TestFile(current_file_name.str(),
  349. llvm::StringRef(current_file_start,
  350. file_content.end() - current_file_start)));
  351. } else {
  352. // If no file splitting happened, use the main file as the test file.
  353. // There will always be a `/` unless tests are in the repo root.
  354. context.test_files.push_back(TestFile(
  355. test_name_.drop_front(test_name_.rfind("/") + 1).str(), file_content));
  356. }
  357. // Assume there is always a suffix `\n` in output.
  358. if (!context.expected_stdout.empty()) {
  359. context.expected_stdout.push_back(StrEq(""));
  360. }
  361. if (!context.expected_stderr.empty()) {
  362. context.expected_stderr.push_back(StrEq(""));
  363. }
  364. return Success();
  365. }
  366. auto FileTestBase::TransformExpectation(int line_index, llvm::StringRef in)
  367. -> ErrorOr<Matcher<std::string>> {
  368. if (in.empty()) {
  369. return Matcher<std::string>{StrEq("")};
  370. }
  371. if (in[0] != ' ') {
  372. return ErrorBuilder() << "Malformated CHECK line: " << in;
  373. }
  374. std::string str = in.substr(1).str();
  375. for (int pos = 0; pos < static_cast<int>(str.size());) {
  376. switch (str[pos]) {
  377. case '(':
  378. case ')':
  379. case ']':
  380. case '}':
  381. case '.':
  382. case '^':
  383. case '$':
  384. case '*':
  385. case '+':
  386. case '?':
  387. case '|':
  388. case '\\': {
  389. // Escape regex characters.
  390. str.insert(pos, "\\");
  391. pos += 2;
  392. break;
  393. }
  394. case '[': {
  395. llvm::StringRef line_keyword_cursor = llvm::StringRef(str).substr(pos);
  396. if (line_keyword_cursor.consume_front("[[")) {
  397. static constexpr llvm::StringLiteral LineKeyword = "@LINE";
  398. if (line_keyword_cursor.consume_front(LineKeyword)) {
  399. // Allow + or - here; consumeInteger handles -.
  400. line_keyword_cursor.consume_front("+");
  401. int offset;
  402. // consumeInteger returns true for errors, not false.
  403. if (line_keyword_cursor.consumeInteger(10, offset) ||
  404. !line_keyword_cursor.consume_front("]]")) {
  405. return ErrorBuilder()
  406. << "Unexpected @LINE offset at `"
  407. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  408. }
  409. std::string int_str = llvm::Twine(line_index + offset).str();
  410. int remove_len = (line_keyword_cursor.data() - str.data()) - pos;
  411. str.replace(pos, remove_len, int_str);
  412. pos += int_str.size();
  413. } else {
  414. return ErrorBuilder()
  415. << "Unexpected [[, should be {{\\[\\[}} at `"
  416. << line_keyword_cursor.substr(0, 5) << "` in: " << in;
  417. }
  418. } else {
  419. // Escape the `[`.
  420. str.insert(pos, "\\");
  421. pos += 2;
  422. }
  423. break;
  424. }
  425. case '{': {
  426. if (pos + 1 == static_cast<int>(str.size()) || str[pos + 1] != '{') {
  427. // Single `{`, escape it.
  428. str.insert(pos, "\\");
  429. pos += 2;
  430. } else {
  431. // Replace the `{{...}}` regex syntax with standard `(...)` syntax.
  432. str.replace(pos, 2, "(");
  433. for (++pos; pos < static_cast<int>(str.size() - 1); ++pos) {
  434. if (str[pos] == '}' && str[pos + 1] == '}') {
  435. str.replace(pos, 2, ")");
  436. ++pos;
  437. break;
  438. }
  439. }
  440. }
  441. break;
  442. }
  443. default: {
  444. ++pos;
  445. }
  446. }
  447. }
  448. return Matcher<std::string>{MatchesRegex(str)};
  449. }
  450. // Returns the tests to run.
  451. static auto GetTests() -> llvm::SmallVector<std::string> {
  452. // Prefer a user-specified list if present.
  453. auto specific_tests = absl::GetFlag(FLAGS_file_tests);
  454. if (!specific_tests.empty()) {
  455. return llvm::SmallVector<std::string>(specific_tests.begin(),
  456. specific_tests.end());
  457. }
  458. // Extracts tests from the target file.
  459. CARBON_CHECK(!absl::GetFlag(FLAGS_test_targets_file).empty())
  460. << "Missing --test_targets_file.";
  461. auto content = ReadFile(absl::GetFlag(FLAGS_test_targets_file));
  462. llvm::SmallVector<std::string> all_tests;
  463. for (llvm::StringRef file_ref : llvm::split(content, "\n")) {
  464. if (file_ref.empty()) {
  465. continue;
  466. }
  467. all_tests.push_back(file_ref.str());
  468. }
  469. return all_tests;
  470. }
  471. // Implements main() within the Carbon::Testing namespace for convenience.
  472. static auto Main(int argc, char** argv) -> int {
  473. absl::ParseCommandLine(argc, argv);
  474. testing::InitGoogleTest(&argc, argv);
  475. llvm::setBugReportMsg(
  476. "Please report issues to "
  477. "https://github.com/carbon-language/carbon-lang/issues and include the "
  478. "crash backtrace.\n");
  479. llvm::InitLLVM init_llvm(argc, argv);
  480. if (argc > 1) {
  481. llvm::errs() << "Unexpected arguments starting at: " << argv[1] << "\n";
  482. return EXIT_FAILURE;
  483. }
  484. llvm::SmallVector<std::string> tests = GetTests();
  485. auto test_factory = GetFileTestFactory();
  486. if (absl::GetFlag(FLAGS_autoupdate)) {
  487. for (const auto& test_name : tests) {
  488. std::unique_ptr<FileTestBase> test(test_factory.factory_fn(test_name));
  489. auto result = test->Autoupdate();
  490. llvm::errs() << (result.ok() ? (*result ? "!" : ".")
  491. : result.error().message());
  492. }
  493. llvm::errs() << "\nDone!\n";
  494. return EXIT_SUCCESS;
  495. } else {
  496. for (llvm::StringRef test_name : tests) {
  497. testing::RegisterTest(test_factory.name, test_name.data(), nullptr,
  498. test_name.data(), __FILE__, __LINE__,
  499. [&test_factory, test_name = test_name]() {
  500. return test_factory.factory_fn(test_name);
  501. });
  502. }
  503. return RUN_ALL_TESTS();
  504. }
  505. }
  506. } // namespace Carbon::Testing
  507. auto main(int argc, char** argv) -> int {
  508. return Carbon::Testing::Main(argc, argv);
  509. }