driver_test.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "toolchain/diagnostics/diagnostic_emitter.h"
  12. namespace Carbon::Testing {
  13. namespace {
  14. using ::testing::ElementsAre;
  15. using ::testing::HasSubstr;
  16. using ::testing::StrEq;
  17. /// A raw_ostream that makes it easy to repeatedly check streamed output.
  18. class RawTestOstream : public llvm::raw_ostream {
  19. public:
  20. ~RawTestOstream() override {
  21. flush();
  22. if (!buffer_.empty()) {
  23. ADD_FAILURE() << "Unchecked output:\n" << buffer_;
  24. }
  25. }
  26. /// Flushes the stream and returns the contents so far, clearing the stream
  27. /// back to empty.
  28. auto TakeStr() -> std::string {
  29. flush();
  30. std::string result = std::move(buffer_);
  31. buffer_.clear();
  32. return result;
  33. }
  34. private:
  35. void write_impl(const char* ptr, size_t size) override {
  36. buffer_.append(ptr, ptr + size);
  37. }
  38. [[nodiscard]] auto current_pos() const -> uint64_t override {
  39. return buffer_.size();
  40. }
  41. std::string buffer_;
  42. };
  43. TEST(DriverTest, FullCommandErrors) {
  44. RawTestOstream test_output_stream;
  45. RawTestOstream test_error_stream;
  46. Driver driver = Driver(test_output_stream, test_error_stream);
  47. EXPECT_FALSE(driver.RunFullCommand({}));
  48. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  49. EXPECT_FALSE(driver.RunFullCommand({"foo"}));
  50. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  51. EXPECT_FALSE(driver.RunFullCommand({"foo --bar --baz"}));
  52. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  53. }
  54. TEST(DriverTest, Help) {
  55. RawTestOstream test_output_stream;
  56. RawTestOstream test_error_stream;
  57. Driver driver = Driver(test_output_stream, test_error_stream);
  58. EXPECT_TRUE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {}));
  59. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  60. auto help_text = test_output_stream.TakeStr();
  61. // Help text should mention each subcommand.
  62. #define CARBON_SUBCOMMAND(Name, Spelling, ...) \
  63. EXPECT_THAT(help_text, HasSubstr(Spelling));
  64. #include "toolchain/driver/flags.def"
  65. // Check that the subcommand dispatch works.
  66. EXPECT_TRUE(driver.RunFullCommand({"help"}));
  67. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  68. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(help_text));
  69. }
  70. TEST(DriverTest, HelpErrors) {
  71. RawTestOstream test_output_stream;
  72. RawTestOstream test_error_stream;
  73. Driver driver = Driver(test_output_stream, test_error_stream);
  74. EXPECT_FALSE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"foo"}));
  75. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  76. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  77. EXPECT_FALSE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"help"}));
  78. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  79. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  80. EXPECT_FALSE(
  81. driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"--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.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  104. {"tokens", test_file_path}));
  105. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  106. auto tokenized_text = test_output_stream.TakeStr();
  107. EXPECT_THAT(Yaml::Value::FromText(tokenized_text),
  108. ElementsAre(Yaml::SequenceValue{
  109. Yaml::MappingValue{{"index", "0"},
  110. {"kind", "Identifier"},
  111. {"line", "1"},
  112. {"column", "1"},
  113. {"indent", "1"},
  114. {"spelling", "Hello"},
  115. {"identifier", "0"},
  116. {"has_trailing_space", "true"}},
  117. Yaml::MappingValue{{"index", "1"},
  118. {"kind", "Identifier"},
  119. {"line", "1"},
  120. {"column", "7"},
  121. {"indent", "1"},
  122. {"spelling", "World"},
  123. {"identifier", "1"},
  124. {"has_trailing_space", "true"}},
  125. Yaml::MappingValue{{"index", "2"},
  126. {"kind", "EndOfFile"},
  127. {"line", "1"},
  128. {"column", "12"},
  129. {"indent", "1"},
  130. {"spelling", ""}}}));
  131. // Check that the subcommand dispatch works.
  132. EXPECT_TRUE(driver.RunFullCommand({"dump", "tokens", test_file_path}));
  133. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  134. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(tokenized_text));
  135. }
  136. TEST(DriverTest, DumpErrors) {
  137. RawTestOstream test_output_stream;
  138. RawTestOstream test_error_stream;
  139. Driver driver = Driver(test_output_stream, test_error_stream);
  140. EXPECT_FALSE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"foo"}));
  141. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  142. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  143. EXPECT_FALSE(
  144. driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"--xyz"}));
  145. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  146. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  147. EXPECT_FALSE(
  148. driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"tokens"}));
  149. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  150. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  151. EXPECT_FALSE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  152. {"tokens", "/not/a/real/file/name"}));
  153. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  154. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  155. }
  156. TEST(DriverTest, DumpParseTree) {
  157. RawTestOstream test_output_stream;
  158. RawTestOstream test_error_stream;
  159. Driver driver = Driver(test_output_stream, test_error_stream);
  160. auto test_file_path = CreateTestFile("var v: Int = 42;");
  161. EXPECT_TRUE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  162. {"parse-tree", test_file_path}));
  163. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  164. // Verify there is output without examining it.
  165. EXPECT_FALSE(test_output_stream.TakeStr().empty());
  166. // Check that the subcommand dispatch works.
  167. EXPECT_TRUE(driver.RunFullCommand({"dump", "parse-tree", test_file_path}));
  168. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  169. // Verify there is output without examining it.
  170. EXPECT_FALSE(test_output_stream.TakeStr().empty());
  171. }
  172. } // namespace
  173. } // namespace Carbon::Testing