Debian Codegraph @

tests/test_builders/test_build_html_image.py

raw
1 from __future__ import annotations
2
3 import re
4 from pathlib import Path
5 from typing import TYPE_CHECKING
6
7 import docutils
8 import pytest
9
10 if TYPE_CHECKING:
11 from sphinx.testing.util import SphinxTestApp
12
13
14 @pytest.mark.usefixtures('_http_teapot')
15 @pytest.mark.sphinx('html', testroot='images')
16 def test_html_remote_images(app: SphinxTestApp) -> None:
17 app.build(force_all=True)
18
19 result = (app.outdir / 'index.html').read_text(encoding='utf8')
20 assert (
21 '<img alt="http://localhost:7777/sphinx.png" '
22 'src="http://localhost:7777/sphinx.png" />'
23 ) in result
24 assert not (app.outdir / 'sphinx.png').exists()
25
26
27 @pytest.mark.sphinx('html', testroot='image-escape')
28 def test_html_encoded_image(app: SphinxTestApp) -> None:
29 app.build(force_all=True)
30
31 result = (app.outdir / 'index.html').read_text(encoding='utf8')
32 assert '<img alt="_images/img_%231.png" src="_images/img_%231.png" />' in result
33 assert (app.outdir / '_images/img_#1.png').exists()
34
35
36 @pytest.mark.sphinx('html', testroot='remote-logo')
37 def test_html_remote_logo(app: SphinxTestApp) -> None:
38 app.build(force_all=True)
39
40 result = (app.outdir / 'index.html').read_text(encoding='utf8')
41 assert (
42 '<img class="logo" src="https://www.python.org/static/img/python-logo.png" alt="Logo of Project name not set"/>'
43 ) in result
44 assert (
45 '<link rel="icon" href="https://www.python.org/static/favicon.ico"/>'
46 ) in result
47 assert not (app.outdir / 'python-logo.png').exists()
48
49
50 @pytest.mark.sphinx('html', testroot='local-logo')
51 def test_html_local_logo(app: SphinxTestApp) -> None:
52 app.build(force_all=True)
53
54 result = (app.outdir / 'index.html').read_text(encoding='utf8')
55 assert (
56 '<img class="logo" src="_static/img.png" alt="Logo of Project name not set"/>'
57 ) in result
58 assert (app.outdir / '_static/img.png').exists()
59
60
61 @pytest.mark.sphinx('html', testroot='html_scaled_image_link')
62 def test_html_scaled_image_link(app: SphinxTestApp) -> None:
63 app.build()
64 context = (app.outdir / 'index.html').read_text(encoding='utf8')
65
66 # no scaled parameters
67 assert re.search('\n<img alt="_images/img.png" src="_images/img.png" />', context)
68
69 # scaled_image_link
70 if docutils.__version_info__[:2] >= (0, 22):
71 assert re.search(
72 '\n<a class="reference internal image-reference" href="_images/img.png">'
73 '<img alt="_images/img.png" height="[^"]+" src="_images/img.png" width="[^"]+" />'
74 '\n</a>',
75 context,
76 )
77 else:
78 assert re.search(
79 '\n<a class="reference internal image-reference" href="_images/img.png">'
80 '<img alt="_images/img.png" src="_images/img.png" style="[^"]+" />'
81 '\n</a>',
82 context,
83 )
84
85 # no-scaled-link class disables the feature
86 if docutils.__version_info__[:2] >= (0, 22):
87 assert re.search(
88 '\n<img alt="_images/img.png" class="no-scaled-link"'
89 ' height="[^"]+" src="_images/img.png" width="[^"]+" />',
90 context,
91 )
92 else:
93 assert re.search(
94 '\n<img alt="_images/img.png" class="no-scaled-link"'
95 ' src="_images/img.png" style="[^"]+" />',
96 context,
97 )
98
99
100 @pytest.mark.usefixtures('_http_teapot')
101 @pytest.mark.sphinx('html', testroot='images')
102 def test_copy_images(app: SphinxTestApp) -> None:
103 app.build()
104
105 images_dir = Path(app.outdir) / '_images'
106 images = {image.name for image in images_dir.rglob('*')}
107 assert images == {
108 # 'ba30773957c3fe046897111afd65a80b81cad089.png', # html: image from data:image/png URI in source
109 'img.png',
110 'rimg.png',
111 'rimg1.png',
112 'svgimg.svg',
113 'testimäge.png',
114 }