exe_path_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "common/exe_path.h"
  5. #include <gtest/gtest.h>
  6. #include <string>
  7. #include <system_error>
  8. #include "llvm/ADT/SmallString.h"
  9. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/Path.h"
  11. namespace Carbon {
  12. namespace {
  13. TEST(ExePath, FailureFallback) {
  14. static int static_for_main_addr;
  15. std::string running_binary =
  16. llvm::sys::fs::getMainExecutable("exe_path_test", &static_for_main_addr);
  17. llvm::SmallString<128> path = llvm::StringRef(getenv("TEST_TMPDIR"));
  18. llvm::sys::path::append(path, "non_existant_binary");
  19. std::string exe_path = FindExecutablePath(path.c_str());
  20. EXPECT_EQ(running_binary, exe_path);
  21. }
  22. TEST(ExePath, Symlink) {
  23. static int static_for_main_addr;
  24. std::string running_binary =
  25. llvm::sys::fs::getMainExecutable("exe_path_test", &static_for_main_addr);
  26. llvm::SmallString<128> path = llvm::StringRef(getenv("TEST_TMPDIR"));
  27. llvm::sys::path::append(path, "test_binary");
  28. std::error_code ec;
  29. std::filesystem::create_symlink(running_binary, path.c_str(), ec);
  30. ASSERT_TRUE(!ec) << "Error code: " << ec;
  31. std::string exe_path = FindExecutablePath(path.c_str());
  32. EXPECT_EQ(path, exe_path);
  33. }
  34. TEST(ExePath, PathLookup) {
  35. // TODO: This is not likely to work well on Windows (outside of WSL). But some
  36. // of that may be hidden by Bazel's test environment. Regardless, we should
  37. // revisit this when we have good coverage of Windows build with something
  38. // appropriate for that platform.
  39. std::string exe_path = FindExecutablePath("bash");
  40. EXPECT_NE(exe_path, "bash");
  41. EXPECT_TRUE(llvm::sys::fs::exists(exe_path));
  42. EXPECT_TRUE(llvm::sys::fs::can_execute(exe_path));
  43. }
  44. } // namespace
  45. } // namespace Carbon