install_paths_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public:
  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 : {"ld.lld", "ld64.lld"}) {
  64. llvm::SmallString<128> bin_path;
  65. bin_path.assign(llvm_bin_path);
  66. llvm::sys::path::append(bin_path, llvm_bin);
  67. EXPECT_TRUE(llvm::sys::fs::exists(bin_path)) << "path: " << bin_path;
  68. EXPECT_TRUE(llvm::sys::fs::can_execute(bin_path)) << "path: " << bin_path;
  69. }
  70. }
  71. std::unique_ptr<Runfiles> test_runfiles_;
  72. };
  73. TEST_F(InstallPathsTest, PrefixRootBusybox) {
  74. std::string installed_driver_path = test_runfiles_->Rlocation(
  75. "carbon/toolchain/install/prefix_root/lib/carbon/carbon-busybox");
  76. auto paths = InstallPaths::MakeExeRelative(installed_driver_path);
  77. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  78. TestInstallPaths(paths);
  79. }
  80. TEST_F(InstallPathsTest, PrefixRootExplicit) {
  81. std::string marker_path = test_runfiles_->Rlocation(
  82. "carbon/toolchain/install/prefix_root/lib/carbon/carbon_install.txt");
  83. llvm::StringRef prefix_path = marker_path;
  84. CARBON_CHECK(prefix_path.consume_back("lib/carbon/carbon_install.txt"),
  85. "Unexpected suffix of the marker path: {0}", marker_path);
  86. auto paths = InstallPaths::Make(prefix_path);
  87. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  88. TestInstallPaths(paths);
  89. }
  90. TEST_F(InstallPathsTest, TestRunfiles) {
  91. auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  92. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  93. TestInstallPaths(paths);
  94. }
  95. TEST_F(InstallPathsTest, BinaryRunfiles) {
  96. std::string test_binary_path =
  97. test_runfiles_->Rlocation("carbon/toolchain/install/test_binary");
  98. CARBON_CHECK(llvm::sys::fs::can_execute(test_binary_path), "{0}",
  99. test_binary_path);
  100. auto paths = InstallPaths::MakeForBazelRunfiles(test_binary_path);
  101. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  102. TestInstallPaths(paths);
  103. }
  104. TEST_F(InstallPathsTest, Errors) {
  105. auto paths = InstallPaths::Make("/foo/bar/baz");
  106. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  107. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  108. paths = InstallPaths::MakeExeRelative("foo/bar/baz");
  109. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  110. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  111. // Note that we can't test the runfiles code path from within a test because
  112. // it succeeds some of the time even with a bogus executable name.
  113. }
  114. } // namespace
  115. } // namespace Carbon