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
Remove astor and reproduce the original assertion expression #5512
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
"pluggy>=0.12,<1.0", | ||
"importlib-metadata>=0.12", | ||
"wcwidth", | ||
"astor", | ||
] | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import _pytest._code | ||
import pytest | ||
from _pytest.assertion import util | ||
from _pytest.assertion.rewrite import _get_assertion_exprs | ||
from _pytest.assertion.rewrite import AssertionRewritingHook | ||
from _pytest.assertion.rewrite import PYTEST_TAG | ||
from _pytest.assertion.rewrite import rewrite_asserts | ||
|
@@ -31,7 +32,7 @@ def teardown_module(mod): | |
|
||
def rewrite(src): | ||
tree = ast.parse(src) | ||
rewrite_asserts(tree) | ||
rewrite_asserts(tree, src.encode()) | ||
return tree | ||
|
||
|
||
|
@@ -1292,10 +1293,10 @@ def test_pattern_contains_subdirectories(self, testdir, hook): | |
""" | ||
p = testdir.makepyfile( | ||
**{ | ||
"tests/file.py": """ | ||
def test_simple_failure(): | ||
assert 1 + 1 == 3 | ||
""" | ||
"tests/file.py": """\ | ||
def test_simple_failure(): | ||
assert 1 + 1 == 3 | ||
""" | ||
} | ||
) | ||
testdir.syspathinsert(p.dirpath()) | ||
|
@@ -1315,19 +1316,19 @@ def test_cwd_changed(self, testdir, monkeypatch): | |
|
||
testdir.makepyfile( | ||
**{ | ||
"test_setup_nonexisting_cwd.py": """ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
d = tempfile.mkdtemp() | ||
os.chdir(d) | ||
shutil.rmtree(d) | ||
""", | ||
"test_test.py": """ | ||
def test(): | ||
pass | ||
""", | ||
"test_setup_nonexisting_cwd.py": """\ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
d = tempfile.mkdtemp() | ||
os.chdir(d) | ||
shutil.rmtree(d) | ||
""", | ||
"test_test.py": """\ | ||
def test(): | ||
pass | ||
""", | ||
} | ||
) | ||
result = testdir.runpytest() | ||
|
@@ -1339,23 +1340,22 @@ def test_option_default(self, testdir): | |
config = testdir.parseconfig() | ||
assert config.getini("enable_assertion_pass_hook") is False | ||
|
||
def test_hook_call(self, testdir): | ||
@pytest.fixture | ||
def flag_on(self, testdir): | ||
testdir.makeini("[pytest]\nenable_assertion_pass_hook = True\n") | ||
|
||
@pytest.fixture | ||
def hook_on(self, testdir): | ||
testdir.makeconftest( | ||
""" | ||
"""\ | ||
def pytest_assertion_pass(item, lineno, orig, expl): | ||
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno)) | ||
""" | ||
) | ||
|
||
testdir.makeini( | ||
""" | ||
[pytest] | ||
enable_assertion_pass_hook = True | ||
""" | ||
) | ||
|
||
def test_hook_call(self, testdir, flag_on, hook_on): | ||
testdir.makepyfile( | ||
""" | ||
"""\ | ||
def test_simple(): | ||
a=1 | ||
b=2 | ||
|
@@ -1371,10 +1371,21 @@ def test_fails(): | |
) | ||
result = testdir.runpytest() | ||
result.stdout.fnmatch_lines( | ||
"*Assertion Passed: a + b == c + d (1 + 2) == (3 + 0) at line 7*" | ||
"*Assertion Passed: a+b == c+d (1 + 2) == (3 + 0) at line 7*" | ||
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. Did you see the approach I took in #5511 of using 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. I did yeah, didn't quite feel like expanding the api scope of pytester further but I can take another look at that again 👍 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. I added a two-liner helper function to Anyway just a suggestion really. 👍 |
||
) | ||
|
||
def test_hook_call_with_parens(self, testdir, flag_on, hook_on): | ||
testdir.makepyfile( | ||
"""\ | ||
def f(): return 1 | ||
def test(): | ||
assert f() | ||
""" | ||
) | ||
result = testdir.runpytest() | ||
result.stdout.fnmatch_lines("*Assertion Passed: f() 1") | ||
|
||
def test_hook_not_called_without_hookimpl(self, testdir, monkeypatch): | ||
def test_hook_not_called_without_hookimpl(self, testdir, monkeypatch, flag_on): | ||
"""Assertion pass should not be called (and hence formatting should | ||
not occur) if there is no hook declared for pytest_assertion_pass""" | ||
|
||
|
@@ -1385,15 +1396,8 @@ def raise_on_assertionpass(*_, **__): | |
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass | ||
) | ||
|
||
testdir.makeini( | ||
""" | ||
[pytest] | ||
enable_assertion_pass_hook = True | ||
""" | ||
) | ||
|
||
testdir.makepyfile( | ||
""" | ||
"""\ | ||
def test_simple(): | ||
a=1 | ||
b=2 | ||
|
@@ -1418,21 +1422,14 @@ def raise_on_assertionpass(*_, **__): | |
) | ||
|
||
testdir.makeconftest( | ||
""" | ||
"""\ | ||
def pytest_assertion_pass(item, lineno, orig, expl): | ||
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno)) | ||
""" | ||
) | ||
|
||
testdir.makeini( | ||
""" | ||
[pytest] | ||
enable_assertion_pass_hook = False | ||
""" | ||
) | ||
|
||
testdir.makepyfile( | ||
""" | ||
"""\ | ||
def test_simple(): | ||
a=1 | ||
b=2 | ||
|
@@ -1444,3 +1441,90 @@ def test_simple(): | |
) | ||
result = testdir.runpytest() | ||
result.assert_outcomes(passed=1) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("src", "expected"), | ||
( | ||
# fmt: off | ||
pytest.param(b"", {}, id="trivial"), | ||
pytest.param( | ||
b"def x(): assert 1\n", | ||
{1: "1"}, | ||
id="assert statement not on own line", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert 1\n" | ||
b" assert 1+2\n", | ||
{2: "1", 3: "1+2"}, | ||
id="multiple assertions", | ||
), | ||
pytest.param( | ||
# changes in encoding cause the byte offsets to be different | ||
"# -*- coding: latin1\n" | ||
"def ÀÀÀÀÀ(): assert 1\n".encode("latin1"), | ||
{2: "1"}, | ||
id="latin1 encoded on first line\n", | ||
), | ||
pytest.param( | ||
# using the default utf-8 encoding | ||
"def ÀÀÀÀÀ(): assert 1\n".encode(), | ||
{1: "1"}, | ||
id="utf-8 encoded on first line", | ||
), | ||
pytest.param( | ||
asottile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b"def x():\n" | ||
b" assert (\n" | ||
b" 1 + 2 # comment\n" | ||
b" )\n", | ||
{2: "(\n 1 + 2 # comment\n )"}, | ||
id="multi-line assertion", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert y == [\n" | ||
b" 1, 2, 3\n" | ||
b" ]\n", | ||
{2: "y == [\n 1, 2, 3\n ]"}, | ||
id="multi line assert with list continuation", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert 1 + \\\n" | ||
b" 2\n", | ||
{2: "1 + \\\n 2"}, | ||
id="backslash continuation", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert x, y\n", | ||
{2: "x"}, | ||
id="assertion with message", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert (\n" | ||
b" f(1, 2, 3)\n" | ||
b" ), 'f did not work!'\n", | ||
{2: "(\n f(1, 2, 3)\n )"}, | ||
id="assertion with message, test spanning multiple lines", | ||
), | ||
pytest.param( | ||
b"def x():\n" | ||
b" assert \\\n" | ||
b" x\\\n" | ||
b" , 'failure message'\n", | ||
{2: "x"}, | ||
id="escaped newlines plus message", | ||
), | ||
pytest.param( | ||
b"def x(): assert 5", | ||
{1: "5"}, | ||
id="no newline at end of file", | ||
), | ||
# fmt: on | ||
), | ||
) | ||
def test_get_assertion_exprs(src, expected): | ||
assert _get_assertion_exprs(src) == expected |
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.
Not that this is a blocker, but this will end up breaking https://pypi.org/project/pytest-ast-back-to-python.
cc @tomviner
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.
ah, I can restore the api and just re-read the source in the visitor using
fn
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.
Just to update here, pytest-ast-back-to-python has now been fixed.
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.
Awesome Tom, thanks for the heads up!