1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
import pytest
from pytest_subprocess.utils import Any
from pytest_subprocess.utils import Command
def check_match(command_instance, command):
assert command_instance == command
assert command_instance == tuple(command)
assert command_instance == " ".join(command)
return True
def check_not_match(command_instance, command):
assert command_instance != command
assert command_instance != tuple(command)
assert command_instance != " ".join(command)
return True
@pytest.mark.parametrize("command", ("whoami", ["whoami"]))
def test_simple_command(command):
command = Command(command)
assert check_match(command, ["whoami"])
@pytest.mark.parametrize("command", ("test command", ("test", "command")))
def test_more_complex_command(command):
command = Command(command)
assert check_match(command, ["test", "command"])
assert check_not_match(command, ["other", "command"])
def test_simple_wildcards():
command = Command([Any()])
assert check_match(command, ["test"])
assert check_match(command, ["not_test"])
assert check_match(command, ["something", "a", "bit", "longer"])
assert check_match(command, ["basically", "everything", "will", "match"])
command = Command(["test", Any()])
assert check_match(command, ["test", "something"])
assert check_match(command, ["test", "something_else"])
assert check_not_match(command, ["something", "test"])
assert check_match(command, ["test"])
command = Command([Any(), "test"])
assert check_match(command, ["something", "test"])
assert check_match(command, ["something_else", "test"])
assert check_match(command, ["test"])
assert check_not_match(command, ["test", "something"])
command = Command(["test", Any(), "other_test"])
assert check_match(command, ["test", "something", "other_test"])
assert check_match(command, ["test", "something_else", "other_test"])
assert check_match(command, ["test", "other_test"])
assert check_not_match(command, ["test", "something_else"])
def test_more_complex_wildcards():
command = Command(["test", Any()])
assert check_match(command, ["test", "with", "more", "arguments"])
command = Command(["test", Any(), "end"])
assert check_match(command, ["test", "blah", "end"])
assert check_match(command, ["test", "with", "more", "arguments", "end"])
assert check_not_match(
command, ["test", "with", "more", "arguments", "invalid_end"]
)
def test_any_max():
command = Command([Any(max=1)])
assert check_match(command, ["test"])
assert check_match(command, ["other_test"])
assert check_not_match(command, ["two", "arguments"])
assert check_not_match(command, ["th", "ree", "arguments"])
command = Command(["test", Any(max=3)])
assert check_match(command, ["test", "max", "3", "args"])
assert check_match(command, ["test", "can_be", "less"])
assert check_match(command, ["test"])
assert check_not_match(command, ["wrong", "first", "argument"])
assert check_not_match(command, ["test", "more", "than", "3", "args"])
command = Command(["test", Any(max=2), "end"])
assert check_match(command, ["test", "two", "args", "end"])
assert check_match(command, ["test", "one_arg", "end"])
assert check_not_match(command, ["test", "oops", "three", "args", "end"])
assert check_not_match(command, ["test", "two", "args", "wrong_end"])
command = Command(["test", Any(max=1), "middle", Any(max=2), "end"])
assert check_match(
command, ["test", "one_argument", "middle", "two", "args", "end"]
)
assert check_match(command, ["test", "middle", "one_arg", "end"])
assert check_not_match(
command, ["test", "two", "args", "middle", "two", "args", "end"]
)
def test_any_min():
command = Command([Any(min=2)])
assert check_match(command, ["any", "two"])
assert check_match(command, ["or", "even", "three"])
assert check_not_match(command, ["but_not_one"])
command = Command(["test", Any(min=1), "end"])
assert check_match(command, ["test", "with", "more", "arguments", "end"])
assert check_match(command, ["test", "only_one", "end"])
assert check_not_match(command, ["only_one", "end"])
assert check_not_match(command, ["test", "only_one"])
assert check_not_match(command, ["test", "end"])
command = Command(["test", Any(min=1), "middle", Any(min=2), "end"])
assert check_match(
command, ["test", "one_argument", "middle", "two", "args", "end"]
)
assert check_not_match(command, ["test", "middle", "two", "args", "end"])
assert check_not_match(
command, ["test", "one_argument", "middle", "one_argument", "end"]
)
def test_min_max_combined():
command = Command([Any(min=2, max=4)])
assert check_match(command, ["just", "two"])
assert check_match(command, ["now", "three", "arguments"])
assert check_match(command, ["up", "to", "even", "four"])
assert check_not_match(command, ["single_argument"])
assert check_not_match(command, ["five", "is", "little", "too", "many"])
command = Command(["start", Any(min=1, max=1), "end"])
assert check_match(command, ["start", "anything", "end"])
assert check_not_match(command, ["start", "end"])
assert check_not_match(command, ["start", "too", "many", "end"])
def test_invalid_instantiation():
with pytest.raises(AttributeError, match="min cannot be greater than max"):
Any(min=3, max=2)
with pytest.raises(
AttributeError, match=r"Cannot use `Any\(\)` one after another."
):
Command([Any(), Any()])
with pytest.raises(
TypeError, match="Command can be only of type string, list or tuple."
):
Command(dict(command="ls"))
def test_str_conversions():
no_arguments = Any()
assert str(no_arguments) == "Any (min=None, max=None)"
min_max = Any(min=1, max=3)
assert str(min_max) == "Any (min=1, max=3)"
simple_command = Command(["ls", "-lah"])
assert str(simple_command) == "('ls', '-lah')"
command_with_any = Command(["ls", Any()])
assert str(command_with_any) == "('ls', Any (min=None, max=None))"
def test_command_iter():
"""Make sure Command supports iteration"""
command = Command(["a", "a", "a"])
assert all(elem == "a" for elem in command)
|