exe_path_test.cpp 1.5 KB

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