clang_runner_test.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/clang_runner.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <filesystem>
  8. #include <fstream>
  9. #include <utility>
  10. #include "common/check.h"
  11. #include "common/ostream.h"
  12. #include "llvm/ADT/ScopeExit.h"
  13. #include "llvm/Object/Binary.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. #include "llvm/Support/Program.h"
  16. #include "llvm/TargetParser/Host.h"
  17. #include "testing/base/test_raw_ostream.h"
  18. namespace Carbon {
  19. namespace {
  20. using ::Carbon::Testing::TestRawOstream;
  21. using ::testing::HasSubstr;
  22. using ::testing::StrEq;
  23. // While these are marked as "internal" APIs, they seem to work and be pretty
  24. // widely used for their exact documented behavior.
  25. using ::testing::internal::CaptureStderr;
  26. using ::testing::internal::CaptureStdout;
  27. using ::testing::internal::GetCapturedStderr;
  28. using ::testing::internal::GetCapturedStdout;
  29. // Calls the provided lambda with `stderr` and `stdout` captured and saved into
  30. // the provided output parameters. The lambda's result is returned. It is
  31. // important to not put anything inside the lambda whose output would be useful
  32. // in interpreting test errors such as Google Test assertions as their output
  33. // will end up captured as well.
  34. template <typename CallableT>
  35. static auto RunWithCapturedOutput(std::string& out, std::string& err,
  36. CallableT callable) {
  37. CaptureStderr();
  38. CaptureStdout();
  39. auto result = callable();
  40. // No need to flush stderr.
  41. err = GetCapturedStderr();
  42. llvm::outs().flush();
  43. out = GetCapturedStdout();
  44. return result;
  45. }
  46. TEST(ClangRunnerTest, Version) {
  47. TestRawOstream test_os;
  48. std::string target = llvm::sys::getDefaultTargetTriple();
  49. ClangRunner runner("./toolchain/driver/run_clang_test", target, &test_os);
  50. std::string out;
  51. std::string err;
  52. EXPECT_TRUE(RunWithCapturedOutput(out, err,
  53. [&] { return runner.Run({"--version"}); }));
  54. // The arguments to Clang should be part of the verbose log.
  55. EXPECT_THAT(test_os.TakeStr(), HasSubstr("--version"));
  56. // No need to flush stderr, just check its contents.
  57. EXPECT_THAT(err, StrEq(""));
  58. // Flush and get the captured stdout to test that this command worked.
  59. // We don't care about any particular version, just that it is printed.
  60. EXPECT_THAT(out, HasSubstr("clang version"));
  61. // The target should match what we provided.
  62. EXPECT_THAT(out, HasSubstr((llvm::Twine("Target: ") + target).str()));
  63. // The installation should come from the above path of the test binary.
  64. EXPECT_THAT(out, HasSubstr("InstalledDir: ./toolchain/driver"));
  65. }
  66. // Utility to write a test file. We don't need the full power provided here yet,
  67. // but we anticipate adding more tests such as compiling basic C++ code in the
  68. // future and this provides a basis for building those tests.
  69. static auto WriteTestFile(llvm::StringRef name_suffix, llvm::Twine contents)
  70. -> std::filesystem::path {
  71. std::filesystem::path test_tmpdir;
  72. if (char* tmpdir_env = getenv("TEST_TMPDIR"); tmpdir_env != nullptr) {
  73. test_tmpdir = std::string(tmpdir_env);
  74. } else {
  75. test_tmpdir = std::filesystem::temp_directory_path();
  76. }
  77. const auto* unit_test = ::testing::UnitTest::GetInstance();
  78. const auto* test_info = unit_test->current_test_info();
  79. std::filesystem::path test_file =
  80. test_tmpdir / llvm::formatv("{0}_{1}_{2}", test_info->test_suite_name(),
  81. test_info->name(), name_suffix)
  82. .str();
  83. // Make debugging a bit easier by cleaning up any files from previous runs.
  84. // This is only necessary when not run in Bazel's test environment.
  85. std::filesystem::remove(test_file);
  86. CARBON_CHECK(!std::filesystem::exists(test_file));
  87. {
  88. std::error_code ec;
  89. llvm::raw_fd_ostream test_file_stream(test_file.string(), ec);
  90. CARBON_CHECK(!ec) << "Test file error: " << ec.message();
  91. test_file_stream << contents;
  92. }
  93. return test_file;
  94. }
  95. // It's hard to write a portable and reliable unittest for all the layers of the
  96. // Clang driver because they work hard to interact with the underlying
  97. // filesystem and operating system. For now, we just check that a link command
  98. // is echoed back with plausible contents.
  99. //
  100. // TODO: We should eventually strive to have a more complete setup that lets us
  101. // test more complete Clang functionality here.
  102. TEST(ClangRunnerTest, LinkCommandEcho) {
  103. // Just create some empty files to use in a synthetic link command below.
  104. std::filesystem::path foo_file = WriteTestFile("foo.o", "");
  105. std::filesystem::path bar_file = WriteTestFile("bar.o", "");
  106. std::string verbose_out;
  107. llvm::raw_string_ostream verbose_os(verbose_out);
  108. std::string target = llvm::sys::getDefaultTargetTriple();
  109. ClangRunner runner("./toolchain/driver/run_clang_test", target, &verbose_os);
  110. std::string out;
  111. std::string err;
  112. EXPECT_TRUE(RunWithCapturedOutput(out, err,
  113. [&] {
  114. return runner.Run({"-###", "-o", "binary",
  115. foo_file.string(),
  116. bar_file.string()});
  117. }))
  118. << "Verbose output from runner:\n"
  119. << verbose_out << "\n";
  120. // Because we use `-###' above, we should just see the command that the Clang
  121. // driver would have run in a subprocess. This will be very architecture
  122. // dependent and have lots of variety, but we expect to see both file strings
  123. // in it the command at least.
  124. EXPECT_THAT(err, HasSubstr(foo_file.string())) << err;
  125. EXPECT_THAT(err, HasSubstr(bar_file.string())) << err;
  126. // And no non-stderr output should be produced.
  127. EXPECT_THAT(out, StrEq(""));
  128. }
  129. } // namespace
  130. } // namespace Carbon