driver_test.cpp 7.9 KB

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