Cache the docname<->filename map in `Project` instances (#11575)

This commit is contained in:
Adam Turner
2023-08-10 12:25:38 +01:00
committed by GitHub
parent 4dd2ed4bde
commit 8cabf08668
4 changed files with 88 additions and 67 deletions

View File

@@ -4,35 +4,41 @@ import pytest
from sphinx.project import Project
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'}
def test_project_discover(rootdir):
project = Project(str(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'}
def test_project_discover_basic(rootdir):
# basic case
project.source_suffix = ['.txt']
assert project.discover() == docnames
project = Project(rootdir / 'test-root', ['.txt'])
assert project.discover() == DOCNAMES
def test_project_discover_exclude_patterns(rootdir):
project = Project(rootdir / 'test-root', ['.txt'])
# exclude_paths option
assert project.discover(['subdir/*']) == docnames - subdir_docnames
assert project.discover(['subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES
assert project.discover(['.txt', 'subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES
# exclude_patterns
assert project.discover(['.txt', 'subdir/*']) == docnames - subdir_docnames
def test_project_discover_multiple_suffixes(rootdir):
# multiple source_suffixes
project.source_suffix = ['.txt', '.foo']
assert project.discover() == docnames | {'otherext'}
project = Project(rootdir / 'test-root', ['.txt', '.foo'])
assert project.discover() == DOCNAMES | {'otherext'}
def test_project_discover_complicated_suffix(rootdir):
# complicated source_suffix
project.source_suffix = ['.foo.png']
project = Project(rootdir / 'test-root', ['.foo.png'])
assert project.discover() == {'img'}
def test_project_discover_templates_path(rootdir):
# templates_path
project.source_suffix = ['.html']
project = Project(rootdir / 'test-root', ['.html'])
assert project.discover() == {'_templates/layout',
'_templates/customsb',
'_templates/contentssb'}
@@ -40,15 +46,14 @@ def test_project_discover(rootdir):
assert project.discover(['_templates']) == set()
@pytest.mark.sphinx(testroot='basic')
def test_project_path2doc(app):
project = Project(app.srcdir, app.config.source_suffix)
def test_project_path2doc(rootdir):
project = Project(rootdir / 'test-basic', {'.rst': 'restructuredtext'})
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
assert project.path2doc('path/to/index.rst') == 'path/to/index'
assert project.path2doc(str(app.srcdir / 'to/index.rst')) == 'to/index'
assert project.path2doc(rootdir / 'test-basic' / 'to/index.rst') == 'to/index'
@pytest.mark.sphinx(srcdir='project_doc2path', testroot='basic')
@@ -56,17 +61,18 @@ def test_project_doc2path(app):
source_suffix = {'.rst': 'restructuredtext', '.txt': 'restructuredtext'}
project = Project(app.srcdir, source_suffix)
assert project.doc2path('index') == str(app.srcdir / 'index.rst')
# first source_suffix is used for missing file
assert project.doc2path('foo') == str(app.srcdir / 'foo.rst')
# matched source_suffix is used if exists
(app.srcdir / 'foo.txt').write_text('', encoding='utf8')
assert project.doc2path('foo') == str(app.srcdir / 'foo.txt')
project.discover()
# absolute path
assert project.doc2path('index', basedir=True) == str(app.srcdir / 'index.rst')
assert project.doc2path('index', absolute=True) == str(app.srcdir / 'index.rst')
# relative path
assert project.doc2path('index', basedir=False) == 'index.rst'
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'