13
13
from pathlib import Path
14
14
from typing import cast
15
15
from typing import Dict
16
+ from typing import Generator
16
17
from typing import List
17
18
from typing import Mapping
18
19
from typing import Optional
19
20
from typing import Set
21
+ from unittest import mock
20
22
21
23
import _pytest ._code
22
24
import pytest
@@ -1376,7 +1378,7 @@ class TestEarlyRewriteBailout:
1376
1378
@pytest .fixture
1377
1379
def hook (
1378
1380
self , pytestconfig , monkeypatch , pytester : Pytester
1379
- ) -> AssertionRewritingHook :
1381
+ ) -> Generator [ AssertionRewritingHook , None , None ] :
1380
1382
"""Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
1381
1383
if PathFinder.find_spec has been called.
1382
1384
"""
@@ -1397,11 +1399,11 @@ def spy_find_spec(name, path):
1397
1399
1398
1400
hook = AssertionRewritingHook (pytestconfig )
1399
1401
# 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
1405
1407
1406
1408
def test_basic (self , pytester : Pytester , hook : AssertionRewritingHook ) -> None :
1407
1409
"""
@@ -1451,9 +1453,9 @@ def test_simple_failure():
1451
1453
}
1452
1454
)
1453
1455
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" ]
1457
1459
1458
1460
@pytest .mark .skipif (
1459
1461
sys .platform .startswith ("win32" ), reason = "cannot remove cwd on Windows"
0 commit comments