file_test.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "absl/flags/flag.h"
  5. #include "explorer/main.h"
  6. #include "re2/re2.h"
  7. #include "testing/base/test_raw_ostream.h"
  8. #include "testing/file_test/file_test_base.h"
  9. ABSL_FLAG(bool, trace, false,
  10. "Set to true to run tests with tracing enabled, even if they don't "
  11. "otherwise specify it. This does not result in checking trace output "
  12. "contents; it essentially only verifies there's not a crash bug.");
  13. namespace Carbon::Testing {
  14. namespace {
  15. class ExplorerFileTest : public FileTestBase {
  16. public:
  17. explicit ExplorerFileTest(std::filesystem::path path)
  18. : FileTestBase(std::move(path)),
  19. prelude_line_re_(R"(prelude.carbon:(\d+))"),
  20. timing_re_(R"((Time elapsed in \w+: )\d+(ms))") {
  21. CARBON_CHECK(prelude_line_re_.ok()) << prelude_line_re_.error();
  22. CARBON_CHECK(timing_re_.ok()) << timing_re_.error();
  23. }
  24. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  25. const llvm::SmallVector<TestFile>& test_files,
  26. llvm::raw_pwrite_stream& stdout, llvm::raw_pwrite_stream& stderr)
  27. -> ErrorOr<bool> override {
  28. // Create the files in-memory.
  29. llvm::vfs::InMemoryFileSystem fs;
  30. for (const auto& test_file : test_files) {
  31. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  32. llvm::MemoryBuffer::getMemBuffer(test_file.content))) {
  33. return ErrorBuilder() << "File is repeated: " << test_file.filename;
  34. }
  35. }
  36. // Add the prelude.
  37. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
  38. llvm::MemoryBuffer::getFile("explorer/data/prelude.carbon");
  39. if (prelude.getError()) {
  40. return ErrorBuilder() << prelude.getError().message();
  41. }
  42. // TODO: This path is long with a prefix / because of the path expectations
  43. // in tests. Change those to allow a shorter path (e.g., `prelude.carbon`)
  44. // here.
  45. static constexpr llvm::StringLiteral PreludePath =
  46. "/explorer/data/prelude.carbon";
  47. if (!fs.addFile(PreludePath, /*ModificationTime=*/0, std::move(*prelude))) {
  48. return ErrorBuilder() << "Duplicate prelude.carbon";
  49. }
  50. llvm::SmallVector<const char*> args = {"explorer"};
  51. for (auto arg : test_args) {
  52. args.push_back(arg.data());
  53. }
  54. int exit_code = ExplorerMain(
  55. args.size(), args.data(), /*install_path=*/"", PreludePath, stdout,
  56. stderr, check_trace_output() ? stdout : trace_stream_, fs);
  57. return exit_code == EXIT_SUCCESS;
  58. }
  59. auto ValidateRun(const llvm::SmallVector<TestFile>& /*test_files*/)
  60. -> void override {
  61. // Skip trace test check as they use stdout stream instead of
  62. // trace_stream_ostream
  63. if (absl::GetFlag(FLAGS_trace)) {
  64. EXPECT_FALSE(trace_stream_.TakeStr().empty())
  65. << "Tracing should always do something";
  66. }
  67. }
  68. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  69. llvm::SmallVector<std::string> args;
  70. if (absl::GetFlag(FLAGS_trace)) {
  71. args.push_back("--trace_file=-");
  72. args.push_back("--trace_phase=all");
  73. }
  74. args.push_back("%s");
  75. return args;
  76. }
  77. auto GetLineNumberReplacement(llvm::ArrayRef<llvm::StringRef> filenames)
  78. -> LineNumberReplacement override {
  79. if (check_trace_output()) {
  80. return {.has_file = false,
  81. .pattern = R"((DO NOT MATCH))",
  82. // The `{{{{` becomes `{{`.
  83. .line_formatv = "{{{{ *}}{0}"};
  84. }
  85. return FileTestBase::GetLineNumberReplacement(filenames);
  86. }
  87. auto DoExtraCheckReplacements(std::string& check_line) -> void override {
  88. // Ignore the resulting column of EndOfFile because it's often the end of
  89. // the CHECK comment.
  90. RE2::GlobalReplace(&check_line, prelude_line_re_,
  91. R"(prelude.carbon:{{\\d+}})");
  92. if (check_trace_output()) {
  93. // Replace timings in trace output.
  94. RE2::GlobalReplace(&check_line, timing_re_, R"(\1{{\\d+}}\2)");
  95. }
  96. }
  97. private:
  98. // Trace output is directly checked for a few tests.
  99. auto check_trace_output() -> bool {
  100. return path().string().find("/trace/") != std::string::npos;
  101. }
  102. TestRawOstream trace_stream_;
  103. RE2 prelude_line_re_;
  104. RE2 timing_re_;
  105. };
  106. } // namespace
  107. CARBON_FILE_TEST_FACTORY(ExplorerFileTest);
  108. } // namespace Carbon::Testing