22
33import platform
44import sys
5+ from contextlib import contextmanager
56
67import pytest
78
1213IS_PYPY = platform .python_implementation () == 'PyPy'
1314
1415
16+ @contextmanager
17+ def overwrite_file (path , content ):
18+ current_content = path .read_bytes () if path .exists () else None
19+ try :
20+ path .write_text (content , encoding = 'utf-8' )
21+ yield
22+ finally :
23+ if current_content is not None :
24+ path .write_bytes (current_content )
25+ else :
26+ path .unlink ()
27+
28+
1529@pytest .mark .sphinx ('html' , testroot = 'ext-autodoc' )
1630def test_autoclass_content_class (app ):
1731 app .config .autoclass_content = 'class'
@@ -947,7 +961,8 @@ def test_autodoc_typehints_none_for_overload(app):
947961
948962
949963@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
950- confoverrides = {'autodoc_typehints' : "description" })
964+ confoverrides = {'autodoc_typehints' : "description" },
965+ freshenv = True )
951966def test_autodoc_typehints_description (app ):
952967 app .build ()
953968 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
@@ -986,20 +1001,19 @@ def test_autodoc_typehints_description_no_undoc(app):
9861001 # No :type: or :rtype: will be injected for `incr`, which does not have
9871002 # a description for its parameters or its return. `tuple_args` does
9881003 # describe them, so :type: and :rtype: will be added.
989- (app .srcdir / 'index.rst' ).write_text (
990- '.. autofunction:: target.typehints.incr\n '
991- '\n '
992- '.. autofunction:: target.typehints.decr\n '
993- '\n '
994- ' :returns: decremented number\n '
995- '\n '
996- '.. autofunction:: target.typehints.tuple_args\n '
997- '\n '
998- ' :param x: arg\n '
999- ' :return: another tuple\n ' ,
1000- encoding = 'utf8' ,
1001- )
1002- app .build ()
1004+ with overwrite_file (app .srcdir / 'index.rst' ,
1005+ '.. autofunction:: target.typehints.incr\n '
1006+ '\n '
1007+ '.. autofunction:: target.typehints.decr\n '
1008+ '\n '
1009+ ' :returns: decremented number\n '
1010+ '\n '
1011+ '.. autofunction:: target.typehints.tuple_args\n '
1012+ '\n '
1013+ ' :param x: arg\n '
1014+ ' :return: another tuple\n ' ):
1015+ app .build ()
1016+ # Restore the original content of the file
10031017 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
10041018 assert ('target.typehints.incr(a, b=1)\n '
10051019 '\n '
@@ -1033,26 +1047,24 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
10331047 # autodoc_typehints_description_target. `tuple_args` does describe both, so
10341048 # :type: and :rtype: will be added. `nothing` has no parameters but a return
10351049 # type of None, which will be added.
1036- (app .srcdir / 'index.rst' ).write_text (
1037- '.. autofunction:: target.typehints.incr\n '
1038- '\n '
1039- '.. autofunction:: target.typehints.decr\n '
1040- '\n '
1041- ' :returns: decremented number\n '
1042- '\n '
1043- '.. autofunction:: target.typehints.tuple_args\n '
1044- '\n '
1045- ' :param x: arg\n '
1046- ' :return: another tuple\n '
1047- '\n '
1048- '.. autofunction:: target.typehints.Math.nothing\n '
1049- '\n '
1050- '.. autofunction:: target.typehints.Math.horse\n '
1051- '\n '
1052- ' :return: nothing\n ' ,
1053- encoding = 'utf8' ,
1054- )
1055- app .build ()
1050+ with overwrite_file (app .srcdir / 'index.rst' ,
1051+ '.. autofunction:: target.typehints.incr\n '
1052+ '\n '
1053+ '.. autofunction:: target.typehints.decr\n '
1054+ '\n '
1055+ ' :returns: decremented number\n '
1056+ '\n '
1057+ '.. autofunction:: target.typehints.tuple_args\n '
1058+ '\n '
1059+ ' :param x: arg\n '
1060+ ' :return: another tuple\n '
1061+ '\n '
1062+ '.. autofunction:: target.typehints.Math.nothing\n '
1063+ '\n '
1064+ '.. autofunction:: target.typehints.Math.horse\n '
1065+ '\n '
1066+ ' :return: nothing\n ' ):
1067+ app .build ()
10561068 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
10571069 assert context == (
10581070 'target.typehints.incr(a, b=1)\n '
@@ -1094,12 +1106,10 @@ def test_autodoc_typehints_description_no_undoc_doc_rtype(app):
10941106@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
10951107 confoverrides = {'autodoc_typehints' : "description" })
10961108def test_autodoc_typehints_description_with_documented_init (app ):
1097- (app .srcdir / 'index.rst' ).write_text (
1098- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1099- ' :special-members: __init__\n ' ,
1100- encoding = 'utf8' ,
1101- )
1102- app .build ()
1109+ with overwrite_file (app .srcdir / 'index.rst' ,
1110+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1111+ ' :special-members: __init__\n ' ):
1112+ app .build ()
11031113 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
11041114 assert context == (
11051115 'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n '
@@ -1133,12 +1143,10 @@ def test_autodoc_typehints_description_with_documented_init(app):
11331143 confoverrides = {'autodoc_typehints' : "description" ,
11341144 'autodoc_typehints_description_target' : 'documented' })
11351145def test_autodoc_typehints_description_with_documented_init_no_undoc (app ):
1136- (app .srcdir / 'index.rst' ).write_text (
1137- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1138- ' :special-members: __init__\n ' ,
1139- encoding = 'utf8' ,
1140- )
1141- app .build ()
1146+ with overwrite_file (app .srcdir / 'index.rst' ,
1147+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1148+ ' :special-members: __init__\n ' ):
1149+ app .build ()
11421150 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
11431151 assert context == (
11441152 '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
11651173 # see test_autodoc_typehints_description_with_documented_init_no_undoc
11661174 # returnvalue_and_documented_params should not change class or method
11671175 # docstring.
1168- (app .srcdir / 'index.rst' ).write_text (
1169- '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1170- ' :special-members: __init__\n ' ,
1171- encoding = 'utf8' ,
1172- )
1173- app .build ()
1176+ with overwrite_file (app .srcdir / 'index.rst' ,
1177+ '.. autoclass:: target.typehints._ClassWithDocumentedInit\n '
1178+ ' :special-members: __init__\n ' ):
1179+ app .build ()
11741180 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
11751181 assert context == (
11761182 'class target.typehints._ClassWithDocumentedInit(x, *args, **kwargs)\n '
@@ -1200,15 +1206,13 @@ def test_autodoc_typehints_description_for_invalid_node(app):
12001206@pytest .mark .sphinx ('text' , testroot = 'ext-autodoc' ,
12011207 confoverrides = {'autodoc_typehints' : "both" })
12021208def test_autodoc_typehints_both (app ):
1203- (app .srcdir / 'index.rst' ).write_text (
1204- '.. autofunction:: target.typehints.incr\n '
1205- '\n '
1206- '.. autofunction:: target.typehints.tuple_args\n '
1207- '\n '
1208- '.. autofunction:: target.overload.sum\n ' ,
1209- encoding = 'utf8' ,
1210- )
1211- app .build ()
1209+ with overwrite_file (app .srcdir / 'index.rst' ,
1210+ '.. autofunction:: target.typehints.incr\n '
1211+ '\n '
1212+ '.. autofunction:: target.typehints.tuple_args\n '
1213+ '\n '
1214+ '.. autofunction:: target.overload.sum\n ' ):
1215+ app .build ()
12121216 context = (app .outdir / 'index.txt' ).read_text (encoding = 'utf8' )
12131217 assert ('target.typehints.incr(a: int, b: int = 1) -> int\n '
12141218 '\n '
@@ -1387,8 +1391,9 @@ def test_autodoc_type_aliases(app):
13871391 confoverrides = {'autodoc_typehints' : "description" ,
13881392 'autodoc_type_aliases' : {'myint' : 'myint' }})
13891393def test_autodoc_typehints_description_and_type_aliases (app ):
1390- (app .srcdir / 'autodoc_type_aliases.rst' ).write_text ('.. autofunction:: target.autodoc_type_aliases.sum' , encoding = 'utf8' )
1391- app .build ()
1394+ with overwrite_file (app .srcdir / 'autodoc_type_aliases.rst' ,
1395+ '.. autofunction:: target.autodoc_type_aliases.sum' ):
1396+ app .build ()
13921397 context = (app .outdir / 'autodoc_type_aliases.txt' ).read_text (encoding = 'utf8' )
13931398 assert context == (
13941399 'target.autodoc_type_aliases.sum(x, y)\n '
0 commit comments