fix test pollution in test_assertrewrite

originally reproduced with this pollution set:

```
testing/test_assertrewrite.py::TestEarlyRewriteBailout::test_pattern_contains_subdirectories
testing/test_assertrewrite.py::TestRewriteOnImport::test_remember_rewritten_modules
```
This commit is contained in:
Anthony Sottile 2022-02-07 19:29:01 -05:00
parent 6f936aa97c
commit 579785b6cd
2 changed files with 17 additions and 14 deletions

View File

@ -13,10 +13,12 @@ from functools import partial
from pathlib import Path from pathlib import Path
from typing import cast from typing import cast
from typing import Dict from typing import Dict
from typing import Generator
from typing import List from typing import List
from typing import Mapping from typing import Mapping
from typing import Optional from typing import Optional
from typing import Set from typing import Set
from unittest import mock
import _pytest._code import _pytest._code
import pytest import pytest
@ -1376,7 +1378,7 @@ class TestEarlyRewriteBailout:
@pytest.fixture @pytest.fixture
def hook( def hook(
self, pytestconfig, monkeypatch, pytester: Pytester self, pytestconfig, monkeypatch, pytester: Pytester
) -> AssertionRewritingHook: ) -> Generator[AssertionRewritingHook, None, None]:
"""Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
if PathFinder.find_spec has been called. if PathFinder.find_spec has been called.
""" """
@ -1397,11 +1399,11 @@ class TestEarlyRewriteBailout:
hook = AssertionRewritingHook(pytestconfig) hook = AssertionRewritingHook(pytestconfig)
# use default patterns, otherwise we inherit pytest's testing config # use default patterns, otherwise we inherit pytest's testing config
hook.fnpats[:] = ["test_*.py", "*_test.py"] with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]):
monkeypatch.setattr(hook, "_find_spec", spy_find_spec) monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
hook.set_session(StubSession()) # type: ignore[arg-type] hook.set_session(StubSession()) # type: ignore[arg-type]
pytester.syspathinsert() pytester.syspathinsert()
return hook yield hook
def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None: def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None:
""" """
@ -1451,9 +1453,9 @@ class TestEarlyRewriteBailout:
} }
) )
pytester.syspathinsert("tests") pytester.syspathinsert("tests")
hook.fnpats[:] = ["tests/**.py"] with mock.patch.object(hook, "fnpats", ["tests/**.py"]):
assert hook.find_spec("file") is not None assert hook.find_spec("file") is not None
assert self.find_spec_calls == ["file"] assert self.find_spec_calls == ["file"]
@pytest.mark.skipif( @pytest.mark.skipif(
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"

View File

@ -802,11 +802,12 @@ class TestDoctests:
p = pytester.makepyfile( p = pytester.makepyfile(
setup=""" setup="""
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup(name='sample', if __name__ == '__main__':
version='0.0', setup(name='sample',
description='description', version='0.0',
packages=find_packages() description='description',
) packages=find_packages()
)
""" """
) )
result = pytester.runpytest(p, "--doctest-modules") result = pytester.runpytest(p, "--doctest-modules")