Skip to content

Commit be15e4a

Browse files
Add some tests (#160)
* Add some tests * Cleanup docstrings * chore: auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add some tests * MMMMMMMMerge conflict resolution * MMMMMMMMerge conflict resolution --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2f49d5c commit be15e4a

File tree

12 files changed

+761
-31
lines changed

12 files changed

+761
-31
lines changed

.config/dictionary.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ cpart
1010
cpath
1111
crepository
1212
csource
13+
delenv
1314
excinfo
1415
fileh
1516
fqcn
17+
jsonschema
1618
levelname
1719
levelno
1820
netcommon
@@ -23,5 +25,6 @@ setenv
2325
specp
2426
treemaker
2527
uninstallation
28+
usefixtures
2629
vuuid
2730
xmltodict

src/ansible_dev_environment/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
via :command:`python -m ansible_dev_environment`.
55
"""
66

7-
from .cli import main
7+
from ansible_dev_environment.cli import main
88

99

1010
if __name__ == "__main__":

src/ansible_dev_environment/arg_parser.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,25 @@ def parse() -> argparse.Namespace:
200200
help="Uninstall a collection.",
201201
)
202202

203-
for grp in parser._action_groups: # noqa: SLF001
204-
if grp.title is None:
205-
continue
206-
grp.title = grp.title.capitalize()
203+
_group_titles(parser)
207204
for subparser in subparsers.choices.values():
208-
for grp in subparser._action_groups: # noqa: SLF001
209-
if grp.title is None:
210-
continue
211-
grp.title = grp.title.capitalize()
212-
# pylint: enable=protected-access
205+
_group_titles(subparser)
213206

214207
return parser.parse_args()
215208

216209

210+
def _group_titles(parser: ArgumentParser) -> None:
211+
"""Set the group titles to be capitalized.
212+
213+
Args:
214+
parser: The parser to set the group titles for
215+
"""
216+
for group in parser._action_groups: # noqa: SLF001
217+
if group.title is None:
218+
continue
219+
group.title = group.title.capitalize()
220+
221+
217222
class ArgumentParser(argparse.ArgumentParser):
218223
"""A custom argument parser."""
219224

src/ansible_dev_environment/cli.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,3 @@ def main() -> None:
170170
cli.args_sanity()
171171
cli.ensure_isolated()
172172
cli.run()
173-
174-
175-
if __name__ == "__main__":
176-
main()

src/ansible_dev_environment/subcommands/treemaker.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
5+
from typing import TYPE_CHECKING, cast
66

77
from ansible_dev_environment.tree import Tree
88
from ansible_dev_environment.utils import builder_introspect, collect_manifests
@@ -44,10 +44,7 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
4444
target=self._config.site_pkg_collections_path,
4545
venv_cache_dir=self._config.venv_cache_dir,
4646
)
47-
tree_dict: JSONVal = {c: {} for c in collections}
48-
if not isinstance(tree_dict, dict):
49-
msg = "Tree dict is not a dict."
50-
raise TypeError(msg)
47+
tree_dict: dict[str, dict[str, JSONVal]] = {c: {} for c in collections}
5148

5249
links: dict[str, str] = {}
5350
for collection_name, collection in collections.items():
@@ -65,10 +62,6 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
6562
self._output.error(err)
6663
continue
6764
target = tree_dict[collection_name]
68-
if not isinstance(target, dict):
69-
msg = "Tree dict is not a dict."
70-
raise TypeError(msg)
71-
7265
target[dep] = tree_dict[dep]
7366

7467
docs = collection["collection_info"].get("documentation")
@@ -97,7 +90,8 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
9790

9891
more_verbose = 2
9992
if self._config.args.verbose >= more_verbose:
100-
tree = Tree(obj=tree_dict, term_features=self._config.term_features)
93+
j_tree_dict = cast(JSONVal, tree_dict)
94+
tree = Tree(obj=j_tree_dict, term_features=self._config.term_features)
10195
tree.links = links
10296
tree.green.extend(green)
10397
rendered = tree.render()
@@ -132,7 +126,7 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
132126

133127

134128
def add_python_reqs(
135-
tree_dict: dict[str, JSONVal],
129+
tree_dict: dict[str, dict[str, JSONVal]],
136130
collection_name: str,
137131
python_deps: list[str],
138132
) -> None:

tests/integration/test_basic.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,13 @@ def test_non_local(
137137
assert string in captured.out
138138
monkeypatch.setattr(
139139
"sys.argv",
140-
["ade", "tree", f"--venv={tmp_path / 'venv'}"],
140+
["ade", "tree", f"--venv={tmp_path / 'venv'}", "-v"],
141141
)
142142
with pytest.raises(SystemExit):
143143
main()
144144
captured = capsys.readouterr()
145-
with pytest.raises(SystemExit):
146-
main()
147-
captured = capsys.readouterr()
148-
string = "ansible.scm\n└──ansible.utils\n\n"
149-
assert string == captured.out
145+
assert "ansible.scm\n├──ansible.utils" in captured.out
146+
assert "├──jsonschema" in captured.out
150147

151148

152149
def test_requirements(

tests/test_argparser.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Tests for the arg_parser module."""
2+
3+
import pytest
4+
5+
from ansible_dev_environment.arg_parser import (
6+
ArgumentParser,
7+
CustomHelpFormatter,
8+
_group_titles,
9+
)
10+
11+
12+
def test_no_option_string(
13+
capsys: pytest.CaptureFixture[str],
14+
) -> None:
15+
"""Test an argument without an option string.
16+
17+
Args:
18+
capsys: Pytest fixture.
19+
"""
20+
parser = ArgumentParser(
21+
formatter_class=CustomHelpFormatter,
22+
)
23+
parser.add_argument(
24+
dest="test",
25+
action="store_true",
26+
help="Test this",
27+
)
28+
parser.print_help()
29+
captured = capsys.readouterr()
30+
assert "Test this" in captured.out
31+
32+
33+
def test_one_string(
34+
capsys: pytest.CaptureFixture[str],
35+
) -> None:
36+
"""Test an argument without an option string.
37+
38+
Args:
39+
capsys: Pytest fixture.
40+
"""
41+
parser = ArgumentParser(
42+
formatter_class=CustomHelpFormatter,
43+
)
44+
parser.add_argument(
45+
"-t",
46+
dest="test",
47+
action="store_true",
48+
help="Test this",
49+
)
50+
parser.print_help()
51+
captured = capsys.readouterr()
52+
assert "-t Test this" in captured.out
53+
54+
55+
def test_too_many_string(
56+
monkeypatch: pytest.MonkeyPatch,
57+
) -> None:
58+
"""Test an argument with too many option strings.
59+
60+
Args:
61+
monkeypatch: Pytest fixture.
62+
"""
63+
monkeypatch.setattr("sys.argv", ["prog", "--help"])
64+
65+
parser = ArgumentParser(
66+
formatter_class=CustomHelpFormatter,
67+
)
68+
parser.add_argument(
69+
"-t",
70+
"-test",
71+
"--test",
72+
action="store_true",
73+
help="Test this",
74+
)
75+
with pytest.raises(ValueError, match="Too many option strings"):
76+
parser.parse_args()
77+
78+
79+
def test_group_no_title(capsys: pytest.CaptureFixture[str]) -> None:
80+
"""Test a group without a title.
81+
82+
Args:
83+
capsys: Pytest fixture.
84+
"""
85+
parser = ArgumentParser(
86+
formatter_class=CustomHelpFormatter,
87+
)
88+
parser.add_argument_group()
89+
_group_titles(parser)
90+
parser.print_help()
91+
captured = capsys.readouterr()
92+
assert "--help" in captured.out

tests/unit/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,25 @@ def output(tmp_path: Path) -> Output:
3131
term_features=TermFeatures(color=False, links=False),
3232
verbosity=0,
3333
)
34+
35+
36+
@pytest.fixture(name="_wide_console")
37+
def _wide_console(monkeypatch: pytest.MonkeyPatch) -> None:
38+
"""Fixture to set the terminal width to 1000 to prevent wrapping.
39+
40+
Args:
41+
monkeypatch: Pytest fixture.
42+
"""
43+
44+
def _console_width() -> int:
45+
"""Return a large console width.
46+
47+
Returns:
48+
int: Console width.
49+
"""
50+
return 1000
51+
52+
monkeypatch.setattr(
53+
"ansible_dev_environment.output.console_width",
54+
_console_width,
55+
)

0 commit comments

Comments
 (0)