"""Test the Theme class."""
import os
import shutil
from pathlib import Path
from xml.etree.ElementTree import ParseError
import pytest
from defusedxml.ElementTree import parse as xml_parse
import sphinx.builders.html
from sphinx.errors import ThemeError
from sphinx.theming import (
_ConfigFile,
_convert_theme_conf,
_convert_theme_toml,
_load_theme,
_load_theme_conf,
_load_theme_toml,
)
HERE = Path(__file__).resolve().parent
@pytest.mark.sphinx(
testroot='theming',
confoverrides={'html_theme': 'ziptheme', 'html_theme_options.testopt': 'foo'},
)
def test_theme_api(app, status, warning):
themes = [
'basic',
'default',
'scrolls',
'agogo',
'sphinxdoc',
'haiku',
'traditional',
'epub',
'nature',
'pyramid',
'bizstyle',
'classic',
'nonav',
'test-theme',
'ziptheme',
'staticfiles',
'parent',
'child',
'alabaster',
]
# test Theme class API
assert set(app.registry.html_themes.keys()) == set(themes)
assert app.registry.html_themes['test-theme'] == str(
app.srcdir / 'test_theme' / 'test-theme'
)
assert app.registry.html_themes['ziptheme'] == str(app.srcdir / 'ziptheme.zip')
assert app.registry.html_themes['staticfiles'] == str(
app.srcdir / 'test_theme' / 'staticfiles'
)
# test Theme instance API
theme = app.builder.theme
assert theme.name == 'ziptheme'
assert len(theme.get_theme_dirs()) == 2
# direct setting
assert theme.get_config('theme', 'stylesheet') == 'custom.css'
# inherited setting
assert theme.get_config('options', 'nosidebar') == 'false'
# nonexisting setting
assert theme.get_config('theme', 'foobar', 'def') == 'def'
with pytest.raises(ThemeError):
theme.get_config('theme', 'foobar')
# options API
options = theme.get_options({'nonexisting': 'foo'})
assert 'nonexisting' not in options
options = theme.get_options(app.config.html_theme_options)
assert options['testopt'] == 'foo'
assert options['nosidebar'] == 'false'
# cleanup temp directories
theme._cleanup()
assert not any(map(os.path.exists, theme._tmp_dirs))
def test_nonexistent_theme_settings(tmp_path):
# Check that error occurs with a non-existent theme.toml or theme.conf
# (https://github.com/sphinx-doc/sphinx/issues/11668)
with pytest.raises(ThemeError):
_load_theme('', str(tmp_path))
@pytest.mark.sphinx(testroot='double-inheriting-theme')
def test_double_inheriting_theme(app, status, warning):
assert app.builder.theme.name == 'base_theme2'
app.build() # => not raises TemplateNotFound
@pytest.mark.sphinx(testroot='theming', confoverrides={'html_theme': 'child'})
def test_nested_zipped_theme(app, status, warning):
assert app.builder.theme.name == 'child'
app.build() # => not raises TemplateNotFound
@pytest.mark.sphinx(testroot='theming', confoverrides={'html_theme': 'staticfiles'})
def test_staticfiles(app, status, warning):
app.build()
assert (app.outdir / '_static' / 'staticimg.png').exists()
assert (app.outdir / '_static' / 'statictmpl.html').exists()
assert (app.outdir / '_static' / 'statictmpl.html').read_text(encoding='utf8') == (
'\n