Skip to content

Commit

Permalink
test: make at-point completion tests easier
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Apr 26, 2020
1 parent 32421d1 commit e9450b8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
61 changes: 43 additions & 18 deletions test/t/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,34 +557,59 @@ def completion(request, bash: pexpect.spawn) -> CompletionResult:
) % ((cmd,) * 2)
if marker.kwargs.get("require_cmd") and not is_bash_type(bash, cmd):
pytest.skip("Command not found")

if "trail" in marker.kwargs:
return assert_complete_at_point(
bash, cmd=marker.args[0], trail=marker.kwargs["trail"]
)

return assert_complete(bash, marker.args[0], **marker.kwargs)


def complete_at_point(
bash: pexpect.spawn, cmd: str, trail: str, expected: str
) -> bool:
def assert_complete_at_point(
bash: pexpect.spawn, cmd: str, trail: str
) -> CompletionResult:
# TODO: merge to assert_complete
fullcmd = "%s%s%s" % (
cmd,
trail,
"\002" * len(trail),
) # \002 = ^B = cursor left
bash.send(fullcmd + "\t")
bash.expect_exact(fullcmd.replace("\002", "\b"))
bash.send(MAGIC_MARK)
bash.expect(r"\r\n%s\r\n" % expected)
bash.expect_exact(PS1 + fullcmd.replace("\002", "\b"))

# At this point, something weird happens. For most test setups, as
# expected (pun intended!), MAGIC_MARK follows as is. But for some
# others (e.g. CentOS 6, Ubuntu 14 test containers), we get MAGIC_MARK
# one character a time, followed each time by trail and the corresponding
# number of \b's. Don't know why, but accept it until/if someone finds out.
# Or just be fine with it indefinitely, the visible and practical end
# result on a terminal is the same anyway.
repeat = "(%s%s)?" % (re.escape(trail), "\b" * len(trail))
fullexpected = "".join("%s%s" % (re.escape(x), repeat) for x in MAGIC_MARK)
bash.expect(fullexpected)
return True
bash.expect_exact(fullcmd.replace("\002", "\b"))

got = bash.expect_exact(
[
# 0: multiple lines, result in .before
PS1 + fullcmd.replace("\002", "\b"),
# 1: no completion
MAGIC_MARK,
pexpect.EOF,
pexpect.TIMEOUT,
]
)
if got == 0:
output = bash.before
result = CompletionResult(output)

# At this point, something weird happens. For most test setups, as
# expected (pun intended!), MAGIC_MARK follows as is. But for some
# others (e.g. CentOS 6, Ubuntu 14 test containers), we get MAGIC_MARK
# one character a time, followed each time by trail and the corresponding
# number of \b's. Don't know why, but accept it until/if someone finds out.
# Or just be fine with it indefinitely, the visible and practical end
# result on a terminal is the same anyway.
repeat = "(%s%s)?" % (re.escape(trail), "\b" * len(trail))
fullexpected = "".join(
"%s%s" % (re.escape(x), repeat) for x in MAGIC_MARK
)
bash.expect(fullexpected)
else:
# TODO: warn about EOF/TIMEOUT?
result = CompletionResult("", [])

return result


def in_container() -> bool:
Expand Down
9 changes: 3 additions & 6 deletions test/t/test_alias.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest

from conftest import complete_at_point


@pytest.mark.bashcomp(
pre_cmds=("unalias -a", "alias foo=bar", "alias bar='foo foo'"),
Expand All @@ -18,7 +16,6 @@ def test_2(self, completion):
assert completion == "foo='bar'"
assert not completion.endswith(" ")

def test_alias_at_point(self, bash):
assert complete_at_point(
bash=bash, cmd="alias ", trail="foo", expected=r"bar\s+foo\s*?"
)
@pytest.mark.complete("alias ", trail="foo")
def test_alias_at_point(self, completion):
assert completion == "bar foo".split()
12 changes: 3 additions & 9 deletions test/t/test_cd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest

from conftest import complete_at_point


@pytest.mark.bashcomp(ignore_env=r"^\+CDPATH=$")
class TestCd:
Expand All @@ -23,10 +21,6 @@ def test_3(self, completion):
def test_4(self, completion):
assert not completion # No subdirs nor CDPATH

def test_dir_at_point(self, bash):
assert complete_at_point(
bash=bash,
cmd="cd shared/default/",
trail="foo",
expected=r"bar bar\.d/\s+foo\.d/",
)
@pytest.mark.complete("cd shared/default/", trail="foo")
def test_dir_at_point(self, completion):
assert completion == ["bar bar.d/", "foo.d/"]

0 comments on commit e9450b8

Please sign in to comment.