forked from jupyter-book/jupyter-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_build.py
311 lines (268 loc) · 11.7 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
from pathlib import Path
import pytest
import sphinx
from bs4 import BeautifulSoup
from click.testing import CliRunner
from jupyter_book.cli import main as commands
PATH_BOOKS = Path(__file__).parent.joinpath("books")
SPHINX_VERSION = f".sphinx{sphinx.version_info[0]}"
def test_version(cli: CliRunner):
result = cli.invoke(commands.main, "--version")
assert result.exit_code == 0, result.output
assert "Jupyter Book" in result.output, result.output
def test_create(temp_with_override: Path, cli):
book = temp_with_override / "new_book"
result = cli.invoke(commands.create, book.as_posix())
assert result.exit_code == 0
assert book.joinpath("_config.yml").exists()
assert len(list(book.iterdir())) == 9
def test_create_from_cookiecutter(temp_with_override: Path, cli):
book = temp_with_override / "new_book"
result = cli.invoke(commands.create, [book.as_posix(), "--cookiecutter"])
assert result.exit_code == 0
# this test uses default cookiecutter prompt values
# note that default cookiecutter book name is "my_book"
assert book.joinpath("my_book", "my_book", "_config.yml").exists()
assert len(list(book.joinpath("my_book").iterdir())) == 7
assert len(list(book.joinpath("my_book", ".github", "workflows").iterdir())) == 1
assert len(list(book.joinpath("my_book", "my_book").iterdir())) == 8
def test_build_from_template(temp_with_override, cli):
"""Test building the book template and a few test configs."""
# Create the book from the template
book = temp_with_override / "new_book"
_ = cli.invoke(commands.create, book.as_posix())
build_result = cli.invoke(
commands.build, [book.as_posix(), "-n", "-W", "--keep-going"]
)
assert build_result.exit_code == 0, build_result.output
html = book.joinpath("_build", "html")
assert html.joinpath("index.html").exists()
assert html.joinpath("intro.html").exists()
def test_build_dirhtml_from_template(temp_with_override, cli):
"""Test building the book template with dirhtml."""
# Create the book from the template
book = temp_with_override / "new_book"
_ = cli.invoke(commands.create, book.as_posix())
build_result = cli.invoke(
commands.build, [book.as_posix(), "-n", "-W", "--builder", "dirhtml"]
)
assert build_result.exit_code == 0, build_result.output
html = book.joinpath("_build", "dirhtml")
assert html.joinpath("index.html").exists()
assert html.joinpath("intro", "index.html").exists()
def test_build_singlehtml_from_template(temp_with_override, cli):
"""Test building the book template with singlehtml."""
# Create the book from the template
book = temp_with_override / "new_book"
_ = cli.invoke(commands.create, book.as_posix())
build_result = cli.invoke(
commands.build, [book.as_posix(), "-n", "-W", "--builder", "singlehtml"]
)
assert build_result.exit_code == 0, build_result.output
html = book.joinpath("_build", "singlehtml")
assert html.joinpath("index.html").exists()
assert html.joinpath("intro.html").exists()
def test_custom_config(cli, build_resources):
"""Test a variety of custom configuration values."""
books, _ = build_resources
config = books.joinpath("config")
result = cli.invoke(commands.build, [config.as_posix(), "-n", "-W", "--keep-going"])
assert result.exit_code == 0, result.output
html = config.joinpath("_build", "html", "index.html").read_text(encoding="utf8")
soup = BeautifulSoup(html, "html.parser")
assert '<h1 class="site-logo" id="site-title">TEST PROJECT NAME</h1>' in html
assert '<div class="sphinx-tabs docutils container">' in html
assert '<link rel="stylesheet" type="text/css" href="_static/mycss.css" />' in html
assert '<script src="_static/js/myjs.js"></script>' in html
# Check that our comments engines were correctly added
assert soup.find("script", attrs={"kind": "hypothesis"})
assert soup.find("script", attrs={"kind": "utterances"})
@pytest.mark.parametrize("toc", ["_toc.yml", "_toc_startwithlist.yml"])
def test_toc_builds(cli, build_resources, toc):
"""Test building the book template with several different TOC files."""
books, tocs = build_resources
result = cli.invoke(
commands.build,
[tocs.as_posix(), "--toc", (tocs / toc).as_posix(), "-n", "-W", "--keep-going"],
)
assert result.exit_code == 0, result.output
def test_toc_rebuild(cli, build_resources):
"""Changes to the TOC should force a re-build of pages. Also tests for changes
to the relative ordering of content pages.
"""
_, tocs = build_resources
toc = tocs / "_toc_simple.yml"
index_html = tocs.joinpath("_build", "html", "index.html")
# Not using -W because we expect warnings for pages not listed in TOC
result = cli.invoke(
commands.build,
[tocs.as_posix(), "--toc", toc.as_posix(), "-n"],
)
html = BeautifulSoup(index_html.read_text(encoding="utf8"), "html.parser")
tags = html.find_all("a", "reference internal")
assert result.exit_code == 0, result.output
assert tags[1].attrs["href"] == "content1.html"
assert tags[2].attrs["href"] == "content2.html"
toc = tocs / "_toc_simple_changed.yml"
result = cli.invoke(
commands.build,
[tocs.as_posix(), "--toc", toc.as_posix(), "-n"],
)
print(result.exception)
assert result.exit_code == 0, result.output
html = BeautifulSoup(index_html.read_text(encoding="utf8"), "html.parser")
tags = html.find_all("a", "reference internal")
# The rendered TOC should reflect the order in the modified _toc.yml
assert tags[1].attrs["href"] == "content2.html"
assert tags[2].attrs["href"] == "content1.html"
@pytest.mark.parametrize(
"toc,msg",
[
(
"_toc_emptysections.yml",
"entry not a mapping containing 'chapters' key @ '/parts/0/'",
),
# sphinx-ext-toc does not enforce url titles
# ("_toc_urlwithouttitle.yml", "`url:` link should"),
("_toc_url.yml", "'root' key not found"),
("_toc_wrongkey.yml", "Unknown keys found"),
],
)
def test_corrupt_toc(build_resources, cli, toc, msg):
books, tocs = build_resources
with pytest.raises(RuntimeError, match=msg):
result = cli.invoke(
commands.build, [tocs.as_posix(), "--toc", (tocs / toc).as_posix(), "-W"]
)
assert result.exit_code == 1
raise result.exception
def test_build_errors(build_resources, cli):
books, tocs = build_resources
path = books.joinpath("mybook").absolute()
# Bad builder
result = cli.invoke(commands.build, [path.as_posix(), "--builder", "blah"])
assert result.exit_code == 2
# No table of contents message
p_notoc = books.joinpath("notoc")
with pytest.raises(RuntimeError):
result = cli.invoke(commands.build, [p_notoc.as_posix()])
assert result.exit_code == 1
assert "Couldn't find a Table of Contents file" in str(result.exception)
raise result.exception
# Test error on warnings and book error message
p_syntax = books.joinpath("sphinx_syntaxerr")
with pytest.raises(RuntimeError):
result = cli.invoke(commands.build, [p_syntax.as_posix(), "-W"])
assert result.exit_code == 1
assert "There was an error in building your book" in str(result.exception)
raise result.exception
# Config file path does not exist
with pytest.raises(IOError):
result = cli.invoke(
commands.build, [p_syntax.as_posix(), "--config", "non_existent_path"]
)
assert result.exit_code == 1
assert "Config file path given, but not found" in str(result.exception)
raise result.exception
def test_build_page(pages, cli):
"""Test building a page."""
page = pages.joinpath("single_page.ipynb")
html = pages.joinpath("_build", "_page", "single_page", "html")
index = html.joinpath("index.html")
result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going"])
assert result.exit_code == 0, result.output
assert html.joinpath("single_page.html").exists()
assert not html.joinpath("extra_page.html").exists()
assert 'url=single_page.html" />' in index.read_text(encoding="utf8")
def test_build_page_nested(build_resources, cli):
"""Test building a page."""
books, _ = build_resources
src = books.joinpath("nested")
page = src.joinpath("contents", "markdown.md")
html = src.joinpath("_build", "_page", "contents-markdown", "html")
index = html.joinpath("index.html")
result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going"])
assert result.exit_code == 0, result.output
assert html.joinpath("markdown.html").exists()
assert not html.joinpath("extra_page.html").exists()
assert 'url=markdown.html" />' in index.read_text(encoding="utf8")
@pytest.mark.skipif(sphinx.version_info[0] == 2, reason="randomly fails on CI")
def test_execution_timeout(pages, build_resources, cli):
"""Testing timeout execution for a page."""
books, _ = build_resources
path_page = pages.joinpath("loop_unrun.ipynb")
path_c = books.joinpath("config", "_config_timeout.yml")
path_html = pages.joinpath("_build", "_page", "loop_unrun", "html")
result = cli.invoke(
commands.build,
[
path_page.as_posix(),
"--config",
path_c.as_posix(),
"-n",
"-W",
"--keep-going",
],
)
assert "Execution Failed" in result.stdout
assert path_html.joinpath("reports", "loop_unrun.log").exists()
def test_build_using_custom_builder(cli, build_resources):
"""Test building the book template using a custom builder"""
books, _ = build_resources
config = books.joinpath("config_custombuilder")
result = cli.invoke(
commands.build,
[
config.as_posix(),
"--builder=custom",
"--custom-builder=mycustombuilder",
"-n",
"-W",
"--keep-going",
],
)
assert result.exit_code == 0, result.output
html = config.joinpath("_build", "mycustombuilder", "index.html").read_text(
encoding="utf8"
)
assert '<h1 class="site-logo" id="site-title">TEST PROJECT NAME</h1>' in html
assert '<link rel="stylesheet" type="text/css" href="_static/mycss.css" />' in html
assert '<script src="_static/js/myjs.js"></script>' in html
@pytest.mark.parametrize(
"toc_file",
(
"_toc_numbered.yml", # Numbered at top-level
"_toc_numbered_depth.yml", # Numbered at top-level, limited to depth 1
"_toc_numbered_parts.yml", # Numbered at top-level w/ parts
"_toc_numbered_parts_subset.yml", # Only some sections numbered
"_toc_numbered_depth_parts_subset.yml", # Selected numbering limited to depth 1
),
)
def test_toc_numbered(
toc_file: str, cli: CliRunner, temp_with_override, file_regression
):
"""Testing that numbers make it into the sidebar"""
path_output = temp_with_override.joinpath("book1").absolute()
p_toc = PATH_BOOKS.joinpath("toc")
path_toc = p_toc.joinpath(toc_file)
result = cli.invoke(
commands.build,
[
p_toc.as_posix(),
"--path-output",
path_output.as_posix(),
"--toc",
path_toc.as_posix(),
"-W",
],
)
assert result.exit_code == 0, result.output
path_toc_directive = path_output.joinpath("_build", "html", "index.html")
# get the tableofcontents markup
soup = BeautifulSoup(path_toc_directive.read_text(encoding="utf8"), "html.parser")
toc = soup.select("nav.bd-links")[0]
file_regression.check(
toc.prettify(),
basename=toc_file.split(".")[0],
extension=f"{SPHINX_VERSION}.html",
)