file_test.cpp 3.8 KB

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