driver_test.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "common/test_raw_ostream.h"
  8. #include "llvm/ADT/SmallString.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/SourceMgr.h"
  11. #include "toolchain/common/yaml_test_helpers.h"
  12. #include "toolchain/diagnostics/diagnostic_emitter.h"
  13. namespace Carbon::Testing {
  14. namespace {
  15. using ::testing::ElementsAre;
  16. using ::testing::HasSubstr;
  17. using ::testing::StrEq;
  18. TEST(DriverTest, FullCommandErrors) {
  19. TestRawOstream test_output_stream;
  20. TestRawOstream test_error_stream;
  21. Driver driver = Driver(test_output_stream, test_error_stream);
  22. EXPECT_FALSE(driver.RunFullCommand({}));
  23. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  24. EXPECT_FALSE(driver.RunFullCommand({"foo"}));
  25. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  26. EXPECT_FALSE(driver.RunFullCommand({"foo --bar --baz"}));
  27. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  28. }
  29. TEST(DriverTest, Help) {
  30. TestRawOstream test_output_stream;
  31. TestRawOstream test_error_stream;
  32. Driver driver = Driver(test_output_stream, test_error_stream);
  33. EXPECT_TRUE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {}));
  34. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  35. auto help_text = test_output_stream.TakeStr();
  36. // Help text should mention each subcommand.
  37. #define CARBON_SUBCOMMAND(Name, Spelling, ...) \
  38. EXPECT_THAT(help_text, HasSubstr(Spelling));
  39. #include "toolchain/driver/flags.def"
  40. // Check that the subcommand dispatch works.
  41. EXPECT_TRUE(driver.RunFullCommand({"help"}));
  42. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  43. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(help_text));
  44. }
  45. TEST(DriverTest, HelpErrors) {
  46. TestRawOstream test_output_stream;
  47. TestRawOstream test_error_stream;
  48. Driver driver = Driver(test_output_stream, test_error_stream);
  49. EXPECT_FALSE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"foo"}));
  50. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  51. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  52. EXPECT_FALSE(driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"help"}));
  53. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  54. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  55. EXPECT_FALSE(
  56. driver.RunHelpSubcommand(ConsoleDiagnosticConsumer(), {"--xyz"}));
  57. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  58. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  59. }
  60. auto CreateTestFile(llvm::StringRef text) -> std::string {
  61. int fd = -1;
  62. llvm::SmallString<1024> path;
  63. auto ec = llvm::sys::fs::createTemporaryFile("test_file", ".txt", fd, path);
  64. if (ec) {
  65. llvm::report_fatal_error(llvm::Twine("Failed to create temporary file: ") +
  66. ec.message());
  67. }
  68. llvm::raw_fd_ostream s(fd, /*shouldClose=*/true);
  69. s << text;
  70. s.close();
  71. return path.str().str();
  72. }
  73. TEST(DriverTest, DumpTokens) {
  74. TestRawOstream test_output_stream;
  75. TestRawOstream test_error_stream;
  76. Driver driver = Driver(test_output_stream, test_error_stream);
  77. auto test_file_path = CreateTestFile("Hello World");
  78. EXPECT_TRUE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  79. {"tokens", test_file_path}));
  80. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  81. auto tokenized_text = test_output_stream.TakeStr();
  82. EXPECT_THAT(Yaml::Value::FromText(tokenized_text),
  83. ElementsAre(Yaml::SequenceValue{
  84. Yaml::MappingValue{{"index", "0"},
  85. {"kind", "Identifier"},
  86. {"line", "1"},
  87. {"column", "1"},
  88. {"indent", "1"},
  89. {"spelling", "Hello"},
  90. {"identifier", "0"},
  91. {"has_trailing_space", "true"}},
  92. Yaml::MappingValue{{"index", "1"},
  93. {"kind", "Identifier"},
  94. {"line", "1"},
  95. {"column", "7"},
  96. {"indent", "1"},
  97. {"spelling", "World"},
  98. {"identifier", "1"},
  99. {"has_trailing_space", "true"}},
  100. Yaml::MappingValue{{"index", "2"},
  101. {"kind", "EndOfFile"},
  102. {"line", "1"},
  103. {"column", "12"},
  104. {"indent", "1"},
  105. {"spelling", ""}}}));
  106. // Check that the subcommand dispatch works.
  107. EXPECT_TRUE(driver.RunFullCommand({"dump", "tokens", test_file_path}));
  108. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  109. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(tokenized_text));
  110. }
  111. TEST(DriverTest, DumpErrors) {
  112. TestRawOstream test_output_stream;
  113. TestRawOstream test_error_stream;
  114. Driver driver = Driver(test_output_stream, test_error_stream);
  115. EXPECT_FALSE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"foo"}));
  116. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  117. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  118. EXPECT_FALSE(
  119. driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"--xyz"}));
  120. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  121. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  122. EXPECT_FALSE(
  123. driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(), {"tokens"}));
  124. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  125. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  126. EXPECT_FALSE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  127. {"tokens", "/not/a/real/file/name"}));
  128. EXPECT_THAT(test_output_stream.TakeStr(), StrEq(""));
  129. EXPECT_THAT(test_error_stream.TakeStr(), HasSubstr("ERROR"));
  130. }
  131. TEST(DriverTest, DumpParseTree) {
  132. TestRawOstream test_output_stream;
  133. TestRawOstream test_error_stream;
  134. Driver driver = Driver(test_output_stream, test_error_stream);
  135. auto test_file_path = CreateTestFile("var v: Int = 42;");
  136. EXPECT_TRUE(driver.RunDumpSubcommand(ConsoleDiagnosticConsumer(),
  137. {"parse-tree", test_file_path}));
  138. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  139. // Verify there is output without examining it.
  140. EXPECT_FALSE(test_output_stream.TakeStr().empty());
  141. // Check that the subcommand dispatch works.
  142. EXPECT_TRUE(driver.RunFullCommand({"dump", "parse-tree", test_file_path}));
  143. EXPECT_THAT(test_error_stream.TakeStr(), StrEq(""));
  144. // Verify there is output without examining it.
  145. EXPECT_FALSE(test_output_stream.TakeStr().empty());
  146. }
  147. } // namespace
  148. } // namespace Carbon::Testing