Skip to content

Commit 00ed1a1

Browse files
authored
Do not hardcode CSS/JS checksums (readthedocs#226)
These checksums are tied to a particular version of Sphinx, Alabaster or Pygments, and the tests may fail when a different version is used. Instead, calculate the checksum dynamically during the test.
1 parent 9d1af35 commit 00ed1a1

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

tests/test_urls.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def test_default_settings(app, status, warning):
7474
'<li><a href="/en/latest/index.html">Documentation overview</a><ul>',
7575

7676
# resources
77-
_get_css_html_link_tag('en', 'latest', 'alabaster.css'),
78-
_get_css_html_link_tag('en', 'latest', 'pygments.css'),
77+
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
78+
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
7979
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
8080
]
8181

@@ -145,8 +145,8 @@ def test_urls_prefix_setting(app, status, warning):
145145
'<img class="logo" src="/language/version/_static/logo.svg" alt="Logo"/>',
146146

147147
# resources
148-
_get_css_html_link_tag('language', 'version', 'alabaster.css'),
149-
_get_css_html_link_tag('language', 'version', 'pygments.css'),
148+
_get_css_html_link_tag(app, 'language', 'version', 'alabaster.css'),
149+
_get_css_html_link_tag(app, 'language', 'version', 'pygments.css'),
150150
'<link rel="stylesheet" href="/language/version/_static/custom.css" type="text/css" />',
151151
]
152152

@@ -183,8 +183,8 @@ def test_urls_prefix_setting_none(app, status, warning):
183183
'<img class="logo" src="/_static/logo.svg" alt="Logo"/>',
184184

185185
# resources
186-
_get_css_html_link_tag('', '', 'alabaster.css'),
187-
_get_css_html_link_tag('', '', 'pygments.css'),
186+
_get_css_html_link_tag(app, '', '', 'alabaster.css'),
187+
_get_css_html_link_tag(app, '', '', 'pygments.css'),
188188
'<link rel="stylesheet" href="/_static/custom.css" type="text/css" />',
189189
]
190190

@@ -247,8 +247,8 @@ def test_custom_404_rst_source(app, status, warning):
247247
'<li><a href="/en/latest/index.html">Documentation overview</a><ul>',
248248

249249
# resources
250-
_get_css_html_link_tag('en', 'latest', 'alabaster.css'),
251-
_get_css_html_link_tag('en', 'latest', 'pygments.css'),
250+
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
251+
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
252252
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
253253
]
254254

@@ -350,8 +350,8 @@ def test_urls_for_dirhtml_builder(app, status, warning):
350350
'<li class="toctree-l1"><a class="reference internal" href="/en/latest/chapter/">Chapter</a></li>',
351351

352352
# resources
353-
_get_css_html_link_tag('en', 'latest', 'alabaster.css'),
354-
_get_css_html_link_tag('en', 'latest', 'pygments.css'),
353+
_get_css_html_link_tag(app, 'en', 'latest', 'alabaster.css'),
354+
_get_css_html_link_tag(app, 'en', 'latest', 'pygments.css'),
355355
'<link rel="stylesheet" href="/en/latest/_static/custom.css" type="text/css" />',
356356
]
357357

@@ -369,13 +369,13 @@ def test_sphinx_resource_urls(app, status, warning):
369369

370370
chunks = [
371371
# Sphinx's resources URLs
372-
_get_js_html_link_tag('en', 'latest', 'doctools.js'),
372+
_get_js_html_link_tag(app, 'en', 'latest', 'doctools.js'),
373373
]
374374

375375
if sphinx.version_info < (6, 0):
376376
chunks.extend([
377-
_get_js_html_link_tag('en', 'latest', 'underscore.js'),
378-
_get_js_html_link_tag('en', 'latest', 'jquery.js'),
377+
_get_js_html_link_tag(app, 'en', 'latest', 'underscore.js'),
378+
_get_js_html_link_tag(app, 'en', 'latest', 'jquery.js'),
379379
])
380380

381381
for chunk in chunks:
@@ -401,8 +401,8 @@ def test_toctree_urls_notfound_default(app, status, warning):
401401
'<li class="toctree-l1"><a class="reference internal" href="/ja/default/chapter.html">Chapter</a></li>',
402402

403403
# resources
404-
_get_css_html_link_tag('ja', 'default', 'alabaster.css'),
405-
_get_css_html_link_tag('ja', 'default', 'pygments.css'),
404+
_get_css_html_link_tag(app, 'ja', 'default', 'alabaster.css'),
405+
_get_css_html_link_tag(app, 'ja', 'default', 'pygments.css'),
406406
'<link rel="stylesheet" href="/ja/default/_static/custom.css" type="text/css" />',
407407
]
408408

@@ -469,7 +469,7 @@ def test_resources_from_extension(app, status, warning):
469469
chunks = [
470470
'<link rel="stylesheet" type="text/css" href="/en/latest/_static/css_added_by_extension.css" />',
471471
'<link rel="stylesheet" type="text/css" href="/en/latest/_static/css_added_by_extension.css" />',
472-
_get_js_html_link_tag('en', 'latest', 'js_added_by_extension.js'),
472+
_get_js_html_link_tag(app, 'en', 'latest', 'js_added_by_extension.js'),
473473
]
474474

475475
for chunk in chunks:

tests/utils.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sphinx
22

33

4-
def _get_css_html_link_tag(language, version, filename):
4+
def _get_css_html_link_tag(app, language, version, filename):
55
if not language and not version:
66
href = '/_static/{filename}'.format(filename=filename)
77
else:
@@ -13,18 +13,18 @@ def _get_css_html_link_tag(language, version, filename):
1313

1414
if sphinx.version_info >= (7, 1):
1515
# it requires `?v={hash}`
16-
hashes = {
17-
"pygments.css": "4f649999",
18-
"alabaster.css": "039e1c02",
19-
}
20-
filehash = hashes.get(filename)
16+
if sphinx.version_info < (7, 2):
17+
from sphinx.builders.html import _file_checksum
18+
else:
19+
from sphinx.builders.html._assets import _file_checksum
20+
filehash = _file_checksum(app.outdir / "_static", filename)
2121
if filehash:
2222
href = f"{href}?v={filehash}"
2323

2424
return '<link rel="stylesheet" type="text/css" href="{href}" />'.format(href=href)
2525

2626

27-
def _get_js_html_link_tag(language, version, filename):
27+
def _get_js_html_link_tag(app, language, version, filename):
2828
if not language and not version:
2929
src = '/_static/{filename}'.format(filename=filename)
3030
else:
@@ -36,12 +36,11 @@ def _get_js_html_link_tag(language, version, filename):
3636

3737
if sphinx.version_info >= (7, 1):
3838
# it requires `?v={hash}`
39-
hashes = {
40-
"documentation_options.js": "5929fcd5",
41-
"doctools.js": "888ff710",
42-
"sphinx_highlight.js": "dc90522c",
43-
}
44-
filehash = hashes.get(filename)
39+
if sphinx.version_info < (7, 2):
40+
from sphinx.builders.html import _file_checksum
41+
else:
42+
from sphinx.builders.html._assets import _file_checksum
43+
filehash = _file_checksum(app.outdir / "_static", filename)
4544
if filehash:
4645
src = f"{src}?v={filehash}"
4746

0 commit comments

Comments
 (0)