Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions test/t/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@ def assert_bash_exec(
return output


def _bash_copy_variable(bash: pexpect.spawn, src_var: str, dst_var: str):
assert_bash_exec(
bash,
"if [[ ${%s+set} ]]; then %s=${%s}; else unset -v %s; fi"
% (src_var, dst_var, src_var, dst_var),
)


def bash_save_variable(
bash: pexpect.spawn, varname: str, new_value: Optional[str] = None
):
_bash_copy_variable(bash, varname, "_bash_completion_test_" + varname)
if new_value:
assert_bash_exec(
bash, "%s=%s" % (varname, shlex.quote(str(new_value)))
)


def bash_restore_variable(bash: pexpect.spawn, varname: str):
_bash_copy_variable(bash, "_bash_completion_test_" + varname, varname)


def get_env(bash: pexpect.spawn) -> List[str]:
return [
x
Expand All @@ -410,7 +432,10 @@ def diff_env(before: List[str], after: List[str], ignore: str):
# Remove unified diff markers:
if not re.search(r"^(---|\+\+\+|@@ )", x)
# Ignore variables expected to change:
and not re.search("^[-+](_|PPID|BASH_REMATCH|OLDPWD)=", x)
and not re.search(
"^[-+](_|PPID|BASH_REMATCH|_bash_completion_test_[a-zA-Z_0-9]*)=",
x,
)
# Ignore likely completion functions added by us:
and not re.search(r"^\+declare -f _.+", x)
# ...and additional specified things:
Expand Down Expand Up @@ -496,6 +521,7 @@ def assert_complete(
pytest.xfail(xfail)
cwd = kwargs.get("cwd")
if cwd:
bash_save_variable(bash, "OLDPWD")
assert_bash_exec(bash, "cd '%s'" % cwd)
env_prefix = "_BASHCOMP_TEST_"
env = kwargs.get("env", {})
Expand Down Expand Up @@ -560,6 +586,7 @@ def assert_complete(
)
if cwd:
assert_bash_exec(bash, "cd - >/dev/null")
bash_restore_variable(bash, "OLDPWD")
return result


Expand Down Expand Up @@ -676,21 +703,41 @@ def prepare_fixture_dir(
tempdir = Path(tempfile.mkdtemp(prefix="bash-completion-fixture-dir"))
request.addfinalizer(lambda: shutil.rmtree(str(tempdir)))

old_cwd = os.getcwd()
try:
os.chdir(tempdir)
new_files, new_dirs = create_dummy_filedirs(files, dirs)
finally:
os.chdir(old_cwd)

return tempdir, new_files, new_dirs


def create_dummy_filedirs(
files: Iterable[str], dirs: Iterable[str]
) -> Tuple[List[str], List[str]]:
"""
Create dummy files and directories on the fly in the current directory.

Tests that contain filenames differing only by case should use this to
prepare a dir on the fly rather than including their fixtures in git and
the tarball. This is to work better with case insensitive file systems.
"""
new_files = []
new_dirs = []

for dir_ in dirs:
path = tempdir / dir_
path = Path(dir_)
if not path.exists():
path.mkdir()
new_dirs.append(dir_)
for file_ in files:
path = tempdir / file_
path = Path(file_)
if not path.exists():
path.touch()
new_files.append(file_)

return tempdir, sorted(new_files), sorted(new_dirs)
return sorted(new_files), sorted(new_dirs)


class TestUnitBase:
Expand Down
23 changes: 5 additions & 18 deletions test/t/test_evince.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import shlex
from pathlib import Path
from typing import List, Tuple

import pytest

from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
from conftest import assert_complete, create_dummy_filedirs


@pytest.mark.bashcomp(temp_cwd=True)
class TestEvince:
@pytest.fixture(scope="class")
def setup_fixture(self, request) -> Tuple[Path, List[str], List[str]]:
return prepare_fixture_dir(
request,
def test_1(self, bash):
files, dirs = create_dummy_filedirs(
(
".bmp .BMP .cbr .CBR .cbz .CBZ .djv .DJV .djvu .DJVU .dvi "
".DVI .dvi.bz2 .dvi.BZ2 .DVI.bz2 .DVI.BZ2 .dvi.gz .dvi.GZ "
Expand All @@ -27,15 +22,7 @@ def setup_fixture(self, request) -> Tuple[Path, List[str], List[str]]:
"foo".split(),
)

def test_1(self, bash, setup_fixture):
fixture_dir, files, dirs = setup_fixture

assert_bash_exec(bash, "cd %s" % shlex.quote(str(fixture_dir)))
try:
completion = assert_complete(bash, "evince ")
finally:
assert_bash_exec(bash, "cd -", want_output=None)

completion = assert_complete(bash, "evince ")
assert completion == [
x
for x in sorted(files + ["%s/" % d for d in dirs])
Expand Down
23 changes: 5 additions & 18 deletions test/t/test_kdvi.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
import shlex
from pathlib import Path
from typing import List, Tuple

import pytest

from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
from conftest import assert_complete, create_dummy_filedirs


@pytest.mark.bashcomp(temp_cwd=True)
class TestKdvi:
@pytest.fixture(scope="class")
def setup_fixture(self, request) -> Tuple[Path, List[str], List[str]]:
return prepare_fixture_dir(
request,
def test_1(self, bash):
files, dirs = create_dummy_filedirs(
(
".dvi .DVI .dvi.bz2 .DVI.bz2 .dvi.gz .DVI.gz .dvi.Z .DVI.Z "
".txt"
).split(),
"foo".split(),
)

def test_1(self, bash, setup_fixture):
fixture_dir, files, dirs = setup_fixture

assert_bash_exec(bash, "cd %s" % shlex.quote(str(fixture_dir)))
try:
completion = assert_complete(bash, "kdvi ")
finally:
assert_bash_exec(bash, "cd -", want_output=None)

completion = assert_complete(bash, "kdvi ")
assert completion == [
x
for x in sorted(files + ["%s/" % d for d in dirs])
Expand Down
23 changes: 5 additions & 18 deletions test/t/test_kpdf.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import shlex
from pathlib import Path
from typing import List, Tuple

import pytest

from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
from conftest import assert_complete, create_dummy_filedirs


@pytest.mark.bashcomp(temp_cwd=True)
class TestKpdf:
@pytest.fixture(scope="class")
def setup_fixture(self, request) -> Tuple[Path, List[str], List[str]]:
return prepare_fixture_dir(
request,
def test_1(self, bash):
files, dirs = create_dummy_filedirs(
".eps .EPS .pdf .PDF .ps .PS .txt".split(),
"foo".split(),
)

def test_1(self, bash, setup_fixture):
fixture_dir, files, dirs = setup_fixture

assert_bash_exec(bash, "cd %s" % shlex.quote(str(fixture_dir)))
try:
completion = assert_complete(bash, "kpdf ")
finally:
assert_bash_exec(bash, "cd -", want_output=None)

completion = assert_complete(bash, "kpdf ")
assert completion == [
x
for x in sorted(files + ["%s/" % d for d in dirs])
Expand Down
19 changes: 11 additions & 8 deletions test/t/test_man.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

from conftest import assert_bash_exec, assert_complete, prepare_fixture_dir
from conftest import (
assert_bash_exec,
assert_complete,
bash_restore_variable,
bash_save_variable,
prepare_fixture_dir,
)


@pytest.mark.bashcomp(
Expand Down Expand Up @@ -103,15 +109,12 @@ def test_9(self, bash, completion):

@pytest.mark.complete(require_cmd=True)
def test_10(self, request, bash, colonpath):
assert_bash_exec(
bash,
'manpath=${MANPATH-}; export MANPATH="%s:%s/man"'
% (TestMan.manpath, colonpath),
)
request.addfinalizer(
lambda: assert_bash_exec(bash, "MANPATH=$manpath")
bash_save_variable(
bash, "MANPATH", "%s:%s/man" % (TestMan.manpath, colonpath)
)
assert_bash_exec(bash, "export MANPATH")
completion = assert_complete(bash, "man Bash::C")
bash_restore_variable(bash, "MANPATH")
assert completion == "ompletion"

@pytest.mark.complete("man -", require_cmd=True)
Expand Down
20 changes: 11 additions & 9 deletions test/t/unit/test_unit_known_hosts_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import pytest

from conftest import assert_bash_exec
from conftest import (
assert_bash_exec,
bash_restore_variable,
bash_save_variable,
)


@pytest.mark.bashcomp(
Expand Down Expand Up @@ -126,23 +130,20 @@ def test_included_configs(self, bash, hosts):
# fixtures/_known_hosts_real/.ssh/config_question_mark
expected.append("question_mark")

assert_bash_exec(
bash, 'OLDHOME="$HOME"; HOME="%s/_known_hosts_real"' % bash.cwd
)
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
output = assert_bash_exec(
bash,
"unset -v COMPREPLY COMP_KNOWN_HOSTS_WITH_HOSTFILE; "
"_known_hosts_real -aF _known_hosts_real/config_include ''; "
r'printf "%s\n" "${COMPREPLY[@]}"',
want_output=True,
)
assert_bash_exec(bash, 'HOME="$OLDHOME"')
bash_restore_variable(bash, "HOME")
assert sorted(set(output.strip().split())) == sorted(expected)

def test_no_globbing(self, bash):
assert_bash_exec(
bash, 'OLDHOME="$HOME"; HOME="%s/_known_hosts_real"' % bash.cwd
)
bash_save_variable(bash, "HOME", "%s/_known_hosts_real" % bash.cwd)
bash_save_variable(bash, "OLDPWD")
output = assert_bash_exec(
bash,
"cd _known_hosts_real; "
Expand All @@ -152,7 +153,8 @@ def test_no_globbing(self, bash):
"cd - &>/dev/null",
want_output=True,
)
assert_bash_exec(bash, 'HOME="$OLDHOME"')
bash_restore_variable(bash, "OLDPWD")
bash_restore_variable(bash, "HOME")
completion = sorted(set(output.strip().split()))
assert "gee" in completion
assert "gee-filename-canary" not in completion