Skip to content

Commit

Permalink
test: Convert some more test cases to pytest+pexpect
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Aug 16, 2018
1 parent 0db4768 commit b6f4e50
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 67 deletions.
23 changes: 0 additions & 23 deletions test/lib/completions/chown.exp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ foreach ug {user group} {

# These tests require an unique completion.
if {!$failed_find_unique_completion} {
assert_complete $fulluser "chown $partuser"
sync_after_int

assert_complete $fulluser:$fullgroup "chown $fulluser:$partgroup"
sync_after_int

assert_complete "dot.user:$fullgroup" "chown dot.user:$partgroup"
sync_after_int

foreach prefix {
"funky\\ user:" "funky.user:" "funky\\.user:" "fu\\ nky.user:"
Expand All @@ -45,21 +37,6 @@ if {!$failed_find_unique_completion} {
assert_complete $prefix$fullgroup "chown $prefix$partgroup" $test
sync_after_int
}

# Check that we give up in degenerate cases instead of spewing various junk.

assert_no_complete "chown $fulluser\\\\:$partgroup"
sync_after_int

assert_no_complete "chown $fulluser\\\\\\:$partgroup"
sync_after_int

assert_no_complete "chown $fulluser\\\\\\\\:$partgroup"
sync_after_int

# Colons in user/groupnames are not usually allowed.
assert_no_complete "chown foo:bar:$partgroup"
sync_after_int
}


Expand Down
22 changes: 22 additions & 0 deletions test/t/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ def find_unique_completion_pair(
return result


@pytest.fixture(autouse=True, scope="class")
def part_full_user(bash: pexpect.spawn) -> Optional[Tuple[str, str]]:
res = assert_bash_exec(
bash, "compgen -u", want_output=True,
).strip().split()
pair = find_unique_completion_pair(res)
if not pair:
pytest.skip("No suitable test user found")
return pair


@pytest.fixture(autouse=True, scope="class")
def part_full_group(bash: pexpect.spawn) -> Optional[Tuple[str, str]]:
res = assert_bash_exec(
bash, "compgen -g", want_output=True,
).strip().split()
pair = find_unique_completion_pair(res)
if not pair:
pytest.skip("No suitable test user found")
return pair


@pytest.fixture(autouse=True, scope="class")
def bash(request) -> pexpect.spawn:

Expand Down
64 changes: 56 additions & 8 deletions test/t/test_chown.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import getpass

import pytest

from conftest import assert_bash_exec
from conftest import assert_bash_exec, assert_complete


@pytest.mark.bashcomp(
pre_cmds=(
# Fake root command to get all users/groups completed at least for now
"root_command=sudo",
),
)
class TestChown:

@pytest.mark.complete("chown ")
def test_1(self, bash, completion):
if getpass.getuser() == "root":
users = sorted(assert_bash_exec(
bash, "compgen -A user", want_output=True).split())
else:
users = [getpass.getuser()]
users = sorted(assert_bash_exec(
bash, "compgen -A user", want_output=True).split())
assert completion.list == users

@pytest.mark.xfail # TODO: whitespace split issue
Expand All @@ -25,3 +26,50 @@ def test_2(self, completion):
@pytest.mark.complete("chown :foo shared/default/")
def test_3(self, completion):
assert completion.list == ["bar", "bar bar.d/", "foo", "foo.d/"]

def test_4(self, bash, part_full_user):
part, full = part_full_user
completion = assert_complete(bash, "chown %s" % part)
assert completion.list == [full]
assert completion.line.endswith(" ")

def test_5(self, bash, part_full_user, part_full_group):
_, user = part_full_user
partgroup, fullgroup = part_full_group
completion = assert_complete(
bash, "chown %s:%s" % (user, partgroup))
assert completion.list == ["%s:%s" % (user, fullgroup)]
assert completion.line.endswith(" ")

def test_6(self, bash, part_full_group):
part, full = part_full_group
completion = assert_complete(bash, "chown dot.user:%s" % part)
assert completion.list == ["dot.user:%s" % full]
assert completion.line.endswith(" ")

@pytest.mark.xfail # TODO check escaping, whitespace
def test_7(self, bash, part_full_group):
"""Test preserving special chars in $prefix$partgroup<TAB>."""
part, full = part_full_group
for prefix in (r"funky\ user:", "funky.user:", r"funky\.user:",
r"fu\ nky.user:", r"f\ o\ o\.\bar:",
r"foo\_b\ a\.r\ :"):
completion = assert_complete(
bash, "chown %s%s" % (prefix, part))
assert completion.list == ["%s%s" % (prefix, full)]
assert completion.line.endswith(" ")

def test_8(self, bash, part_full_user, part_full_group):
"""Test giving up on degenerate cases instead of spewing junk."""
_, user = part_full_user
partgroup, _ = part_full_group
for x in range(2, 5):
completion = assert_complete(
bash, "chown %s%s:%s" % (user, x * "\\", partgroup))
assert not completion.list

def test_9(self, bash, part_full_group):
"""Test graceful fail on colon in user/group name."""
part, _ = part_full_group
completion = assert_complete(bash, "chown foo:bar:%s" % part)
assert not completion.list
25 changes: 3 additions & 22 deletions test/t/test_sudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@

class TestSudo:

@pytest.fixture
def part_full_user(self, bash):
res = assert_bash_exec(
bash, "compgen -u", want_output=True,
).strip().split()
pair = find_unique_completion_pair(res)
if not pair:
pytest.skip("No suitable test user found")
return pair

@pytest.fixture
def part_full_group(self, bash):
res = assert_bash_exec(
bash, "compgen -g", want_output=True,
).strip().split()
pair = find_unique_completion_pair(res)
if not pair:
pytest.skip("No suitable test user found")
return pair

@pytest.mark.complete("sudo -")
def test_1(self, completion):
assert completion.list
Expand Down Expand Up @@ -75,7 +55,8 @@ def test_9(self, bash, part_full_group):
"""Test preserving special chars in $prefix$partgroup<TAB>."""
part, full = part_full_group
for prefix in (r"funky\ user:", "funky.user:", r"funky\.user:",
r"fu\ nky.user:"):
r"fu\ nky.user:", r"f\ o\ o\.\bar:",
r"foo\_b\ a\.r\ :"):
completion = assert_complete(
bash, "sudo chown %s%s" % (prefix, part))
assert completion.list == ["%s%s" % (prefix, full)]
Expand All @@ -91,7 +72,7 @@ def test_10(self, bash, part_full_user, part_full_group):
assert not completion.list

def test_11(self, bash, part_full_group):
"""Test graful fail on colon in user/group name."""
"""Test graceful fail on colon in user/group name."""
part, _ = part_full_group
completion = assert_complete(bash, "sudo chown foo:bar:%s" % part)
assert not completion.list
18 changes: 4 additions & 14 deletions test/t/unit/test_unit_tilde.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
@pytest.mark.bashcomp(cmd=None, ignore_env=r"^\+COMPREPLY=")
class TestUnitTilde:

@pytest.fixture
def part_full(self, bash):
res = assert_bash_exec(
bash, "compgen -u", want_output=True,
).strip().split()
pair = find_unique_completion_pair(res)
if not pair:
pytest.skip("No suitable test user found")
return pair

def test_1(self, bash):
assert_bash_exec(bash, "_tilde >/dev/null")

Expand All @@ -38,12 +28,12 @@ def _test_part_full(self, bash, part, full):
assert res
assert res[0] == "~%s" % full

def test_4(self, bash, part_full):
def test_4(self, bash, part_full_user):
"""~full should complete to ~full unmodified."""
_, full = part_full
_, full = part_full_user
self._test_part_full(bash, full, full)

def test_5(self, bash, part_full):
def test_5(self, bash, part_full_user):
"""~part should complete to ~full."""
part, full = part_full
part, full = part_full_user
self._test_part_full(bash, part, full)

0 comments on commit b6f4e50

Please sign in to comment.