From 579785b6cd201b6cb4da6bdf9be6988db0d469b2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 7 Feb 2022 19:29:01 -0500 Subject: [PATCH] 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 ``` --- testing/test_assertrewrite.py | 20 +++++++++++--------- testing/test_doctest.py | 11 ++++++----- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index ae18b4be34a..8f18e89c94f 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -13,10 +13,12 @@ from pathlib import Path from typing import cast from typing import Dict +from typing import Generator from typing import List from typing import Mapping from typing import Optional from typing import Set +from unittest import mock import _pytest._code import pytest @@ -1376,7 +1378,7 @@ class TestEarlyRewriteBailout: @pytest.fixture def hook( self, pytestconfig, monkeypatch, pytester: Pytester - ) -> AssertionRewritingHook: + ) -> Generator[AssertionRewritingHook, None, None]: """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track if PathFinder.find_spec has been called. """ @@ -1397,11 +1399,11 @@ def spy_find_spec(name, path): hook = AssertionRewritingHook(pytestconfig) # use default patterns, otherwise we inherit pytest's testing config - hook.fnpats[:] = ["test_*.py", "*_test.py"] - monkeypatch.setattr(hook, "_find_spec", spy_find_spec) - hook.set_session(StubSession()) # type: ignore[arg-type] - pytester.syspathinsert() - return hook + with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]): + monkeypatch.setattr(hook, "_find_spec", spy_find_spec) + hook.set_session(StubSession()) # type: ignore[arg-type] + pytester.syspathinsert() + yield hook def test_basic(self, pytester: Pytester, hook: AssertionRewritingHook) -> None: """ @@ -1451,9 +1453,9 @@ def test_simple_failure(): } ) pytester.syspathinsert("tests") - hook.fnpats[:] = ["tests/**.py"] - assert hook.find_spec("file") is not None - assert self.find_spec_calls == ["file"] + with mock.patch.object(hook, "fnpats", ["tests/**.py"]): + assert hook.find_spec("file") is not None + assert self.find_spec_calls == ["file"] @pytest.mark.skipif( sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" diff --git a/testing/test_doctest.py b/testing/test_doctest.py index e85f44f93f4..c2c83272079 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -802,11 +802,12 @@ def test_valid_setup_py(self, pytester: Pytester): p = pytester.makepyfile( setup=""" from setuptools import setup, find_packages - setup(name='sample', - version='0.0', - description='description', - packages=find_packages() - ) + if __name__ == '__main__': + setup(name='sample', + version='0.0', + description='description', + packages=find_packages() + ) """ ) result = pytester.runpytest(p, "--doctest-modules")