exe_path_test.cpp 1.5 KB

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