Skip to content

Commit efa16c2

Browse files
authored
Merge pull request #9641 from asottile/fix-test-pollution-assertrewrite
fix test pollution in test_assertrewrite
2 parents bc33ba0 + 579785b commit efa16c2

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

testing/test_assertrewrite.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
from pathlib import Path
1414
from typing import cast
1515
from typing import Dict
16+
from typing import Generator
1617
from typing import List
1718
from typing import Mapping
1819
from typing import Optional
1920
from typing import Set
21+
from unittest import mock
2022

2123
import _pytest._code
2224
import pytest
@@ -1376,7 +1378,7 @@ class TestEarlyRewriteBailout:
13761378
@pytest.fixture
13771379
def hook(
13781380
self, pytestconfig, monkeypatch, pytester: Pytester
1379-
) -> AssertionRewritingHook:
1381+
) -> Generator[AssertionRewritingHook, None, None]:
13801382
"""Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
13811383
if PathFinder.find_spec has been called.
13821384
"""
@@ -1397,11 +1399,11 @@ def spy_find_spec(name, path):
13971399

13981400
hook = AssertionRewritingHook(pytestconfig)
13991401
# use default patterns, otherwise we inherit pytest's testing config
1400-
hook.fnpats[:] = ["test_*.py", "*_test.py"]
1401-
monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
1402-
hook.set_session(StubSession()) # type: ignore[arg-type]
1403-
pytester.syspathinsert()
1404-
return hook
1402+
with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]):
1403+
monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
1404+
hook.set_session(StubSession()) # type: ignore[arg-type]
1405+
pytester.syspathinsert()
1406+
yield hook
14051407

14061408
def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None:
14071409
"""
@@ -1451,9 +1453,9 @@ def test_simple_failure():
14511453
}
14521454
)
14531455
pytester.syspathinsert("tests")
1454-
hook.fnpats[:] = ["tests/**.py"]
1455-
assert hook.find_spec("file") is not None
1456-
assert self.find_spec_calls == ["file"]
1456+
with mock.patch.object(hook, "fnpats", ["tests/**.py"]):
1457+
assert hook.find_spec("file") is not None
1458+
assert self.find_spec_calls == ["file"]
14571459

14581460
@pytest.mark.skipif(
14591461
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"

0 commit comments

Comments
 (0)