install_paths_test.cpp 4.6 KB

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