Skip to content

Commit

Permalink
tests: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrezina committed Oct 22, 2024
1 parent 27f1234 commit 1059245
Show file tree
Hide file tree
Showing 12 changed files with 1,495 additions and 98 deletions.
23 changes: 22 additions & 1 deletion conftest.py
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__})"),
)
3 changes: 3 additions & 0 deletions pytest.ini
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

3 changes: 2 additions & 1 deletion pytest_mh/_private/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from collections.abc import Mapping
from copy import deepcopy
from functools import partial
from inspect import getfullargspec
Expand Down Expand Up @@ -40,7 +41,7 @@ def is_property_in_dict(property: str, d: dict[str, Any]) -> bool:

return is_property_in_dict(subpath, d[key])

return property in d and d[property]
return isinstance(d, Mapping) and property in d and d[property]

for key in required_keys:
if not is_property_in_dict(key, confdict):
Expand Down
2 changes: 2 additions & 0 deletions requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r ./requirements.txt
pytest-mock
222 changes: 222 additions & 0 deletions tests/test_cli.py
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]
Loading

0 comments on commit 1059245

Please sign in to comment.