Skip to content

Commit 61872e9

Browse files
committed
Workaround for testdir.syspathinsert requiring pkg_resources
`setuptools` no longer vendors `pkg_resources`. Use a simple workaround instead, which we can remove once we drop pytest 6 support.
1 parent 1e19c94 commit 61872e9

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

tests/test_pytest_mock.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
NEWEST_FORMATTING = sys.version_info >= (3, 11, 7)
2929

3030

31+
def syspath_insert_workaround(request: pytest.FixtureRequest, testdir: Any) -> None:
32+
if pytest.__version__.split(".")[0] == "6":
33+
# Avoid testdir.syspathinsert() which requires pkg_resources (setuptools) in
34+
# pytest 6.2.5; insert directly instead (#169).
35+
sys.path.insert(0, str(testdir.tmpdir))
36+
request.addfinalizer(lambda: sys.path.remove(str(testdir.tmpdir)))
37+
else:
38+
testdir.syspathinsert()
39+
40+
3141
@pytest.fixture
3242
def needs_assert_rewrite(pytestconfig):
3343
"""
@@ -517,7 +527,9 @@ class Foo(Base):
517527
assert spy.spy_return_list == [20]
518528

519529

520-
def test_callable_like_spy(testdir: Any, mocker: MockerFixture) -> None:
530+
def test_callable_like_spy(
531+
testdir: Any, mocker: MockerFixture, request: pytest.FixtureRequest
532+
) -> None:
521533
testdir.makepyfile(
522534
uut="""
523535
class CallLike(object):
@@ -527,7 +539,7 @@ def __call__(self, x):
527539
call_like = CallLike()
528540
"""
529541
)
530-
testdir.syspathinsert()
542+
syspath_insert_workaround(request, testdir)
531543

532544
uut = __import__("uut")
533545

@@ -1158,7 +1170,7 @@ def doIt(self):
11581170
assert len(warn_record) == 0
11591171

11601172

1161-
def test_abort_patch_context_manager_with_stale_pyc(testdir: Any) -> None:
1173+
def test_abort_patch_context_manager_with_stale_pyc(testdir: Any, request: Any) -> None:
11621174
"""Ensure we don't trigger an error in case the frame where mocker.patch is being
11631175
used doesn't have a 'context' (#169)"""
11641176
import compileall
@@ -1173,7 +1185,7 @@ def check(mocker):
11731185
assert C.x == 2
11741186
"""
11751187
)
1176-
testdir.syspathinsert()
1188+
syspath_insert_workaround(request, testdir)
11771189

11781190
testdir.makepyfile(
11791191
"""

0 commit comments

Comments
 (0)