| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import hashlib |
| 4 |
import re |
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from sphinx.testing.util import SphinxTestApp |
| 11 |
|
| 12 |
|
| 13 |
@pytest.mark.sphinx('html', testroot='root') |
| 14 |
def test_html_download(app: SphinxTestApp) -> None: |
| 15 |
app.build() |
| 16 |
|
| 17 |
# subdir/includes.html |
| 18 |
result = (app.outdir / 'subdir' / 'includes.html').read_text(encoding='utf8') |
| 19 |
pattern = ( |
| 20 |
'<a class="reference download internal" download="" ' |
| 21 |
'href="../(_downloads/.*/img.png)">' |
| 22 |
) |
| 23 |
matched = re.search(pattern, result) |
| 24 |
assert matched |
| 25 |
assert (app.outdir / matched.group(1)).exists() |
| 26 |
filename = matched.group(1) |
| 27 |
|
| 28 |
# includes.html |
| 29 |
result = (app.outdir / 'includes.html').read_text(encoding='utf8') |
| 30 |
pattern = ( |
| 31 |
'<a class="reference download internal" download="" ' |
| 32 |
'href="(_downloads/.*/img.png)">' |
| 33 |
) |
| 34 |
matched = re.search(pattern, result) |
| 35 |
assert matched |
| 36 |
assert (app.outdir / matched.group(1)).exists() |
| 37 |
assert matched.group(1) == filename |
| 38 |
|
| 39 |
pattern = ( |
| 40 |
'<a class="reference download internal" download="" ' |
| 41 |
'href="(_downloads/.*/)(file_with_special_%23_chars.xyz)">' |
| 42 |
) |
| 43 |
matched = re.search(pattern, result) |
| 44 |
assert matched |
| 45 |
assert (app.outdir / matched.group(1) / 'file_with_special_#_chars.xyz').exists() |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.sphinx('html', testroot='roles-download') |
| 49 |
def test_html_download_role(app: SphinxTestApp) -> None: |
| 50 |
app.build() |
| 51 |
digest = hashlib.md5(b'dummy.dat', usedforsecurity=False).hexdigest() |
| 52 |
assert (app.outdir / '_downloads' / digest / 'dummy.dat').exists() |
| 53 |
digest_another = hashlib.md5( |
| 54 |
b'another/dummy.dat', usedforsecurity=False |
| 55 |
).hexdigest() |
| 56 |
assert (app.outdir / '_downloads' / digest_another / 'dummy.dat').exists() |
| 57 |
|
| 58 |
content = (app.outdir / 'index.html').read_text(encoding='utf8') |
| 59 |
assert ( |
| 60 |
'<li><p><a class="reference download internal" download="" ' |
| 61 |
'href="_downloads/%s/dummy.dat">' |
| 62 |
'<code class="xref download docutils literal notranslate">' |
| 63 |
'<span class="pre">dummy.dat</span></code></a></p></li>' % digest |
| 64 |
) in content |
| 65 |
assert ( |
| 66 |
'<li><p><a class="reference download internal" download="" ' |
| 67 |
'href="_downloads/%s/dummy.dat">' |
| 68 |
'<code class="xref download docutils literal notranslate">' |
| 69 |
'<span class="pre">another/dummy.dat</span></code></a></p></li>' |
| 70 |
% digest_another |
| 71 |
) in content |
| 72 |
assert ( |
| 73 |
'<li><p><code class="xref download docutils literal notranslate">' |
| 74 |
'<span class="pre">not_found.dat</span></code></p></li>' |
| 75 |
) in content |
| 76 |
assert ( |
| 77 |
'<li><p><a class="reference download external" download="" ' |
| 78 |
'href="https://www.sphinx-doc.org/en/master/_static/sphinx-logo.svg">' |
| 79 |
'<code class="xref download docutils literal notranslate">' |
| 80 |
'<span class="pre">Sphinx</span> <span class="pre">logo</span>' |
| 81 |
'</code></a></p></li>' |
| 82 |
) in content |