install_paths_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/base/install_paths.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <filesystem>
  8. #include <memory>
  9. #include <string>
  10. #include "common/check.h"
  11. #include "common/error_test_helpers.h"
  12. #include "common/filesystem.h"
  13. #include "llvm/ADT/SmallString.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. #include "testing/base/global_exe_path.h"
  16. #include "tools/cpp/runfiles/runfiles.h"
  17. namespace Carbon {
  18. class InstallPathsTestPeer {
  19. public:
  20. static auto GetRoot(const InstallPaths& paths) -> std::filesystem::path {
  21. return paths.root_;
  22. }
  23. };
  24. namespace {
  25. using ::bazel::tools::cpp::runfiles::Runfiles;
  26. using ::testing::_;
  27. using ::testing::EndsWith;
  28. using ::testing::Eq;
  29. using ::testing::HasSubstr;
  30. using Testing::IsSuccess;
  31. using ::testing::Optional;
  32. using ::testing::StartsWith;
  33. class InstallPathsTest : public ::testing::Test {
  34. public:
  35. InstallPathsTest() {
  36. std::string error;
  37. test_runfiles_.reset(Runfiles::Create(Testing::GetExePath().str(), &error));
  38. CARBON_CHECK(test_runfiles_ != nullptr, "{0}", error);
  39. }
  40. // Test the install paths found with the given `exe_path`. Will check that
  41. // the detected install root path has the expected location relative to the
  42. // FHS layout, and then check that the path accessors point to the right kind
  43. // of file or directory.
  44. auto TestInstallPaths(const InstallPaths& paths) -> void {
  45. std::filesystem::path root_path = InstallPathsTestPeer::GetRoot(paths);
  46. SCOPED_TRACE(llvm::formatv("Install root: '{0}'", root_path));
  47. // Open the root directory.
  48. auto root_result = Filesystem::Cwd().OpenDir(root_path);
  49. ASSERT_THAT(root_result, IsSuccess(_));
  50. Filesystem::Dir root = *std::move(root_result);
  51. // Check that the root is located in the expected part of the FHS layout.
  52. // TODO: Adjust this to work equally well on Windows.
  53. EXPECT_THAT(root_path.native(), EndsWith("lib/carbon/"));
  54. EXPECT_THAT(
  55. root.Access("../../bin/carbon", Filesystem::AccessCheckFlags::Execute),
  56. IsSuccess(Eq(true)))
  57. << "path: " << (root_path / "../../bin/carbon");
  58. std::filesystem::path core_package_path = paths.core_package();
  59. ASSERT_THAT(core_package_path, StartsWith(root_path));
  60. EXPECT_THAT(Filesystem::Cwd().Access(core_package_path / "prelude.carbon"),
  61. IsSuccess(Eq(true)))
  62. << "path: " << core_package_path;
  63. std::filesystem::path llvm_bin_path = paths.llvm_install_bin();
  64. ASSERT_THAT(llvm_bin_path, StartsWith(root_path));
  65. auto open_result = Filesystem::Cwd().OpenDir(llvm_bin_path);
  66. ASSERT_THAT(open_result, IsSuccess(_));
  67. Filesystem::Dir llvm_bin = *std::move(open_result);
  68. for (std::filesystem::path bin_name : {"ld.lld", "ld64.lld"}) {
  69. EXPECT_THAT(
  70. llvm_bin.Access(bin_name, Filesystem::AccessCheckFlags::Execute),
  71. IsSuccess(Eq(true)))
  72. << "path: " << (llvm_bin_path / bin_name);
  73. }
  74. }
  75. std::unique_ptr<Runfiles> test_runfiles_;
  76. };
  77. TEST_F(InstallPathsTest, RootBusybox) {
  78. std::string installed_busybox_path = test_runfiles_->Rlocation(
  79. "carbon/toolchain/install/prefix/lib/carbon/carbon-busybox");
  80. auto paths = InstallPaths::MakeExeRelative(installed_busybox_path);
  81. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  82. TestInstallPaths(paths);
  83. }
  84. TEST_F(InstallPathsTest, RootExplicit) {
  85. std::string marker_path = test_runfiles_->Rlocation(
  86. "carbon/toolchain/install/prefix/lib/carbon/carbon_install.txt");
  87. llvm::StringRef root_path = marker_path;
  88. CARBON_CHECK(root_path.consume_back("carbon_install.txt"),
  89. "Unexpected suffix of the marker path: {0}", marker_path);
  90. auto paths = InstallPaths::Make(root_path);
  91. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  92. TestInstallPaths(paths);
  93. }
  94. TEST_F(InstallPathsTest, TestRunfiles) {
  95. auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  96. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  97. TestInstallPaths(paths);
  98. }
  99. TEST_F(InstallPathsTest, BinaryRunfiles) {
  100. std::filesystem::path test_binary_path =
  101. test_runfiles_->Rlocation("carbon/toolchain/base/test_binary");
  102. ASSERT_THAT(Filesystem::Cwd().Access(test_binary_path,
  103. Filesystem::AccessCheckFlags::Execute),
  104. IsSuccess(Eq(true)))
  105. << "path: " << test_binary_path;
  106. auto paths = InstallPaths::MakeForBazelRunfiles(test_binary_path.native());
  107. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  108. TestInstallPaths(paths);
  109. }
  110. TEST_F(InstallPathsTest, Errors) {
  111. auto paths = InstallPaths::Make("/foo/bar/baz");
  112. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  113. EXPECT_THAT(InstallPathsTestPeer::GetRoot(paths), Eq(""));
  114. paths = InstallPaths::MakeExeRelative("foo/bar/baz");
  115. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  116. EXPECT_THAT(InstallPathsTestPeer::GetRoot(paths), Eq(""));
  117. // Note that we can't test the runfiles code path from within a test because
  118. // it succeeds some of the time even with a bogus executable name.
  119. }
  120. } // namespace
  121. } // namespace Carbon