driver_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <filesystem>
  8. #include <fstream>
  9. #include <utility>
  10. #include "llvm/ADT/ScopeExit.h"
  11. #include "llvm/Object/Binary.h"
  12. #include "llvm/Support/FormatVariadic.h"
  13. #include "testing/base/test_raw_ostream.h"
  14. #include "toolchain/base/yaml_test_helpers.h"
  15. namespace Carbon::Testing {
  16. namespace {
  17. using ::testing::ContainsRegex;
  18. using ::testing::ElementsAre;
  19. using ::testing::HasSubstr;
  20. using ::testing::StrEq;
  21. // Reads a file to string.
  22. // TODO: Extract this to a helper and share it with other tests.
  23. static auto ReadFile(std::filesystem::path path) -> std::string {
  24. std::ifstream proto_file(path);
  25. std::stringstream buffer;
  26. buffer << proto_file.rdbuf();
  27. proto_file.close();
  28. return buffer.str();
  29. }
  30. class DriverTest : public testing::Test {
  31. protected:
  32. DriverTest() : driver_(fs_, test_output_stream_, test_error_stream_) {
  33. char* tmpdir_env = getenv("TEST_TMPDIR");
  34. CARBON_CHECK(tmpdir_env != nullptr);
  35. test_tmpdir_ = tmpdir_env;
  36. }
  37. auto CreateTestFile(llvm::StringRef text,
  38. llvm::StringRef file_name = "test_file.carbon")
  39. -> llvm::StringRef {
  40. fs_.addFile(file_name, /*ModificationTime=*/0,
  41. llvm::MemoryBuffer::getMemBuffer(text));
  42. return file_name;
  43. }
  44. // Makes a temp directory and changes the working directory to it. Returns an
  45. // LLVM `scope_exit` that will restore the working directory and remove the
  46. // temporary directory (and everything it contains) when destroyed.
  47. auto ScopedTempWorkingDir() {
  48. // Save our current working directory.
  49. std::error_code ec;
  50. auto original_dir = std::filesystem::current_path(ec);
  51. CARBON_CHECK(!ec) << ec.message();
  52. const auto* unit_test = ::testing::UnitTest::GetInstance();
  53. const auto* test_info = unit_test->current_test_info();
  54. std::filesystem::path test_dir = test_tmpdir_.append(
  55. llvm::formatv("{0}_{1}", test_info->test_suite_name(),
  56. test_info->name())
  57. .str());
  58. std::filesystem::create_directory(test_dir, ec);
  59. CARBON_CHECK(!ec) << "Could not create test working dir '" << test_dir
  60. << "': " << ec.message();
  61. std::filesystem::current_path(test_dir, ec);
  62. CARBON_CHECK(!ec) << "Could not change the current working dir to '"
  63. << test_dir << "': " << ec.message();
  64. return llvm::make_scope_exit([original_dir, test_dir] {
  65. std::error_code ec;
  66. std::filesystem::current_path(original_dir, ec);
  67. CARBON_CHECK(!ec) << "Could not change the current working dir to '"
  68. << original_dir << "': " << ec.message();
  69. std::filesystem::remove_all(test_dir, ec);
  70. CARBON_CHECK(!ec) << "Could not remove the test working dir '" << test_dir
  71. << "': " << ec.message();
  72. });
  73. }
  74. llvm::vfs::InMemoryFileSystem fs_;
  75. TestRawOstream test_output_stream_;
  76. TestRawOstream test_error_stream_;
  77. // Some tests work directly with files in the test temporary directory.
  78. std::filesystem::path test_tmpdir_;
  79. Driver driver_;
  80. };
  81. TEST_F(DriverTest, BadCommandErrors) {
  82. EXPECT_FALSE(driver_.RunCommand({}));
  83. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
  84. EXPECT_FALSE(driver_.RunCommand({"foo"}));
  85. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
  86. EXPECT_FALSE(driver_.RunCommand({"foo --bar --baz"}));
  87. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("ERROR"));
  88. }
  89. TEST_F(DriverTest, CompileCommandErrors) {
  90. // No input file. This error message is important so check all of it.
  91. EXPECT_FALSE(driver_.RunCommand({"compile"}));
  92. EXPECT_THAT(
  93. test_error_stream_.TakeStr(),
  94. StrEq("ERROR: Not all required positional arguments were provided. First "
  95. "missing and required positional argument: 'FILE'\n"));
  96. // Invalid output filename. No reliably error message here.
  97. // TODO: Likely want a different filename on Windows.
  98. auto empty_file = CreateTestFile("");
  99. EXPECT_FALSE(
  100. driver_.RunCommand({"compile", "--output=/dev/empty", empty_file}));
  101. EXPECT_THAT(test_error_stream_.TakeStr(),
  102. ContainsRegex("ERROR: .*/dev/empty.*"));
  103. }
  104. TEST_F(DriverTest, DumpTokens) {
  105. auto file = CreateTestFile("Hello World");
  106. EXPECT_TRUE(
  107. driver_.RunCommand({"compile", "--phase=lex", "--dump-tokens", file}));
  108. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  109. auto tokenized_text = test_output_stream_.TakeStr();
  110. EXPECT_THAT(Yaml::Value::FromText(tokenized_text),
  111. ElementsAre(Yaml::SequenceValue{
  112. Yaml::MappingValue{{"index", "0"},
  113. {"kind", "Identifier"},
  114. {"line", "1"},
  115. {"column", "1"},
  116. {"indent", "1"},
  117. {"spelling", "Hello"},
  118. {"identifier", "0"},
  119. {"has_trailing_space", "true"}},
  120. Yaml::MappingValue{{"index", "1"},
  121. {"kind", "Identifier"},
  122. {"line", "1"},
  123. {"column", "7"},
  124. {"indent", "1"},
  125. {"spelling", "World"},
  126. {"identifier", "1"},
  127. {"has_trailing_space", "true"}},
  128. Yaml::MappingValue{{"index", "2"},
  129. {"kind", "EndOfFile"},
  130. {"line", "1"},
  131. {"column", "12"},
  132. {"indent", "1"},
  133. {"spelling", ""}}}));
  134. }
  135. TEST_F(DriverTest, DumpParseTree) {
  136. auto file = CreateTestFile("var v: i32 = 42;");
  137. EXPECT_TRUE(driver_.RunCommand(
  138. {"compile", "--phase=parse", "--dump-parse-tree", file}));
  139. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  140. // Verify there is output without examining it.
  141. EXPECT_FALSE(test_output_stream_.TakeStr().empty());
  142. }
  143. TEST_F(DriverTest, StdoutOutput) {
  144. // Use explicit filenames so we can look for those to validate output.
  145. CreateTestFile("fn Main() -> i32 { return 0; }", "test.carbon");
  146. EXPECT_TRUE(driver_.RunCommand({"compile", "--output=-", "test.carbon"}));
  147. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  148. // The default is textual assembly.
  149. EXPECT_THAT(test_output_stream_.TakeStr(), ContainsRegex("Main:"));
  150. EXPECT_TRUE(driver_.RunCommand(
  151. {"compile", "--output=-", "--force-obj-output", "test.carbon"}));
  152. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  153. std::string output = test_output_stream_.TakeStr();
  154. auto result =
  155. llvm::object::createBinary(llvm::MemoryBufferRef(output, "test_output"));
  156. if (auto error = result.takeError()) {
  157. FAIL() << toString(std::move(error));
  158. }
  159. EXPECT_TRUE(result->get()->isObject());
  160. }
  161. TEST_F(DriverTest, FileOutput) {
  162. auto scope = ScopedTempWorkingDir();
  163. // Use explicit filenames as the default output filename is computed from
  164. // this, and we can use this to validate output.
  165. CreateTestFile("fn Main() -> i32 { return 0; }", "test.carbon");
  166. // Object output (the default) uses `.o`.
  167. // TODO: This should actually reflect the platform defaults.
  168. EXPECT_TRUE(driver_.RunCommand({"compile", "test.carbon"}));
  169. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  170. // Ensure we wrote an object file of some form with the correct name.
  171. auto result = llvm::object::createBinary("test.o");
  172. if (auto error = result.takeError()) {
  173. FAIL() << toString(std::move(error));
  174. }
  175. EXPECT_TRUE(result->getBinary()->isObject());
  176. // Assembly output uses `.s`.
  177. // TODO: This should actually reflect the platform defaults.
  178. EXPECT_TRUE(driver_.RunCommand({"compile", "--asm-output", "test.carbon"}));
  179. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  180. // TODO: This may need to be tailored to other assembly formats.
  181. EXPECT_THAT(ReadFile("test.s"), ContainsRegex("Main:"));
  182. }
  183. } // namespace
  184. } // namespace Carbon::Testing