1import subprocess
2
3import pytest
4from typer.testing import CliRunner
5
6from docs_src.subcommands import tutorial003
7
8runner = CliRunner()
9
10
11@pytest.fixture()
12def mod(monkeypatch):
13    with monkeypatch.context() as m:
14        m.syspath_prepend(list(tutorial003.__path__)[0])
15        from docs_src.subcommands.tutorial003 import main
16
17        return main
18
19
20@pytest.fixture()
21def app(mod):
22    return mod.app
23
24
25def test_help(app):
26    result = runner.invoke(app, ["--help"])
27    assert result.exit_code == 0
28    assert "[OPTIONS] COMMAND [ARGS]..." in result.output
29    assert "Commands:" in result.output
30    assert "items" in result.output
31    assert "users" in result.output
32    assert "lands" in result.output
33
34
35def test_help_items(app):
36    result = runner.invoke(app, ["items", "--help"])
37    assert result.exit_code == 0
38    assert "[OPTIONS] COMMAND [ARGS]..." in result.output
39    assert "Commands:" in result.output
40    assert "create" in result.output
41    assert "delete" in result.output
42    assert "sell" in result.output
43
44
45def test_items_create(app):
46    result = runner.invoke(app, ["items", "create", "Wand"])
47    assert result.exit_code == 0
48    assert "Creating item: Wand" in result.output
49
50
51def test_items_sell(app):
52    result = runner.invoke(app, ["items", "sell", "Vase"])
53    assert result.exit_code == 0
54    assert "Selling item: Vase" in result.output
55
56
57def test_items_delete(app):
58    result = runner.invoke(app, ["items", "delete", "Vase"])
59    assert result.exit_code == 0
60    assert "Deleting item: Vase" in result.output
61
62
63def test_help_users(app):
64    result = runner.invoke(app, ["users", "--help"])
65    assert result.exit_code == 0
66    assert "[OPTIONS] COMMAND [ARGS]..." in result.output
67    assert "Commands:" in result.output
68    assert "create" in result.output
69    assert "delete" in result.output
70    assert "sell" not in result.output
71
72
73def test_users_create(app):
74    result = runner.invoke(app, ["users", "create", "Camila"])
75    assert result.exit_code == 0
76    assert "Creating user: Camila" in result.output
77
78
79def test_users_delete(app):
80    result = runner.invoke(app, ["users", "delete", "Camila"])
81    assert result.exit_code == 0
82    assert "Deleting user: Camila" in result.output
83
84
85def test_help_lands(app):
86    result = runner.invoke(app, ["lands", "--help"])
87    assert result.exit_code == 0
88    assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output
89    assert "Commands:" in result.output
90    assert "reigns" in result.output
91    assert "towns" in result.output
92
93
94def test_help_lands_reigns(app):
95    result = runner.invoke(app, ["lands", "reigns", "--help"])
96    assert result.exit_code == 0
97    assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output
98    assert "Commands:" in result.output
99    assert "conquer" in result.output
100    assert "destroy" in result.output
101
102
103def test_lands_reigns_conquer(app):
104    result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"])
105    assert result.exit_code == 0
106    assert "Conquering reign: Gondor" in result.output
107
108
109def test_lands_reigns_destroy(app):
110    result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"])
111    assert result.exit_code == 0
112    assert "Destroying reign: Mordor" in result.output
113
114
115def test_help_lands_towns(app):
116    result = runner.invoke(app, ["lands", "towns", "--help"])
117    assert result.exit_code == 0
118    assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output
119    assert "Commands:" in result.output
120    assert "burn" in result.output
121    assert "found" in result.output
122
123
124def test_lands_towns_found(app):
125    result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"])
126    assert result.exit_code == 0
127    assert "Founding town: Cartagena" in result.output
128
129
130def test_lands_towns_burn(app):
131    result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"])
132    assert result.exit_code == 0
133    assert "Burning town: New Asgard" in result.output
134
135
136def test_scripts(mod):
137    from docs_src.subcommands.tutorial003 import items, lands, reigns, towns, users
138
139    for module in [mod, items, lands, reigns, towns, users]:
140        result = subprocess.run(
141            ["coverage", "run", module.__file__, "--help"],
142            stdout=subprocess.PIPE,
143            stderr=subprocess.PIPE,
144            encoding="utf-8",
145        )
146        assert "Usage" in result.stdout
147