1"""
2    test_coverage
3    ~~~~~~~~~~~~~
4
5    Test the coverage builder.
6
7    :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8    :license: BSD, see LICENSE for details.
9"""
10
11import pickle
12
13import pytest
14
15
16@pytest.mark.sphinx('coverage')
17def test_build(app, status, warning):
18    app.builder.build_all()
19
20    py_undoc = (app.outdir / 'python.txt').read_text()
21    assert py_undoc.startswith('Undocumented Python objects\n'
22                               '===========================\n')
23    assert 'autodoc_target\n--------------\n' in py_undoc
24    assert ' * Class -- missing methods:\n' in py_undoc
25    assert ' * raises\n' in py_undoc
26    assert ' * function\n' not in py_undoc  # these two are documented
27    assert ' * Class\n' not in py_undoc     # in autodoc.txt
28
29    assert ' * mod -- No module named mod'  # in the "failed import" section
30
31    assert "undocumented  py" not in status.getvalue()
32
33    c_undoc = (app.outdir / 'c.txt').read_text()
34    assert c_undoc.startswith('Undocumented C API elements\n'
35                              '===========================\n')
36    assert 'api.h' in c_undoc
37    assert ' * Py_SphinxTest' in c_undoc
38
39    undoc_py, undoc_c = pickle.loads((app.outdir / 'undoc.pickle').read_bytes())
40    assert len(undoc_c) == 1
41    # the key is the full path to the header file, which isn't testable
42    assert list(undoc_c.values())[0] == {('function', 'Py_SphinxTest')}
43
44    assert 'autodoc_target' in undoc_py
45    assert 'funcs' in undoc_py['autodoc_target']
46    assert 'raises' in undoc_py['autodoc_target']['funcs']
47    assert 'classes' in undoc_py['autodoc_target']
48    assert 'Class' in undoc_py['autodoc_target']['classes']
49    assert 'undocmeth' in undoc_py['autodoc_target']['classes']['Class']
50
51    assert "undocumented  c" not in status.getvalue()
52
53
54@pytest.mark.sphinx('coverage', testroot='ext-coverage')
55def test_coverage_ignore_pyobjects(app, status, warning):
56    app.builder.build_all()
57    actual = (app.outdir / 'python.txt').read_text()
58    expected = '''Undocumented Python objects
59===========================
60coverage_not_ignored
61--------------------
62Classes:
63 * Documented -- missing methods:
64
65   - not_ignored1
66   - not_ignored2
67 * NotIgnored
68
69'''
70    assert actual == expected
71
72
73@pytest.mark.sphinx('coverage', confoverrides={'coverage_show_missing_items': True})
74def test_show_missing_items(app, status, warning):
75    app.builder.build_all()
76
77    assert "undocumented" in status.getvalue()
78
79    assert "py  function  raises" in status.getvalue()
80    assert "py  class     Base" in status.getvalue()
81    assert "py  method    Class.roger" in status.getvalue()
82
83    assert "c   api       Py_SphinxTest [ function]" in status.getvalue()
84
85
86@pytest.mark.sphinx('coverage', confoverrides={'coverage_show_missing_items': True})
87def test_show_missing_items_quiet(app, status, warning):
88    app.quiet = True
89    app.builder.build_all()
90
91    assert "undocumented python function: autodoc_target :: raises" in warning.getvalue()
92    assert "undocumented python class: autodoc_target :: Base" in warning.getvalue()
93    assert "undocumented python method: autodoc_target :: Class :: roger" in warning.getvalue()
94
95    assert "undocumented c api: Py_SphinxTest [function]" in warning.getvalue()
96