2009-02-15 09:10:10 -06:00
|
|
|
"""
|
|
|
|
test_theming
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Test the Theme class.
|
|
|
|
|
2020-12-31 11:00:29 -06:00
|
|
|
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
|
2009-02-15 09:10:10 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2018-06-19 07:58:06 -05:00
|
|
|
import alabaster
|
2020-03-14 13:46:37 -05:00
|
|
|
import pytest
|
2020-11-11 05:00:27 -06:00
|
|
|
|
2017-04-20 06:19:55 -05:00
|
|
|
from sphinx.theming import ThemeError
|
2015-03-14 10:47:43 -05:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx(
|
2017-12-13 08:48:07 -06:00
|
|
|
testroot='theming',
|
2017-01-05 10:14:47 -06:00
|
|
|
confoverrides={'html_theme': 'ziptheme',
|
|
|
|
'html_theme_options.testopt': 'foo'})
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_theme_api(app, status, warning):
|
2009-02-15 09:10:10 -06:00
|
|
|
cfg = app.config
|
|
|
|
|
2018-06-19 07:58:06 -05:00
|
|
|
themes = ['basic', 'default', 'scrolls', 'agogo', 'sphinxdoc', 'haiku',
|
|
|
|
'traditional', 'epub', 'nature', 'pyramid', 'bizstyle', 'classic', 'nonav',
|
|
|
|
'test-theme', 'ziptheme', 'staticfiles', 'parent', 'child']
|
|
|
|
if alabaster.version.__version_info__ >= (0, 7, 11):
|
|
|
|
themes.append('alabaster')
|
|
|
|
|
2009-02-15 09:10:10 -06:00
|
|
|
# test Theme class API
|
2018-06-19 07:58:06 -05:00
|
|
|
assert set(app.html_themes.keys()) == set(themes)
|
2017-12-13 08:48:07 -06:00
|
|
|
assert app.html_themes['test-theme'] == app.srcdir / 'test_theme' / 'test-theme'
|
2017-04-20 06:19:55 -05:00
|
|
|
assert app.html_themes['ziptheme'] == app.srcdir / 'ziptheme.zip'
|
2017-12-13 08:48:07 -06:00
|
|
|
assert app.html_themes['staticfiles'] == app.srcdir / 'test_theme' / 'staticfiles'
|
2009-02-15 09:10:10 -06:00
|
|
|
|
|
|
|
# test Theme instance API
|
|
|
|
theme = app.builder.theme
|
|
|
|
assert theme.name == 'ziptheme'
|
|
|
|
themedir = theme.themedir
|
|
|
|
assert theme.base.name == 'basic'
|
2017-04-20 09:38:48 -05:00
|
|
|
assert len(theme.get_theme_dirs()) == 2
|
2009-02-15 09:10:10 -06:00
|
|
|
|
|
|
|
# direct setting
|
2017-04-20 09:38:48 -05:00
|
|
|
assert theme.get_config('theme', 'stylesheet') == 'custom.css'
|
2009-02-15 09:10:10 -06:00
|
|
|
# inherited setting
|
2017-04-20 09:38:48 -05:00
|
|
|
assert theme.get_config('options', 'nosidebar') == 'false'
|
2009-02-15 09:10:10 -06:00
|
|
|
# nonexisting setting
|
2017-04-20 09:38:48 -05:00
|
|
|
assert theme.get_config('theme', 'foobar', 'def') == 'def'
|
2017-01-05 09:46:42 -06:00
|
|
|
with pytest.raises(ThemeError):
|
2017-04-20 09:38:48 -05:00
|
|
|
theme.get_config('theme', 'foobar')
|
2009-02-15 09:10:10 -06:00
|
|
|
|
|
|
|
# options API
|
2017-06-26 02:17:08 -05:00
|
|
|
|
|
|
|
options = theme.get_options({'nonexisting': 'foo'})
|
|
|
|
assert 'nonexisting' not in options.keys()
|
|
|
|
|
2009-02-15 09:10:10 -06:00
|
|
|
options = theme.get_options(cfg.html_theme_options)
|
|
|
|
assert options['testopt'] == 'foo'
|
|
|
|
assert options['nosidebar'] == 'false'
|
|
|
|
|
|
|
|
# cleanup temp directories
|
|
|
|
theme.cleanup()
|
|
|
|
assert not os.path.exists(themedir)
|
2014-06-07 04:32:56 -05:00
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx(testroot='tocdepth') # a minimal root
|
2014-09-21 10:17:02 -05:00
|
|
|
def test_js_source(app, status, warning):
|
2014-06-07 04:32:56 -05:00
|
|
|
# Now sphinx provides non-minified JS files for jquery.js and underscore.js
|
|
|
|
# to clarify the source of the minified files. see also #1434.
|
|
|
|
# If you update the version of the JS file, please update the source of the
|
|
|
|
# JS file and version number in this test.
|
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
app.builder.build(['contents'])
|
2014-06-07 04:32:56 -05:00
|
|
|
|
2020-05-21 09:50:20 -05:00
|
|
|
v = '3.5.1'
|
2014-06-07 04:32:56 -05:00
|
|
|
msg = 'jquery.js version does not match to {v}'.format(v=v)
|
2020-01-31 20:58:51 -06:00
|
|
|
jquery_min = (app.outdir / '_static' / 'jquery.js').read_text()
|
2014-06-07 04:32:56 -05:00
|
|
|
assert 'jQuery v{v}'.format(v=v) in jquery_min, msg
|
2020-01-31 20:58:51 -06:00
|
|
|
jquery_src = (app.outdir / '_static' / 'jquery-{v}.js'.format(v=v)).read_text()
|
2014-06-07 04:32:56 -05:00
|
|
|
assert 'jQuery JavaScript Library v{v}'.format(v=v) in jquery_src, msg
|
|
|
|
|
2021-01-23 01:26:10 -06:00
|
|
|
v = '1.12.0'
|
2014-06-07 04:32:56 -05:00
|
|
|
msg = 'underscore.js version does not match to {v}'.format(v=v)
|
2020-01-31 20:58:51 -06:00
|
|
|
underscore_min = (app.outdir / '_static' / 'underscore.js').read_text()
|
2014-06-07 04:32:56 -05:00
|
|
|
assert 'Underscore.js {v}'.format(v=v) in underscore_min, msg
|
2020-01-31 20:58:51 -06:00
|
|
|
underscore_src = (app.outdir / '_static' / 'underscore-{v}.js'.format(v=v)).read_text()
|
2014-06-07 04:32:56 -05:00
|
|
|
assert 'Underscore.js {v}'.format(v=v) in underscore_src, msg
|
2015-11-21 09:13:42 -06:00
|
|
|
|
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
@pytest.mark.sphinx(testroot='double-inheriting-theme')
|
2017-04-20 00:04:49 -05:00
|
|
|
def test_double_inheriting_theme(app, status, warning):
|
|
|
|
assert app.builder.theme.name == 'base_theme2'
|
|
|
|
app.build() # => not raises TemplateNotFound
|
2017-04-18 09:22:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(testroot='theming',
|
|
|
|
confoverrides={'html_theme': 'child'})
|
|
|
|
def test_nested_zipped_theme(app, status, warning):
|
2017-04-20 00:04:49 -05:00
|
|
|
assert app.builder.theme.name == 'child'
|
2017-04-18 09:22:03 -05:00
|
|
|
app.build() # => not raises TemplateNotFound
|
2017-10-22 01:12:31 -05:00
|
|
|
|
|
|
|
|
2017-12-13 08:48:07 -06:00
|
|
|
@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()
|
2020-01-31 20:58:51 -06:00
|
|
|
assert (app.outdir / '_static' / 'statictmpl.html').read_text() == (
|
2017-12-13 08:48:07 -06:00
|
|
|
'<!-- testing static templates -->\n'
|
|
|
|
'<html><project>Python</project></html>'
|
|
|
|
)
|
|
|
|
|
2020-01-31 20:58:51 -06:00
|
|
|
result = (app.outdir / 'index.html').read_text()
|
2017-12-13 08:48:07 -06:00
|
|
|
assert '<meta name="testopt" content="optdefault" />' in result
|
2017-12-15 09:53:55 -06:00
|
|
|
|
|
|
|
|
2020-03-14 12:46:02 -05:00
|
|
|
@pytest.mark.sphinx(testroot='theming',
|
|
|
|
confoverrides={'html_theme': 'test-theme'})
|
2020-03-14 13:46:37 -05:00
|
|
|
def test_dark_style(app, status, warning):
|
2020-03-14 12:49:25 -05:00
|
|
|
style = app.builder.dark_highlighter.formatter_args.get('style')
|
2020-03-14 12:46:02 -05:00
|
|
|
assert style.__name__ == 'MonokaiStyle'
|
|
|
|
|
|
|
|
app.build()
|
|
|
|
assert (app.outdir / '_static' / 'pygments_dark.css').exists()
|
|
|
|
|
|
|
|
result = (app.outdir / 'index.html').read_text()
|
2021-01-02 11:04:36 -06:00
|
|
|
assert '<link rel="stylesheet" type="text/css" href="_static/pygments.css" />' in result
|
2020-03-14 13:46:37 -05:00
|
|
|
assert ('<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" '
|
|
|
|
'rel="stylesheet" type="text/css" '
|
|
|
|
'href="_static/pygments_dark.css" />') in result
|
2020-03-14 12:46:02 -05:00
|
|
|
|
|
|
|
|
2017-10-22 01:12:31 -05:00
|
|
|
@pytest.mark.sphinx(testroot='theming')
|
|
|
|
def test_theme_sidebars(app, status, warning):
|
|
|
|
app.build()
|
|
|
|
|
|
|
|
# test-theme specifies globaltoc and searchbox as default sidebars
|
2020-01-31 21:25:49 -06:00
|
|
|
result = (app.outdir / 'index.html').read_text()
|
2018-07-21 16:13:16 -05:00
|
|
|
assert '<h3><a href="#">Table of Contents</a></h3>' in result
|
2017-10-22 01:12:31 -05:00
|
|
|
assert '<h3>Related Topics</h3>' not in result
|
|
|
|
assert '<h3>This Page</h3>' not in result
|
2019-04-22 05:55:32 -05:00
|
|
|
assert '<h3 id="searchlabel">Quick search</h3>' in result
|