driver_test.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/testing/yaml_test_helpers.h"
  15. namespace Carbon::Testing {
  16. namespace {
  17. using ::testing::_;
  18. using ::testing::ContainsRegex;
  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. // Verify there is output without examining it.
  110. EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
  111. Yaml::IsYaml(_));
  112. }
  113. TEST_F(DriverTest, DumpParseTree) {
  114. auto file = CreateTestFile("var v: i32 = 42;");
  115. EXPECT_TRUE(driver_.RunCommand(
  116. {"compile", "--phase=parse", "--dump-parse-tree", file}));
  117. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  118. // Verify there is output without examining it.
  119. EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
  120. Yaml::IsYaml(_));
  121. }
  122. TEST_F(DriverTest, StdoutOutput) {
  123. // Use explicit filenames so we can look for those to validate output.
  124. CreateTestFile("fn Main() -> i32 { return 0; }", "test.carbon");
  125. EXPECT_TRUE(driver_.RunCommand({"compile", "--output=-", "test.carbon"}));
  126. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  127. // The default is textual assembly.
  128. EXPECT_THAT(test_output_stream_.TakeStr(), ContainsRegex("Main:"));
  129. EXPECT_TRUE(driver_.RunCommand(
  130. {"compile", "--output=-", "--force-obj-output", "test.carbon"}));
  131. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  132. std::string output = test_output_stream_.TakeStr();
  133. auto result =
  134. llvm::object::createBinary(llvm::MemoryBufferRef(output, "test_output"));
  135. if (auto error = result.takeError()) {
  136. FAIL() << toString(std::move(error));
  137. }
  138. EXPECT_TRUE(result->get()->isObject());
  139. }
  140. TEST_F(DriverTest, FileOutput) {
  141. auto scope = ScopedTempWorkingDir();
  142. // Use explicit filenames as the default output filename is computed from
  143. // this, and we can use this to validate output.
  144. CreateTestFile("fn Main() -> i32 { return 0; }", "test.carbon");
  145. // Object output (the default) uses `.o`.
  146. // TODO: This should actually reflect the platform defaults.
  147. EXPECT_TRUE(driver_.RunCommand({"compile", "test.carbon"}));
  148. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  149. // Ensure we wrote an object file of some form with the correct name.
  150. auto result = llvm::object::createBinary("test.o");
  151. if (auto error = result.takeError()) {
  152. FAIL() << toString(std::move(error));
  153. }
  154. EXPECT_TRUE(result->getBinary()->isObject());
  155. // Assembly output uses `.s`.
  156. // TODO: This should actually reflect the platform defaults.
  157. EXPECT_TRUE(driver_.RunCommand({"compile", "--asm-output", "test.carbon"}));
  158. EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
  159. // TODO: This may need to be tailored to other assembly formats.
  160. EXPECT_THAT(ReadFile("test.s"), ContainsRegex("Main:"));
  161. }
  162. } // namespace
  163. } // namespace Carbon::Testing