/usr/lib/python2.7/dist-packages/sphinx_gallery/tests/test_gen_gallery.py is in python-sphinx-gallery 0.1.13-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | # -*- coding: utf-8 -*-
# Author: Óscar Nájera
# License: 3-clause BSD
r"""
Test Sphinx-Gallery
"""
from __future__ import (division, absolute_import, print_function,
unicode_literals)
import codecs
import os
import re
import shutil
import pytest
from sphinx.application import Sphinx
from sphinx.errors import ExtensionError
from sphinx_gallery.gen_rst import MixedEncodingStringIO
from sphinx_gallery import sphinx_compatibility
from sphinx_gallery.utils import _TempDir
@pytest.fixture
def conf_file(request):
env = request.node.get_marker('conf_file')
kwargs = env.kwargs if env else {}
result = {
'content': "",
}
result.update(kwargs)
return result
@pytest.fixture
def tempdir():
"""
temporary directory that wrapped with `path` class.
this fixture is for compat with old test implementation.
"""
return _TempDir()
@pytest.fixture
def config_app(tempdir, conf_file):
_fixturedir = os.path.join(os.path.dirname(__file__), 'testconfs')
srcdir = os.path.join(tempdir, "config_test")
shutil.copytree(_fixturedir, srcdir)
shutil.copytree(os.path.join(_fixturedir, "src"),
os.path.join(tempdir, "examples"))
with open(os.path.join(srcdir, "conf.py"), "w") as conffile:
conffile.write(conf_file['content'])
app = Sphinx(srcdir, srcdir, os.path.join(srcdir, "_build"),
os.path.join(srcdir, "_build", "toctree"),
"html", warning=MixedEncodingStringIO())
sphinx_compatibility._app = app
return app
@pytest.mark.conf_file(content="""
import os
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
# General information about the project.
project = u'Sphinx-Gallery <Tests>'""")
def test_default_config(config_app):
"""Test the default Sphinx-Gallery configuration is loaded
if only the extension is added to Sphinx"""
cfg = config_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
# no duplicate values allowed The config is present already
with pytest.raises(ExtensionError) as excinfo:
config_app.add_config_value('sphinx_gallery_conf', 'x', True)
assert 'already present' in str(excinfo.value)
@pytest.mark.conf_file(content="""
import os
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
# General information about the project.
project = u'Sphinx-Gallery <Tests>'
sphinx_gallery_conf = {
'mod_example_dir' : os.path.join('modules', 'gen'),
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_config_old_backreferences_conf(config_app):
"""Testing Deprecation warning message against old backreference config
In this case the user is required to update the mod_example_dir config
variable Sphinx-Gallery should notify the user and also silently update
the old config to the new one. """
cfg = config_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
assert cfg.sphinx_gallery_conf['backreferences_dir'] == os.path.join(
'modules', 'gen')
build_warn = config_app._warning.getvalue()
assert "WARNING:" in build_warn
assert "Old configuration" in build_warn
assert "mod_example_dir" in build_warn
assert "Gallery now requires" in build_warn
assert "For a quick fix" in build_warn
assert "'backreferences_dir': False" not in build_warn
@pytest.mark.conf_file(content="""
import os
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
# General information about the project.
project = u'Sphinx-Gallery <Tests>'
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_config_unset_backreferences(config_app):
"""Testing Deprecation warning message against unset backreference config
In this case the user is notified to update the set the
backreferences_dir config variable if such feature is to be enabled or
otherwise to deactivate the feature. Sphinx-Gallery should notify the
user and also silently setup the old default config value into the new
config style. """
cfg = config_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
assert cfg.sphinx_gallery_conf['backreferences_dir'] == os.path.join(
'modules', 'generated')
build_warn = config_app._warning.getvalue()
assert "Gallery now requires" in build_warn
assert "'backreferences_dir': False" in build_warn
assert "WARNING:" in build_warn
assert "mod_example_dir" not in build_warn
@pytest.mark.conf_file(content="""
import os
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
# General information about the project.
project = u'Sphinx-Gallery <Tests>'
sphinx_gallery_conf = {
'backreferences_dir': os.path.join('gen_modules', 'backreferences'),
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_config_backreferences(config_app):
"""Test no warning is issued under the new configuration"""
cfg = config_app.config
assert cfg.project == "Sphinx-Gallery <Tests>"
assert cfg.sphinx_gallery_conf['backreferences_dir'] == os.path.join(
'gen_modules', 'backreferences')
build_warn = config_app._warning.getvalue()
assert build_warn == ""
def _check_order(config_app, key):
index_fname = os.path.join(config_app.outdir, '..', 'ex', 'index.rst')
order = list()
regex = '.*:%s=(.):.*' % key
with codecs.open(index_fname, 'r', 'utf-8') as fid:
for line in fid:
if 'sphx-glr-thumbcontainer' in line:
order.append(int(re.match(regex, line).group(1)))
assert len(order) == 3
assert order == [1, 2, 3]
@pytest.mark.conf_file(content="""
import sphinx_gallery
extensions = ['sphinx_gallery.gen_gallery']
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
}""")
def test_example_sorting_default(config_app):
"""Test sorting of examples by default key (number of code lines)."""
_check_order(config_app, 'lines')
@pytest.mark.conf_file(content="""
import sphinx_gallery
from sphinx_gallery.sorting import FileSizeSortKey
extensions = ['sphinx_gallery.gen_gallery']
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': FileSizeSortKey,
}""")
def test_example_sorting_filesize(config_app):
"""Test sorting of examples by filesize."""
_check_order(config_app, 'filesize')
@pytest.mark.conf_file(content="""
import sphinx_gallery
from sphinx_gallery.sorting import FileNameSortKey
extensions = ['sphinx_gallery.gen_gallery']
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': FileNameSortKey,
}""")
def test_example_sorting_filename(config_app):
"""Test sorting of examples by filename."""
_check_order(config_app, 'filename')
@pytest.mark.conf_file(content="""
import sphinx_gallery
from sphinx_gallery.sorting import ExampleTitleSortKey
extensions = ['sphinx_gallery.gen_gallery']
sphinx_gallery_conf = {
'examples_dirs': 'src',
'gallery_dirs': 'ex',
'within_subsection_order': ExampleTitleSortKey,
}""")
def test_example_sorting_title(config_app):
"""Test sorting of examples by title."""
_check_order(config_app, 'title')
|