2021-05-20 15:44:26 +05:30
|
|
|
import builtins
|
2022-01-31 21:26:34 +05:30
|
|
|
import os.path
|
2016-12-24 16:35:08 +08:00
|
|
|
from contextlib import contextmanager
|
Introduce new resolver for executables to replace commands_cache usages and speed up everything (#5544)
* remove commands_cache from pyghooks to avoid cc.update_cache on every key press
* create executables.py
* replace cc.locate_binary to locate_executable
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* vc: replace locate_binary
* pyghooks: remove commands_cache
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unused func _yield_executables
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Move `executables_in` from tools to commands_cache to avoid circular imports.
* First steps to `procs.executables` that is based on `commands_cache` and `tools`.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test_executables
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add not recommended notes
* tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add `get_paths` with test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix source test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* vc: remove tests because commands cache is not used
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* specs: fix exception for recursive call
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* specs: fix exception for recursive call
* improve test_locate_executable
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix test
* beautify pathext
* tests
* docs
* tests
* update locators
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* locate_file
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* comments
* return environ locate bin test
* comments
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add itertools
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* moving is_executable
* doc
* optimization is_executable_in_windows
* micro improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* news
* news
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* bump test
* [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>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
2024-07-09 09:44:03 +02:00
|
|
|
from pathlib import Path
|
2016-12-24 16:35:08 +08:00
|
|
|
from unittest.mock import MagicMock
|
2022-01-31 21:26:34 +05:30
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from xonsh.aliases import make_default_aliases, source_alias
|
2016-12-24 16:35:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2022-01-08 04:03:22 +05:30
|
|
|
def mockopen(xession, monkeypatch):
|
2016-12-24 16:35:08 +08:00
|
|
|
@contextmanager
|
|
|
|
def mocked_open(fpath, *args, **kwargs):
|
|
|
|
yield MagicMock(read=lambda: fpath)
|
2018-08-30 09:18:49 -05:00
|
|
|
|
|
|
|
monkeypatch.setattr(builtins, "open", mocked_open)
|
2016-12-24 16:35:08 +08:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
@pytest.fixture
|
|
|
|
def mocked_execx_checker(xession, monkeypatch):
|
2016-12-24 16:35:08 +08:00
|
|
|
checker = []
|
|
|
|
|
|
|
|
def mocked_execx(src, *args, **kwargs):
|
|
|
|
checker.append(src.strip())
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
monkeypatch.setattr(xession.builtins, "execx", mocked_execx)
|
|
|
|
return checker
|
2016-12-24 16:35:08 +08:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_source_current_dir(mockopen, monkeypatch, mocked_execx_checker):
|
|
|
|
monkeypatch.setattr(os.path, "isfile", lambda x: True)
|
|
|
|
source_alias(["foo", "bar"])
|
|
|
|
assert mocked_execx_checker == ["foo", "bar"]
|
2016-12-24 16:35:08 +08:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
Introduce new resolver for executables to replace commands_cache usages and speed up everything (#5544)
* remove commands_cache from pyghooks to avoid cc.update_cache on every key press
* create executables.py
* replace cc.locate_binary to locate_executable
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* vc: replace locate_binary
* pyghooks: remove commands_cache
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unused func _yield_executables
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Move `executables_in` from tools to commands_cache to avoid circular imports.
* First steps to `procs.executables` that is based on `commands_cache` and `tools`.
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* test_executables
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add not recommended notes
* tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add `get_paths` with test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix source test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* vc: remove tests because commands cache is not used
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* specs: fix exception for recursive call
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* specs: fix exception for recursive call
* improve test_locate_executable
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix test
* beautify pathext
* tests
* docs
* tests
* update locators
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* locate_file
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* comments
* return environ locate bin test
* comments
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* Update xonsh/procs/executables.py
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add itertools
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* moving is_executable
* doc
* optimization is_executable_in_windows
* micro improvements
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* news
* news
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* bump test
* [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>
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
2024-07-09 09:44:03 +02:00
|
|
|
def test_source_path(mockopen, mocked_execx_checker, xession):
|
|
|
|
with xession.env.swap(PATH=[Path(__file__).parent.parent / "bin"]):
|
|
|
|
source_alias(["foo", "bar"])
|
2022-03-24 00:52:02 +05:30
|
|
|
path_foo = os.path.join("bin", "foo")
|
|
|
|
path_bar = os.path.join("bin", "bar")
|
2021-05-20 15:44:26 +05:30
|
|
|
assert mocked_execx_checker[0].endswith(path_foo)
|
|
|
|
assert mocked_execx_checker[1].endswith(path_bar)
|
2021-11-27 16:28:04 +05:30
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"alias",
|
|
|
|
[
|
|
|
|
"source-bash",
|
|
|
|
"source-zsh",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_source_foreign_fn_parser(alias, xession):
|
|
|
|
aliases = make_default_aliases()
|
|
|
|
source_bash = aliases[alias]
|
|
|
|
|
|
|
|
positionals = [act.dest for act in source_bash.parser._get_positional_actions()]
|
|
|
|
options = [act.dest for act in source_bash.parser._get_optional_actions()]
|
|
|
|
|
|
|
|
assert positionals == ["files_or_code"]
|
|
|
|
assert options == [
|
|
|
|
"help",
|
|
|
|
"interactive",
|
|
|
|
"login",
|
|
|
|
"envcmd",
|
|
|
|
"aliascmd",
|
|
|
|
"extra_args",
|
|
|
|
"safe",
|
|
|
|
"prevcmd",
|
|
|
|
"postcmd",
|
|
|
|
"funcscmd",
|
|
|
|
"sourcer",
|
|
|
|
"use_tmpfile",
|
|
|
|
"seterrprevcmd",
|
|
|
|
"seterrpostcmd",
|
|
|
|
"overwrite_aliases",
|
|
|
|
"suppress_skip_message",
|
|
|
|
"show",
|
|
|
|
"dryrun",
|
2024-04-18 10:21:39 -04:00
|
|
|
"interactive",
|
2021-11-27 16:28:04 +05:30
|
|
|
]
|