1"""
2    test_project
3    ~~~~~~~~~~~~
4
5    Tests project module.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11from collections import OrderedDict
12
13import pytest
14
15from sphinx.project import Project
16
17
18def test_project_discover(rootdir):
19    project = Project(rootdir / 'test-root', {})
20
21    docnames = {'autodoc', 'bom', 'extapi', 'extensions', 'footnote', 'images',
22                'includes', 'index', 'lists', 'markup', 'math', 'objects',
23                'subdir/excluded', 'subdir/images', 'subdir/includes'}
24    subdir_docnames = {'subdir/excluded', 'subdir/images', 'subdir/includes'}
25
26    # basic case
27    project.source_suffix = ['.txt']
28    assert project.discover() == docnames
29
30    # exclude_paths option
31    assert project.discover(['subdir/*']) == docnames - subdir_docnames
32
33    # exclude_patterns
34    assert project.discover(['.txt', 'subdir/*']) == docnames - subdir_docnames
35
36    # multiple source_suffixes
37    project.source_suffix = ['.txt', '.foo']
38    assert project.discover() == docnames | {'otherext'}
39
40    # complicated source_suffix
41    project.source_suffix = ['.foo.png']
42    assert project.discover() == {'img'}
43
44    # templates_path
45    project.source_suffix = ['.html']
46    assert project.discover() == {'_templates/layout',
47                                  '_templates/customsb',
48                                  '_templates/contentssb'}
49
50    assert project.discover(['_templates']) == set()
51
52
53@pytest.mark.sphinx(testroot='basic')
54def test_project_path2doc(app):
55    project = Project(app.srcdir, app.config.source_suffix)
56    assert project.path2doc('index.rst') == 'index'
57    assert project.path2doc('index.foo') is None  # unknown extension
58    assert project.path2doc('index.foo.rst') == 'index.foo'
59    assert project.path2doc('index') is None
60    assert project.path2doc('path/to/index.rst') == 'path/to/index'
61    assert project.path2doc(app.srcdir / 'to/index.rst') == 'to/index'
62
63
64@pytest.mark.sphinx(srcdir='project_doc2path', testroot='basic')
65def test_project_doc2path(app):
66    source_suffix = OrderedDict([('.rst', 'restructuredtext'),
67                                 ('.txt', 'restructuredtext')])
68
69    project = Project(app.srcdir, source_suffix)
70    assert project.doc2path('index') == (app.srcdir / 'index.rst')
71
72    # first source_suffix is used for missing file
73    assert project.doc2path('foo') == (app.srcdir / 'foo.rst')
74
75    # matched source_suffix is used if exists
76    (app.srcdir / 'foo.txt').write_text('')
77    assert project.doc2path('foo') == (app.srcdir / 'foo.txt')
78
79    # absolute path
80    assert project.doc2path('index', basedir=True) == (app.srcdir / 'index.rst')
81
82    # relative path
83    assert project.doc2path('index', basedir=False) == 'index.rst'
84