2018-01-18 03:23:57 -06:00
|
|
|
"""Tests project module."""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from sphinx.project import Project
|
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
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'}
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
def test_project_discover_basic(rootdir):
|
2018-01-18 03:23:57 -06:00
|
|
|
# basic case
|
2023-08-10 06:25:38 -05:00
|
|
|
project = Project(rootdir / 'test-root', ['.txt'])
|
|
|
|
assert project.discover() == DOCNAMES
|
|
|
|
|
|
|
|
|
|
|
|
def test_project_discover_exclude_patterns(rootdir):
|
|
|
|
project = Project(rootdir / 'test-root', ['.txt'])
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
# exclude_paths option
|
2023-08-10 06:25:38 -05:00
|
|
|
assert project.discover(['subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES
|
|
|
|
assert project.discover(['.txt', 'subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES
|
2018-01-18 03:23:57 -06:00
|
|
|
|
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
def test_project_discover_multiple_suffixes(rootdir):
|
2018-01-18 03:23:57 -06:00
|
|
|
# multiple source_suffixes
|
2023-08-10 06:25:38 -05:00
|
|
|
project = Project(rootdir / 'test-root', ['.txt', '.foo'])
|
|
|
|
assert project.discover() == DOCNAMES | {'otherext'}
|
|
|
|
|
2018-01-18 03:23:57 -06:00
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
def test_project_discover_complicated_suffix(rootdir):
|
2018-01-18 03:23:57 -06:00
|
|
|
# complicated source_suffix
|
2023-08-10 06:25:38 -05:00
|
|
|
project = Project(rootdir / 'test-root', ['.foo.png'])
|
2018-09-23 19:15:07 -05:00
|
|
|
assert project.discover() == {'img'}
|
2018-01-18 03:23:57 -06:00
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
|
|
|
|
def test_project_discover_templates_path(rootdir):
|
2018-01-18 03:23:57 -06:00
|
|
|
# templates_path
|
2023-08-10 06:25:38 -05:00
|
|
|
project = Project(rootdir / 'test-root', ['.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
|
|
|
|
|
|
|
|
2023-08-10 06:25:38 -05:00
|
|
|
def test_project_path2doc(rootdir):
|
|
|
|
project = Project(rootdir / 'test-basic', {'.rst': 'restructuredtext'})
|
2018-01-18 07:20:48 -06:00
|
|
|
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
|
2020-05-22 12:08:09 -05:00
|
|
|
assert project.path2doc('path/to/index.rst') == 'path/to/index'
|
2023-08-10 06:25:38 -05:00
|
|
|
assert project.path2doc(rootdir / 'test-basic' / 'to/index.rst') == 'to/index'
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx(srcdir='project_doc2path', testroot='basic')
|
|
|
|
def test_project_doc2path(app):
|
2023-03-05 12:59:05 -06:00
|
|
|
source_suffix = {'.rst': 'restructuredtext', '.txt': 'restructuredtext'}
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
project = Project(app.srcdir, source_suffix)
|
2023-08-10 06:25:38 -05:00
|
|
|
project.discover()
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
# absolute path
|
2023-08-10 06:25:38 -05:00
|
|
|
assert project.doc2path('index', absolute=True) == str(app.srcdir / 'index.rst')
|
2018-01-18 07:20:48 -06:00
|
|
|
|
|
|
|
# relative path
|
2023-08-10 06:25:38 -05:00
|
|
|
assert project.doc2path('index', absolute=False) == 'index.rst'
|
|
|
|
|
|
|
|
# first source_suffix is used for missing file
|
|
|
|
assert project.doc2path('foo', absolute=False) == 'foo.rst'
|
|
|
|
|
|
|
|
# matched source_suffix is used if exists
|
|
|
|
(app.srcdir / 'bar.txt').touch()
|
|
|
|
project.discover()
|
|
|
|
assert project.doc2path('bar', absolute=False) == 'bar.txt'
|