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 13, 2018
1 parent 1389110 commit 0db4768
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 23 deletions.
23 changes: 0 additions & 23 deletions test/lib/completions/sudo.exp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ foreach ug {user group} {

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

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

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

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

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

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

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

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

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


Expand Down
70 changes: 70 additions & 0 deletions test/t/test_sudo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import pytest

from conftest import (
assert_bash_exec, assert_complete, find_unique_completion_pair,
)


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 All @@ -25,3 +49,49 @@ def test_4(self, completion):
@pytest.mark.complete("sudo -e -u root bar foo", cwd="shared/default")
def test_5(self, completion):
assert completion.list == ["foo", "foo.d/"]

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

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

def test_8(self, bash, part_full_group):
part, full = part_full_group
completion = assert_complete(bash, "sudo 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_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:"):
completion = assert_complete(
bash, "sudo chown %s%s" % (prefix, part))
assert completion.list == ["%s%s" % (prefix, full)]
assert completion.line.endswith(" ")

def test_10(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, "sudo chown %s%s:%s" % (user, x * "\\", partgroup))
assert not completion.list

def test_11(self, bash, part_full_group):
"""Test graful 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

0 comments on commit 0db4768

Please sign in to comment.