sphinx/tests/test_builders/test_build_html_download.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

79 lines
2.8 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-17 18:08:30 -06:00
import hashlib
import re
import pytest
@pytest.mark.sphinx('html', testroot='root')
2024-01-17 18:08:30 -06:00
def test_html_download(app):
app.build()
# subdir/includes.html
result = (app.outdir / 'subdir' / 'includes.html').read_text(encoding='utf8')
2024-08-11 08:58:56 -05:00
pattern = (
'<a class="reference download internal" download="" '
'href="../(_downloads/.*/img.png)">'
)
2024-01-17 18:08:30 -06:00
matched = re.search(pattern, result)
assert matched
assert (app.outdir / matched.group(1)).exists()
filename = matched.group(1)
# includes.html
result = (app.outdir / 'includes.html').read_text(encoding='utf8')
2024-08-11 08:58:56 -05:00
pattern = (
'<a class="reference download internal" download="" '
'href="(_downloads/.*/img.png)">'
)
2024-01-17 18:08:30 -06:00
matched = re.search(pattern, result)
assert matched
assert (app.outdir / matched.group(1)).exists()
assert matched.group(1) == filename
2024-08-11 08:58:56 -05:00
pattern = (
'<a class="reference download internal" download="" '
'href="(_downloads/.*/)(file_with_special_%23_chars.xyz)">'
)
2024-01-17 18:08:30 -06:00
matched = re.search(pattern, result)
assert matched
2024-08-11 08:58:56 -05:00
assert (app.outdir / matched.group(1) / 'file_with_special_#_chars.xyz').exists()
2024-01-17 18:08:30 -06:00
@pytest.mark.sphinx('html', testroot='roles-download')
def test_html_download_role(app):
2024-01-17 18:08:30 -06:00
app.build()
digest = hashlib.md5(b'dummy.dat', usedforsecurity=False).hexdigest()
assert (app.outdir / '_downloads' / digest / 'dummy.dat').exists()
2024-08-11 08:58:56 -05:00
digest_another = hashlib.md5(
b'another/dummy.dat', usedforsecurity=False
).hexdigest()
2024-01-17 18:08:30 -06:00
assert (app.outdir / '_downloads' / digest_another / 'dummy.dat').exists()
content = (app.outdir / 'index.html').read_text(encoding='utf8')
2024-08-11 08:58:56 -05:00
assert (
'<li><p><a class="reference download internal" download="" '
'href="_downloads/%s/dummy.dat">'
'<code class="xref download docutils literal notranslate">'
'<span class="pre">dummy.dat</span></code></a></p></li>' % digest
) in content
assert (
'<li><p><a class="reference download internal" download="" '
'href="_downloads/%s/dummy.dat">'
'<code class="xref download docutils literal notranslate">'
'<span class="pre">another/dummy.dat</span></code></a></p></li>'
% digest_another
) in content
assert (
'<li><p><code class="xref download docutils literal notranslate">'
'<span class="pre">not_found.dat</span></code></p></li>'
) in content
assert (
'<li><p><a class="reference download external" download="" '
'href="https://www.sphinx-doc.org/en/master/_static/sphinx-logo.svg">'
'<code class="xref download docutils literal notranslate">'
'<span class="pre">Sphinx</span> <span class="pre">logo</span>'
'</code></a></p></li>'
) in content