file_test_base.cpp 20 KB

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