2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2021-01-01 23:25:11 +08:00
|
|
|
from pathlib import Path
|
2017-12-27 11:47:26 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2021-01-01 23:25:11 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2017-07-17 07:25:09 +08:00
|
|
|
import pytest
|
2024-02-01 04:12:33 +08:00
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
|
2020-07-18 17:35:13 +08:00
|
|
|
# Test for _argcomplete but not specific for any application.
|
2013-08-06 21:33:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
def equal_with_bash(prefix, ffc, fc, out=None):
|
|
|
|
res = ffc(prefix)
|
|
|
|
res_bash = set(fc(prefix))
|
|
|
|
retval = set(res) == res_bash
|
|
|
|
if out:
|
2020-10-03 04:16:22 +08:00
|
|
|
out.write(f"equal_with_bash({prefix}) {retval} {res}\n")
|
2013-08-06 21:33:27 +08:00
|
|
|
if not retval:
|
|
|
|
out.write(" python - bash: %s\n" % (set(res) - res_bash))
|
|
|
|
out.write(" bash - python: %s\n" % (res_bash - set(res)))
|
|
|
|
return retval
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-18 17:35:13 +08:00
|
|
|
# Copied from argcomplete.completers as import from there.
|
|
|
|
# Also pulls in argcomplete.__init__ which opens filedescriptor 9.
|
|
|
|
# This gives an OSError at the end of testrun.
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
def _wrapcall(*args, **kargs):
|
|
|
|
try:
|
2019-05-28 07:31:52 +08:00
|
|
|
return subprocess.check_output(*args, **kargs).decode().splitlines()
|
2017-12-27 11:47:26 +08:00
|
|
|
except subprocess.CalledProcessError:
|
2013-08-06 21:33:27 +08:00
|
|
|
return []
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
class FilesCompleter:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""File completer class, optionally takes a list of allowed extensions."""
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
def __init__(self, allowednames=(), directories=True):
|
2013-08-06 21:33:27 +08:00
|
|
|
# Fix if someone passes in a string instead of a list
|
|
|
|
if type(allowednames) is str:
|
|
|
|
allowednames = [allowednames]
|
|
|
|
|
|
|
|
self.allowednames = [x.lstrip("*").lstrip(".") for x in allowednames]
|
|
|
|
self.directories = directories
|
|
|
|
|
|
|
|
def __call__(self, prefix, **kwargs):
|
|
|
|
completion = []
|
|
|
|
if self.allowednames:
|
|
|
|
if self.directories:
|
2020-10-03 04:16:22 +08:00
|
|
|
files = _wrapcall(["bash", "-c", f"compgen -A directory -- '{prefix}'"])
|
2017-07-17 07:25:08 +08:00
|
|
|
completion += [f + "/" for f in files]
|
2013-08-06 21:33:27 +08:00
|
|
|
for x in self.allowednames:
|
|
|
|
completion += _wrapcall(
|
2020-10-03 04:16:22 +08:00
|
|
|
["bash", "-c", f"compgen -A file -X '!*.{x}' -- '{prefix}'"]
|
2017-07-17 07:25:08 +08:00
|
|
|
)
|
2013-08-06 21:33:27 +08:00
|
|
|
else:
|
2020-10-03 04:16:22 +08:00
|
|
|
completion += _wrapcall(["bash", "-c", f"compgen -A file -- '{prefix}'"])
|
2013-08-06 21:33:27 +08:00
|
|
|
|
2020-10-03 04:16:22 +08:00
|
|
|
anticomp = _wrapcall(["bash", "-c", f"compgen -A directory -- '{prefix}'"])
|
2013-08-06 21:33:27 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
completion = list(set(completion) - set(anticomp))
|
2013-08-06 21:33:27 +08:00
|
|
|
|
|
|
|
if self.directories:
|
|
|
|
completion += [f + "/" for f in anticomp]
|
|
|
|
return completion
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestArgComplete:
|
2013-11-12 20:52:16 +08:00
|
|
|
@pytest.mark.skipif("sys.platform in ('win32', 'darwin')")
|
2021-01-01 23:25:11 +08:00
|
|
|
def test_compare_with_compgen(
|
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2013-08-06 21:33:27 +08:00
|
|
|
from _pytest._argcomplete import FastFilesCompleter
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
ffc = FastFilesCompleter()
|
|
|
|
fc = FilesCompleter()
|
2018-11-09 11:01:26 +08:00
|
|
|
|
2021-01-01 23:25:11 +08:00
|
|
|
monkeypatch.chdir(tmp_path)
|
2018-11-09 11:01:26 +08:00
|
|
|
|
2021-01-01 23:25:11 +08:00
|
|
|
assert equal_with_bash("", ffc, fc, out=sys.stdout)
|
2018-11-09 11:01:26 +08:00
|
|
|
|
2021-01-01 23:25:11 +08:00
|
|
|
tmp_path.cwd().joinpath("data").touch()
|
|
|
|
|
|
|
|
for x in ["d", "data", "doesnotexist", ""]:
|
|
|
|
assert equal_with_bash(x, ffc, fc, out=sys.stdout)
|
2013-08-06 21:33:27 +08:00
|
|
|
|
2013-11-12 20:52:16 +08:00
|
|
|
@pytest.mark.skipif("sys.platform in ('win32', 'darwin')")
|
2013-08-06 21:33:27 +08:00
|
|
|
def test_remove_dir_prefix(self):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""This is not compatible with compgen but it is with bash itself: ls /usr/<TAB>."""
|
2013-08-06 21:33:27 +08:00
|
|
|
from _pytest._argcomplete import FastFilesCompleter
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2013-08-06 21:33:27 +08:00
|
|
|
ffc = FastFilesCompleter()
|
|
|
|
fc = FilesCompleter()
|
|
|
|
for x in "/usr/".split():
|
2017-12-27 11:47:26 +08:00
|
|
|
assert not equal_with_bash(x, ffc, fc, out=sys.stdout)
|