| 1 |
"""Test the coverage builder.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import pickle |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from sphinx.testing.util import SphinxTestApp |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.sphinx('coverage', testroot='root') |
| 15 |
def test_build(app: SphinxTestApp) -> None: |
| 16 |
app.build(force_all=True) |
| 17 |
|
| 18 |
py_undoc = (app.outdir / 'python.txt').read_text(encoding='utf8') |
| 19 |
assert py_undoc.startswith( |
| 20 |
'Undocumented Python objects\n===========================\n', |
| 21 |
) |
| 22 |
assert 'autodoc_target\n--------------\n' in py_undoc |
| 23 |
assert ' * Class -- missing methods:\n' in py_undoc |
| 24 |
assert ' * raises\n' in py_undoc |
| 25 |
# these two are documented in autodoc.txt |
| 26 |
assert ' * function\n' not in py_undoc |
| 27 |
assert ' * Class\n' not in py_undoc |
| 28 |
|
| 29 |
# in the "failed import" section |
| 30 |
assert " * mod -- No module named 'mod'" in py_undoc |
| 31 |
|
| 32 |
assert 'undocumented py' not in app.status.getvalue() |
| 33 |
|
| 34 |
c_undoc = (app.outdir / 'c.txt').read_text(encoding='utf8') |
| 35 |
assert c_undoc.startswith( |
| 36 |
'Undocumented C API elements\n===========================\n', |
| 37 |
) |
| 38 |
assert 'api.h' in c_undoc |
| 39 |
assert ' * Py_SphinxTest' in c_undoc |
| 40 |
|
| 41 |
undoc_py, undoc_c, _py_undocumented, _py_documented = pickle.loads( |
| 42 |
(app.outdir / 'undoc.pickle').read_bytes() |
| 43 |
) |
| 44 |
assert len(undoc_c) == 1 |
| 45 |
# the key is the full path to the header file, which isn't testable |
| 46 |
assert next(iter(undoc_c.values())) == {('function', 'Py_SphinxTest')} |
| 47 |
|
| 48 |
assert 'autodoc_target' in undoc_py |
| 49 |
assert 'funcs' in undoc_py['autodoc_target'] |
| 50 |
assert 'raises' in undoc_py['autodoc_target']['funcs'] |
| 51 |
assert 'classes' in undoc_py['autodoc_target'] |
| 52 |
assert 'Class' in undoc_py['autodoc_target']['classes'] |
| 53 |
assert 'undocmeth' in undoc_py['autodoc_target']['classes']['Class'] |
| 54 |
|
| 55 |
assert 'undocumented c' not in app.status.getvalue() |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.sphinx('coverage', testroot='ext-coverage') |
| 59 |
def test_coverage_ignore_pyobjects(app: SphinxTestApp) -> None: |
| 60 |
app.build(force_all=True) |
| 61 |
actual = (app.outdir / 'python.txt').read_text(encoding='utf8') |
| 62 |
expected = """\ |
| 63 |
Undocumented Python objects |
| 64 |
=========================== |
| 65 |
|
| 66 |
Statistics |
| 67 |
---------- |
| 68 |
|
| 69 |
+---------------------------+----------+--------------+ |
| 70 |
| Module | Coverage | Undocumented | |
| 71 |
+===========================+==========+==============+ |
| 72 |
| grog | 100.00% | 0 | |
| 73 |
+---------------------------+----------+--------------+ |
| 74 |
| grog.coverage_missing | 100.00% | 0 | |
| 75 |
+---------------------------+----------+--------------+ |
| 76 |
| grog.coverage_not_ignored | 0.00% | 2 | |
| 77 |
+---------------------------+----------+--------------+ |
| 78 |
| TOTAL | 0.00% | 2 | |
| 79 |
+---------------------------+----------+--------------+ |
| 80 |
|
| 81 |
grog.coverage_missing |
| 82 |
--------------------- |
| 83 |
|
| 84 |
Classes: |
| 85 |
* Missing |
| 86 |
|
| 87 |
grog.coverage_not_ignored |
| 88 |
------------------------- |
| 89 |
|
| 90 |
Classes: |
| 91 |
* Documented -- missing methods: |
| 92 |
|
| 93 |
- not_ignored1 |
| 94 |
- not_ignored2 |
| 95 |
* NotIgnored |
| 96 |
|
| 97 |
""" |
| 98 |
assert actual == expected |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.sphinx( |
| 102 |
'coverage', testroot='root', confoverrides={'coverage_show_missing_items': True} |
| 103 |
) |
| 104 |
def test_show_missing_items(app: SphinxTestApp) -> None: |
| 105 |
app.build(force_all=True) |
| 106 |
|
| 107 |
assert 'undocumented' in app.status.getvalue() |
| 108 |
|
| 109 |
assert 'py function raises' in app.status.getvalue() |
| 110 |
assert 'py class Base' in app.status.getvalue() |
| 111 |
assert 'py method Class.roger' in app.status.getvalue() |
| 112 |
|
| 113 |
assert 'c api Py_SphinxTest [ function]' in app.status.getvalue() |
| 114 |
|
| 115 |
|
| 116 |
@pytest.mark.sphinx( |
| 117 |
'coverage', testroot='root', confoverrides={'coverage_show_missing_items': True} |
| 118 |
) |
| 119 |
def test_show_missing_items_quiet(app: SphinxTestApp) -> None: |
| 120 |
app.config._verbosity = -1 # mimics status=None / app.quiet = True |
| 121 |
app.build(force_all=True) |
| 122 |
|
| 123 |
assert ( |
| 124 |
'undocumented python function: autodoc_target :: raises' |
| 125 |
) in app.warning.getvalue() |
| 126 |
assert 'undocumented python class: autodoc_target :: Base' in app.warning.getvalue() |
| 127 |
assert ( |
| 128 |
'undocumented python method: autodoc_target :: Class :: roger' |
| 129 |
) in app.warning.getvalue() |
| 130 |
|
| 131 |
assert 'undocumented c api: Py_SphinxTest [function]' in app.warning.getvalue() |