forked from canonical/documentation-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_builder.py
431 lines (336 loc) · 11.2 KB
/
test_builder.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""
Most of the builder's actual operations should be tested in
test_operations.py, so hopefully we don't need to test every
permutation.
However, here we will try to test at the basic level the overall
function of the builder.
"""
# Core modules
import re
from glob import glob
from os import path, utime
from shutil import rmtree
# Third party modules
from bs4 import BeautifulSoup
from io import StringIO
from git import Repo
from pytest import raises
# Local modules
from ubuntudesign.documentation_builder.builder import Builder
fixtures_base = path.join(path.dirname(__file__), 'fixtures')
def test_missing_base():
# Build normally
with raises(FileNotFoundError):
Builder(
base_directory='/a/non/existent/base',
output_path='doesnt/matter',
quiet=True
)
def test_no_metadata():
base = path.join(fixtures_base, 'builder', 'no-metadata')
output = path.join(fixtures_base, 'builder', 'output')
if path.exists(output):
rmtree(output)
with raises(SystemExit):
Builder(
base_directory=base,
output_path=output,
quiet=True
)
assert not path.exists(output)
def test_quiet():
fixtures = path.join(fixtures_base, 'builder')
base = path.join(fixtures, 'base')
output = path.join(fixtures, 'output')
if path.exists(output):
rmtree(output)
mock_out = StringIO()
mock_err = StringIO()
Builder(
base_directory=base,
output_path=output,
out=mock_out,
err=mock_err,
)
assert mock_out.getvalue() > ''
assert mock_err.getvalue() == ''
mock_out = StringIO()
mock_err = StringIO()
Builder(
base_directory=base,
output_path=output,
quiet=True,
out=mock_out,
err=mock_err,
)
assert mock_out.getvalue() == ''
assert mock_err.getvalue() == ''
rmtree(output)
def test_basic_build():
fixtures = path.join(fixtures_base, 'builder')
base = path.join(fixtures, 'base')
output = path.join(fixtures, 'output')
index = path.join(fixtures, 'output', 'en', 'index.html')
expected_output = path.join(fixtures, 'output_basic')
if path.exists(output):
rmtree(output)
# Build normally
Builder(
base_directory=base,
output_path=output,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
# Check changes aren't updated
# Set far future modified time
future = 2000000000
utime(index, (future, future))
Builder(
base_directory=base,
output_path=output,
quiet=True
)
# Check it hasn't been modified
assert path.getmtime(index) == future
# Check we can force updates
Builder(
base_directory=base,
output_path=output,
force=True,
quiet=True
)
# Check it's been modified now
assert path.getmtime(index) < future
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_no_media():
fixtures = path.join(fixtures_base, 'builder')
base = path.join(fixtures, 'base-no-media')
output = path.join(fixtures, 'output')
expected_output = path.join(fixtures, 'output_no_media')
if path.exists(output):
rmtree(output)
# Build normally
Builder(
base_directory=base,
output_path=output,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_custom_template():
base = path.join(fixtures_base, 'builder', 'base')
output = path.join(fixtures_base, 'builder', 'output')
template_path = path.join(fixtures_base, 'builder', 'template.jinja2')
expected_output = path.join(
fixtures_base, 'builder', 'output_custom_template'
)
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
output_path=output,
template_path=template_path,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_source_folder():
base = path.join(fixtures_base, 'builder', 'base-source-folder')
output = path.join(fixtures_base, 'builder', 'output')
expected_output = path.join(
fixtures_base, 'builder', 'output_basic'
)
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
source_folder='./src',
output_path=output,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_versions():
fixtures = path.join(fixtures_base, 'builder')
base = path.join(fixtures, 'base-repo')
output = path.join(fixtures, 'output')
expected_output = path.join(fixtures, 'output_versions')
# make sure things don't exist
if path.exists(output):
rmtree(output)
if path.exists(base):
rmtree(base)
# Clone repository and pull down all branches
repo = Repo.clone_from(
(
'https://github.com/CanonicalLtd/'
'documentation-builder-test-builder-repo.git'
),
base
)
origin = repo.remotes.origin
repo.create_head('1.0', origin.refs['1.0'])
repo.create_head('latest', origin.refs['latest'])
Builder(
base_directory=base,
output_path=output,
build_version_branches=True,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
rmtree(base)
def test_output_media_path():
base = path.join(fixtures_base, 'builder', 'base')
output = path.join(fixtures_base, 'builder', 'output')
output_media_path = path.join(
fixtures_base, 'builder', 'output', 'files', 'media'
)
expected_output = path.join(
fixtures_base, 'builder', 'output_media_path'
)
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
output_path=output,
output_media_path=output_media_path,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_search():
base = path.join(fixtures_base, 'builder', 'base')
output = path.join(fixtures_base, 'builder', 'output')
expected_output = path.join(
fixtures_base, 'builder', 'output_search'
)
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
output_path=output,
search_url='https://example.com/search',
search_placeholder='Placeholder text',
search_domains=['one.example.com', 'two.example.com/path'],
quiet=True
)
# Check the output file structure is as expected
_compare_trees(output, expected_output)
# Compare search HTML
output_index = path.join(output, 'en/index.html')
expected_index = path.join(expected_output, 'en/index.html')
with open(output_index, encoding="utf-8") as output_file:
output_soup = BeautifulSoup(output_file.read(), 'html.parser')
with open(expected_index, encoding="utf-8") as expected_file:
expected_soup = BeautifulSoup(expected_file.read(), 'html.parser')
output_search = output_soup.select('form#search')[0]
expected_search = expected_soup.select('form#search')[0]
assert output_search == expected_search
rmtree(output)
def test_media_url():
base = path.join(fixtures_base, 'builder', 'base')
output = path.join(fixtures_base, 'builder', 'output')
expected_output = path.join(
fixtures_base, 'builder', 'output_media_url'
)
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
media_url='/static/media',
output_path=output,
quiet=True
)
_compare_trees(output, expected_output)
_compare_html_parts(output, expected_output)
rmtree(output)
def test_tag_manager():
fixtures = path.join(fixtures_base, 'builder')
base = path.join(fixtures, 'base')
output = path.join(fixtures, 'output')
index_filepath = path.join(fixtures, 'output', 'en', 'index.html')
if path.exists(output):
rmtree(output)
Builder(
base_directory=base,
output_path=output,
tag_manager_code='GTM_654321',
quiet=True
)
with open(index_filepath) as index_file:
index_content = index_file.read()
assert 'googletagmanager.com' in index_content
assert 'GTM_654321' in index_content
rmtree(output)
Builder(
base_directory=base,
output_path=output,
quiet=True
)
with open(index_filepath) as index_file:
index_content = index_file.read()
assert 'googletagmanager.com' not in index_content
assert 'GTM_654321' not in index_content
rmtree(output)
def _compare_trees(directory_a, directory_b):
a_files = []
b_files = []
for filepath in glob(path.join(directory_a, '**'), recursive=True):
a_files.append(path.relpath(filepath, directory_a))
for filepath in glob(path.join(directory_b, '**'), recursive=True):
b_files.append(path.relpath(filepath, directory_b))
assert sorted(a_files) == sorted(b_files)
def _compare_html_parts(directory_a, directory_b):
for a_filepath in glob(
path.join(directory_a, '**/*.html'),
recursive=True
):
local_filepath = path.relpath(a_filepath, directory_a)
b_filepath = path.join(directory_b, local_filepath)
with open(a_filepath, encoding="utf-8") as a_file:
a_soup = BeautifulSoup(a_file.read(), 'html.parser')
with open(b_filepath, encoding="utf-8") as b_file:
b_soup = BeautifulSoup(b_file.read(), 'html.parser')
# Compare titles
a_title = a_soup.select('head title')[0].contents
b_title = b_soup.select('head title')[0].contents
assert a_title == b_title
# Compare links
a_links = (
a_soup.select('.p-sidebar-nav a') + a_soup.select('main a')
)
b_links = (
b_soup.select('.p-sidebar-nav a') + b_soup.select('main a')
)
for index, a_link in enumerate(a_links):
b_link = b_links[index]
assert a_link['href'] == b_link['href']
assert a_link.contents == b_link.contents
# Compare images
a_imgs = a_soup.select('main img')
b_imgs = b_soup.select('main img')
for index, a_img in enumerate(a_imgs):
b_img = b_imgs[index]
assert a_img['src'] == b_img['src']
assert a_img['alt'] == b_img['alt']
# Compare notifications
a_notifications = a_soup.findAll(
"div", {"class" : re.compile('p-notification.*')}
)
b_notifications = b_soup.findAll(
"div", {"class" : re.compile('p-notification.*')}
)
for index, a_notification in enumerate(a_notifications):
b_notification = b_notifications[index]
a_stripped = str(a_notification).replace(' ', '').replace("\n", '')
b_stripped = str(b_notification).replace(' ', '').replace("\n", '')
assert a_stripped == b_stripped