2020-11-16 14:46:42 +00:00
|
|
|
import tempfile
|
2022-01-31 21:26:34 +05:30
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2016-09-25 15:07:42 -04:00
|
|
|
import xonsh.completers.path as xcp
|
|
|
|
|
2017-11-15 13:03:45 -05:00
|
|
|
|
2018-06-16 07:30:15 -04:00
|
|
|
@pytest.fixture(autouse=True)
|
2021-05-20 15:44:26 +05:30
|
|
|
def xonsh_execer_autouse(xession, xonsh_execer):
|
2018-06-16 07:30:15 -04:00
|
|
|
return xonsh_execer
|
|
|
|
|
|
|
|
|
2016-09-25 15:07:42 -04:00
|
|
|
def test_pattern_need_quotes():
|
|
|
|
# just make sure the regex compiles
|
2018-08-30 09:18:49 -05:00
|
|
|
xcp.PATTERN_NEED_QUOTES.match("")
|
2017-11-15 13:03:45 -05:00
|
|
|
|
|
|
|
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_complete_path(xession, completion_context_parse):
|
|
|
|
xession.env = {
|
2018-08-30 09:18:49 -05:00
|
|
|
"CASE_SENSITIVE_COMPLETIONS": False,
|
|
|
|
"GLOB_SORTED": True,
|
|
|
|
"SUBSEQUENCE_PATH_COMPLETION": False,
|
|
|
|
"FUZZY_PATH_COMPLETION": False,
|
|
|
|
"SUGGEST_THRESHOLD": 3,
|
|
|
|
"CDPATH": set(),
|
2017-11-15 13:03:45 -05:00
|
|
|
}
|
2021-05-11 22:49:45 +03:00
|
|
|
xcp.complete_path(completion_context_parse("[1-0.1]", 7))
|
2018-06-16 07:30:15 -04:00
|
|
|
|
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
@patch("xonsh.completers.path._add_cdpaths")
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_cd_path_no_cd(mock_add_cdpaths, xession, completion_context_parse):
|
|
|
|
xession.env = {
|
2018-08-30 09:18:49 -05:00
|
|
|
"CASE_SENSITIVE_COMPLETIONS": False,
|
|
|
|
"GLOB_SORTED": True,
|
|
|
|
"SUBSEQUENCE_PATH_COMPLETION": False,
|
|
|
|
"FUZZY_PATH_COMPLETION": False,
|
|
|
|
"SUGGEST_THRESHOLD": 3,
|
|
|
|
"CDPATH": ["/"],
|
2018-06-16 07:30:15 -04:00
|
|
|
}
|
2021-05-11 22:49:45 +03:00
|
|
|
xcp.complete_path(completion_context_parse("cat a", 5))
|
2018-06-16 07:30:15 -04:00
|
|
|
mock_add_cdpaths.assert_not_called()
|
2020-11-16 14:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("quote", ('"', "'"))
|
2021-05-20 15:44:26 +05:30
|
|
|
def test_complete_path_when_prefix_is_raw_path_string(
|
|
|
|
quote, xession, completion_context_parse
|
|
|
|
):
|
|
|
|
xession.env = {
|
2020-11-16 14:46:42 +00:00
|
|
|
"CASE_SENSITIVE_COMPLETIONS": True,
|
|
|
|
"GLOB_SORTED": True,
|
|
|
|
"SUBSEQUENCE_PATH_COMPLETION": False,
|
|
|
|
"FUZZY_PATH_COMPLETION": False,
|
|
|
|
"SUGGEST_THRESHOLD": 1,
|
|
|
|
"CDPATH": set(),
|
|
|
|
}
|
|
|
|
with tempfile.NamedTemporaryFile(suffix="_dummySuffix") as tmp:
|
|
|
|
prefix_file_name = tmp.name.replace("_dummySuffix", "")
|
|
|
|
prefix = f"pr{quote}{prefix_file_name}"
|
|
|
|
line = f"ls {prefix}"
|
2021-05-11 22:49:45 +03:00
|
|
|
out = xcp.complete_path(completion_context_parse(line, len(line)))
|
2020-11-16 14:46:42 +00:00
|
|
|
expected = f"pr{quote}{tmp.name}{quote}"
|
|
|
|
assert expected == out[0].pop()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("prefix", ("", "r", "p", "pr", "rp"))
|
|
|
|
def test_path_from_partial_string(prefix):
|
|
|
|
string = "hello"
|
|
|
|
quote = "'"
|
|
|
|
out = xcp._path_from_partial_string(f"{prefix}{quote}{string}{quote}")
|
|
|
|
if "r" in prefix:
|
|
|
|
expected = (f"r{quote}{string}{quote}", string, f"{prefix}{quote}", quote)
|
|
|
|
else:
|
|
|
|
expected = (f"{quote}{string}{quote}", string, f"{prefix}{quote}", quote)
|
|
|
|
assert out == expected
|
2021-09-07 01:23:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("num_args", (0, 1, 2, 3))
|
|
|
|
def test_path_in_python_code(num_args, completion_context_parse):
|
|
|
|
with tempfile.NamedTemporaryFile(prefix="long_name") as tmp:
|
|
|
|
args = []
|
|
|
|
if num_args:
|
|
|
|
args = ["blah"] * 3 + [tmp.name[:-2]]
|
|
|
|
args = args[-num_args:]
|
|
|
|
|
|
|
|
inner_line = " ".join(map(repr, args))
|
|
|
|
exp = xcp.complete_path(completion_context_parse(inner_line, len(inner_line)))
|
|
|
|
line = "@(" + inner_line
|
|
|
|
out = xcp.complete_path(completion_context_parse(line, len(line)))
|
|
|
|
assert out == exp
|