file_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "absl/strings/str_split.h"
  6. #include "common/raw_string_ostream.h"
  7. #include "explorer/main.h"
  8. #include "re2/re2.h"
  9. #include "testing/base/file_helpers.h"
  10. #include "testing/file_test/file_test_base.h"
  11. #include "testing/file_test/manifest.h"
  12. ABSL_FLAG(bool, trace, false,
  13. "Set to true to run tests with tracing enabled, even if they don't "
  14. "otherwise specify it. This does not result in checking trace output "
  15. "contents; it essentially only verifies there's not a crash bug.");
  16. ABSL_FLAG(std::string, explorer_test_targets_file, "",
  17. "A path to a file containing repo-relative names of test files.");
  18. namespace Carbon::Testing {
  19. namespace {
  20. class ExplorerFileTest : public FileTestBase {
  21. public:
  22. explicit ExplorerFileTest(llvm::StringRef /*exe_path*/,
  23. llvm::StringRef test_name)
  24. : FileTestBase(test_name),
  25. prelude_line_re_(R"(prelude.carbon:(\d+))"),
  26. timing_re_(R"((Time elapsed in \w+: )\d+(ms))") {
  27. CARBON_CHECK(prelude_line_re_.ok(), "{0}", prelude_line_re_.error());
  28. CARBON_CHECK(timing_re_.ok(), "{0}", timing_re_.error());
  29. }
  30. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  31. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& fs,
  32. FILE* /*input_stream*/, llvm::raw_pwrite_stream& output_stream,
  33. llvm::raw_pwrite_stream& error_stream) const
  34. -> ErrorOr<RunResult> override {
  35. // Add the prelude.
  36. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> prelude =
  37. llvm::MemoryBuffer::getFile("explorer/data/prelude.carbon");
  38. if (prelude.getError()) {
  39. return ErrorBuilder() << prelude.getError().message();
  40. }
  41. // TODO: This path is long with a prefix / because of the path expectations
  42. // in tests. Change those to allow a shorter path (e.g., `prelude.carbon`)
  43. // here.
  44. static constexpr llvm::StringLiteral PreludePath =
  45. "/explorer/data/prelude.carbon";
  46. if (!fs->addFile(PreludePath, /*ModificationTime=*/0,
  47. 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. RawStringOstream trace_stream;
  55. int exit_code =
  56. ExplorerMain(args.size(), args.data(), /*install_path=*/"", PreludePath,
  57. output_stream, error_stream,
  58. check_trace_output() ? output_stream : trace_stream, *fs);
  59. // Skip trace test check as they use stdout stream instead of
  60. // trace_stream_ostream
  61. if (absl::GetFlag(FLAGS_trace) && trace_stream.TakeStr().empty()) {
  62. return Error("Tracing should always do something");
  63. }
  64. return {{.success = exit_code == EXIT_SUCCESS}};
  65. }
  66. auto GetDefaultArgs() const -> llvm::SmallVector<std::string> override {
  67. llvm::SmallVector<std::string> args;
  68. if (absl::GetFlag(FLAGS_trace)) {
  69. args.push_back("--trace_file=-");
  70. args.push_back("--trace_phase=all");
  71. }
  72. args.push_back("%s");
  73. return args;
  74. }
  75. auto GetLineNumberReplacements(llvm::ArrayRef<llvm::StringRef> filenames)
  76. const -> llvm::SmallVector<LineNumberReplacement> override {
  77. if (check_trace_output()) {
  78. return {};
  79. }
  80. return FileTestBase::GetLineNumberReplacements(filenames);
  81. }
  82. auto DoExtraCheckReplacements(std::string& check_line) const
  83. -> void override {
  84. // Ignore the resulting column of EndOfFile because it's often the end of
  85. // the CHECK comment.
  86. RE2::GlobalReplace(&check_line, prelude_line_re_,
  87. R"(prelude.carbon:{{\\d+}})");
  88. if (check_trace_output()) {
  89. // Replace timings in trace output.
  90. RE2::GlobalReplace(&check_line, timing_re_, R"(\1{{\\d+}}\2)");
  91. }
  92. }
  93. // Cannot execute in parallel.
  94. auto AllowParallelRun() const -> bool override { return false; }
  95. private:
  96. // Trace output is directly checked for a few tests.
  97. auto check_trace_output() const -> bool {
  98. return test_name().find("/trace/") != std::string::npos;
  99. }
  100. RE2 prelude_line_re_;
  101. RE2 timing_re_;
  102. };
  103. } // namespace
  104. // Explorer uses a non-standard approach to getting the manifest path.
  105. auto GetFileTestManifest() -> llvm::SmallVector<std::string> {
  106. llvm::SmallVector<std::string> manifest;
  107. auto content = ReadFile(absl::GetFlag(FLAGS_explorer_test_targets_file));
  108. for (const auto& line : absl::StrSplit(*content, "\n", absl::SkipEmpty())) {
  109. manifest.push_back(std::string(line));
  110. }
  111. return manifest;
  112. }
  113. CARBON_FILE_TEST_FACTORY(ExplorerFileTest)
  114. } // namespace Carbon::Testing