file_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "common/raw_string_ostream.h"
  6. #include "explorer/main.h"
  7. #include "re2/re2.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(llvm::StringRef /*exe_path*/,
  18. std::mutex* output_mutex, llvm::StringRef test_name)
  19. : FileTestBase(output_mutex, test_name),
  20. prelude_line_re_(R"(prelude.carbon:(\d+))"),
  21. timing_re_(R"((Time elapsed in \w+: )\d+(ms))") {
  22. CARBON_CHECK(prelude_line_re_.ok(), "{0}", prelude_line_re_.error());
  23. CARBON_CHECK(timing_re_.ok(), "{0}", timing_re_.error());
  24. }
  25. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  26. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& fs,
  27. FILE* /*input_stream*/, llvm::raw_pwrite_stream& output_stream,
  28. llvm::raw_pwrite_stream& error_stream)
  29. -> ErrorOr<RunResult> override {
  30. // Add the prelude.
  31. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
  32. llvm::MemoryBuffer::getFile("explorer/data/prelude.carbon");
  33. if (prelude.getError()) {
  34. return ErrorBuilder() << prelude.getError().message();
  35. }
  36. // TODO: This path is long with a prefix / because of the path expectations
  37. // in tests. Change those to allow a shorter path (e.g., `prelude.carbon`)
  38. // here.
  39. static constexpr llvm::StringLiteral PreludePath =
  40. "/explorer/data/prelude.carbon";
  41. if (!fs->addFile(PreludePath, /*ModificationTime=*/0,
  42. std::move(*prelude))) {
  43. return ErrorBuilder() << "Duplicate prelude.carbon";
  44. }
  45. llvm::SmallVector<const char*> args = {"explorer"};
  46. for (auto arg : test_args) {
  47. args.push_back(arg.data());
  48. }
  49. int exit_code =
  50. ExplorerMain(args.size(), args.data(), /*install_path=*/"", PreludePath,
  51. output_stream, error_stream,
  52. check_trace_output() ? output_stream : trace_stream_, *fs);
  53. return {{.success = exit_code == EXIT_SUCCESS}};
  54. }
  55. auto ValidateRun() -> void override {
  56. // Skip trace test check as they use stdout stream instead of
  57. // trace_stream_ostream
  58. if (absl::GetFlag(FLAGS_trace)) {
  59. EXPECT_FALSE(trace_stream_.TakeStr().empty())
  60. << "Tracing should always do something";
  61. }
  62. }
  63. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  64. llvm::SmallVector<std::string> args;
  65. if (absl::GetFlag(FLAGS_trace)) {
  66. args.push_back("--trace_file=-");
  67. args.push_back("--trace_phase=all");
  68. }
  69. args.push_back("%s");
  70. return args;
  71. }
  72. auto GetLineNumberReplacements(llvm::ArrayRef<llvm::StringRef> filenames)
  73. -> llvm::SmallVector<LineNumberReplacement> override {
  74. if (check_trace_output()) {
  75. return {};
  76. }
  77. return FileTestBase::GetLineNumberReplacements(filenames);
  78. }
  79. auto DoExtraCheckReplacements(std::string& check_line) -> void override {
  80. // Ignore the resulting column of EndOfFile because it's often the end of
  81. // the CHECK comment.
  82. RE2::GlobalReplace(&check_line, prelude_line_re_,
  83. R"(prelude.carbon:{{\\d+}})");
  84. if (check_trace_output()) {
  85. // Replace timings in trace output.
  86. RE2::GlobalReplace(&check_line, timing_re_, R"(\1{{\\d+}}\2)");
  87. }
  88. }
  89. private:
  90. // Trace output is directly checked for a few tests.
  91. auto check_trace_output() -> bool {
  92. return test_name().find("/trace/") != std::string::npos;
  93. }
  94. RawStringOstream trace_stream_;
  95. RE2 prelude_line_re_;
  96. RE2 timing_re_;
  97. };
  98. } // namespace
  99. CARBON_FILE_TEST_FACTORY(ExplorerFileTest)
  100. } // namespace Carbon::Testing