| 1 |
"""Test sphinx.ext.duration extension.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
import re |
| 7 |
from pathlib import Path |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from sphinx.testing.util import SphinxTestApp |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.sphinx( |
| 17 |
'dummy', |
| 18 |
testroot='basic', |
| 19 |
confoverrides={'extensions': ['sphinx.ext.duration']}, |
| 20 |
freshenv=True, |
| 21 |
) |
| 22 |
def test_duration(app: SphinxTestApp) -> None: |
| 23 |
app.build() |
| 24 |
|
| 25 |
assert 'slowest reading durations' in app.status.getvalue() |
| 26 |
assert re.search('\\d+\\.\\d{3}s index\n', app.status.getvalue()) |
| 27 |
|
| 28 |
assert 'total reading duration' in app.status.getvalue() |
| 29 |
assert 'Total time reading 1 file: ' in app.status.getvalue() |
| 30 |
assert re.search( |
| 31 |
r'Total time reading 1 file: \d+m \d+\.\d{3}s', app.status.getvalue() |
| 32 |
) |
| 33 |
|
| 34 |
fname = app.outdir / 'sphinx-reading-durations.json' |
| 35 |
assert fname.is_file() |
| 36 |
|
| 37 |
data = json.loads(fname.read_bytes()) |
| 38 |
keys = list(data.keys()) |
| 39 |
values = list(data.values()) |
| 40 |
assert keys == ['index'] |
| 41 |
assert len(values) == 1 |
| 42 |
assert isinstance(values[0], float) |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.sphinx( |
| 46 |
'dummy', |
| 47 |
testroot='root', |
| 48 |
confoverrides={'extensions': ['sphinx.ext.duration'], 'duration_n_slowest': 2}, |
| 49 |
freshenv=True, |
| 50 |
) |
| 51 |
def test_n_slowest_value(app: SphinxTestApp) -> None: |
| 52 |
app.build() |
| 53 |
|
| 54 |
matches = re.findall(r'\d+\.\d{3}s\s+[A-Za-z0-9/]+\n', app.status.getvalue()) |
| 55 |
assert len(matches) == 2 |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.sphinx( |
| 59 |
'dummy', |
| 60 |
testroot='basic', |
| 61 |
confoverrides={'extensions': ['sphinx.ext.duration'], 'duration_n_slowest': 0}, |
| 62 |
freshenv=True, |
| 63 |
) |
| 64 |
def test_n_slowest_all(app: SphinxTestApp) -> None: |
| 65 |
app.build() |
| 66 |
|
| 67 |
assert 'slowest reading durations' in app.status.getvalue() |
| 68 |
matches = re.findall(r'\d+\.\d{3}s\s+[A-Za-z0-9/]+\n', app.status.getvalue()) |
| 69 |
assert len(matches) > 0 |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.sphinx( |
| 73 |
'dummy', |
| 74 |
testroot='basic', |
| 75 |
confoverrides={ |
| 76 |
'extensions': ['sphinx.ext.duration'], |
| 77 |
'duration_print_slowest': False, |
| 78 |
}, |
| 79 |
freshenv=True, |
| 80 |
) |
| 81 |
def test_print_slowest_false(app: SphinxTestApp) -> None: |
| 82 |
app.build() |
| 83 |
|
| 84 |
assert 'slowest reading durations' not in app.status.getvalue() |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.sphinx( |
| 88 |
'dummy', |
| 89 |
testroot='basic', |
| 90 |
confoverrides={ |
| 91 |
'extensions': ['sphinx.ext.duration'], |
| 92 |
'duration_print_total': False, |
| 93 |
}, |
| 94 |
freshenv=True, |
| 95 |
) |
| 96 |
def test_print_total_false(app: SphinxTestApp) -> None: |
| 97 |
app.build() |
| 98 |
|
| 99 |
assert 'total reading duration' not in app.status.getvalue() |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize('write_json', [True, False]) |
| 103 |
@pytest.mark.sphinx( |
| 104 |
'dummy', |
| 105 |
testroot='basic', |
| 106 |
confoverrides={'extensions': ['sphinx.ext.duration']}, |
| 107 |
freshenv=True, |
| 108 |
) |
| 109 |
def test_write_json(app: SphinxTestApp, write_json: bool) -> None: |
| 110 |
if not write_json: |
| 111 |
app.config.duration_write_json = None |
| 112 |
app.build() |
| 113 |
|
| 114 |
expected = Path(app.outdir) / 'sphinx-reading-durations.json' |
| 115 |
assert expected.is_file() == write_json |
| 116 |
expected.unlink(missing_ok=not write_json) |
| 117 |
|
| 118 |
|
| 119 |
@pytest.mark.sphinx( |
| 120 |
'dummy', |
| 121 |
testroot='basic', |
| 122 |
confoverrides={'extensions': ['sphinx.ext.duration']}, |
| 123 |
freshenv=True, |
| 124 |
) |
| 125 |
def test_write_json_path(app: SphinxTestApp) -> None: |
| 126 |
parent_name = 'durations' |
| 127 |
file_name = 'durations.json' |
| 128 |
app.config.duration_write_json = str(Path(parent_name, file_name)) |
| 129 |
app.build() |
| 130 |
|
| 131 |
expected_path = app.outdir / parent_name / file_name |
| 132 |
assert expected_path.parent.is_dir() |
| 133 |
assert expected_path.is_file() |
| 134 |
assert expected_path.parent.name == parent_name |
| 135 |
assert expected_path.name == file_name |
| 136 |
|
| 137 |
|
| 138 |
@pytest.mark.parametrize( |
| 139 |
('duration_limit', 'expect_warning'), [(0.0, True), (1.0, False)] |
| 140 |
) |
| 141 |
@pytest.mark.sphinx( |
| 142 |
'dummy', |
| 143 |
testroot='basic', |
| 144 |
confoverrides={'extensions': ['sphinx.ext.duration']}, |
| 145 |
freshenv=True, |
| 146 |
) |
| 147 |
def test_duration_limit( |
| 148 |
app: SphinxTestApp, duration_limit: float, expect_warning: bool |
| 149 |
) -> None: |
| 150 |
app.config.duration_limit = duration_limit |
| 151 |
app.build() |
| 152 |
|
| 153 |
match = re.search( |
| 154 |
r'index\.rst: WARNING: ' |
| 155 |
r'Reading duration \d+\.\d{3}s exceeded the duration limit 0\.000s ' |
| 156 |
r'\[duration\]', |
| 157 |
app.warning.getvalue(), |
| 158 |
) |
| 159 |
if expect_warning: |
| 160 |
assert match is not None |
| 161 |
else: |
| 162 |
assert match is None |