Skip to content

Commit 8c95bcd

Browse files
committed
13403: Disable assertion rewriting for external modules - add tests
1 parent eceae2e commit 8c95bcd

File tree

3 files changed

+4
-64
lines changed

3 files changed

+4
-64
lines changed

src/_pytest/pytester.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from collections.abc import Iterable
1313
from collections.abc import Sequence
1414
import contextlib
15+
import dataclasses
1516
from fnmatch import fnmatch
1617
import gc
1718
import importlib
@@ -752,10 +753,8 @@ def chdir(self) -> None:
752753
self._monkeypatch.setattr(
753754
self._request.config,
754755
"invocation_params",
755-
Config.InvocationParams(
756-
args=self._request.config.invocation_params.args,
757-
plugins=self._request.config.invocation_params.plugins,
758-
dir=Path(self._path),
756+
dataclasses.replace(
757+
self._request.config.invocation_params, dir=Path(self._path)
759758
),
760759
)
761760

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import annotations
22

33

4-
def func(x: int, y: int):
5-
assert x > 0
6-
assert y > 0
4+
def func(x: int, y: int) -> int:
75
return 0 if x == y else 1 if x > y else -1

testing/test_assertrewrite.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,63 +2032,6 @@ def test_simple_failure():
20322032
with mock.patch.object(hook, "fnpats", ["*.py"]):
20332033
assert hook.find_spec("file") is None
20342034

2035-
def test_assert_rewrite_correct_for_conftfest(
2036-
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
2037-
) -> None:
2038-
"""Conftest is always rewritten regardless of the root dir"""
2039-
pytester.makeconftest(
2040-
"""
2041-
import pytest
2042-
@pytest.fixture
2043-
def fix(): return 1
2044-
"""
2045-
)
2046-
2047-
rootpath = f"{os.getcwd()}/tests"
2048-
if not os.path.exists(rootpath):
2049-
mkdir(rootpath)
2050-
monkeypatch.setattr(
2051-
pytester._request.config,
2052-
"invocation_params",
2053-
Config.InvocationParams(
2054-
args=(),
2055-
plugins=(),
2056-
dir=Path(rootpath),
2057-
),
2058-
)
2059-
with mock.patch.object(hook, "fnpats", ["*.py"]):
2060-
assert hook.find_spec("conftest") is not None
2061-
2062-
def test_assert_rewrite_correct_for_plugins(
2063-
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch
2064-
) -> None:
2065-
"""Plugins has always been rewritten regardless of the root dir"""
2066-
pkgdir = pytester.mkpydir("plugin")
2067-
pkgdir.joinpath("__init__.py").write_text(
2068-
"import pytest\n"
2069-
"@pytest.fixture\n"
2070-
"def special_asserter():\n"
2071-
" def special_assert(x, y):\n"
2072-
" assert x == y\n"
2073-
" return special_assert\n",
2074-
encoding="utf-8",
2075-
)
2076-
hook.mark_rewrite("plugin")
2077-
rootpath = f"{os.getcwd()}/tests"
2078-
if not os.path.exists(rootpath):
2079-
mkdir(rootpath)
2080-
monkeypatch.setattr(
2081-
pytester._request.config,
2082-
"invocation_params",
2083-
Config.InvocationParams(
2084-
args=(),
2085-
plugins=(),
2086-
dir=Path(rootpath),
2087-
),
2088-
)
2089-
with mock.patch.object(hook, "fnpats", ["*.py"]):
2090-
assert hook.find_spec("plugin") is not None
2091-
20922035
@pytest.mark.skipif(
20932036
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
20942037
)

0 commit comments

Comments
 (0)