mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Use `overwrite_file context manager in test_ext_autodoc_configs` (#11320)
The tests modify source files (e.g. index.rst) that are not restored and thus another test could read an altered source file, it leading to unexpected test results. Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -12,6 +13,19 @@ from .test_ext_autodoc import do_autodoc
|
|||||||
IS_PYPY = platform.python_implementation() == 'PyPy'
|
IS_PYPY = platform.python_implementation() == 'PyPy'
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def overwrite_file(path, content):
|
||||||
|
current_content = path.read_bytes() if path.exists() else None
|
||||||
|
try:
|
||||||
|
path.write_text(content, encoding='utf-8')
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
if current_content is not None:
|
||||||
|
path.write_bytes(current_content)
|
||||||
|
else:
|
||||||
|
path.unlink()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
@pytest.mark.sphinx('html', testroot='ext-autodoc')
|
||||||
def test_autoclass_content_class(app):
|
def test_autoclass_content_class(app):
|
||||||
app.config.autoclass_content = 'class'
|
app.config.autoclass_content = 'class'
|
||||||
@@ -947,7 +961,8 @@ def test_autodoc_typehints_none_for_overload(app):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
||||||
confoverrides={'autodoc_typehints': "description"})
|
confoverrides={'autodoc_typehints': "description"},
|
||||||
|
freshenv=True)
|
||||||
def test_autodoc_typehints_description(app):
|
def test_autodoc_typehints_description(app):
|
||||||
app.build()
|
app.build()
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
@@ -986,20 +1001,19 @@ def test_autodoc_typehints_description_no_undoc(app):
|
|||||||
# No :type: or :rtype: will be injected for `incr`, which does not have
|
# No :type: or :rtype: will be injected for `incr`, which does not have
|
||||||
# a description for its parameters or its return. `tuple_args` does
|
# a description for its parameters or its return. `tuple_args` does
|
||||||
# describe them, so :type: and :rtype: will be added.
|
# describe them, so :type: and :rtype: will be added.
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autofunction:: target.typehints.incr\n'
|
'.. autofunction:: target.typehints.incr\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.decr\n'
|
'.. autofunction:: target.typehints.decr\n'
|
||||||
'\n'
|
'\n'
|
||||||
' :returns: decremented number\n'
|
' :returns: decremented number\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.tuple_args\n'
|
'.. autofunction:: target.typehints.tuple_args\n'
|
||||||
'\n'
|
'\n'
|
||||||
' :param x: arg\n'
|
' :param x: arg\n'
|
||||||
' :return: another tuple\n',
|
' :return: another tuple\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
# Restore the original content of the file
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert ('target.typehints.incr(a, b=1)\n'
|
assert ('target.typehints.incr(a, b=1)\n'
|
||||||
'\n'
|
'\n'
|
||||||
@@ -1033,26 +1047,24 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
|
|||||||
# autodoc_typehints_description_target. `tuple_args` does describe both, so
|
# autodoc_typehints_description_target. `tuple_args` does describe both, so
|
||||||
# :type: and :rtype: will be added. `nothing` has no parameters but a return
|
# :type: and :rtype: will be added. `nothing` has no parameters but a return
|
||||||
# type of None, which will be added.
|
# type of None, which will be added.
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autofunction:: target.typehints.incr\n'
|
'.. autofunction:: target.typehints.incr\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.decr\n'
|
'.. autofunction:: target.typehints.decr\n'
|
||||||
'\n'
|
'\n'
|
||||||
' :returns: decremented number\n'
|
' :returns: decremented number\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.tuple_args\n'
|
'.. autofunction:: target.typehints.tuple_args\n'
|
||||||
'\n'
|
'\n'
|
||||||
' :param x: arg\n'
|
' :param x: arg\n'
|
||||||
' :return: another tuple\n'
|
' :return: another tuple\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.Math.nothing\n'
|
'.. autofunction:: target.typehints.Math.nothing\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.Math.horse\n'
|
'.. autofunction:: target.typehints.Math.horse\n'
|
||||||
'\n'
|
'\n'
|
||||||
' :return: nothing\n',
|
' :return: nothing\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert context == (
|
assert context == (
|
||||||
'target.typehints.incr(a, b=1)\n'
|
'target.typehints.incr(a, b=1)\n'
|
||||||
@@ -1094,12 +1106,10 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
|
|||||||
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
||||||
confoverrides={'autodoc_typehints': "description"})
|
confoverrides={'autodoc_typehints': "description"})
|
||||||
def test_autodoc_typehints_description_with_documented_init(app):
|
def test_autodoc_typehints_description_with_documented_init(app):
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
||||||
' :special-members: __init__\n',
|
' :special-members: __init__\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert context == (
|
assert context == (
|
||||||
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
||||||
@@ -1133,12 +1143,10 @@ def test_autodoc_typehints_description_with_documented_init(app):
|
|||||||
confoverrides={'autodoc_typehints': "description",
|
confoverrides={'autodoc_typehints': "description",
|
||||||
'autodoc_typehints_description_target': 'documented'})
|
'autodoc_typehints_description_target': 'documented'})
|
||||||
def test_autodoc_typehints_description_with_documented_init_no_undoc(app):
|
def test_autodoc_typehints_description_with_documented_init_no_undoc(app):
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
||||||
' :special-members: __init__\n',
|
' :special-members: __init__\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert context == (
|
assert context == (
|
||||||
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
||||||
@@ -1165,12 +1173,10 @@ def test_autodoc_typehints_description_with_documented_init_no_undoc_doc_rtype(a
|
|||||||
# see test_autodoc_typehints_description_with_documented_init_no_undoc
|
# see test_autodoc_typehints_description_with_documented_init_no_undoc
|
||||||
# returnvalue_and_documented_params should not change class or method
|
# returnvalue_and_documented_params should not change class or method
|
||||||
# docstring.
|
# docstring.
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
|
||||||
' :special-members: __init__\n',
|
' :special-members: __init__\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert context == (
|
assert context == (
|
||||||
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
|
||||||
@@ -1200,15 +1206,13 @@ def test_autodoc_typehints_description_for_invalid_node(app):
|
|||||||
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
@pytest.mark.sphinx('text', testroot='ext-autodoc',
|
||||||
confoverrides={'autodoc_typehints': "both"})
|
confoverrides={'autodoc_typehints': "both"})
|
||||||
def test_autodoc_typehints_both(app):
|
def test_autodoc_typehints_both(app):
|
||||||
(app.srcdir / 'index.rst').write_text(
|
with overwrite_file(app.srcdir / 'index.rst',
|
||||||
'.. autofunction:: target.typehints.incr\n'
|
'.. autofunction:: target.typehints.incr\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.typehints.tuple_args\n'
|
'.. autofunction:: target.typehints.tuple_args\n'
|
||||||
'\n'
|
'\n'
|
||||||
'.. autofunction:: target.overload.sum\n',
|
'.. autofunction:: target.overload.sum\n'):
|
||||||
encoding='utf8',
|
app.build()
|
||||||
)
|
|
||||||
app.build()
|
|
||||||
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
|
||||||
assert ('target.typehints.incr(a: int, b: int = 1) -> int\n'
|
assert ('target.typehints.incr(a: int, b: int = 1) -> int\n'
|
||||||
'\n'
|
'\n'
|
||||||
@@ -1387,8 +1391,9 @@ def test_autodoc_type_aliases(app):
|
|||||||
confoverrides={'autodoc_typehints': "description",
|
confoverrides={'autodoc_typehints': "description",
|
||||||
'autodoc_type_aliases': {'myint': 'myint'}})
|
'autodoc_type_aliases': {'myint': 'myint'}})
|
||||||
def test_autodoc_typehints_description_and_type_aliases(app):
|
def test_autodoc_typehints_description_and_type_aliases(app):
|
||||||
(app.srcdir / 'autodoc_type_aliases.rst').write_text('.. autofunction:: target.autodoc_type_aliases.sum', encoding='utf8')
|
with overwrite_file(app.srcdir / 'autodoc_type_aliases.rst',
|
||||||
app.build()
|
'.. autofunction:: target.autodoc_type_aliases.sum'):
|
||||||
|
app.build()
|
||||||
context = (app.outdir / 'autodoc_type_aliases.txt').read_text(encoding='utf8')
|
context = (app.outdir / 'autodoc_type_aliases.txt').read_text(encoding='utf8')
|
||||||
assert context == (
|
assert context == (
|
||||||
'target.autodoc_type_aliases.sum(x, y)\n'
|
'target.autodoc_type_aliases.sum(x, y)\n'
|
||||||
|
|||||||
Reference in New Issue
Block a user