mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 00:14:41 +01:00
fix locate_file (#5606)
* fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
dd656ab7fa
commit
2414ec167a
4 changed files with 37 additions and 6 deletions
23
news/fix_locate_file.rst
Normal file
23
news/fix_locate_file.rst
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
**Added:**
|
||||||
|
|
||||||
|
* <news item>
|
||||||
|
|
||||||
|
**Changed:**
|
||||||
|
|
||||||
|
* <news item>
|
||||||
|
|
||||||
|
**Deprecated:**
|
||||||
|
|
||||||
|
* <news item>
|
||||||
|
|
||||||
|
**Removed:**
|
||||||
|
|
||||||
|
* <news item>
|
||||||
|
|
||||||
|
**Fixed:**
|
||||||
|
|
||||||
|
* Fixed wrong order of directories in PATH (#5606 #5605). Please do not use 0.18.0 and 0.18.1 versions.
|
||||||
|
|
||||||
|
**Security:**
|
||||||
|
|
||||||
|
* <news item>
|
|
@ -24,18 +24,26 @@ def test_get_paths(tmpdir):
|
||||||
|
|
||||||
|
|
||||||
def test_locate_executable(tmpdir, xession):
|
def test_locate_executable(tmpdir, xession):
|
||||||
bindir = tmpdir.mkdir("bindir")
|
bindir1 = tmpdir.mkdir("bindir1")
|
||||||
bindir.mkdir("subdir")
|
bindir2 = tmpdir.mkdir("bindir2")
|
||||||
|
bindir3 = tmpdir.mkdir("bindir3")
|
||||||
|
bindir2.mkdir("subdir")
|
||||||
executables = ["file1.EXE", "file2.COM", "file2.EXE", "file3"]
|
executables = ["file1.EXE", "file2.COM", "file2.EXE", "file3"]
|
||||||
not_executables = ["file4.EXE", "file5"]
|
not_executables = ["file4.EXE", "file5"]
|
||||||
for exefile in executables + not_executables:
|
for exefile in executables + not_executables:
|
||||||
f = bindir / exefile
|
f = bindir2 / exefile
|
||||||
f.write_text("binary", encoding="utf8")
|
f.write_text("binary", encoding="utf8")
|
||||||
if exefile in executables:
|
if exefile in executables:
|
||||||
os.chmod(f, 0o777)
|
os.chmod(f, 0o777)
|
||||||
|
|
||||||
|
# Test overlapping file names in different bin directories.
|
||||||
|
(f := bindir3 / "file3").write_text("binary", encoding="utf8")
|
||||||
|
os.chmod(f, 0o777)
|
||||||
|
|
||||||
pathext = [".EXE", ".COM"] if ON_WINDOWS else []
|
pathext = [".EXE", ".COM"] if ON_WINDOWS else []
|
||||||
with xession.env.swap(PATH=str(bindir), PATHEXT=pathext):
|
with xession.env.swap(
|
||||||
|
PATH=[str(bindir1), str(bindir2), str(bindir3)], PATHEXT=pathext
|
||||||
|
):
|
||||||
assert locate_executable("file1.EXE")
|
assert locate_executable("file1.EXE")
|
||||||
assert locate_executable("nofile") is None
|
assert locate_executable("nofile") is None
|
||||||
assert locate_executable("file5") is None
|
assert locate_executable("file5") is None
|
||||||
|
@ -45,7 +53,7 @@ def test_locate_executable(tmpdir, xession):
|
||||||
assert locate_executable("file4")
|
assert locate_executable("file4")
|
||||||
assert locate_executable("file2").endswith("file2.exe")
|
assert locate_executable("file2").endswith("file2.exe")
|
||||||
else:
|
else:
|
||||||
assert locate_executable("file3")
|
assert locate_executable("file3").find("bindir2") > 0
|
||||||
assert locate_executable("file1") is None
|
assert locate_executable("file1") is None
|
||||||
assert locate_executable("file4") is None
|
assert locate_executable("file4") is None
|
||||||
assert locate_executable("file2") is None
|
assert locate_executable("file2") is None
|
||||||
|
|
|
@ -89,7 +89,7 @@ def locate_file(name, env=None, check_executable=False, use_pathext=False):
|
||||||
"""
|
"""
|
||||||
env = env if env is not None else XSH.env
|
env = env if env is not None else XSH.env
|
||||||
env_path = env.get("PATH", [])
|
env_path = env.get("PATH", [])
|
||||||
paths = tuple(reversed(tuple(clear_paths(env_path))))
|
paths = tuple(tuple(clear_paths(env_path)))
|
||||||
possible_names = get_possible_names(name, env) if use_pathext else [name]
|
possible_names = get_possible_names(name, env) if use_pathext else [name]
|
||||||
|
|
||||||
for path, possible_name in itertools.product(paths, possible_names):
|
for path, possible_name in itertools.product(paths, possible_names):
|
||||||
|
|
Loading…
Add table
Reference in a new issue