-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,495 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
# Configuration file for lib tests. | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from pytest_mock import MockerFixture | ||
|
||
from pytest_mh.conn import Connection | ||
from pytest_mh.conn.container import ContainerClient | ||
from pytest_mh.conn.ssh import SSHClient | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def disallow_connection(mocker: MockerFixture): | ||
""" | ||
Raise RuntimeError if a test tries to connect to the remote host. | ||
""" | ||
classes = [Connection, SSHClient, ContainerClient] | ||
for cls in classes: | ||
mocker.patch.object( | ||
cls, | ||
"connect", | ||
side_effect=RuntimeError(f"Test attempted to connect to remote host ({cls.__name__})"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[pytest] | ||
addopts = --strict-markers | ||
testpaths = tests | ||
markers = | ||
topology: mock of pytest.mark.topology | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-r ./requirements.txt | ||
pytest-mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from pytest_mh.cli import CLIBuilder | ||
from pytest_mh.conn import Bash, Powershell | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, expected", | ||
[ | ||
({"arg": (CLIBuilder.option.VALUE, None)}, ""), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, "--arg value"), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, "--arg 'value'"), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, '--arg "value"'), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, "--arg 'value'"), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, ""), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, "--arg"), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, "'value'"), | ||
], | ||
ids=[ | ||
"none", | ||
"plain-no-quotes", | ||
"plain-single-quotes", | ||
"plain-double-quotes", | ||
"value", | ||
"switch-false", | ||
"switch-true", | ||
"positional", | ||
], | ||
) | ||
def test_cli__bash__CLIBuilder__command(args, expected): | ||
cli = CLIBuilder(Bash()) | ||
line = cli.command("/bin/test", args) | ||
|
||
assert line == f"/bin/test {expected}".strip() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, expected", | ||
[ | ||
({"arg": (CLIBuilder.option.VALUE, None)}, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, ["--arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, ["--arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, ["--arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, ["--arg", "value"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, [None]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, ["--arg"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, ["value"]), | ||
], | ||
ids=[ | ||
"none", | ||
"plain-no-quotes", | ||
"plain-single-quotes", | ||
"plain-double-quotes", | ||
"value", | ||
"switch-false", | ||
"switch-true", | ||
"positional", | ||
], | ||
) | ||
def test_cli__bash__CLIBuilder__argv(args, expected): | ||
cli = CLIBuilder(Bash()) | ||
argv = cli.argv("/bin/test", args) | ||
|
||
assert argv == ["/bin/test", *[x for x in expected if x is not None]] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, quote_value, expected", | ||
[ | ||
# quote_value=False | ||
({"arg": (CLIBuilder.option.VALUE, None)}, False, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, False, ["--arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, False, ["--arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, False, ["--arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, False, ["--arg", "value"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, False, [None]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, False, ["--arg"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, False, ["value"]), | ||
# quote_value=True | ||
({"arg": (CLIBuilder.option.VALUE, None)}, True, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, True, ["--arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, True, ["--arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, True, ["--arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, True, ["--arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, True, [None]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, True, ["--arg"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, True, ["'value'"]), | ||
], | ||
ids=[ | ||
"unquoted-none", | ||
"unquoted-plain-no-quotes", | ||
"unquoted-plain-single-quotes", | ||
"unquoted-plain-double-quotes", | ||
"unquoted-value", | ||
"unquoted-switch-false", | ||
"unquoted-switch-true", | ||
"unquoted-positional", | ||
"quoted-none", | ||
"quoted-plain-no-quotes", | ||
"quoted-plain-single-quotes", | ||
"quoted-plain-double-quotes", | ||
"quoted-value", | ||
"quoted-switch-false", | ||
"quoted-switch-true", | ||
"quoted-positional", | ||
], | ||
) | ||
def test_cli__bash__CLIBuilder__args(args, quote_value, expected): | ||
cli = CLIBuilder(Bash()) | ||
args = cli.args(args, quote_value=quote_value) | ||
|
||
assert args == [x for x in expected if x is not None] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, expected", | ||
[ | ||
({"arg": (CLIBuilder.option.VALUE, None)}, ""), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, "-arg value"), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, "-arg 'value'"), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, '-arg "value"'), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, "-arg 'value'"), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, "-arg:$False"), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, "-arg:$True"), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, "'value'"), | ||
], | ||
ids=[ | ||
"none", | ||
"plain-no-quotes", | ||
"plain-single-quotes", | ||
"plain-double-quotes", | ||
"value", | ||
"switch-false", | ||
"switch-true", | ||
"positional", | ||
], | ||
) | ||
def test_cli__powershell__CLIBuilder__command(args, expected): | ||
cli = CLIBuilder(Powershell()) | ||
line = cli.command("/bin/test", args) | ||
|
||
assert line == f"/bin/test {expected}".strip() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, expected", | ||
[ | ||
({"arg": (CLIBuilder.option.VALUE, None)}, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, ["-arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, ["-arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, ["-arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, ["-arg", "value"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, ["-arg:$False"]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, ["-arg:$True"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, ["value"]), | ||
], | ||
ids=[ | ||
"none", | ||
"plain-no-quotes", | ||
"plain-single-quotes", | ||
"plain-double-quotes", | ||
"value", | ||
"switch-false", | ||
"switch-true", | ||
"positional", | ||
], | ||
) | ||
def test_cli__powershell__CLIBuilder__argv(args, expected): | ||
cli = CLIBuilder(Powershell()) | ||
argv = cli.argv("/bin/test", args) | ||
|
||
assert argv == ["/bin/test", *[x for x in expected if x is not None]] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"args, quote_value, expected", | ||
[ | ||
# quote_value=False | ||
({"arg": (CLIBuilder.option.VALUE, None)}, False, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, False, ["-arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, False, ["-arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, False, ["-arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, False, ["-arg", "value"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, False, ["-arg:$False"]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, False, ["-arg:$True"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, False, ["value"]), | ||
# quote_value=True | ||
({"arg": (CLIBuilder.option.VALUE, None)}, True, [None]), | ||
({"arg": (CLIBuilder.option.PLAIN, "value")}, True, ["-arg", "value"]), | ||
({"arg": (CLIBuilder.option.PLAIN, "'value'")}, True, ["-arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.PLAIN, '"value"')}, True, ["-arg", '"value"']), | ||
({"arg": (CLIBuilder.option.VALUE, "value")}, True, ["-arg", "'value'"]), | ||
({"arg": (CLIBuilder.option.SWITCH, False)}, True, ["-arg:$False"]), | ||
({"arg": (CLIBuilder.option.SWITCH, True)}, True, ["-arg:$True"]), | ||
({"arg": (CLIBuilder.option.POSITIONAL, "value")}, True, ["'value'"]), | ||
], | ||
ids=[ | ||
"unquoted-none", | ||
"unquoted-plain-no-quotes", | ||
"unquoted-plain-single-quotes", | ||
"unquoted-plain-double-quotes", | ||
"unquoted-value", | ||
"unquoted-switch-false", | ||
"unquoted-switch-true", | ||
"unquoted-positional", | ||
"quoted-none", | ||
"quoted-plain-no-quotes", | ||
"quoted-plain-single-quotes", | ||
"quoted-plain-double-quotes", | ||
"quoted-value", | ||
"quoted-switch-false", | ||
"quoted-switch-true", | ||
"quoted-positional", | ||
], | ||
) | ||
def test_cli__powershell__CLIBuilder__args(args, quote_value, expected): | ||
cli = CLIBuilder(Powershell()) | ||
args = cli.args(args, quote_value=quote_value) | ||
|
||
assert args == [x for x in expected if x is not None] |
Oops, something went wrong.