| 1 |
"""Tests sphinx.util.fileutil functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
from pathlib import Path |
| 7 |
from typing import TYPE_CHECKING |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from sphinx._cli.util.errors import strip_escape_sequences |
| 13 |
from sphinx.jinja2glue import BuiltinTemplateLoader |
| 14 |
from sphinx.util.fileutil import _template_basename, copy_asset, copy_asset_file |
| 15 |
from sphinx.util.template import BaseRenderer |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from sphinx.testing.util import SphinxTestApp |
| 19 |
|
| 20 |
|
| 21 |
class DummyTemplateLoader(BuiltinTemplateLoader, BaseRenderer): |
| 22 |
def __init__(self) -> None: |
| 23 |
super().__init__() |
| 24 |
builder = mock.Mock() |
| 25 |
builder.config.templates_path = [] |
| 26 |
builder._translator = None |
| 27 |
self.init(builder) |
| 28 |
|
| 29 |
|
| 30 |
def test_copy_asset_file(tmp_path: Path) -> None: |
| 31 |
renderer = DummyTemplateLoader() |
| 32 |
|
| 33 |
# copy normal file |
| 34 |
src = tmp_path / 'asset.txt' |
| 35 |
src.write_text('# test data', encoding='utf8') |
| 36 |
dest = tmp_path / 'output.txt' |
| 37 |
|
| 38 |
copy_asset_file(src, dest) |
| 39 |
assert dest.exists() |
| 40 |
assert src.read_text(encoding='utf8') == dest.read_text(encoding='utf8') |
| 41 |
|
| 42 |
# copy template file |
| 43 |
src = tmp_path / 'asset.txt.jinja' |
| 44 |
src.write_text('# {{var1}} data', encoding='utf8') |
| 45 |
dest = tmp_path / 'output.txt.jinja' |
| 46 |
|
| 47 |
copy_asset_file(str(src), str(dest), {'var1': 'template'}, renderer) |
| 48 |
assert not dest.exists() |
| 49 |
assert (tmp_path / 'output.txt').exists() |
| 50 |
assert (tmp_path / 'output.txt').read_text(encoding='utf8') == '# template data' |
| 51 |
|
| 52 |
# copy template file to subdir |
| 53 |
src = tmp_path / 'asset.txt.jinja' |
| 54 |
src.write_text('# {{var1}} data', encoding='utf8') |
| 55 |
subdir1 = tmp_path / 'subdir' |
| 56 |
subdir1.mkdir(parents=True, exist_ok=True) |
| 57 |
|
| 58 |
copy_asset_file(src, subdir1, context={'var1': 'template'}, renderer=renderer) |
| 59 |
assert (subdir1 / 'asset.txt').exists() |
| 60 |
assert (subdir1 / 'asset.txt').read_text(encoding='utf8') == '# template data' |
| 61 |
|
| 62 |
# copy template file without context |
| 63 |
src = tmp_path / 'asset.txt.jinja' |
| 64 |
subdir2 = tmp_path / 'subdir2' |
| 65 |
subdir2.mkdir(parents=True, exist_ok=True) |
| 66 |
|
| 67 |
copy_asset_file(src, subdir2) |
| 68 |
assert not (subdir2 / 'asset.txt').exists() |
| 69 |
assert (subdir2 / 'asset.txt.jinja').exists() |
| 70 |
assert (subdir2 / 'asset.txt.jinja').read_text(encoding='utf8') == '# {{var1}} data' |
| 71 |
|
| 72 |
|
| 73 |
def test_copy_asset(tmp_path: Path) -> None: |
| 74 |
renderer = DummyTemplateLoader() |
| 75 |
|
| 76 |
# prepare source files |
| 77 |
source = tmp_path / 'source' |
| 78 |
source.mkdir(parents=True, exist_ok=True) |
| 79 |
(source / 'index.rst').write_text('index.rst', encoding='utf8') |
| 80 |
(source / 'foo.rst.jinja').write_text('{{var1}}.rst', encoding='utf8') |
| 81 |
(source / '_static').mkdir(parents=True, exist_ok=True) |
| 82 |
(source / '_static' / 'basic.css').write_text('basic.css', encoding='utf8') |
| 83 |
(source / '_templates').mkdir(parents=True, exist_ok=True) |
| 84 |
(source / '_templates' / 'layout.html').write_text('layout.html', encoding='utf8') |
| 85 |
(source / '_templates' / 'sidebar.html.jinja').write_text( |
| 86 |
'sidebar: {{var2}}', encoding='utf8' |
| 87 |
) |
| 88 |
sibling = tmp_path / 'sibling' |
| 89 |
sibling.mkdir(parents=True, exist_ok=True) |
| 90 |
sibling.joinpath('spam').mkdir(parents=True, exist_ok=True) |
| 91 |
sibling.joinpath('spam', 'ham').touch() |
| 92 |
|
| 93 |
# copy a single file |
| 94 |
assert not (tmp_path / 'test1').exists() |
| 95 |
copy_asset(source / 'index.rst', tmp_path / 'test1') |
| 96 |
assert (tmp_path / 'test1').exists() |
| 97 |
assert (tmp_path / 'test1/index.rst').exists() |
| 98 |
|
| 99 |
# copy directories |
| 100 |
destdir = tmp_path / 'test2' |
| 101 |
copy_asset( |
| 102 |
source, |
| 103 |
destdir, |
| 104 |
context={'var1': 'bar', 'var2': 'baz'}, |
| 105 |
renderer=renderer, |
| 106 |
) |
| 107 |
assert (destdir / 'index.rst').exists() |
| 108 |
assert (destdir / 'foo.rst').exists() |
| 109 |
assert (destdir / 'foo.rst').read_text(encoding='utf8') == 'bar.rst' |
| 110 |
assert (destdir / '_static' / 'basic.css').exists() |
| 111 |
assert (destdir / '_templates' / 'layout.html').exists() |
| 112 |
assert (destdir / '_templates' / 'sidebar.html').exists() |
| 113 |
sidebar = (destdir / '_templates' / 'sidebar.html').read_text(encoding='utf8') |
| 114 |
assert sidebar == 'sidebar: baz' |
| 115 |
|
| 116 |
# copy with exclusion |
| 117 |
def excluded(path: str) -> bool: |
| 118 |
return 'sidebar.html' in path or 'basic.css' in path |
| 119 |
|
| 120 |
destdir = tmp_path / 'test3' |
| 121 |
copy_asset( |
| 122 |
source, |
| 123 |
destdir, |
| 124 |
excluded, |
| 125 |
context={'var1': 'bar', 'var2': 'baz'}, |
| 126 |
renderer=renderer, |
| 127 |
) |
| 128 |
assert (destdir / 'index.rst').exists() |
| 129 |
assert (destdir / 'foo.rst').exists() |
| 130 |
assert not (destdir / '_static' / 'basic.css').exists() |
| 131 |
assert (destdir / '_templates' / 'layout.html').exists() |
| 132 |
assert not (destdir / '_templates' / 'sidebar.html').exists() |
| 133 |
|
| 134 |
# copy sibling |
| 135 |
assert not (tmp_path / 'test4').exists() |
| 136 |
copy_asset(source / '../sibling', tmp_path / 'test4') |
| 137 |
assert (tmp_path / 'test4').is_dir() |
| 138 |
assert (tmp_path / 'test4' / 'spam').is_dir() |
| 139 |
assert (tmp_path / 'test4' / 'spam' / 'ham').is_file() |
| 140 |
assert (tmp_path / 'test4' / 'spam' / 'ham').read_bytes() == b'' |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.sphinx('html', testroot='html_assets') |
| 144 |
def test_copy_asset_template(app: SphinxTestApp) -> None: |
| 145 |
app.build(force_all=True) |
| 146 |
|
| 147 |
expected_msg = r'^Writing evaluated template result to [^\n]*\bAPI.html$' |
| 148 |
output = strip_escape_sequences(app.status.getvalue()) |
| 149 |
assert re.findall(expected_msg, output, flags=re.MULTILINE) |
| 150 |
|
| 151 |
|
| 152 |
@pytest.mark.sphinx('html', testroot='util-copyasset_overwrite') |
| 153 |
def test_copy_asset_overwrite(app: SphinxTestApp) -> None: |
| 154 |
app.build() |
| 155 |
src = app.srcdir / 'myext_static' / 'custom-styles.css' |
| 156 |
dst = app.outdir / '_static' / 'custom-styles.css' |
| 157 |
assert strip_escape_sequences(app.warning.getvalue()) == ( |
| 158 |
f'WARNING: Aborted attempted copy from {src} to {dst} ' |
| 159 |
'(the destination path has existing data). ' |
| 160 |
'[misc.copy_overwrite]\n' |
| 161 |
) |
| 162 |
|
| 163 |
|
| 164 |
def test_template_basename() -> None: |
| 165 |
assert _template_basename(Path('asset.txt')) is None |
| 166 |
assert _template_basename(Path('asset.txt.jinja')) == Path('asset.txt') |
| 167 |
assert _template_basename(Path('sidebar.html.jinja')) == Path('sidebar.html') |
| 168 |
|
| 169 |
|
| 170 |
def test_legacy_template_basename() -> None: |
| 171 |
assert _template_basename(Path('asset.txt_t')) == Path('asset.txt') |