install_paths_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <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 GetPrefix(const InstallPaths& paths) -> std::filesystem::path {
  21. return paths.prefix_;
  22. }
  23. };
  24. namespace {
  25. using ::bazel::tools::cpp::runfiles::Runfiles;
  26. using ::testing::_;
  27. using ::testing::Eq;
  28. using ::testing::HasSubstr;
  29. using Testing::IsSuccess;
  30. using ::testing::Optional;
  31. using ::testing::StartsWith;
  32. class InstallPathsTest : public ::testing::Test {
  33. public:
  34. InstallPathsTest() {
  35. std::string error;
  36. test_runfiles_.reset(Runfiles::Create(Testing::GetExePath().str(), &error));
  37. CARBON_CHECK(test_runfiles_ != nullptr, "{0}", error);
  38. }
  39. // Test the install paths found with the given `exe_path`. Will check that
  40. // the detected install prefix path starts with `prefix_startswith`, and then
  41. // check that the path accessors point to the right kind of file or
  42. // directory.
  43. auto TestInstallPaths(const InstallPaths& paths) -> void {
  44. std::filesystem::path prefix_path = InstallPathsTestPeer::GetPrefix(paths);
  45. SCOPED_TRACE(llvm::formatv("Install prefix: '{0}'", prefix_path));
  46. // Open the prefix directory.
  47. auto prefix_result = Filesystem::Cwd().OpenDir(prefix_path);
  48. ASSERT_THAT(prefix_result, IsSuccess(_));
  49. Filesystem::Dir prefix = *std::move(prefix_result);
  50. // Now check that all the expected parts of the toolchain's install are in
  51. // fact found using the API.
  52. // TODO: Adjust this to work equally well on Windows.
  53. EXPECT_THAT(
  54. prefix.Access("bin/carbon", Filesystem::AccessCheckFlags::Execute),
  55. IsSuccess(Eq(true)))
  56. << "path: " << (prefix_path / "bin/carbon");
  57. std::filesystem::path core_package_path = paths.core_package();
  58. ASSERT_THAT(core_package_path, StartsWith(prefix_path));
  59. EXPECT_THAT(Filesystem::Cwd().Access(core_package_path / "prelude.carbon"),
  60. IsSuccess(Eq(true)))
  61. << "path: " << core_package_path;
  62. std::filesystem::path llvm_bin_path = paths.llvm_install_bin();
  63. ASSERT_THAT(llvm_bin_path, StartsWith(prefix_path));
  64. auto open_result = Filesystem::Cwd().OpenDir(llvm_bin_path);
  65. ASSERT_THAT(open_result, IsSuccess(_));
  66. Filesystem::Dir llvm_bin = *std::move(open_result);
  67. for (std::filesystem::path bin_name : {"ld.lld", "ld64.lld"}) {
  68. EXPECT_THAT(
  69. llvm_bin.Access(bin_name, Filesystem::AccessCheckFlags::Execute),
  70. IsSuccess(Eq(true)))
  71. << "path: " << (llvm_bin_path / bin_name);
  72. }
  73. }
  74. std::unique_ptr<Runfiles> test_runfiles_;
  75. };
  76. TEST_F(InstallPathsTest, PrefixRootBusybox) {
  77. std::string installed_driver_path = test_runfiles_->Rlocation(
  78. "carbon/toolchain/install/prefix_root/lib/carbon/carbon-busybox");
  79. auto paths = InstallPaths::MakeExeRelative(installed_driver_path);
  80. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  81. TestInstallPaths(paths);
  82. }
  83. TEST_F(InstallPathsTest, PrefixRootExplicit) {
  84. std::string marker_path = test_runfiles_->Rlocation(
  85. "carbon/toolchain/install/prefix_root/lib/carbon/carbon_install.txt");
  86. llvm::StringRef prefix_path = marker_path;
  87. CARBON_CHECK(prefix_path.consume_back("lib/carbon/carbon_install.txt"),
  88. "Unexpected suffix of the marker path: {0}", marker_path);
  89. auto paths = InstallPaths::Make(prefix_path);
  90. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  91. TestInstallPaths(paths);
  92. }
  93. TEST_F(InstallPathsTest, TestRunfiles) {
  94. auto paths = InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
  95. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  96. TestInstallPaths(paths);
  97. }
  98. TEST_F(InstallPathsTest, BinaryRunfiles) {
  99. std::filesystem::path test_binary_path =
  100. test_runfiles_->Rlocation("carbon/toolchain/install/test_binary");
  101. ASSERT_THAT(Filesystem::Cwd().Access(test_binary_path,
  102. Filesystem::AccessCheckFlags::Execute),
  103. IsSuccess(Eq(true)))
  104. << "path: " << test_binary_path;
  105. auto paths = InstallPaths::MakeForBazelRunfiles(test_binary_path.native());
  106. ASSERT_THAT(paths.error(), Eq(std::nullopt)) << *paths.error();
  107. TestInstallPaths(paths);
  108. }
  109. TEST_F(InstallPathsTest, Errors) {
  110. auto paths = InstallPaths::Make("/foo/bar/baz");
  111. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  112. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  113. paths = InstallPaths::MakeExeRelative("foo/bar/baz");
  114. EXPECT_THAT(paths.error(), Optional(HasSubstr("foo/bar/baz")));
  115. EXPECT_THAT(InstallPathsTestPeer::GetPrefix(paths), Eq(""));
  116. // Note that we can't test the runfiles code path from within a test because
  117. // it succeeds some of the time even with a bogus executable name.
  118. }
  119. } // namespace
  120. } // namespace Carbon