file_test.cpp 4.0 KB

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