-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Disable assertion rewriting external modules #13421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tusenka
wants to merge
16
commits into
pytest-dev:main
Choose a base branch
from
Tusenka:disable_assertion_rewriting_external_modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fb29583
13403: Disable assertion rewriting for external modules
Tusenka 14b0382
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 10a2709
13403: Disable assertion rewriting for external modules - move root p…
Tusenka fc0c87c
13403: Disable assertion rewriting for external modules
Tusenka 3918365
13403: Disable assertion rewriting for external modules - refactor
Tusenka 62fe69d
13403: Disable assertion rewriting for external modules - refactor
Tusenka 8671103
13403: Disable assertion rewriting for external modules - refactor
Tusenka 3beb48e
13403: Disable assertion rewriting for external modules - add tests
Tusenka 0015d0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d5eb2a6
13403: Disable assertion rewriting for external modules - add test fo…
Tusenka 392a01d
13403: Disable assertion rewriting for external modules - add test fo…
Tusenka ccb0dca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e0bbaa3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dbc5a59
13403: Disable assertion rewriting for external modules - add test fo…
Tusenka 23e0d70
13403: Disable assertion rewriting for external modules - eliminate o…
Tusenka 559a5c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Disable assertion rewriting of external modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import inspect | ||
import marshal | ||
import os | ||
from os import mkdir | ||
from pathlib import Path | ||
import py_compile | ||
import re | ||
|
@@ -35,6 +36,7 @@ | |
from _pytest.assertion.rewrite import rewrite_asserts | ||
from _pytest.config import Config | ||
from _pytest.config import ExitCode | ||
from _pytest.monkeypatch import MonkeyPatch | ||
from _pytest.pathlib import make_numbered_dir | ||
from _pytest.pytester import Pytester | ||
import pytest | ||
|
@@ -370,6 +372,7 @@ def test_rewrites_plugin_as_a_package(self, pytester: Pytester) -> None: | |
pytester.makeconftest('pytest_plugins = ["plugin"]') | ||
pytester.makepyfile("def test(special_asserter): special_asserter(1, 2)\n") | ||
result = pytester.runpytest() | ||
|
||
result.stdout.fnmatch_lines(["*assert 1 == 2*"]) | ||
|
||
def test_honors_pep_235(self, pytester: Pytester, monkeypatch) -> None: | ||
|
@@ -1294,6 +1297,36 @@ def test_meta_path(): | |
) | ||
assert pytester.runpytest().ret == 0 | ||
|
||
def test_rootpath_base(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: | ||
""" | ||
Base cases for get rootpath from AssertionState | ||
""" | ||
from _pytest.assertion import AssertionState | ||
|
||
config = pytester.parseconfig() | ||
state = AssertionState(config, "rewrite") | ||
assert state.rootpath == str(config.invocation_params.dir) | ||
new_rootpath = str(pytester.path / "test") | ||
if not os.path.exists(new_rootpath): | ||
os.mkdir(new_rootpath) | ||
monkeypatch.setattr( | ||
config, | ||
"invocation_params", | ||
Config.InvocationParams( | ||
args=(), | ||
plugins=(), | ||
dir=Path(new_rootpath), | ||
), | ||
) | ||
state = AssertionState(config, "rewrite") | ||
assert state.rootpath == new_rootpath | ||
|
||
@pytest.mark.skipif( | ||
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" | ||
) | ||
@pytest.mark.skipif( | ||
sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris" | ||
) | ||
def test_write_pyc(self, pytester: Pytester, tmp_path) -> None: | ||
from _pytest.assertion import AssertionState | ||
from _pytest.assertion.rewrite import _write_pyc | ||
|
@@ -1971,6 +2004,99 @@ def test_simple_failure(): | |
assert hook.find_spec("file") is not None | ||
assert self.find_spec_calls == ["file"] | ||
|
||
def test_assert_rewrites_only_rootpath( | ||
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch | ||
) -> None: | ||
""" | ||
If test files contained outside the rootpath, then skip them | ||
""" | ||
pytester.makepyfile( | ||
**{ | ||
"file.py": """\ | ||
def test_simple_failure(): | ||
assert 1 + 1 == 3 | ||
""" | ||
} | ||
) | ||
with mock.patch.object(hook, "fnpats", ["*.py"]): | ||
assert hook.find_spec("file") is not None | ||
|
||
rootpath = f"{os.getcwd()}/tests" | ||
if not os.path.exists(rootpath): | ||
mkdir(rootpath) | ||
monkeypatch.setattr( | ||
pytester._request.config, | ||
"invocation_params", | ||
Config.InvocationParams( | ||
args=(), | ||
plugins=(), | ||
dir=Path(rootpath), | ||
), | ||
) | ||
with mock.patch.object(hook, "fnpats", ["*.py"]): | ||
assert hook.find_spec("file") is None | ||
|
||
def test_assert_rewrite_correct_for_conftfest( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mamy name it in another way - test_assert_rewrite_for_conftfest |
||
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch | ||
) -> None: | ||
""" | ||
Conftest is always rewritten regardless of the root dir | ||
""" | ||
pytester.makeconftest( | ||
""" | ||
import pytest | ||
@pytest.fixture | ||
def fix(): return 1 | ||
""" | ||
) | ||
|
||
rootpath = f"{os.getcwd()}/tests" | ||
if not os.path.exists(rootpath): | ||
mkdir(rootpath) | ||
monkeypatch.setattr( | ||
pytester._request.config, | ||
"invocation_params", | ||
Config.InvocationParams( | ||
args=(), | ||
plugins=(), | ||
dir=Path(rootpath), | ||
), | ||
) | ||
with mock.patch.object(hook, "fnpats", ["*.py"]): | ||
assert hook.find_spec("conftest") is not None | ||
|
||
def test_assert_rewrite_correct_for_plugins( | ||
self, pytester: Pytester, hook: AssertionRewritingHook, monkeypatch | ||
) -> None: | ||
""" | ||
Plugins has always been rewritten regardless of the root dir | ||
""" | ||
pkgdir = pytester.mkpydir("plugin") | ||
pkgdir.joinpath("__init__.py").write_text( | ||
"import pytest\n" | ||
"@pytest.fixture\n" | ||
"def special_asserter():\n" | ||
" def special_assert(x, y):\n" | ||
" assert x == y\n" | ||
" return special_assert\n", | ||
encoding="utf-8", | ||
) | ||
hook.mark_rewrite("plugin") | ||
rootpath = f"{os.getcwd()}/tests" | ||
if not os.path.exists(rootpath): | ||
mkdir(rootpath) | ||
monkeypatch.setattr( | ||
pytester._request.config, | ||
"invocation_params", | ||
Config.InvocationParams( | ||
args=(), | ||
plugins=(), | ||
dir=Path(rootpath), | ||
), | ||
) | ||
with mock.patch.object(hook, "fnpats", ["*.py"]): | ||
assert hook.find_spec("plugin") is not None | ||
|
||
@pytest.mark.skipif( | ||
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows" | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must mot use getwd instead use the invoction params
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then we cannot change it on runtime as far as invocation param for pytester changes rootpath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for the type - this code shoud use the rootdir or the invocationdir from the invocation params of the config
see https://docs.pytest.org/en/stable/reference/reference.html#pytest.Config.invocation_params as well as the config rootdir
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My error, I mean as far as Invocation Param is frozen - it can't be changed on runtime. Pytester starts after the config has been loaded. So to change the rootpath for pytester I need to rewrite the rootpath in someway. I could mock all invocation params or I could use getcwd alongside with config rootdir for the testing purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The complete object can be replaced with a changed one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. would be done)