| 1 |
"""Tests project module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from sphinx.project import Project |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from sphinx.testing.util import SphinxTestApp |
| 14 |
|
| 15 |
DOCNAMES = { |
| 16 |
'autodoc', |
| 17 |
'bom', |
| 18 |
'extapi', |
| 19 |
'extensions', |
| 20 |
'footnote', |
| 21 |
'images', |
| 22 |
'includes', |
| 23 |
'index', |
| 24 |
'lists', |
| 25 |
'markup', |
| 26 |
'math', |
| 27 |
'objects', |
| 28 |
'subdir/excluded', |
| 29 |
'subdir/images', |
| 30 |
'subdir/includes', |
| 31 |
} |
| 32 |
SUBDIR_DOCNAMES = {'subdir/excluded', 'subdir/images', 'subdir/includes'} |
| 33 |
|
| 34 |
|
| 35 |
def test_project_discover_basic(rootdir: Path) -> None: |
| 36 |
# basic case |
| 37 |
project = Project(rootdir / 'test-root', ['.txt']) |
| 38 |
assert project.discover() == DOCNAMES |
| 39 |
|
| 40 |
|
| 41 |
def test_project_discover_exclude_patterns(rootdir: Path) -> None: |
| 42 |
project = Project(rootdir / 'test-root', ['.txt']) |
| 43 |
|
| 44 |
# exclude_paths option |
| 45 |
assert project.discover(['subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES |
| 46 |
assert project.discover(['.txt', 'subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES |
| 47 |
|
| 48 |
|
| 49 |
def test_project_discover_multiple_suffixes(rootdir: Path) -> None: |
| 50 |
# multiple source_suffixes |
| 51 |
project = Project(rootdir / 'test-root', ['.txt', '.foo']) |
| 52 |
assert project.discover() == DOCNAMES | {'otherext'} |
| 53 |
|
| 54 |
|
| 55 |
def test_project_discover_complicated_suffix(rootdir: Path) -> None: |
| 56 |
# complicated source_suffix |
| 57 |
project = Project(rootdir / 'test-root', ['.foo.png']) |
| 58 |
assert project.discover() == {'img'} |
| 59 |
|
| 60 |
|
| 61 |
def test_project_discover_templates_path(rootdir: Path) -> None: |
| 62 |
# templates_path |
| 63 |
project = Project(rootdir / 'test-root', ['.html']) |
| 64 |
assert project.discover() == { |
| 65 |
'_templates/layout', |
| 66 |
'_templates/customsb', |
| 67 |
'_templates/contentssb', |
| 68 |
} |
| 69 |
|
| 70 |
assert project.discover(['_templates']) == set() |
| 71 |
|
| 72 |
|
| 73 |
def test_project_path2doc(rootdir: Path) -> None: |
| 74 |
project = Project(rootdir / 'test-basic', {'.rst': 'restructuredtext'}) |
| 75 |
assert project.path2doc('index.rst') == 'index' |
| 76 |
assert project.path2doc('index.foo') is None # unknown extension |
| 77 |
assert project.path2doc('index.foo.rst') == 'index.foo' |
| 78 |
assert project.path2doc('index') is None |
| 79 |
assert project.path2doc('path/to/index.rst') == 'path/to/index' |
| 80 |
assert project.path2doc(rootdir / 'test-basic' / 'to/index.rst') == 'to/index' |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.sphinx( |
| 84 |
'html', |
| 85 |
testroot='basic', |
| 86 |
srcdir='project_doc2path', |
| 87 |
) |
| 88 |
def test_project_doc2path(app: SphinxTestApp) -> None: |
| 89 |
source_suffix = {'.rst': 'restructuredtext', '.txt': 'restructuredtext'} |
| 90 |
|
| 91 |
project = Project(app.srcdir, source_suffix) |
| 92 |
project.discover() |
| 93 |
|
| 94 |
# absolute path |
| 95 |
assert project.doc2path('index', absolute=True) == app.srcdir / 'index.rst' |
| 96 |
|
| 97 |
# relative path |
| 98 |
assert project.doc2path('index', absolute=False) == Path('index.rst') |
| 99 |
|
| 100 |
# first source_suffix is used for missing file |
| 101 |
assert project.doc2path('foo', absolute=False) == Path('foo.rst') |
| 102 |
|
| 103 |
# matched source_suffix is used if exists |
| 104 |
(app.srcdir / 'bar.txt').touch() |
| 105 |
project.discover() |
| 106 |
assert project.doc2path('bar', absolute=False) == Path('bar.txt') |