install_paths_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/install/install_paths.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/check.h"
  8. #include "llvm/ADT/SmallString.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/FormatVariadic.h"
  11. #include "llvm/Support/Path.h"
  12. #include "testing/base/global_exe_path.h"
  13. #include "tools/cpp/runfiles/runfiles.h"
  14. namespace Carbon {
  15. class InstallPathsTestPeer {
  16. public:
  17. static auto GetPrefix(const InstallPaths& paths) -> llvm::StringRef {
  18. return paths.prefix_;
  19. }
  20. };
  21. namespace {
  22. using ::bazel::tools::cpp::runfiles::Runfiles;
  23. using ::testing::Eq;
  24. using ::testing::HasSubstr;
  25. using ::testing::Optional;
  26. using ::testing::StartsWith;
  27. class InstallPathsTest : public ::testing::Test {
  28. protected:
  29. InstallPathsTest() {
  30. std::string error;
  31. test_runfiles_.reset(Runfiles::Create(Testing::GetExePath().str(), &error));
  32. CARBON_CHECK(test_runfiles_ != nullptr, "{0}", error);
  33. }
  34. // Test the install paths found with the given `exe_path`. Will check that
  35. // the detected install prefix path starts with `prefix_startswith`, and then
  36. // check that the path accessors point to the right kind of file or
  37. // directory.
  38. auto TestInstallPaths(const InstallPaths& paths) -> void {
  39. auto prefix = InstallPathsTestPeer::GetPrefix(paths);
  40. SCOPED_TRACE(llvm::formatv("Install prefix: '{0}'", prefix));
  41. // Grab a the prefix into a string to make it easier to use in the test.
  42. EXPECT_TRUE(llvm::sys::fs::exists(prefix));
  43. EXPECT_TRUE(llvm::sys::fs::is_directory(prefix));
  44. // Now check that all the expected parts of the toolchain's install are in
  45. // fact found using the API.
  46. llvm::SmallString<256> driver_path(prefix);
  47. // TODO: Adjust this to work equally well on Windows.
  48. llvm::sys::path::append(driver_path, llvm::sys::path::Style::posix,
  49. "bin/carbon");
  50. EXPECT_TRUE(llvm::sys::fs::exists(driver_path)) << "path: " << driver_path;
  51. EXPECT_TRUE(llvm::sys::fs::can_execute(driver_path))
  52. << "path: " << driver_path;
  53. std::string core_package_path = paths.core_package();
  54. ASSERT_THAT(core_package_path, StartsWith(prefix));
  55. EXPECT_TRUE(llvm::sys::fs::exists(core_package_path + "/prelude.carbon"))
  56. << "path: " << core_package_path;
  57. std::string llvm_bin_path = paths.llvm_install_bin();
  58. ASSERT_THAT(llvm_bin_path, StartsWith(prefix));
  59. EXPECT_TRUE(llvm::sys::fs::exists(llvm_bin_path))
  60. << "path: " << llvm_bin_path;
  61. EXPECT_TRUE(llvm::sys::fs::is_directory(llvm_bin_path))
  62. << "path: " << llvm_bin_path;
  63. for (llvm::StringRef llvm_bin :
  64. {"lld", "ld.lld", "ld64.lld", "lld-link", "wasm-ld"}) {
  65. llvm::SmallString<128> bin_path;
  66. bin_path.assign(llvm_bin_path);
  67. llvm::sys::path::append(bin_path, llvm_bin);
  68. EXPECT_TRUE(llvm::sys::fs::exists(bin_path)) << "path: " << bin_path;
  69. EXPECT_TRUE(llvm::sys::fs::can_execute(bin_path)) << "path: " << bin_path;
  70. }
  71. }
  72. std::unique_ptr<Runfiles> test_runfiles_;
  73. };
  74. TEST_F(InstallPathsTest, PrefixRootDriver) {
  75. std::string installed_driver_path = test_runfiles_->Rlocation(
  76. "carbon/toolchain/install/prefix_root/bin/carbon");
  77. auto paths = InstallPaths::MakeExeRelative(installed_driver_path);
  78. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  79. TestInstallPaths(paths);
  80. }
  81. TEST_F(InstallPathsTest, PrefixRootExplicit) {
  82. std::string marker_path = test_runfiles_->Rlocation(
  83. "carbon/toolchain/install/prefix_root/lib/carbon/carbon_install.txt");
  84. llvm::StringRef prefix_path = marker_path;
  85. CARBON_CHECK(prefix_path.consume_back("lib/carbon/carbon_install.txt"),
  86. "Unexpected suffix of the marker path: {0}", marker_path);
  87. auto paths = InstallPaths::Make(prefix_path);
  88. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  89. TestInstallPaths(paths);
  90. }
  91. TEST_F(InstallPathsTest, TestRunfiles) {
  92. auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  93. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  94. TestInstallPaths(paths);
  95. }
  96. TEST_F(InstallPathsTest, BinaryRunfiles) {
  97. std::string test_binary_path =
  98. test_runfiles_->Rlocation("carbon/toolchain/install/test_binary");
  99. CARBON_CHECK(llvm::sys::fs::can_execute(test_binary_path), "{0}",
  100. test_binary_path);
  101. auto paths = InstallPaths::MakeForBazelRunfiles(test_binary_path);
  102. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  103. TestInstallPaths(paths);
  104. }
  105. TEST_F(InstallPathsTest, Errors) {
  106. auto paths = InstallPaths::Make("/foo/bar/baz");
  107. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  108. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  109. paths = InstallPaths::MakeExeRelative("foo/bar/baz");
  110. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  111. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  112. // Note that we can't test the runfiles code path from within a test because
  113. // it succeeds some of the time even with a bogus executable name.
  114. }
  115. } // namespace
  116. } // namespace Carbon