summaryrefslogtreecommitdiffstats
path: root/tests/test_environment/test_environment.py
blob: 82de5975877741fb43b641a5be62e08aef6f501e (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""Test the BuildEnvironment class."""
import os
import shutil
from pathlib import Path

import pytest

from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.builders.latex import LaTeXBuilder
from sphinx.environment import CONFIG_CHANGED, CONFIG_EXTENSIONS_CHANGED, CONFIG_NEW, CONFIG_OK


@pytest.mark.sphinx('dummy', testroot='basic')
def test_config_status(make_app, app_params):
    args, kwargs = app_params

    # clean build
    app1 = make_app(*args, freshenv=True, **kwargs)
    assert app1.env.config_status == CONFIG_NEW
    app1.build()
    assert '[new config] 1 added' in app1._status.getvalue()

    # incremental build (no config changed)
    app2 = make_app(*args, **kwargs)
    assert app2.env.config_status == CONFIG_OK
    app2.build()
    assert "0 added, 0 changed, 0 removed" in app2._status.getvalue()

    # incremental build (config entry changed)
    app3 = make_app(*args, confoverrides={'root_doc': 'indexx'}, **kwargs)
    fname = os.path.join(app3.srcdir, 'index.rst')
    assert os.path.isfile(fname)
    shutil.move(fname, fname[:-4] + 'x.rst')
    assert app3.env.config_status == CONFIG_CHANGED
    app3.build()
    shutil.move(fname[:-4] + 'x.rst', fname)
    assert "[config changed ('master_doc')] 1 added" in app3._status.getvalue()

    # incremental build (extension changed)
    app4 = make_app(*args, confoverrides={'extensions': ['sphinx.ext.autodoc']}, **kwargs)
    assert app4.env.config_status == CONFIG_EXTENSIONS_CHANGED
    app4.build()
    want_str = "[extensions changed ('sphinx.ext.autodoc')] 1 added"
    assert want_str in app4._status.getvalue()


@pytest.mark.sphinx('dummy')
def test_images(app):
    app.build()

    tree = app.env.get_doctree('images')
    htmlbuilder = StandaloneHTMLBuilder(app, app.env)
    htmlbuilder.init()
    htmlbuilder.imgpath = 'dummy'
    htmlbuilder.post_process_images(tree)
    assert set(htmlbuilder.images.keys()) == \
        {'subdir/img.png', 'img.png', 'subdir/simg.png', 'svgimg.svg', 'img.foo.png'}
    assert set(htmlbuilder.images.values()) == \
        {'img.png', 'img1.png', 'simg.png', 'svgimg.svg', 'img.foo.png'}

    latexbuilder = LaTeXBuilder(app, app.env)
    latexbuilder.init()
    latexbuilder.post_process_images(tree)
    assert set(latexbuilder.images.keys()) == \
        {'subdir/img.png', 'subdir/simg.png', 'img.png', 'img.pdf',
         'svgimg.pdf', 'img.foo.png'}
    assert set(latexbuilder.images.values()) == \
        {'img.pdf', 'img.png', 'img1.png', 'simg.png',
         'svgimg.pdf', 'img.foo.png'}


@pytest.mark.sphinx('dummy')
def test_object_inventory(app):
    app.build()
    refs = app.env.domaindata['py']['objects']

    assert 'func_without_module' in refs
    assert refs['func_without_module'] == ('objects', 'func_without_module', 'function', False)
    assert 'func_without_module2' in refs
    assert 'mod.func_in_module' in refs
    assert 'mod.Cls' in refs
    assert 'mod.Cls.meth1' in refs
    assert 'mod.Cls.meth2' in refs
    assert 'mod.Cls.meths' in refs

    assert 'mod.Error' not in refs
    assert 'errmod.Error' in refs

    assert 'func_in_module' not in refs
    assert 'func_noindex' not in refs

    assert app.env.domaindata['py']['modules']['mod'] == \
        ('objects', 'module-mod', 'Module synopsis.', 'UNIX', False)

    assert app.env.domains['py'].data is app.env.domaindata['py']
    assert app.env.domains['c'].data is app.env.domaindata['c']


@pytest.mark.sphinx('dummy', testroot='basic')
def test_env_relfn2path(app):
    # relative filename and root document
    relfn, absfn = app.env.relfn2path('logo.jpg', 'index')
    assert relfn == 'logo.jpg'
    assert absfn == str(app.srcdir / 'logo.jpg')

    # absolute filename and root document
    relfn, absfn = app.env.relfn2path('/logo.jpg', 'index')
    assert relfn == 'logo.jpg'
    assert absfn == str(app.srcdir / 'logo.jpg')

    # relative filename and a document in subdir
    relfn, absfn = app.env.relfn2path('logo.jpg', 'subdir/index')
    assert Path(relfn) == Path('subdir/logo.jpg')
    assert absfn == str(app.srcdir / 'subdir' / 'logo.jpg')

    # absolute filename and a document in subdir
    relfn, absfn = app.env.relfn2path('/logo.jpg', 'subdir/index')
    assert relfn == 'logo.jpg'
    assert absfn == str(app.srcdir / 'logo.jpg')

    # relative filename having subdir
    relfn, absfn = app.env.relfn2path('images/logo.jpg', 'index')
    assert relfn == 'images/logo.jpg'
    assert absfn == str(app.srcdir / 'images' / 'logo.jpg')

    # relative path traversal
    relfn, absfn = app.env.relfn2path('../logo.jpg', 'index')
    assert relfn == '../logo.jpg'
    assert absfn == str(app.srcdir.parent / 'logo.jpg')

    # relative path traversal
    relfn, absfn = app.env.relfn2path('subdir/../logo.jpg', 'index')
    assert relfn == 'logo.jpg'
    assert absfn == str(app.srcdir / 'logo.jpg')

    # omit docname (w/ current docname)
    app.env.temp_data['docname'] = 'subdir/document'
    relfn, absfn = app.env.relfn2path('images/logo.jpg')
    assert Path(relfn) == Path('subdir/images/logo.jpg')
    assert absfn == str(app.srcdir / 'subdir' / 'images' / 'logo.jpg')

    # omit docname (w/o current docname)
    app.env.temp_data.clear()
    with pytest.raises(KeyError):
        app.env.relfn2path('images/logo.jpg')