Files
sphinx/tests/test_environment.py
T

137 lines
4.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
test_env
~~~~~~~~
Test the BuildEnvironment class.
2016-01-14 22:54:04 +01:00
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
2014-09-21 17:17:02 +02:00
2014-04-30 21:30:46 +09:00
from six import PY3
2014-09-21 17:17:02 +02:00
from util import TestApp, remove_unicode_literals, path
2008-12-23 20:04:45 +01:00
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.builders.latex import LaTeXBuilder
app = env = None
warnings = []
2014-09-21 17:17:02 +02:00
def setup_module():
global app, env
app = TestApp(srcdir='root-envtest')
env = app.env
2016-04-22 18:40:41 +09:00
env.set_warnfunc(lambda *args, **kwargs: warnings.append(args))
2014-09-21 17:17:02 +02:00
def teardown_module():
app.cleanup()
2014-09-21 17:17:02 +02:00
def warning_emitted(file, text):
for warning in warnings:
2010-11-12 08:34:47 +01:00
if len(warning) == 2 and file in warning[1] and text in warning[0]:
return True
return False
2014-09-21 17:17:02 +02:00
# Tests are run in the order they appear in the file, therefore we can
# afford to not run update() in the setup but in its own test
def test_first_update():
2014-09-22 15:08:15 +02:00
updated = env.update(app.config, app.srcdir, app.doctreedir, app)
assert set(updated) == env.found_docs == set(env.all_docs)
# test if exclude_patterns works ok
assert 'subdir/excluded' not in env.found_docs
2014-09-21 17:17:02 +02:00
def test_images():
2010-11-12 08:34:47 +01:00
assert warning_emitted('images', 'image file not readable: foo.png')
assert warning_emitted('images', 'nonlocal image URI found: '
'http://www.python.org/logo.png')
tree = env.get_doctree('images')
app._warning.reset()
htmlbuilder = StandaloneHTMLBuilder(app)
htmlbuilder.imgpath = 'dummy'
htmlbuilder.post_process_images(tree)
assert set(htmlbuilder.images.keys()) == \
set(['subdir/img.png', 'img.png', 'subdir/simg.png', 'svgimg.svg',
'img.foo.png'])
assert set(htmlbuilder.images.values()) == \
set(['img.png', 'img1.png', 'simg.png', 'svgimg.svg', 'img.foo.png'])
app._warning.reset()
latexbuilder = LaTeXBuilder(app)
latexbuilder.post_process_images(tree)
assert set(latexbuilder.images.keys()) == \
set(['subdir/img.png', 'subdir/simg.png', 'img.png', 'img.pdf',
'svgimg.pdf', 'img.foo.png'])
assert set(latexbuilder.images.values()) == \
set(['img.pdf', 'img.png', 'img1.png', 'simg.png',
'svgimg.pdf', 'img.foo.png'])
2014-09-21 17:17:02 +02:00
def test_second_update():
# delete, add and "edit" (change saved mtime) some files and update again
env.all_docs['contents'] = 0
root = path(app.srcdir)
2009-02-22 15:22:23 +01:00
# important: using "autodoc" because it is the last one to be included in
# the contents.txt toctree; otherwise section numbers would shift
(root / 'autodoc.txt').unlink()
(root / 'new.txt').write_text('New file\n========\n')
2014-09-22 15:08:15 +02:00
updated = env.update(app.config, app.srcdir, app.doctreedir, app)
2009-02-22 15:22:23 +01:00
# "includes" and "images" are in there because they contain references
# to nonexisting downloadable or image files, which are given another
# chance to exist
2014-09-22 15:08:15 +02:00
assert set(updated) == set(['contents', 'new', 'includes', 'images'])
2009-02-22 15:22:23 +01:00
assert 'autodoc' not in env.all_docs
assert 'autodoc' not in env.found_docs
2008-10-18 17:57:35 +00:00
def test_env_read_docs():
"""By default, docnames are read in alphanumeric order"""
def on_env_read_docs_1(app, env, docnames):
pass
app.connect('env-before-read-docs', on_env_read_docs_1)
2014-09-22 15:08:15 +02:00
read_docnames = env.update(app.config, app.srcdir, app.doctreedir, app)
assert len(read_docnames) > 2 and read_docnames == sorted(read_docnames)
def on_env_read_docs_2(app, env, docnames):
2015-01-25 16:25:32 +01:00
docnames.remove('images')
app.connect('env-before-read-docs', on_env_read_docs_2)
2014-09-22 15:08:15 +02:00
read_docnames = env.update(app.config, app.srcdir, app.doctreedir, app)
2015-01-25 16:25:32 +01:00
assert len(read_docnames) == 2
2008-10-18 17:57:35 +00:00
def test_object_inventory():
2009-07-13 21:31:05 +02:00
refs = env.domaindata['py']['objects']
2008-10-18 17:57:35 +00:00
assert 'func_without_module' in refs
assert refs['func_without_module'] == ('objects', 'function')
2008-10-18 17:57:35 +00:00
assert 'func_without_module2' in refs
assert 'mod.func_in_module' in refs
assert 'mod.Cls' in refs
assert 'mod.Cls.meth1' in refs
assert 'mod.Cls.meth2' in refs
assert 'mod.Cls.meths' in refs
assert 'mod.Error' not in refs
assert 'errmod.Error' in refs
assert 'func_in_module' not in refs
assert 'func_noindex' not in refs
2009-07-13 21:31:05 +02:00
assert env.domaindata['py']['modules']['mod'] == \
('objects', 'Module synopsis.', 'UNIX', False)
2009-07-13 21:31:05 +02:00
assert env.domains['py'].data is env.domaindata['py']
assert env.domains['c'].data is env.domaindata['c']