2010-01-02 07:59:27 -06:00
|
|
|
"""Test the doctest extension."""
|
2024-08-11 08:58:56 -05:00
|
|
|
|
2024-11-22 15:54:26 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-05-01 12:02:20 -05:00
|
|
|
import os
|
2018-08-16 09:56:29 -05:00
|
|
|
from collections import Counter
|
2018-07-28 06:19:30 -05:00
|
|
|
|
2017-01-05 10:14:47 -06:00
|
|
|
import pytest
|
2020-11-11 05:00:27 -06:00
|
|
|
from docutils import nodes
|
2018-02-19 07:39:14 -06:00
|
|
|
from packaging.specifiers import InvalidSpecifier
|
|
|
|
from packaging.version import InvalidVersion
|
|
|
|
|
2018-01-07 20:59:50 -06:00
|
|
|
from sphinx.ext.doctest import is_allowed_version
|
2013-04-01 04:39:32 -05:00
|
|
|
|
2011-01-03 14:20:29 -06:00
|
|
|
cleanup_called = 0
|
2010-01-02 07:59:27 -06:00
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2017-11-01 10:03:29 -05:00
|
|
|
@pytest.mark.sphinx('doctest', testroot='ext-doctest')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_build(app):
|
2024-10-18 19:50:45 -05:00
|
|
|
global cleanup_called # NoQA: PLW0603
|
2011-01-03 14:20:29 -06:00
|
|
|
cleanup_called = 0
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2024-07-23 09:35:55 -05:00
|
|
|
assert app.statuscode == 0, f'failures in doctests:\n{app.status.getvalue()}'
|
2011-01-03 14:20:29 -06:00
|
|
|
# in doctest.txt, there are two named groups and the default group,
|
|
|
|
# so the cleanup function must be called three times
|
|
|
|
assert cleanup_called == 3, 'testcleanup did not get executed enough times'
|
|
|
|
|
2014-09-21 10:17:02 -05:00
|
|
|
|
2018-12-15 03:35:12 -06:00
|
|
|
@pytest.mark.sphinx('dummy', testroot='ext-doctest')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_highlight_language_default(app):
|
2018-12-15 03:35:12 -06:00
|
|
|
app.build()
|
|
|
|
doctree = app.env.get_doctree('doctest')
|
2022-01-01 10:06:24 -06:00
|
|
|
for node in doctree.findall(nodes.literal_block):
|
2022-09-24 18:10:59 -05:00
|
|
|
assert node['language'] in {'python', 'pycon', 'none'}
|
2018-12-15 03:35:12 -06:00
|
|
|
|
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
@pytest.mark.sphinx(
|
|
|
|
'dummy',
|
|
|
|
testroot='ext-doctest',
|
|
|
|
confoverrides={'highlight_language': 'python'},
|
|
|
|
)
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_highlight_language_python3(app):
|
2018-12-15 03:35:12 -06:00
|
|
|
app.build()
|
|
|
|
doctree = app.env.get_doctree('doctest')
|
2022-01-01 10:06:24 -06:00
|
|
|
for node in doctree.findall(nodes.literal_block):
|
2022-09-24 18:10:59 -05:00
|
|
|
assert node['language'] in {'python', 'pycon', 'none'}
|
2018-12-15 03:35:12 -06:00
|
|
|
|
|
|
|
|
2018-01-07 20:59:50 -06:00
|
|
|
def test_is_allowed_version():
|
|
|
|
assert is_allowed_version('<3.4', '3.3') is True
|
|
|
|
assert is_allowed_version('<3.4', '3.3') is True
|
|
|
|
assert is_allowed_version('<3.2', '3.3') is False
|
2023-08-15 12:16:27 -05:00
|
|
|
assert is_allowed_version('<=3.4', '3.3') is True
|
|
|
|
assert is_allowed_version('<=3.2', '3.3') is False
|
|
|
|
assert is_allowed_version('==3.3', '3.3') is True
|
|
|
|
assert is_allowed_version('==3.4', '3.3') is False
|
|
|
|
assert is_allowed_version('>=3.2', '3.3') is True
|
|
|
|
assert is_allowed_version('>=3.4', '3.3') is False
|
2018-01-07 20:59:50 -06:00
|
|
|
assert is_allowed_version('>3.2', '3.3') is True
|
|
|
|
assert is_allowed_version('>3.4', '3.3') is False
|
|
|
|
assert is_allowed_version('~=3.4', '3.4.5') is True
|
|
|
|
assert is_allowed_version('~=3.4', '3.5.0') is True
|
|
|
|
|
|
|
|
# invalid spec
|
|
|
|
with pytest.raises(InvalidSpecifier):
|
|
|
|
is_allowed_version('&3.4', '3.5')
|
|
|
|
|
|
|
|
# invalid version
|
|
|
|
with pytest.raises(InvalidVersion):
|
|
|
|
is_allowed_version('>3.4', 'Sphinx')
|
2017-01-14 11:57:19 -06:00
|
|
|
|
|
|
|
|
2011-01-03 14:20:29 -06:00
|
|
|
def cleanup_call():
|
2024-10-18 19:50:45 -05:00
|
|
|
global cleanup_called # NoQA: PLW0603
|
2011-01-03 14:20:29 -06:00
|
|
|
cleanup_called += 1
|
2017-11-16 01:28:30 -06:00
|
|
|
|
|
|
|
|
2025-01-26 13:41:15 -06:00
|
|
|
recorded_calls: Counter[tuple[str, str, int]] = Counter()
|
2018-08-16 05:22:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.sphinx('doctest', testroot='ext-doctest-skipif')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_skipif(app):
|
2018-08-16 05:22:59 -05:00
|
|
|
"""Tests for the :skipif: option
|
|
|
|
|
|
|
|
The tests are separated into a different test root directory since the
|
|
|
|
``app`` object only evaluates options once in its lifetime. If these tests
|
|
|
|
were combined with the other doctest tests, the ``:skipif:`` evaluations
|
2024-01-16 20:38:46 -06:00
|
|
|
would be recorded only on the first ``app.build(force_all=True)`` run, i.e.
|
2018-08-16 05:22:59 -05:00
|
|
|
in ``test_build`` above, and the assertion below would fail.
|
|
|
|
|
|
|
|
"""
|
2024-10-18 19:50:45 -05:00
|
|
|
global recorded_calls # NoQA: PLW0603
|
2018-08-16 09:56:29 -05:00
|
|
|
recorded_calls = Counter()
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2018-08-16 05:22:59 -05:00
|
|
|
if app.statuscode != 0:
|
2024-07-23 09:35:55 -05:00
|
|
|
raise AssertionError('failures in doctests:' + app.status.getvalue())
|
2018-08-16 05:22:59 -05:00
|
|
|
# The `:skipif:` expressions are always run.
|
|
|
|
# Actual tests and setup/cleanup code is only run if the `:skipif:`
|
|
|
|
# expression evaluates to a False value.
|
2018-08-16 09:56:29 -05:00
|
|
|
# Global setup/cleanup are run before/after evaluating the `:skipif:`
|
|
|
|
# option in each directive - thus 11 additional invocations for each on top
|
|
|
|
# of the ones made for the whole test file.
|
2024-08-11 08:58:56 -05:00
|
|
|
assert recorded_calls == {
|
|
|
|
('doctest_global_setup', 'body', True): 13,
|
|
|
|
('testsetup', ':skipif:', True): 1,
|
|
|
|
('testsetup', ':skipif:', False): 1,
|
|
|
|
('testsetup', 'body', False): 1,
|
|
|
|
('doctest', ':skipif:', True): 1,
|
|
|
|
('doctest', ':skipif:', False): 1,
|
|
|
|
('doctest', 'body', False): 1,
|
|
|
|
('testcode', ':skipif:', True): 1,
|
|
|
|
('testcode', ':skipif:', False): 1,
|
|
|
|
('testcode', 'body', False): 1,
|
|
|
|
('testoutput-1', ':skipif:', True): 1,
|
|
|
|
('testoutput-2', ':skipif:', True): 1,
|
|
|
|
('testoutput-2', ':skipif:', False): 1,
|
|
|
|
('testcleanup', ':skipif:', True): 1,
|
|
|
|
('testcleanup', ':skipif:', False): 1,
|
|
|
|
('testcleanup', 'body', False): 1,
|
|
|
|
('doctest_global_cleanup', 'body', True): 13,
|
|
|
|
}
|
2018-08-16 05:22:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
def record(directive, part, should_skip):
|
2024-11-30 13:22:30 -06:00
|
|
|
recorded_calls[directive, part, should_skip] += 1
|
2022-10-17 09:54:59 -05:00
|
|
|
return f'Recorded {directive} {part} {should_skip}'
|
2018-08-16 05:22:59 -05:00
|
|
|
|
|
|
|
|
2017-11-16 01:28:30 -06:00
|
|
|
@pytest.mark.sphinx('doctest', testroot='ext-doctest-with-autodoc')
|
2024-07-23 09:35:55 -05:00
|
|
|
def test_reporting_with_autodoc(app, capfd):
|
2017-11-16 01:28:30 -06:00
|
|
|
# Patch builder to get a copy of the output
|
|
|
|
written = []
|
|
|
|
app.builder._warn_out = written.append
|
2024-01-16 20:38:46 -06:00
|
|
|
app.build(force_all=True)
|
2023-07-23 15:29:04 -05:00
|
|
|
|
2024-08-11 08:58:56 -05:00
|
|
|
failures = [
|
|
|
|
line.replace(os.sep, '/')
|
|
|
|
for line in '\n'.join(written).splitlines()
|
|
|
|
if line.startswith('File')
|
|
|
|
]
|
2023-07-23 15:29:04 -05:00
|
|
|
|
|
|
|
assert 'File "dir/inner.rst", line 1, in default' in failures
|
|
|
|
assert 'File "dir/bar.py", line ?, in default' in failures
|
|
|
|
assert 'File "foo.py", line ?, in default' in failures
|
|
|
|
assert 'File "index.rst", line 4, in default' in failures
|