install_paths_test.cpp 4.9 KB

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