file_test.cpp 3.9 KB

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