-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.py
233 lines (189 loc) · 6.63 KB
/
tests.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import pytest
import yaml
from click.testing import CliRunner
from mkdocs.__main__ import build_command
def setup_mkdocs(plugin_config, monkeypatch, tmpdir, enabled=True):
monkeypatch.chdir(tmpdir)
monkeypatch.syspath_prepend(tmpdir)
(tmpdir / "docs").mkdir()
(tmpdir / "site").mkdir()
runner = CliRunner()
with open(str(tmpdir / "mkdocs.yml"), "w") as f:
yaml.dump(
{
"site_name": "test",
"docs_dir": "docs",
"site_dir": "site",
"plugins": [
{
"mkdocs-simple-hooks": {
"hooks": plugin_config,
"enabled": enabled,
}
}
],
},
f,
)
return runner
def test_no_hooks_defined(tmpdir, monkeypatch):
runner = setup_mkdocs({}, monkeypatch, tmpdir)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert (
"Warning: No hooks defined. "
"The mkdocs-simple-hooks plugin will not run anything." in result.output
)
def test_wrong_hook(tmpdir, monkeypatch):
runner = setup_mkdocs({"no_hook": "test"}, monkeypatch, tmpdir)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert "'no_hook' is not valid hook name, will be ignored."
def test_no_such_module(tmpdir, monkeypatch):
runner = setup_mkdocs(
{"on_pre_build": "test_docs.hooks:on_pre_build"}, monkeypatch, tmpdir
)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert "Cannot import module 'test_docs.hooks'" in result.output
def test_no_attribute(tmpdir, monkeypatch):
test_docs = tmpdir / "no_attribute"
test_docs.mkdir()
with open(str(test_docs / "hooks.py"), "w") as f:
f.write("TEST = True")
runner = setup_mkdocs(
{"on_pre_build": "no_attribute.hooks:on_pre_build"}, monkeypatch, tmpdir
)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert (
"Config value: 'plugins'. "
"Warning: Module 'no_attribute.hooks' doesn't have attribute 'on_pre_build'"
in result.output
)
def test_no_function(tmpdir, monkeypatch):
test_docs = tmpdir / "no_function"
test_docs.mkdir()
with open(str(test_docs / "hooks.py"), "w") as f:
f.write("on_pre_build = True")
runner = setup_mkdocs(
{"on_pre_build": "no_function.hooks:on_pre_build"}, monkeypatch, tmpdir
)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert "'no_function.hooks:on_pre_build' is not callable." in result.output
def test_valid_hook_package(tmpdir, monkeypatch):
test_docs = tmpdir / "hooks_pkg"
test_docs.mkdir()
open(str(test_docs / "__init__.py"), "w").close()
with open(str(test_docs / "hooks.py"), "w") as f:
f.write(
"def on_pre_build(*args, **kwargs):\n"
' print("from on_pre_build")\n'
"def on_post_build(*args, **kwargs):\n"
' print("from on_post_build")\n'
)
runner = setup_mkdocs(
{
"on_pre_build": "hooks_pkg.hooks:on_pre_build",
"on_post_build": "hooks_pkg.hooks:on_post_build",
},
monkeypatch,
tmpdir,
)
result = runner.invoke(build_command)
assert result.exit_code == 0
output = result.output.splitlines()
assert output[0] == "from on_pre_build"
assert output[-1] == "from on_post_build"
def test_valid_hook_namespace_package(tmpdir, monkeypatch):
test_docs = tmpdir / "hooks_ns_pkg"
test_docs.mkdir()
with open(str(test_docs / "hooks.py"), "w") as f:
f.write(
"def on_pre_build(*args, **kwargs):\n"
' print("from on_pre_build")\n'
"def on_post_build(*args, **kwargs):\n"
' print("from on_post_build")\n'
)
runner = setup_mkdocs(
{
"on_pre_build": "hooks_ns_pkg.hooks:on_pre_build",
"on_post_build": "hooks_ns_pkg.hooks:on_post_build",
},
monkeypatch,
tmpdir,
)
result = runner.invoke(build_command)
assert result.exit_code == 0
output = result.output.splitlines()
assert output[0] == "from on_pre_build"
assert output[-1] == "from on_post_build"
def test_valid_hook_subpackage(tmpdir, monkeypatch):
test_package = tmpdir / "hooks_top_pkg"
test_package.mkdir()
open(str(test_package / "__init__.py"), "w").close()
test_docs = test_package / "hooks_sub_pkg"
test_docs.mkdir()
open(str(test_docs / "__init__.py"), "w").close()
with open(str(test_docs / "hooks.py"), "w") as f:
f.write(
"def on_pre_build(*args, **kwargs):\n"
' print("from on_pre_build")\n'
"def on_post_build(*args, **kwargs):\n"
' print("from on_post_build")\n'
)
runner = setup_mkdocs(
{
"on_pre_build": "hooks_top_pkg.hooks_sub_pkg.hooks:on_pre_build",
"on_post_build": "hooks_top_pkg.hooks_sub_pkg.hooks:on_post_build",
},
monkeypatch,
tmpdir,
)
result = runner.invoke(build_command)
assert result.exit_code == 0
output = result.output.splitlines()
assert output[0] == "from on_pre_build"
assert output[-1] == "from on_post_build"
def test_valid_hook_module(tmpdir, monkeypatch):
with open(str(tmpdir / "hooks.py"), "w") as f:
f.write(
"def on_pre_build(*args, **kwargs):\n"
' print("from on_pre_build")\n'
"def on_post_build(*args, **kwargs):\n"
' print("from on_post_build")\n'
)
runner = setup_mkdocs(
{
"on_pre_build": "hooks:on_pre_build",
"on_post_build": "hooks:on_post_build",
},
monkeypatch,
tmpdir,
)
result = runner.invoke(build_command)
assert result.exit_code == 0
output = result.output.splitlines()
assert output[0] == "from on_pre_build"
assert output[-1] == "from on_post_build"
@pytest.mark.parametrize(
"enabled",
[True, False],
)
def test_disabling_plugin(tmpdir, monkeypatch, enabled):
with open(str(tmpdir / "hooks.py"), "w") as f:
f.write(
"def on_pre_build(*args, **kwargs):\n" ' print("from on_pre_build")\n'
)
runner = setup_mkdocs(
{
"on_pre_build": "hooks:on_pre_build",
},
monkeypatch,
tmpdir,
enabled=enabled,
)
result = runner.invoke(build_command)
assert result.exit_code == 0
assert ("from on_pre_build" in result.output) == enabled