install_paths_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 core_package_path = paths.core_package();
  47. ASSERT_THAT(core_package_path, StartsWith(prefix));
  48. EXPECT_TRUE(llvm::sys::fs::exists(core_package_path + "/prelude.carbon"))
  49. << "path: " << core_package_path;
  50. std::string llvm_bin_path = paths.llvm_install_bin();
  51. ASSERT_THAT(llvm_bin_path, StartsWith(prefix));
  52. EXPECT_TRUE(llvm::sys::fs::exists(llvm_bin_path))
  53. << "path: " << llvm_bin_path;
  54. EXPECT_TRUE(llvm::sys::fs::is_directory(llvm_bin_path))
  55. << "path: " << llvm_bin_path;
  56. for (llvm::StringRef llvm_bin :
  57. {"lld", "ld.lld", "ld64.lld", "lld-link", "wasm-ld"}) {
  58. llvm::SmallString<128> bin_path;
  59. bin_path.assign(llvm_bin_path);
  60. llvm::sys::path::append(bin_path, llvm_bin);
  61. EXPECT_TRUE(llvm::sys::fs::exists(bin_path)) << "path: " << bin_path;
  62. EXPECT_TRUE(llvm::sys::fs::can_execute(bin_path)) << "path: " << bin_path;
  63. }
  64. }
  65. std::unique_ptr<Runfiles> test_runfiles_;
  66. };
  67. TEST_F(InstallPathsTest, PrefixRootDriver) {
  68. std::string installed_driver_path = test_runfiles_->Rlocation(
  69. "_main/toolchain/install/prefix_root/bin/carbon");
  70. auto paths = InstallPaths::MakeExeRelative(installed_driver_path);
  71. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  72. TestInstallPaths(paths);
  73. }
  74. TEST_F(InstallPathsTest, PrefixRootExplicit) {
  75. std::string marker_path = test_runfiles_->Rlocation(
  76. "_main/toolchain/install/prefix_root/lib/carbon/carbon_install.txt");
  77. llvm::StringRef prefix_path = marker_path;
  78. CARBON_CHECK(prefix_path.consume_back("lib/carbon/carbon_install.txt"))
  79. << "Unexpected suffix of the marker path: " << marker_path;
  80. auto paths = InstallPaths::Make(prefix_path);
  81. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  82. TestInstallPaths(paths);
  83. }
  84. TEST_F(InstallPathsTest, TestRunfiles) {
  85. auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath());
  86. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  87. TestInstallPaths(paths);
  88. }
  89. TEST_F(InstallPathsTest, BinaryRunfiles) {
  90. std::string test_binary_path =
  91. test_runfiles_->Rlocation("_main/toolchain/install/test_binary");
  92. CARBON_CHECK(llvm::sys::fs::can_execute(test_binary_path))
  93. << test_binary_path;
  94. auto paths = InstallPaths::MakeForBazelRunfiles(test_binary_path);
  95. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  96. TestInstallPaths(paths);
  97. }
  98. TEST_F(InstallPathsTest, Errors) {
  99. auto paths = InstallPaths::Make("foo/bar/baz");
  100. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  101. EXPECT_THAT(paths.prefix(), Eq(""));
  102. paths = InstallPaths::MakeExeRelative("foo/bar/baz");
  103. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  104. EXPECT_THAT(paths.prefix(), Eq(""));
  105. // Note that we can't test the runfiles code path from within a test because
  106. // it succeeds some of the time even with a bogus executable name.
  107. }
  108. } // namespace
  109. } // namespace Carbon