driver_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <string>
  10. #include <system_error>
  11. #include <utility>
  12. #include "common/raw_string_ostream.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Support/FormatVariadic.h"
  16. #include "testing/base/file_helpers.h"
  17. #include "testing/base/global_exe_path.h"
  18. #include "toolchain/testing/yaml_test_helpers.h"
  19. namespace Carbon {
  20. namespace {
  21. using ::testing::_;
  22. using ::testing::ContainsRegex;
  23. using ::testing::HasSubstr;
  24. using ::testing::StrEq;
  25. namespace Yaml = ::Carbon::Testing::Yaml;
  26. class DriverTest : public testing::Test {
  27. public:
  28. DriverTest()
  29. : installation_(
  30. InstallPaths::MakeForBazelRunfiles(Testing::GetExePath())),
  31. driver_(fs_, &installation_, /*input_stream=*/nullptr,
  32. &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 MakeTestFile(llvm::StringRef text,
  38. llvm::StringRef filename = "test_file.carbon")
  39. -> llvm::StringRef {
  40. fs_->addFile(filename, /*ModificationTime=*/0,
  41. llvm::MemoryBuffer::getMemBuffer(text));
  42. return filename;
  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, "{0}", 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 '{0}': {1}", 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 '{0}': {1}",
  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,
  68. "Could not change the current working dir to '{0}': {1}",
  69. original_dir, ec.message());
  70. std::filesystem::remove_all(test_dir, ec);
  71. CARBON_CHECK(!ec, "Could not remove the test working dir '{0}': {1}",
  72. test_dir, ec.message());
  73. });
  74. }
  75. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> fs_ =
  76. new llvm::vfs::InMemoryFileSystem;
  77. const InstallPaths installation_;
  78. RawStringOstream test_output_stream_;
  79. RawStringOstream test_error_stream_;
  80. // Some tests work directly with files in the test temporary directory.
  81. std::filesystem::path test_tmpdir_;
  82. Driver driver_;
  83. };
  84. TEST_F(DriverTest, BadCommandErrors) {
  85. EXPECT_FALSE(driver_.RunCommand({}).success);
  86. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("error:"));
  87. EXPECT_FALSE(driver_.RunCommand({"foo"}).success);
  88. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("error:"));
  89. EXPECT_FALSE(driver_.RunCommand({"foo --bar --baz"}).success);
  90. EXPECT_THAT(test_error_stream_.TakeStr(), HasSubstr("error:"));
  91. }
  92. TEST_F(DriverTest, CompileCommandErrors) {
  93. // No input file. This error message is important so check all of it.
  94. EXPECT_FALSE(driver_.RunCommand({"compile"}).success);
  95. EXPECT_THAT(
  96. test_error_stream_.TakeStr(),
  97. StrEq("error: not all required positional arguments were provided; first "
  98. "missing and required positional argument: `FILE`\n"));
  99. // Pass non-existing file
  100. EXPECT_FALSE(driver_
  101. .RunCommand({"compile", "--dump-mem-usage",
  102. "non-existing-file.carbon"})
  103. .success);
  104. EXPECT_THAT(
  105. test_error_stream_.TakeStr(),
  106. ContainsRegex("No such file or directory[\\n]*non-existing-file.carbon"));
  107. // Flush output stream, because it's ok that it's not empty here.
  108. test_output_stream_.TakeStr();
  109. // Invalid output filename. No reliably error message here.
  110. // TODO: Likely want a different filename on Windows.
  111. auto empty_file = MakeTestFile("");
  112. EXPECT_FALSE(driver_
  113. .RunCommand({"compile", "--no-prelude-import",
  114. "--output=/dev/empty", empty_file})
  115. .success);
  116. EXPECT_THAT(test_error_stream_.TakeStr(),
  117. ContainsRegex("error: .*/dev/empty.*"));
  118. }
  119. TEST_F(DriverTest, DumpTokens) {
  120. auto file = MakeTestFile("Hello World");
  121. EXPECT_TRUE(driver_
  122. .RunCommand({"compile", "--no-prelude-import", "--phase=lex",
  123. "--dump-tokens", file})
  124. .success);
  125. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  126. // Verify there is output without examining it.
  127. EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
  128. Yaml::IsYaml(_));
  129. }
  130. TEST_F(DriverTest, DumpParseTree) {
  131. auto file = MakeTestFile("var v: () = ();");
  132. EXPECT_TRUE(driver_
  133. .RunCommand({"compile", "--no-prelude-import",
  134. "--phase=parse", "--dump-parse-tree", file})
  135. .success);
  136. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  137. // Verify there is output without examining it.
  138. EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
  139. Yaml::IsYaml(_));
  140. }
  141. TEST_F(DriverTest, StdoutOutput) {
  142. // Use explicit filenames so we can look for those to validate output.
  143. MakeTestFile("fn Main() {}", "test.carbon");
  144. EXPECT_TRUE(driver_
  145. .RunCommand({"compile", "--no-prelude-import", "--output=-",
  146. "test.carbon"})
  147. .success);
  148. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  149. // The default is textual assembly.
  150. EXPECT_THAT(test_output_stream_.TakeStr(), ContainsRegex("Main:"));
  151. EXPECT_TRUE(driver_
  152. .RunCommand({"compile", "--no-prelude-import", "--output=-",
  153. "--force-obj-output", "test.carbon"})
  154. .success);
  155. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  156. std::string output = test_output_stream_.TakeStr();
  157. auto result =
  158. llvm::object::createBinary(llvm::MemoryBufferRef(output, "test_output"));
  159. if (auto error = result.takeError()) {
  160. FAIL() << toString(std::move(error));
  161. }
  162. EXPECT_TRUE(result->get()->isObject());
  163. }
  164. TEST_F(DriverTest, FileOutput) {
  165. auto scope = ScopedTempWorkingDir();
  166. // Use explicit filenames as the default output filename is computed from
  167. // this, and we can use this to validate output.
  168. MakeTestFile("fn Main() {}", "test.carbon");
  169. // Object output (the default) uses `.o`.
  170. // TODO: This should actually reflect the platform defaults.
  171. EXPECT_TRUE(
  172. driver_.RunCommand({"compile", "--no-prelude-import", "test.carbon"})
  173. .success);
  174. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  175. // Ensure we wrote an object file of some form with the correct name.
  176. auto result = llvm::object::createBinary("test.o");
  177. if (auto error = result.takeError()) {
  178. FAIL() << toString(std::move(error));
  179. }
  180. EXPECT_TRUE(result->getBinary()->isObject());
  181. // Assembly output uses `.s`.
  182. // TODO: This should actually reflect the platform defaults.
  183. EXPECT_TRUE(driver_
  184. .RunCommand({"compile", "--no-prelude-import", "--asm-output",
  185. "test.carbon"})
  186. .success);
  187. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  188. // TODO: This may need to be tailored to other assembly formats.
  189. EXPECT_THAT(*Testing::ReadFile("test.s"), ContainsRegex("Main:"));
  190. }
  191. } // namespace
  192. } // namespace Carbon