2022-02-19 21:05:56 -06:00
|
|
|
"""Tests sphinx.util.fileutil functions."""
|
2016-06-26 08:22:25 -05:00
|
|
|
|
2024-11-22 15:54:26 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-08-11 10:37:41 -05:00
|
|
|
import re
|
2019-03-07 08:35:36 -06:00
|
|
|
from unittest import mock
|
2016-06-26 08:22:25 -05:00
|
|
|
|
2024-07-19 02:20:48 -05:00
|
|
|
import pytest
|
|
|
|
|
2018-02-19 07:39:14 -06:00
|
|
|
from sphinx.jinja2glue import BuiltinTemplateLoader
|
2024-10-03 14:17:40 -05:00
|
|
|
from sphinx.util.console import strip_colors
|
2024-04-23 04:40:24 -05:00
|
|
|
from sphinx.util.fileutil import _template_basename, copy_asset, copy_asset_file
|
2018-02-19 07:39:14 -06:00
|
|
|
|
2016-06-26 08:22:25 -05:00
|
|
|
|
|
|
|
class DummyTemplateLoader(BuiltinTemplateLoader):
|
2024-10-03 14:17:40 -05:00
|
|
|
def __init__(self) -> None:
|
2018-12-15 10:25:47 -06:00
|
|
|
super().__init__()
|
2016-10-15 02:22:27 -05:00
|
|
|
builder = mock.Mock()
|
2016-06-26 08:22:25 -05:00
|
|
|
builder.config.templates_path = []
|
2021-07-07 11:29:38 -05:00
|
|
|
builder.app.translator = None
|
2016-06-26 08:22:25 -05:00
|
|
|
self.init(builder)
|
|
|
|
|
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
def test_copy_asset_file(tmp_path):
|
2016-06-26 08:22:25 -05:00
|
|
|
renderer = DummyTemplateLoader()
|
|
|
|
|
|
|
|
# copy normal file
|
2024-08-11 08:58:56 -05:00
|
|
|
src = tmp_path / 'asset.txt'
|
2023-08-10 14:48:04 -05:00
|
|
|
src.write_text('# test data', encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
dest = tmp_path / 'output.txt'
|
2016-06-26 08:22:25 -05:00
|
|
|
|
|
|
|
copy_asset_file(src, dest)
|
|
|
|
assert dest.exists()
|
2022-04-26 21:04:19 -05:00
|
|
|
assert src.read_text(encoding='utf8') == dest.read_text(encoding='utf8')
|
2016-06-26 08:22:25 -05:00
|
|
|
|
|
|
|
# copy template file
|
2024-08-11 08:58:56 -05:00
|
|
|
src = tmp_path / 'asset.txt.jinja'
|
2023-08-10 14:48:04 -05:00
|
|
|
src.write_text('# {{var1}} data', encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
dest = tmp_path / 'output.txt.jinja'
|
2016-06-26 08:22:25 -05:00
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
copy_asset_file(str(src), str(dest), {'var1': 'template'}, renderer)
|
2016-06-26 08:22:25 -05:00
|
|
|
assert not dest.exists()
|
2023-07-27 18:39:12 -05:00
|
|
|
assert (tmp_path / 'output.txt').exists()
|
|
|
|
assert (tmp_path / 'output.txt').read_text(encoding='utf8') == '# template data'
|
2016-06-26 08:22:25 -05:00
|
|
|
|
|
|
|
# copy template file to subdir
|
2024-08-11 08:58:56 -05:00
|
|
|
src = tmp_path / 'asset.txt.jinja'
|
2023-08-10 14:48:04 -05:00
|
|
|
src.write_text('# {{var1}} data', encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
subdir1 = tmp_path / 'subdir'
|
2023-07-27 18:39:12 -05:00
|
|
|
subdir1.mkdir(parents=True, exist_ok=True)
|
2016-06-26 08:22:25 -05:00
|
|
|
|
2024-07-22 10:00:16 -05:00
|
|
|
copy_asset_file(src, subdir1, context={'var1': 'template'}, renderer=renderer)
|
2016-06-26 22:13:16 -05:00
|
|
|
assert (subdir1 / 'asset.txt').exists()
|
2022-04-26 21:04:19 -05:00
|
|
|
assert (subdir1 / 'asset.txt').read_text(encoding='utf8') == '# template data'
|
2016-06-26 22:13:16 -05:00
|
|
|
|
|
|
|
# copy template file without context
|
2024-08-11 08:58:56 -05:00
|
|
|
src = tmp_path / 'asset.txt.jinja'
|
|
|
|
subdir2 = tmp_path / 'subdir2'
|
2023-07-27 18:39:12 -05:00
|
|
|
subdir2.mkdir(parents=True, exist_ok=True)
|
2016-06-26 22:13:16 -05:00
|
|
|
|
|
|
|
copy_asset_file(src, subdir2)
|
|
|
|
assert not (subdir2 / 'asset.txt').exists()
|
2024-07-11 06:31:35 -05:00
|
|
|
assert (subdir2 / 'asset.txt.jinja').exists()
|
|
|
|
assert (subdir2 / 'asset.txt.jinja').read_text(encoding='utf8') == '# {{var1}} data'
|
2016-06-26 21:54:42 -05:00
|
|
|
|
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
def test_copy_asset(tmp_path):
|
2016-06-26 21:54:42 -05:00
|
|
|
renderer = DummyTemplateLoader()
|
|
|
|
|
|
|
|
# prepare source files
|
2024-08-11 08:58:56 -05:00
|
|
|
source = tmp_path / 'source'
|
2023-07-27 18:39:12 -05:00
|
|
|
source.mkdir(parents=True, exist_ok=True)
|
2022-04-26 21:11:08 -05:00
|
|
|
(source / 'index.rst').write_text('index.rst', encoding='utf8')
|
2024-07-11 06:31:35 -05:00
|
|
|
(source / 'foo.rst.jinja').write_text('{{var1}}.rst', encoding='utf8')
|
2023-07-27 18:39:12 -05:00
|
|
|
(source / '_static').mkdir(parents=True, exist_ok=True)
|
2022-04-26 21:11:08 -05:00
|
|
|
(source / '_static' / 'basic.css').write_text('basic.css', encoding='utf8')
|
2023-07-27 18:39:12 -05:00
|
|
|
(source / '_templates').mkdir(parents=True, exist_ok=True)
|
2022-04-26 21:11:08 -05:00
|
|
|
(source / '_templates' / 'layout.html').write_text('layout.html', encoding='utf8')
|
2024-08-11 08:58:56 -05:00
|
|
|
(source / '_templates' / 'sidebar.html.jinja').write_text(
|
|
|
|
'sidebar: {{var2}}', encoding='utf8'
|
|
|
|
)
|
2016-06-26 21:54:42 -05:00
|
|
|
|
|
|
|
# copy a single file
|
2023-07-27 18:39:12 -05:00
|
|
|
assert not (tmp_path / 'test1').exists()
|
|
|
|
copy_asset(source / 'index.rst', tmp_path / 'test1')
|
|
|
|
assert (tmp_path / 'test1').exists()
|
|
|
|
assert (tmp_path / 'test1/index.rst').exists()
|
2016-06-26 21:54:42 -05:00
|
|
|
|
|
|
|
# copy directories
|
2023-07-27 18:39:12 -05:00
|
|
|
destdir = tmp_path / 'test2'
|
2024-08-11 08:58:56 -05:00
|
|
|
copy_asset(
|
|
|
|
source,
|
|
|
|
destdir,
|
|
|
|
context={'var1': 'bar', 'var2': 'baz'},
|
|
|
|
renderer=renderer,
|
|
|
|
)
|
2016-06-26 21:54:42 -05:00
|
|
|
assert (destdir / 'index.rst').exists()
|
|
|
|
assert (destdir / 'foo.rst').exists()
|
2022-04-26 21:04:19 -05:00
|
|
|
assert (destdir / 'foo.rst').read_text(encoding='utf8') == 'bar.rst'
|
2016-06-26 21:54:42 -05:00
|
|
|
assert (destdir / '_static' / 'basic.css').exists()
|
|
|
|
assert (destdir / '_templates' / 'layout.html').exists()
|
|
|
|
assert (destdir / '_templates' / 'sidebar.html').exists()
|
2024-08-11 08:58:56 -05:00
|
|
|
sidebar = (destdir / '_templates' / 'sidebar.html').read_text(encoding='utf8')
|
|
|
|
assert sidebar == 'sidebar: baz'
|
2016-06-26 21:54:42 -05:00
|
|
|
|
|
|
|
# copy with exclusion
|
|
|
|
def excluded(path):
|
2024-08-11 08:58:56 -05:00
|
|
|
return 'sidebar.html' in path or 'basic.css' in path
|
2016-06-26 21:54:42 -05:00
|
|
|
|
2023-07-27 18:39:12 -05:00
|
|
|
destdir = tmp_path / 'test3'
|
2024-08-11 08:58:56 -05:00
|
|
|
copy_asset(
|
|
|
|
source,
|
|
|
|
destdir,
|
|
|
|
excluded,
|
|
|
|
context={'var1': 'bar', 'var2': 'baz'},
|
|
|
|
renderer=renderer,
|
|
|
|
)
|
2016-06-26 21:54:42 -05:00
|
|
|
assert (destdir / 'index.rst').exists()
|
|
|
|
assert (destdir / 'foo.rst').exists()
|
|
|
|
assert not (destdir / '_static' / 'basic.css').exists()
|
|
|
|
assert (destdir / '_templates' / 'layout.html').exists()
|
|
|
|
assert not (destdir / '_templates' / 'sidebar.html').exists()
|
2024-04-23 04:40:24 -05:00
|
|
|
|
|
|
|
|
2024-08-11 10:37:41 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='html_assets')
|
|
|
|
def test_copy_asset_template(app):
|
|
|
|
app.build(force_all=True)
|
|
|
|
|
2024-08-11 10:58:19 -05:00
|
|
|
expected_msg = r'^Writing evaluated template result to [^\n]*\bAPI.html$'
|
|
|
|
output = strip_colors(app.status.getvalue())
|
|
|
|
assert re.findall(expected_msg, output, flags=re.MULTILINE)
|
2024-08-11 10:37:41 -05:00
|
|
|
|
|
|
|
|
2024-07-19 02:20:48 -05:00
|
|
|
@pytest.mark.sphinx('html', testroot='util-copyasset_overwrite')
|
|
|
|
def test_copy_asset_overwrite(app):
|
|
|
|
app.build()
|
|
|
|
src = app.srcdir / 'myext_static' / 'custom-styles.css'
|
|
|
|
dst = app.outdir / '_static' / 'custom-styles.css'
|
2024-07-22 10:00:16 -05:00
|
|
|
assert strip_colors(app.warning.getvalue()) == (
|
|
|
|
f'WARNING: Aborted attempted copy from {src} to {dst} '
|
|
|
|
'(the destination path has existing data). '
|
|
|
|
'[misc.copy_overwrite]\n'
|
|
|
|
)
|
2024-07-19 02:20:48 -05:00
|
|
|
|
|
|
|
|
2024-04-23 04:40:24 -05:00
|
|
|
def test_template_basename():
|
|
|
|
assert _template_basename('asset.txt') is None
|
|
|
|
assert _template_basename('asset.txt.jinja') == 'asset.txt'
|
|
|
|
assert _template_basename('sidebar.html.jinja') == 'sidebar.html'
|
|
|
|
|
|
|
|
|
|
|
|
def test_legacy_template_basename():
|
|
|
|
assert _template_basename('asset.txt_t') == 'asset.txt'
|