-
Notifications
You must be signed in to change notification settings - Fork 650
/
test_config.py
280 lines (242 loc) · 9.15 KB
/
test_config.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
# from pathlib import Path
import jsonschema
import pytest
import sphinx as sphinx_build
from jupyter_book.cli.main import sphinx
from jupyter_book.config import get_final_config, validate_yaml
pytest_plugins = "pytester"
SPHINX_VERSION = f".sphinx{sphinx_build.version_info[0]}"
@pytest.mark.parametrize(
"user_config",
[
{},
{"title": "hallo"},
{"html": {"extra_footer": ""}},
{"execute": {"execute_notebooks": "cache"}},
{
"parse": {
"myst_enable_extensions": ["linkify"],
"myst_dmath_double_inline": True,
}
},
{"latex": {"latex_documents": {"targetname": "book.tex", "title": "other"}}},
{"launch_buttons": {"binderhub_url": "other"}},
{"repository": {"url": "other"}},
{"exclude_patterns": ["new"]},
{
"sphinx": {
"extra_extensions": ["other"],
"local_extensions": {"helloworld": "./ext"},
"config": {
"html_theme_options": {
"launch_buttons": {"binderhub_url": "other"},
"analytics": {"google_analytics_id": ""},
},
"html_theme": "other",
"new": "value",
},
}
},
{
"sphinx": {
"extra_extensions": ["other"],
"local_extensions": {"helloworld": "./ext"},
"recursive_update": True,
"config": {
"html_theme_options": {
"launch_buttons": {"binderhub_url": "other"}
},
"html_theme": "other",
"new": "value",
},
}
},
],
ids=[
"empty",
"title",
"html.extra_footer",
"execute.method",
"extended_syntax",
"latex_doc",
"launch_buttons",
"repository",
"exclude_patterns",
"sphinx-default",
"sphinx-recurse",
],
)
def test_get_final_config(user_config, data_regression):
cli_config = {"latex_individualpages": False}
final_config, metadata = get_final_config(
user_yaml=user_config,
cli_config=cli_config,
validate=True,
raise_on_invalid=True,
)
data_regression.check(
{"_user_config": user_config, "final": final_config, "metadata": metadata}
)
def test_validate_yaml():
with pytest.raises(jsonschema.ValidationError):
validate_yaml({"title": 1}, raise_on_errors=True)
assert "Warning" in validate_yaml({"title": 1}, raise_on_errors=False)
assert validate_yaml({"title": ""}, raise_on_errors=False) is None
def test_config_sphinx_command_only_build_toc_files(
cli, temp_with_override, file_regression
):
temp_with_override.joinpath("_config.yml").write_text(
"only_build_toc_files: True\n", encoding="utf8"
)
temp_with_override.joinpath("_config.yml").write_text(
"exclude_patterns: [test_config/*]\n", encoding="utf8"
)
temp_with_override.joinpath("_toc.yml").write_text("root: intro\n", encoding="utf8")
cli.invoke(sphinx, temp_with_override.as_posix())
assert temp_with_override.joinpath("conf.py").exists()
output = temp_with_override.joinpath("conf.py").read_text(encoding="utf8")
file_regression.check(output, encoding="utf8")
def test_config_sphinx_command(cli, temp_with_override, file_regression):
temp_with_override.joinpath("_config.yml").write_text(
"title: test\n", encoding="utf8"
)
temp_with_override.joinpath("_toc.yml").write_text("root: intro\n", encoding="utf8")
cli.invoke(sphinx, temp_with_override.as_posix())
assert temp_with_override.joinpath("conf.py").exists()
output = temp_with_override.joinpath("conf.py").read_text(encoding="utf8")
file_regression.check(output, encoding="utf8")
# TODO sphinx-external-toc now handles appending to exclude_patterns
# but we may want to add similar tests there, checking the output of exclude_patterns
# @pytest.mark.parametrize(
# "toc_file, filename",
# [("p.md", "p.md"), ("p", "p.md"), ("[]p", "[]p.md"), ("[t]p.md", "[t]p.md")],
# )
# def test_only_build_toc_files(testdir, toc_file, filename):
# cli_config = {"latex_individualpages": False}
# toc = Path("toc.yml")
# toc.write_text(f"- file: '{toc_file}'\n")
# Path(filename).write_text("")
# Path("exclude.md").write_text("")
# user_config = {"only_build_toc_files": True}
# final_config, metadata = get_final_config(
# user_yaml=user_config,
# cli_config=cli_config,
# validate=True,
# raise_on_invalid=True,
# )
# assert "exclude.md" in final_config["exclude_patterns"]
# assert filename not in final_config["exclude_patterns"]
# def test_only_build_toc_files_with_exclude_patterns(testdir):
# cli_config = {"latex_individualpages": False}
# toc = Path("toc.yml")
# toc.write_text("- file: landing\n")
# Path("landing.md").write_text("")
# Path("exclude.md").write_text("")
# user_config = {
# "only_build_toc_files": True,
# "exclude_patterns": ["my/*", "patterns"],
# }
# final_config, metadata = get_final_config(
# user_yaml=user_config,
# cli_config=cli_config,
# validate=True,
# raise_on_invalid=True,
# )
# assert "exclude.md" in final_config["exclude_patterns"]
# assert "my/*" in final_config["exclude_patterns"]
# assert "patterns" in final_config["exclude_patterns"]
# assert "landing.md" not in final_config["exclude_patterns"]
# def test_only_build_toc_files_non_default_source_dir(testdir):
# cli_config = {"latex_individualpages": False}
# toc = Path("toc.yml")
# toc.write_text("- file: landing\n")
# sourcedir = Path("s")
# subdir = sourcedir / "subdir"
# subdir.mkdir(parents=True)
# Path(sourcedir / "landing.md").write_text("")
# Path(sourcedir / "exclude.md").write_text("")
# Path(subdir / "sub.md").write_text("")
# user_config = {"only_build_toc_files": True}
# final_config, metadata = get_final_config(
# user_yaml=user_config,
# cli_config=cli_config,
# validate=True,
# raise_on_invalid=True,
# sourcedir=sourcedir,
# )
# assert "exclude.md" in final_config["exclude_patterns"]
# assert "subdir/sub.md" in final_config["exclude_patterns"]
# assert "landing.md" not in final_config["exclude_patterns"]
# def test_only_build_toc_files_missing_toc(testdir):
# cli_config = {"latex_individualpages": False}
# user_config = {"only_build_toc_files": True}
# with pytest.raises(ValueError, match=r".*you must have a toc.*"):
# get_final_config(
# user_yaml=user_config,
# cli_config=cli_config,
# validate=True,
# raise_on_invalid=True,
# )
def test_get_final_config_custom_myst_extensions(data_regression):
cli_config = {"latex_individualpages": False}
user_config = {"parse": {"myst_extra_extensions": ["linkify"]}}
final_config, metadata = get_final_config(
user_yaml=user_config,
cli_config=cli_config,
validate=True,
raise_on_invalid=True,
)
data_regression.check(
{"_user_config": user_config, "final": final_config, "metadata": metadata}
)
def test_get_final_config_bibtex(data_regression):
cli_config = {"latex_individualpages": False}
user_config = {"bibtex_bibfiles": ["tmp.bib"]}
final_config, metadata = get_final_config(
user_yaml=user_config,
cli_config=cli_config,
validate=True,
raise_on_invalid=True,
)
assert "sphinxcontrib.bibtex" in final_config["extensions"]
def test_mathjax_config_warning(data_regression):
mathjax_config = {
"sphinx": {
"config": {
"mathjax_config": {"TeX": {"Macros": {"argmax": "arg\\,max"}}},
}
}
}
cli_config = {"latex_individualpages": False}
user_config = mathjax_config
final_config, metadata = get_final_config(
user_yaml=user_config,
cli_config=cli_config,
validate=True,
raise_on_invalid=True,
)
data_regression.check(
{"_user_config": user_config, "final": final_config, "metadata": metadata},
basename=f"test_mathjax_config_warning{SPHINX_VERSION}",
)
def test_mathjax_config_warning_mathjax2path(data_regression):
mathjax_config = {
"sphinx": {
"config": {
"mathjax_config": {"TeX": {"Macros": {"argmax": "arg\\,max"}}},
"mathjax_path": "https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_CHTML", # noqa: E501
}
}
}
cli_config = {"latex_individualpages": False}
user_config = mathjax_config
final_config, metadata = get_final_config(
user_yaml=user_config,
cli_config=cli_config,
validate=True,
raise_on_invalid=True,
)
data_regression.check(
{"_user_config": user_config, "final": final_config, "metadata": metadata},
basename=f"test_mathjax_config_warning_mathjax2path{SPHINX_VERSION}",
)