Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf723d5

Browse files
committed
Make test_use_shell_on_class more robust
1 parent7ab27c5 commitaf723d5

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

‎test/deprecation/test_cmd_git.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
importcontextlib
1010
importsys
1111
fromtypingimportGenerator
12+
importwarnings
1213

1314
ifsys.version_info>= (3,11):
1415
fromtypingimportassert_type
@@ -26,9 +27,16 @@
2627
"""Beginning text of USE_SHELL deprecation warnings when USE_SHELL is set True."""
2728

2829

30+
@contextlib.contextmanager
31+
def_suppress_deprecation_warning()->Generator[None,None,None]:
32+
withwarnings.catch_warnings():
33+
warnings.filterwarnings("ignore",category=DeprecationWarning)
34+
yield
35+
36+
2937
@pytest.fixture
30-
defreset_backing_attribute()->Generator[None,None,None]:
31-
"""Fixture toreset the private ``_USE_SHELL`` attribute.
38+
deftry_restore_use_shell_state()->Generator[None,None,None]:
39+
"""Fixture toattempt to restore state associated with the ``USE_SHELL`` attribute.
3240
3341
This is used to decrease the likelihood of state changes leaking out and affecting
3442
other tests. But the goal is not to assert that ``_USE_SHELL`` is used, nor anything
@@ -38,18 +46,27 @@ def reset_backing_attribute() -> Generator[None, None, None]:
3846
restores attributes that it has previously been used to change, create, or remove.
3947
"""
4048
no_value=object()
49+
4150
try:
42-
old_value=Git._USE_SHELL
51+
old_backing_value=Git._USE_SHELL
4352
exceptAttributeError:
44-
old_value=no_value
53+
old_backing_value=no_value
54+
try:
55+
with_suppress_deprecation_warning():
56+
old_public_value=Git.USE_SHELL
4557

46-
yield
58+
# This doesn't have its own try-finally because pytest catches exceptions raised
59+
# during the yield. (The outer try-finally catches exceptions in this fixture.)
60+
yield
4761

48-
ifold_valueisno_value:
49-
withcontextlib.suppress(AttributeError):
50-
delGit._USE_SHELL
51-
else:
52-
Git._USE_SHELL=old_value
62+
with_suppress_deprecation_warning():
63+
Git.USE_SHELL=old_public_value
64+
finally:
65+
ifold_backing_valueisno_value:
66+
withcontextlib.suppress(AttributeError):
67+
delGit._USE_SHELL
68+
else:
69+
Git._USE_SHELL=old_backing_value
5370

5471

5572
deftest_cannot_access_undefined_on_git_class()->None:
@@ -76,23 +93,26 @@ def test_get_use_shell_on_class_default() -> None:
7693
assertnotuse_shell
7794

7895

79-
# FIXME: More robustly check that each operation really issues exactly one deprecation
80-
# warning, even if this requires relying more on reset_backing_attribute doing its job.
81-
deftest_use_shell_on_class(reset_backing_attribute)->None:
96+
deftest_use_shell_on_class(try_restore_use_shell_state)->None:
8297
"""USE_SHELL can be written and re-read as a class attribute, always warning."""
83-
# We assert in a "safe" order, using reset_backing_attribute only as a backstop.
84-
withpytest.deprecated_call()asctx:
98+
withpytest.deprecated_call()assetting:
8599
Git.USE_SHELL=True
100+
withpytest.deprecated_call()aschecking:
86101
set_value=Git.USE_SHELL
102+
withpytest.deprecated_call()asresetting:
87103
Git.USE_SHELL=False
104+
withpytest.deprecated_call()asrechecking:
88105
reset_value=Git.USE_SHELL
89106

90107
# The attribute should take on the values set to it.
91108
assertset_valueisTrue
92109
assertreset_valueisFalse
93110

94-
messages= [str(entry.message)forentryinctx]
95-
set_message,check_message,reset_message,recheck_message=messages
111+
# Each access should warn exactly once.
112+
(set_message,)= [str(entry.message)forentryinsetting]
113+
(check_message,)= [str(entry.message)forentryinchecking]
114+
(reset_message,)= [str(entry.message)forentryinresetting]
115+
(recheck_message,)= [str(entry.message)forentryinrechecking]
96116

97117
# Setting it to True should produce the special warning for that.
98118
assert_USE_SHELL_DEPRECATED_FRAGMENTinset_message

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp