File: test_project.py

package info (click to toggle)
sphinx 8.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 26,712 kB
  • sloc: python: 105,846; javascript: 6,474; perl: 451; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (106 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (4)
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
"""Tests project module."""

from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from sphinx.project import Project

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp

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 = 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(['.txt', 'subdir/*']) == DOCNAMES - SUBDIR_DOCNAMES


def test_project_discover_multiple_suffixes(rootdir):
    # multiple source_suffixes
    project = Project(rootdir / 'test-root', ['.txt', '.foo'])
    assert project.discover() == DOCNAMES | {'otherext'}


def test_project_discover_complicated_suffix(rootdir):
    # complicated source_suffix
    project = Project(rootdir / 'test-root', ['.foo.png'])
    assert project.discover() == {'img'}


def test_project_discover_templates_path(rootdir):
    # templates_path
    project = Project(rootdir / 'test-root', ['.html'])
    assert project.discover() == {
        '_templates/layout',
        '_templates/customsb',
        '_templates/contentssb',
    }

    assert project.discover(['_templates']) == set()


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(rootdir / 'test-basic' / 'to/index.rst') == 'to/index'


@pytest.mark.sphinx(
    'html',
    testroot='basic',
    srcdir='project_doc2path',
)
def test_project_doc2path(app: SphinxTestApp) -> None:
    source_suffix = {'.rst': 'restructuredtext', '.txt': 'restructuredtext'}

    project = Project(app.srcdir, source_suffix)
    project.discover()

    # absolute path
    assert project.doc2path('index', absolute=True) == app.srcdir / 'index.rst'

    # relative path
    assert project.doc2path('index', absolute=False) == Path('index.rst')

    # first source_suffix is used for missing file
    assert project.doc2path('foo', absolute=False) == Path('foo.rst')

    # matched source_suffix is used if exists
    (app.srcdir / 'bar.txt').touch()
    project.discover()
    assert project.doc2path('bar', absolute=False) == Path('bar.txt')