2018-01-18 03:23:57 -06:00
|
|
|
"""
|
|
|
|
test_project
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Tests project module.
|
|
|
|
|
2019-12-31 23:27:43 -06:00
|
|
|
:copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
|
2018-01-18 03:23:57 -06:00
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
"""
|
|
|
|
|
2018-01-18 07:20:48 -06:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-01-18 03:23:57 -06:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sphinx.project import Project
|
2019-01-13 03:40:35 -06:00
|
|
|
from sphinx.testing.comparer import PathComparer
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
|
2018-09-23 19:15:07 -05:00
|
|
|
def test_project_discover(rootdir):
|
2018-01-18 03:23:57 -06:00
|
|
|
project = Project(rootdir / 'test-root', {})
|
|
|
|
|
|
|
|
docnames = {'autodoc', 'bom', 'extapi', 'extensions', 'footnote', 'images',
|
|
|
|
'includes', 'index', 'lists', 'markup', 'math', 'objects',
|
|
|
|
'subdir/excluded', 'subdir/images', 'subdir/includes'}
|
|
|
|
subdir_docnames = {'subdir/excluded', 'subdir/images', 'subdir/includes'}
|
|
|
|
|
|
|
|
# basic case
|
|
|
|
project.source_suffix = ['.txt']
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover() == docnames
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# exclude_paths option
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover(['subdir/*']) == docnames - subdir_docnames
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# exclude_patterns
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover(['.txt', 'subdir/*']) == docnames - subdir_docnames
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# multiple source_suffixes
|
|
|
|
project.source_suffix = ['.txt', '.foo']
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover() == docnames | {'otherext'}
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# complicated source_suffix
|
|
|
|
project.source_suffix = ['.foo.png']
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover() == {'img'}
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# templates_path
|
|
|
|
project.source_suffix = ['.html']
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover() == {'_templates/layout',
|
|
|
|
'_templates/customsb',
|
|
|
|
'_templates/contentssb'}
|
2018-01-18 03:23:57 -06:00
|
|
|
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover(['_templates']) == set()
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(testroot='basic')
|
|
|
|
def test_project_path2doc(app):
|
|
|
|
project = Project(app.srcdir, app.config.source_suffix)
|
|
|
|
assert project.path2doc('index.rst') == 'index'
|
|
|
|
assert project.path2doc('index.foo') is None # unknown extension
|
|
|
|
assert project.path2doc('index.foo.rst') == 'index.foo'
|
|
|
|
assert project.path2doc('index') is None
|
2019-01-13 03:40:35 -06:00
|
|
|
assert project.path2doc('/path/to/index.rst') == PathComparer('/path/to/index')
|
|
|
|
assert project.path2doc(app.srcdir / '/to/index.rst') == PathComparer('/to/index')
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(srcdir='project_doc2path', testroot='basic')
|
|
|
|
def test_project_doc2path(app):
|
|
|
|
source_suffix = OrderedDict([('.rst', 'restructuredtext'),
|
|
|
|
('.txt', 'restructuredtext')])
|
|
|
|
|
|
|
|
project = Project(app.srcdir, source_suffix)
|
|
|
|
assert project.doc2path('index') == (app.srcdir / 'index.rst')
|
|
|
|
|
|
|
|
# first source_suffix is used for missing file
|
|
|
|
assert project.doc2path('foo') == (app.srcdir / 'foo.rst')
|
|
|
|
|
|
|
|
# matched source_suffix is used if exists
|
|
|
|
(app.srcdir / 'foo.txt').write_text('')
|
|
|
|
assert project.doc2path('foo') == (app.srcdir / 'foo.txt')
|
|
|
|
|
|
|
|
# absolute path
|
|
|
|
assert project.doc2path('index', basedir=True) == (app.srcdir / 'index.rst')
|
|
|
|
|
|
|
|
# relative path
|
|
|
|
assert project.doc2path('index', basedir=False) == 'index.rst'
|