|
1 | 1 | """Test the build process with LaTeX builder with the test root.""" |
2 | 2 |
|
| 3 | +import http.server |
3 | 4 | import os |
4 | 5 | import re |
5 | 6 | import subprocess |
|
17 | 18 | from sphinx.util.osutil import ensuredir |
18 | 19 | from sphinx.writers.latex import LaTeXTranslator |
19 | 20 |
|
| 21 | +from tests.utils import http_server |
| 22 | + |
20 | 23 | try: |
21 | 24 | from contextlib import chdir |
22 | 25 | except ImportError: |
@@ -79,6 +82,28 @@ def skip_if_stylefiles_notfound(testfunc): |
79 | 82 | return testfunc |
80 | 83 |
|
81 | 84 |
|
| 85 | +class RemoteImageHandler(http.server.BaseHTTPRequestHandler): |
| 86 | + protocol_version = "HTTP/1.1" |
| 87 | + |
| 88 | + def do_GET(self): |
| 89 | + content, content_type = None, None |
| 90 | + if self.path == "/sphinx.png": |
| 91 | + with open("tests/roots/test-local-logo/images/img.png", "rb") as f: |
| 92 | + content = f.read() |
| 93 | + content_type = "image/png" |
| 94 | + |
| 95 | + if content: |
| 96 | + self.send_response(200, "OK") |
| 97 | + self.send_header("Content-Length", str(len(content))) |
| 98 | + self.send_header("Content-Type", content_type) |
| 99 | + self.end_headers() |
| 100 | + self.wfile.write(content) |
| 101 | + else: |
| 102 | + self.send_response(404, "Not Found") |
| 103 | + self.send_header("Content-Length", "0") |
| 104 | + self.end_headers() |
| 105 | + |
| 106 | + |
82 | 107 | @skip_if_requested |
83 | 108 | @skip_if_stylefiles_notfound |
84 | 109 | @pytest.mark.parametrize( |
@@ -112,7 +137,8 @@ def test_build_latex_doc(app, engine, docclass, python_maximum_signature_line_le |
112 | 137 | load_mappings(app) |
113 | 138 | app.builder.init() |
114 | 139 | LaTeXTranslator.ignore_missing_images = True |
115 | | - app.build(force_all=True) |
| 140 | + with http_server(RemoteImageHandler): |
| 141 | + app.build(force_all=True) |
116 | 142 |
|
117 | 143 | # file from latex_additional_files |
118 | 144 | assert (app.outdir / 'svgimg.svg').is_file() |
@@ -1398,21 +1424,21 @@ def test_latex_raw_directive(app, status, warning): |
1398 | 1424 | assert 'LaTeX: abc def ghi' in result |
1399 | 1425 |
|
1400 | 1426 |
|
1401 | | -@pytest.mark.usefixtures('if_online') |
1402 | 1427 | @pytest.mark.sphinx('latex', testroot='images') |
1403 | 1428 | def test_latex_images(app, status, warning): |
1404 | | - app.build(force_all=True) |
| 1429 | + with http_server(RemoteImageHandler, port=7777): |
| 1430 | + app.build(force_all=True) |
1405 | 1431 |
|
1406 | 1432 | result = (app.outdir / 'python.tex').read_text(encoding='utf8') |
1407 | 1433 |
|
1408 | 1434 | # images are copied |
1409 | | - assert '\\sphinxincludegraphics{{python-logo}.png}' in result |
1410 | | - assert (app.outdir / 'python-logo.png').exists() |
| 1435 | + assert '\\sphinxincludegraphics{{sphinx}.png}' in result |
| 1436 | + assert (app.outdir / 'sphinx.png').exists() |
1411 | 1437 |
|
1412 | 1438 | # not found images |
1413 | 1439 | assert '\\sphinxincludegraphics{{NOT_EXIST}.PNG}' not in result |
1414 | 1440 | assert ('WARNING: Could not fetch remote image: ' |
1415 | | - 'https://www.google.com/NOT_EXIST.PNG [404]' in warning.getvalue()) |
| 1441 | + 'http://localhost:7777/NOT_EXIST.PNG [404]' in warning.getvalue()) |
1416 | 1442 |
|
1417 | 1443 | # an image having target |
1418 | 1444 | assert ('\\sphinxhref{https://www.sphinx-doc.org/}' |
@@ -1682,7 +1708,7 @@ def test_copy_images(app, status, warning): |
1682 | 1708 | image.name for image in test_dir.rglob('*') |
1683 | 1709 | if image.suffix in {'.gif', '.pdf', '.png', '.svg'} |
1684 | 1710 | } |
1685 | | - images.discard('python-logo.png') |
| 1711 | + images.discard('sphinx.png') |
1686 | 1712 | assert images == { |
1687 | 1713 | 'img.pdf', |
1688 | 1714 | 'rimg.png', |
|
0 commit comments