file_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "testing/file_test/file_test_base.h"
  7. #include "testing/util/test_raw_ostream.h"
  8. ABSL_FLAG(bool, trace, false,
  9. "Set to true to run tests with tracing enabled, even if they don't "
  10. "otherwise specify it. This does not result in checking trace output "
  11. "contents; it essentially only verifies there's not a crash bug.");
  12. namespace Carbon::Testing {
  13. namespace {
  14. class ExplorerFileTest : public FileTestBase {
  15. public:
  16. using FileTestBase::FileTestBase;
  17. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  18. const llvm::SmallVector<TestFile>& test_files,
  19. llvm::raw_pwrite_stream& stdout, llvm::raw_pwrite_stream& stderr)
  20. -> ErrorOr<bool> override {
  21. // Create the files in-memory.
  22. llvm::vfs::InMemoryFileSystem fs(new llvm::vfs::InMemoryFileSystem());
  23. for (const auto& test_file : test_files) {
  24. if (!fs.addFile(test_file.filename, /*ModificationTime=*/0,
  25. llvm::MemoryBuffer::getMemBuffer(test_file.content))) {
  26. return ErrorBuilder() << "File is repeated: " << test_file.filename;
  27. }
  28. }
  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, std::move(*prelude))) {
  41. return ErrorBuilder() << "Duplicate prelude.carbon";
  42. }
  43. llvm::SmallVector<const char*> args = {"explorer"};
  44. for (auto arg : test_args) {
  45. args.push_back(arg.data());
  46. }
  47. // Trace output is only checked for a few tests.
  48. bool check_trace_output =
  49. path().string().find("trace_testdata/") != std::string::npos;
  50. int exit_code = ExplorerMain(
  51. args.size(), args.data(), /*install_path=*/"", PreludePath, stdout,
  52. stderr, check_trace_output ? stdout : trace_stream_, fs);
  53. return exit_code == EXIT_SUCCESS;
  54. }
  55. auto ValidateRun(const llvm::SmallVector<TestFile>& /*test_files*/)
  56. -> void override {
  57. // Skip trace test check as they use stdout stream instead of
  58. // trace_stream_ostream
  59. if (absl::GetFlag(FLAGS_trace)) {
  60. EXPECT_FALSE(trace_stream_.TakeStr().empty())
  61. << "Tracing should always do something";
  62. }
  63. }
  64. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  65. llvm::SmallVector<std::string> args;
  66. if (absl::GetFlag(FLAGS_trace)) {
  67. args.push_back("--trace_file=-");
  68. args.push_back("--trace_phase=all");
  69. }
  70. args.push_back("%s");
  71. return args;
  72. }
  73. TestRawOstream trace_stream_;
  74. };
  75. } // namespace
  76. CARBON_FILE_TEST_FACTORY(ExplorerFileTest);
  77. } // namespace Carbon::Testing