-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtest_build.py
389 lines (290 loc) · 11.6 KB
/
test_build.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""
Note that pytest offers a `tmp_path`.
You can reproduce locally with
```python
%load_ext autoreload
%autoreload 2
import os
import tempfile
import shutil
from pathlib import Path
tmp_path = Path(tempfile.gettempdir()) / 'pytest-table-builder'
if os.path.exists(tmp_path):
shutil.rmtree(tmp_path)
os.mkdir(tmp_path)
```
"""
import re
import os
import shutil
import logging
from click.testing import CliRunner
from mkdocs.__main__ import build_command
def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
"""
Sets up a clean mkdocs directory
outputpath/testproject
├── docs/
└── mkdocs.yml
Args:
mkdocs_yml_path (Path): Path of mkdocs.yml file to use
output_path (Path): Path of folder in which to create mkdocs project
Returns:
testproject_path (Path): Path to test project
"""
testproject_path = output_path / "testproject"
# Create empty 'testproject' folder
if os.path.exists(testproject_path):
logging.warning(
"""This command does not work on windows.
Refactor your test to use setup_clean_mkdocs_folder() only once"""
)
shutil.rmtree(testproject_path)
# Copy correct mkdocs.yml file and our test 'docs/'
shutil.copytree(
os.path.join(os.path.dirname(mkdocs_yml_path), "docs"),
testproject_path / "docs",
)
if os.path.exists(os.path.join(os.path.dirname(mkdocs_yml_path), "assets")):
shutil.copytree(
os.path.join(os.path.dirname(mkdocs_yml_path), "assets"),
testproject_path / "assets",
)
shutil.copyfile(mkdocs_yml_path, testproject_path / "mkdocs.yml")
return testproject_path
def build_docs_setup(testproject_path):
"""
Runs the `mkdocs build` command
Args:
testproject_path (Path): Path to test project
Returns:
command: Object with results of command
"""
cwd = os.getcwd()
os.chdir(testproject_path)
try:
run = CliRunner().invoke(build_command)
os.chdir(cwd)
return run
except:
os.chdir(cwd)
raise
def test_table_output(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/basic_setup/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
index_file = tmp_proj / "site/index.html"
assert index_file.exists(), f"{index_file} does not exist"
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_csv.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_txt.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_excel.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_fwf.html"
contents = page_with_tag.read_text()
assert re.search(r"35000", contents)
assert re.search(r"Audi A4", contents)
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_yaml.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
assert re.search(r"table1", contents)
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/page_read_json.html"
contents = page_with_tag.read_text()
assert re.search(r"1234json", contents)
# Make sure multiple tags are supported
page_with_tag = tmp_proj / "site/page_read_two_csv.html"
contents = page_with_tag.read_text()
assert re.search(r"table1", contents)
assert re.search(r"table2", contents)
def test_compatibility_macros_plugin(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/basic_setup/mkdocs_w_macros_wrong_order.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 1, "'mkdocs build' command should have failed"
# Make sure correct error is raised
assert (
"[table-reader]: Incompatible plugin order:"
in result.output
)
# With correct order, no error
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/basic_setup/mkdocs_w_macros.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command should have succeeded"
def test_compatibility_markdownextradata(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/markdownextradata/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
index_file = tmp_proj / "site/index.html"
assert index_file.exists(), f"{index_file} does not exist"
# Make sure with markdown tag has the output
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
# Make sure the table is inserted
assert re.search(r"531456", contents)
# Make sure the extradata 'web' is inserted
assert re.search(r"www.example.com", contents)
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/markdownextradata/mkdocs_w_markdownextradata_wrong_order.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 1, "'mkdocs build' command should have failed"
# Make sure correct error is raised
assert (
"[table-reader]: Incompatible plugin order:"
in result.output
)
# With correct order, no error
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/markdownextradata/mkdocs_w_markdownextradata.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command should have succeeded"
def test_search_page_directory(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/search_page_directory/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/folder/page2.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
def test_relative_path(tmp_path):
"""
A project where we specify a path relative to the markdown.
"""
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/relative_path/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/folder/page2.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
def test_raw_cotent(tmp_path):
"""
A project where we insert raw content directly.
"""
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/raw_content/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/folder/page2.html"
contents = page_with_tag.read_text()
assert re.search(r"\$1600", contents)
def test_datapath_1(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/datapathproject/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
# Make sure the basic_table2.csv is inserted
page_with_tag = tmp_proj / "site/page2.html"
contents = page_with_tag.read_text()
assert re.search(r"539956", contents)
def test_datapath_trailing(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/datapathproject/mkdocs_trailingslash.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
# Make sure the basic_table2.csv is inserted
page_with_tag = tmp_proj / "site/page2.html"
contents = page_with_tag.read_text()
assert re.search(r"539956", contents)
def test_datapath_with_spaces(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/data_path_with_space/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
def test_tablepath_with_spaces(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/table_path_with_space/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
def test_using_docs_dir(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/using_docs_dir/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the basic_table.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)
def test_wrong_path(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/wrongpath/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 1, "'mkdocs build' command succeeded but should have failed"
assert "[table-reader-plugin]: Cannot find table file" in result.output
assert "non_existing_table.csv" in result.output
def test_mixed_quotation_marks(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/mixed_quotation_marks/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the file.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"56", contents)
def test_csv_with_no_string_headers(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/nonstringheaders/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the file.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"4242", contents)
def test_macros_jinja2_syntax(tmp_path):
tmp_proj = setup_clean_mkdocs_folder(
"tests/fixtures/jinja/mkdocs.yml", tmp_path
)
result = build_docs_setup(tmp_proj)
assert result.exit_code == 0, "'mkdocs build' command failed"
# Make sure the file.csv is inserted
page_with_tag = tmp_proj / "site/index.html"
contents = page_with_tag.read_text()
assert re.search(r"531456", contents)