From 6b126ddd80b4a5a299ed3ffb1922df5049154439 Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Tue, 16 Jul 2024 13:16:16 +0200 Subject: [PATCH] fix locate_file (#5600) * fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- tests/procs/__init__.py | 0 tests/procs/test_executables.py | 18 +++++++++++++++++- xonsh/procs/executables.py | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) delete mode 100644 tests/procs/__init__.py diff --git a/tests/procs/__init__.py b/tests/procs/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/procs/test_executables.py b/tests/procs/test_executables.py index e552e548f..aa27c1426 100644 --- a/tests/procs/test_executables.py +++ b/tests/procs/test_executables.py @@ -2,7 +2,12 @@ import os from xonsh.environ import Env from xonsh.platform import ON_WINDOWS -from xonsh.procs.executables import get_paths, get_possible_names, locate_executable +from xonsh.procs.executables import ( + get_paths, + get_possible_names, + locate_executable, + locate_file, +) def test_get_possible_names(): @@ -44,3 +49,14 @@ def test_locate_executable(tmpdir, xession): assert locate_executable("file1") is None assert locate_executable("file4") is None assert locate_executable("file2") is None + + +def test_locate_file(tmpdir, xession): + bindir1 = tmpdir.mkdir("bindir1") + bindir2 = tmpdir.mkdir("bindir2") + bindir3 = tmpdir.mkdir("bindir3") + file = bindir2 / "findme" + file.write_text("", encoding="utf8") + with xession.env.swap(PATH=[str(bindir1), str(bindir2), str(bindir3)]): + f = locate_file("findme") + assert str(f) == str(file) diff --git a/xonsh/procs/executables.py b/xonsh/procs/executables.py index a6c81989b..b26131111 100644 --- a/xonsh/procs/executables.py +++ b/xonsh/procs/executables.py @@ -96,7 +96,9 @@ def locate_file(name, env=None, check_executable=False, use_pathext=False): filepath = Path(path) / possible_name try: - if check_executable and not is_executable(filepath): + if not filepath.is_file() or ( + check_executable and not is_executable(filepath) + ): continue return str(filepath) except PermissionError: