driver_test.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "toolchain/driver/driver.h"
  5. #include "gmock/gmock.h"
  6. #include "gtest/gtest.h"
  7. #include "llvm/ADT/SmallString.h"
  8. #include "llvm/Support/FileSystem.h"
  9. #include "llvm/Support/SourceMgr.h"
  10. #include "toolchain/common/yaml_test_helpers.h"
  11. namespace Carbon {
  12. namespace {
  13. using ::testing::ElementsAre;
  14. using ::testing::Eq;
  15. using ::testing::HasSubstr;
  16. using ::testing::NotNull;
  17. using ::testing::StrEq;
  18. namespace Yaml = Carbon::Testing::Yaml;
  19. /// A raw_ostream that makes it easy to repeatedly check streamed output.
  20. class RawTestOstream : public llvm::raw_ostream {
  21. std::string buffer;
  22. void write_impl(const char* ptr, size_t size) override {
  23. buffer.append(ptr, ptr + size);
  24. }
  25. [[nodiscard]] auto current_pos() const -> uint64_t override {
  26. return buffer.size();
  27. }
  28. public:
  29. ~RawTestOstream() override {
  30. flush();
  31. if (!buffer.empty()) {
  32. ADD_FAILURE() << "Unchecked output:\n" << buffer;
  33. }
  34. }
  35. /// Flushes the stream and returns the contents so far, clearing the stream
  36. /// back to empty.
  37. auto TakeStr() -> std::string {
  38. flush();
  39. std::string result = std::move(buffer);
  40. buffer.clear();
  41. return result;
  42. }
  43. };
  44. TEST(DriverTest, FullCommandErrors) {
  45. RawTestOstream test_output_stream;
  46. RawTestOstream test_error_stream;
  47. Driver driver = Driver(test_output_stream, test_error_stream);
  48. EXPECT_FALSE(driver.RunFullCommand({}));
  49. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  50. EXPECT_FALSE(driver.RunFullCommand({"foo"}));
  51. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  52. EXPECT_FALSE(driver.RunFullCommand({"foo --bar --baz"}));
  53. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  54. }
  55. TEST(DriverTest, Help) {
  56. RawTestOstream test_output_stream;
  57. RawTestOstream test_error_stream;
  58. Driver driver = Driver(test_output_stream, test_error_stream);
  59. EXPECT_TRUE(driver.RunHelpSubcommand({}));
  60. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  61. auto help_text = test_output_stream.TakeStr();
  62. // Help text should mention each subcommand.
  63. #define CARBON_SUBCOMMAND(Name, Spelling, ...) \
  64. EXPECT_THAT(help_text, HasSubstr(Spelling));
  65. #include "toolchain/driver/flags.def"
  66. // Check that the subcommand dispatch works.
  67. EXPECT_TRUE(driver.RunFullCommand({"help"}));
  68. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  69. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(help_text));
  70. }
  71. TEST(DriverTest, HelpErrors) {
  72. RawTestOstream test_output_stream;
  73. RawTestOstream test_error_stream;
  74. Driver driver = Driver(test_output_stream, test_error_stream);
  75. EXPECT_FALSE(driver.RunHelpSubcommand({"foo"}));
  76. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  77. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  78. EXPECT_FALSE(driver.RunHelpSubcommand({"help"}));
  79. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  80. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  81. EXPECT_FALSE(driver.RunHelpSubcommand({"--xyz"}));
  82. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  83. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  84. }
  85. auto CreateTestFile(llvm::StringRef text) -> std::string {
  86. int fd = -1;
  87. llvm::SmallString<1024> path;
  88. auto ec = llvm::sys::fs::createTemporaryFile("test_file", ".txt", fd, path);
  89. if (ec) {
  90. llvm::report_fatal_error(llvm::Twine("Failed to create temporary file: ") +
  91. ec.message());
  92. }
  93. llvm::raw_fd_ostream s(fd, /*shouldClose=*/true);
  94. s << text;
  95. s.close();
  96. return path.str().str();
  97. }
  98. TEST(DriverTest, DumpTokens) {
  99. RawTestOstream test_output_stream;
  100. RawTestOstream test_error_stream;
  101. Driver driver = Driver(test_output_stream, test_error_stream);
  102. auto test_file_path = CreateTestFile("Hello World");
  103. EXPECT_TRUE(driver.RunDumpTokensSubcommand({test_file_path}));
  104. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  105. auto tokenized_text = test_output_stream.TakeStr();
  106. EXPECT_THAT(Yaml::Value::FromText(tokenized_text),
  107. ElementsAre(Yaml::MappingValue{
  108. {"token", Yaml::MappingValue{{"index", "0"},
  109. {"kind", "Identifier"},
  110. {"line", "1"},
  111. {"column", "1"},
  112. {"indent", "1"},
  113. {"spelling", "Hello"},
  114. {"identifier", "0"},
  115. {"has_trailing_space", "true"}}},
  116. {"token", Yaml::MappingValue{{"index", "1"},
  117. {"kind", "Identifier"},
  118. {"line", "1"},
  119. {"column", "7"},
  120. {"indent", "1"},
  121. {"spelling", "World"},
  122. {"identifier", "1"},
  123. {"has_trailing_space", "true"}}},
  124. {"token", Yaml::MappingValue{{"index", "2"},
  125. {"kind", "EndOfFile"},
  126. {"line", "1"},
  127. {"column", "12"},
  128. {"indent", "1"},
  129. {"spelling", ""}}}}));
  130. // Check that the subcommand dispatch works.
  131. EXPECT_TRUE(driver.RunFullCommand({"dump-tokens", test_file_path}));
  132. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  133. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(tokenized_text));
  134. }
  135. TEST(DriverTest, DumpTokenErrors) {
  136. RawTestOstream test_output_stream;
  137. RawTestOstream test_error_stream;
  138. Driver driver = Driver(test_output_stream, test_error_stream);
  139. EXPECT_FALSE(driver.RunDumpTokensSubcommand({}));
  140. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  141. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  142. EXPECT_FALSE(driver.RunDumpTokensSubcommand({"--xyz"}));
  143. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  144. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  145. EXPECT_FALSE(driver.RunDumpTokensSubcommand({"/not/a/real/file/name"}));
  146. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  147. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  148. }
  149. } // namespace
  150. } // namespace Carbon