xonsh/tests/completers/test_environment_completer.py
Andy Kipp a3e8b1a025
Use substring for env completion and better way to sort list (#5388)
### Motivation

It's very annoying to search env variable exactly by lprefix. The better
is to search by substring and sort results by the position of substring
and then alphabetically.

Closes #5386

### Before

```xsh
$TRA<Tab>
# nothing
```

### After
```xsh
$TRA<Tab>
# 'XONSH_TRACE_COMPLETIONS', 
# 'XONSH_TRACE_SUBPROC', 
# 'XONSH_TRACE_SUBPROC_FUNC', 
# 'XONSH_TRACEBACK_LOGFILE', 
# 'XONSH_SHOW_TRACEBACK', 
# 'VC_GIT_INCLUDE_UNTRACKED'
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-03 10:30:14 +02:00

41 lines
1.1 KiB
Python

import pytest
from xonsh.completers.environment import complete_environment_vars
from xonsh.parsers.completion_context import CompletionContextParser
@pytest.fixture(scope="session")
def parser():
return CompletionContextParser()
@pytest.mark.parametrize(
"cmd",
(
"ls $WOW",
"ls /home/$WOW",
"ls '/home/$WOW'",
"ls @('hi ' + $WOW",
),
)
def test_simple(cmd, xession, monkeypatch, parser):
xession.env.update({"WOWZER": 1})
context = parser.parse(cmd, len(cmd))
comps, lprefix = complete_environment_vars(context)
# account for the ending quote
if cmd[-1] in "'":
assert lprefix == 5
else:
assert lprefix == 4
assert set(comps) == {"$WOWZER"}
def test_rich_completions(xession, monkeypatch, parser):
xession.env.update({"WOWZER": 1})
xession.env.register("WOWZER", type=int, doc="Nice Docs!")
context = parser.parse("$WOW", 4)
completion = next(complete_environment_vars(context)[0])
assert completion.display == "$WOWZER [int]"
assert completion.description == "Nice Docs!"