Skip to content

Commit

Permalink
Use overwrite_file context manager in test_ext_autodoc_configs (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
marxin and AA-Turner committed Apr 21, 2023
1 parent 77483f2 commit 186d596
Showing 1 changed file with 69 additions and 64 deletions.
133 changes: 69 additions & 64 deletions tests/test_ext_autodoc_configs.py
Expand Up @@ -2,6 +2,7 @@

import platform
import sys
from contextlib import contextmanager

import pytest

Expand All @@ -12,6 +13,19 @@
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')
def test_autoclass_content_class(app):
app.config.autoclass_content = 'class'
Expand Down Expand Up @@ -947,7 +961,8 @@ def test_autodoc_typehints_none_for_overload(app):


@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description"})
confoverrides={'autodoc_typehints': "description"},
freshenv=True)
def test_autodoc_typehints_description(app):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
Expand Down Expand Up @@ -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
# a description for its parameters or its return. `tuple_args` does
# describe them, so :type: and :rtype: will be added.
(app.srcdir / 'index.rst').write_text(
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.decr\n'
'\n'
' :returns: decremented number\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
' :param x: arg\n'
' :return: another tuple\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.decr\n'
'\n'
' :returns: decremented number\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
' :param x: arg\n'
' :return: another tuple\n'):
app.build()
# Restore the original content of the file
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('target.typehints.incr(a, b=1)\n'
'\n'
Expand Down Expand Up @@ -1033,26 +1047,24 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
# autodoc_typehints_description_target. `tuple_args` does describe both, so
# :type: and :rtype: will be added. `nothing` has no parameters but a return
# type of None, which will be added.
(app.srcdir / 'index.rst').write_text(
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.decr\n'
'\n'
' :returns: decremented number\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
' :param x: arg\n'
' :return: another tuple\n'
'\n'
'.. autofunction:: target.typehints.Math.nothing\n'
'\n'
'.. autofunction:: target.typehints.Math.horse\n'
'\n'
' :return: nothing\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.decr\n'
'\n'
' :returns: decremented number\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
' :param x: arg\n'
' :return: another tuple\n'
'\n'
'.. autofunction:: target.typehints.Math.nothing\n'
'\n'
'.. autofunction:: target.typehints.Math.horse\n'
'\n'
' :return: nothing\n'):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert context == (
'target.typehints.incr(a, b=1)\n'
Expand Down Expand Up @@ -1094,12 +1106,10 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "description"})
def test_autodoc_typehints_description_with_documented_init(app):
(app.srcdir / 'index.rst').write_text(
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n'):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert context == (
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
Expand Down Expand Up @@ -1133,12 +1143,10 @@ def test_autodoc_typehints_description_with_documented_init(app):
confoverrides={'autodoc_typehints': "description",
'autodoc_typehints_description_target': 'documented'})
def test_autodoc_typehints_description_with_documented_init_no_undoc(app):
(app.srcdir / 'index.rst').write_text(
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n'):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert context == (
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
Expand All @@ -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
# returnvalue_and_documented_params should not change class or method
# docstring.
(app.srcdir / 'index.rst').write_text(
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autoclass:: target.typehints._ClassWithDocumentedInit\n'
' :special-members: __init__\n'):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert context == (
'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n'
Expand Down Expand Up @@ -1200,15 +1206,13 @@ def test_autodoc_typehints_description_for_invalid_node(app):
@pytest.mark.sphinx('text', testroot='ext-autodoc',
confoverrides={'autodoc_typehints': "both"})
def test_autodoc_typehints_both(app):
(app.srcdir / 'index.rst').write_text(
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
'.. autofunction:: target.overload.sum\n',
encoding='utf8',
)
app.build()
with overwrite_file(app.srcdir / 'index.rst',
'.. autofunction:: target.typehints.incr\n'
'\n'
'.. autofunction:: target.typehints.tuple_args\n'
'\n'
'.. autofunction:: target.overload.sum\n'):
app.build()
context = (app.outdir / 'index.txt').read_text(encoding='utf8')
assert ('target.typehints.incr(a: int, b: int = 1) -> int\n'
'\n'
Expand Down Expand Up @@ -1387,8 +1391,9 @@ def test_autodoc_type_aliases(app):
confoverrides={'autodoc_typehints': "description",
'autodoc_type_aliases': {'myint': 'myint'}})
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')
app.build()
with overwrite_file(app.srcdir / 'autodoc_type_aliases.rst',
'.. autofunction:: target.autodoc_type_aliases.sum'):
app.build()
context = (app.outdir / 'autodoc_type_aliases.txt').read_text(encoding='utf8')
assert context == (
'target.autodoc_type_aliases.sum(x, y)\n'
Expand Down

0 comments on commit 186d596

Please sign in to comment.