2019-05-28 07:31:52 +08:00
|
|
|
import ast
|
2019-08-02 07:55:30 +08:00
|
|
|
import errno
|
2016-11-21 00:56:15 +08:00
|
|
|
import glob
|
2019-06-23 06:02:32 +08:00
|
|
|
import importlib
|
2012-07-07 22:09:53 +08:00
|
|
|
import os
|
2016-11-21 00:56:15 +08:00
|
|
|
import py_compile
|
2013-05-29 06:11:12 +08:00
|
|
|
import stat
|
2011-05-19 04:31:10 +08:00
|
|
|
import sys
|
2017-12-27 11:47:26 +08:00
|
|
|
import textwrap
|
2011-07-26 10:40:38 +08:00
|
|
|
import zipfile
|
2019-08-02 07:55:30 +08:00
|
|
|
from functools import partial
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import Mapping
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Set
|
|
|
|
|
|
|
|
import py
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2011-05-27 01:01:34 +08:00
|
|
|
from _pytest.assertion import util
|
2019-06-28 10:11:20 +08:00
|
|
|
from _pytest.assertion.rewrite import _get_assertion_exprs
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.assertion.rewrite import AssertionRewritingHook
|
2019-09-19 07:29:24 +08:00
|
|
|
from _pytest.assertion.rewrite import get_cache_dir
|
|
|
|
from _pytest.assertion.rewrite import PYC_TAIL
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.assertion.rewrite import PYTEST_TAG
|
|
|
|
from _pytest.assertion.rewrite import rewrite_asserts
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-05-03 01:54:23 +08:00
|
|
|
from _pytest.pathlib import make_numbered_dir
|
2019-11-15 10:53:05 +08:00
|
|
|
from _pytest.pathlib import Path
|
2020-02-25 10:47:35 +08:00
|
|
|
from _pytest.pytester import Testdir
|
2011-05-19 04:31:10 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def rewrite(src: str) -> ast.Module:
|
2011-05-20 05:53:13 +08:00
|
|
|
tree = ast.parse(src)
|
2019-06-28 10:11:20 +08:00
|
|
|
rewrite_asserts(tree, src.encode())
|
2011-05-20 05:53:13 +08:00
|
|
|
return tree
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def getmsg(
|
|
|
|
f, extra_ns: Optional[Mapping[str, object]] = None, *, must_pass: bool = False
|
|
|
|
) -> Optional[str]:
|
2011-05-19 04:31:10 +08:00
|
|
|
"""Rewrite the assertions in f, run it, and get the failure message."""
|
2018-05-23 22:48:46 +08:00
|
|
|
src = "\n".join(_pytest._code.Code(f).source().lines)
|
2011-05-20 05:53:13 +08:00
|
|
|
mod = rewrite(src)
|
2011-05-19 04:31:10 +08:00
|
|
|
code = compile(mod, "<test>", "exec")
|
2020-05-01 19:40:17 +08:00
|
|
|
ns = {} # type: Dict[str, object]
|
2011-05-19 04:31:10 +08:00
|
|
|
if extra_ns is not None:
|
|
|
|
ns.update(extra_ns)
|
2019-05-07 14:07:39 +08:00
|
|
|
exec(code, ns)
|
2011-05-19 04:31:10 +08:00
|
|
|
func = ns[f.__name__]
|
|
|
|
try:
|
2020-05-01 19:40:17 +08:00
|
|
|
func() # type: ignore[operator] # noqa: F821
|
2011-05-19 04:31:10 +08:00
|
|
|
except AssertionError:
|
|
|
|
if must_pass:
|
|
|
|
pytest.fail("shouldn't have raised")
|
2019-06-03 06:32:00 +08:00
|
|
|
s = str(sys.exc_info()[1])
|
2011-05-19 04:31:10 +08:00
|
|
|
if not s.startswith("assert"):
|
|
|
|
return "AssertionError: " + s
|
|
|
|
return s
|
|
|
|
else:
|
|
|
|
if not must_pass:
|
|
|
|
pytest.fail("function didn't raise at all")
|
2020-05-01 19:40:17 +08:00
|
|
|
return None
|
2011-05-19 04:31:10 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestAssertionRewrite:
|
2011-05-20 05:53:13 +08:00
|
|
|
def test_place_initial_imports(self):
|
2011-05-20 22:44:36 +08:00
|
|
|
s = """'Doc string'\nother = stuff"""
|
2011-05-20 05:53:13 +08:00
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert isinstance(m.body[0], ast.Expr)
|
|
|
|
for imp in m.body[1:3]:
|
2011-05-20 05:53:13 +08:00
|
|
|
assert isinstance(imp, ast.Import)
|
2011-05-25 06:21:58 +08:00
|
|
|
assert imp.lineno == 2
|
|
|
|
assert imp.col_offset == 0
|
2019-02-06 04:48:18 +08:00
|
|
|
assert isinstance(m.body[3], ast.Assign)
|
2018-07-08 23:35:53 +08:00
|
|
|
s = """from __future__ import division\nother_stuff"""
|
2011-05-20 05:53:13 +08:00
|
|
|
m = rewrite(s)
|
|
|
|
assert isinstance(m.body[0], ast.ImportFrom)
|
2011-05-27 01:01:34 +08:00
|
|
|
for imp in m.body[1:3]:
|
2011-05-20 05:53:13 +08:00
|
|
|
assert isinstance(imp, ast.Import)
|
2011-05-25 06:21:58 +08:00
|
|
|
assert imp.lineno == 2
|
|
|
|
assert imp.col_offset == 0
|
2011-05-27 01:01:34 +08:00
|
|
|
assert isinstance(m.body[3], ast.Expr)
|
2018-07-08 23:35:53 +08:00
|
|
|
s = """'doc string'\nfrom __future__ import division"""
|
2017-11-04 02:37:18 +08:00
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert isinstance(m.body[0], ast.Expr)
|
|
|
|
assert isinstance(m.body[1], ast.ImportFrom)
|
|
|
|
for imp in m.body[2:4]:
|
2017-11-04 02:37:18 +08:00
|
|
|
assert isinstance(imp, ast.Import)
|
|
|
|
assert imp.lineno == 2
|
|
|
|
assert imp.col_offset == 0
|
2018-07-08 23:35:53 +08:00
|
|
|
s = """'doc string'\nfrom __future__ import division\nother"""
|
2011-05-20 05:53:13 +08:00
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert isinstance(m.body[0], ast.Expr)
|
|
|
|
assert isinstance(m.body[1], ast.ImportFrom)
|
|
|
|
for imp in m.body[2:4]:
|
2011-05-20 05:53:13 +08:00
|
|
|
assert isinstance(imp, ast.Import)
|
2011-05-25 06:21:58 +08:00
|
|
|
assert imp.lineno == 3
|
|
|
|
assert imp.col_offset == 0
|
2019-02-06 04:48:18 +08:00
|
|
|
assert isinstance(m.body[4], ast.Expr)
|
2011-06-28 23:39:11 +08:00
|
|
|
s = """from . import relative\nother_stuff"""
|
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
for imp in m.body[:2]:
|
2011-06-28 23:39:11 +08:00
|
|
|
assert isinstance(imp, ast.Import)
|
|
|
|
assert imp.lineno == 1
|
|
|
|
assert imp.col_offset == 0
|
|
|
|
assert isinstance(m.body[3], ast.Expr)
|
2011-05-20 05:53:13 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_dont_rewrite(self) -> None:
|
2011-05-25 06:30:35 +08:00
|
|
|
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
|
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert len(m.body) == 2
|
2020-05-01 19:40:17 +08:00
|
|
|
assert isinstance(m.body[1], ast.Assert)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert m.body[1].msg is None
|
2011-05-25 06:30:35 +08:00
|
|
|
|
2017-12-13 09:39:10 +08:00
|
|
|
def test_dont_rewrite_plugin(self, testdir):
|
|
|
|
contents = {
|
|
|
|
"conftest.py": "pytest_plugins = 'plugin'; import plugin",
|
|
|
|
"plugin.py": "'PYTEST_DONT_REWRITE'",
|
|
|
|
"test_foo.py": "def test_foo(): pass",
|
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
|
|
|
result = testdir.runpytest_subprocess()
|
2019-10-27 23:02:37 +08:00
|
|
|
assert "warning" not in "".join(result.outlines)
|
2017-12-13 09:39:10 +08:00
|
|
|
|
2019-06-23 06:02:32 +08:00
|
|
|
def test_rewrites_plugin_as_a_package(self, testdir):
|
|
|
|
pkgdir = testdir.mkpydir("plugin")
|
|
|
|
pkgdir.join("__init__.py").write(
|
|
|
|
"import pytest\n"
|
|
|
|
"@pytest.fixture\n"
|
|
|
|
"def special_asserter():\n"
|
|
|
|
" def special_assert(x, y):\n"
|
|
|
|
" assert x == y\n"
|
|
|
|
" return special_assert\n"
|
|
|
|
)
|
|
|
|
testdir.makeconftest('pytest_plugins = ["plugin"]')
|
|
|
|
testdir.makepyfile("def test(special_asserter): special_asserter(1, 2)\n")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*assert 1 == 2*"])
|
|
|
|
|
|
|
|
def test_honors_pep_235(self, testdir, monkeypatch):
|
|
|
|
# note: couldn't make it fail on macos with a single `sys.path` entry
|
|
|
|
# note: these modules are named `test_*` to trigger rewriting
|
|
|
|
testdir.tmpdir.join("test_y.py").write("x = 1")
|
|
|
|
xdir = testdir.tmpdir.join("x").ensure_dir()
|
|
|
|
xdir.join("test_Y").ensure_dir().join("__init__.py").write("x = 2")
|
|
|
|
testdir.makepyfile(
|
|
|
|
"import test_y\n"
|
|
|
|
"import test_Y\n"
|
|
|
|
"def test():\n"
|
|
|
|
" assert test_y.x == 1\n"
|
|
|
|
" assert test_Y.x == 2\n"
|
|
|
|
)
|
|
|
|
monkeypatch.syspath_prepend(xdir)
|
|
|
|
testdir.runpytest().assert_outcomes(passed=1)
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_name(self, request) -> None:
|
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = False
|
|
|
|
assert f
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f3() -> None:
|
|
|
|
assert a_global # type: ignore[name-defined] # noqa
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f3, {"a_global": False}) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f4() -> None:
|
|
|
|
assert sys == 42 # type: ignore[comparison-overlap] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
verbose = request.config.getoption("verbose")
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f4, {"sys": sys})
|
2019-03-18 10:05:33 +08:00
|
|
|
if verbose > 0:
|
|
|
|
assert msg == (
|
|
|
|
"assert <module 'sys' (built-in)> == 42\n"
|
2020-02-04 21:38:18 +08:00
|
|
|
" +<module 'sys' (built-in)>\n"
|
|
|
|
" -42"
|
2019-03-18 10:05:33 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert msg == "assert sys == 42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f5() -> None:
|
|
|
|
assert cls == 42 # type: ignore[name-defined] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class X:
|
2013-01-11 01:59:08 +08:00
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f5, {"cls": X})
|
|
|
|
assert msg is not None
|
|
|
|
lines = msg.splitlines()
|
2019-10-10 07:12:32 +08:00
|
|
|
if verbose > 1:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert lines == [
|
|
|
|
"assert {!r} == 42".format(X),
|
|
|
|
" +{!r}".format(X),
|
|
|
|
" -42",
|
|
|
|
]
|
2019-10-10 07:12:32 +08:00
|
|
|
elif verbose > 0:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert lines == [
|
2019-05-28 07:31:52 +08:00
|
|
|
"assert <class 'test_...e.<locals>.X'> == 42",
|
2020-02-04 21:38:18 +08:00
|
|
|
" +{!r}".format(X),
|
|
|
|
" -42",
|
2019-05-28 07:31:52 +08:00
|
|
|
]
|
2019-03-18 10:05:33 +08:00
|
|
|
else:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert lines == ["assert cls == 42"]
|
2019-03-18 10:05:33 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_assertrepr_compare_same_width(self, request) -> None:
|
2019-08-12 09:41:14 +08:00
|
|
|
"""Should use same width/truncation with same initial width."""
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f() -> None:
|
2019-08-12 09:41:14 +08:00
|
|
|
assert "1234567890" * 5 + "A" == "1234567890" * 5 + "B"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f)
|
|
|
|
assert msg is not None
|
|
|
|
line = msg.splitlines()[0]
|
2019-10-10 07:12:32 +08:00
|
|
|
if request.config.getoption("verbose") > 1:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert line == (
|
2019-10-10 07:12:32 +08:00
|
|
|
"assert '12345678901234567890123456789012345678901234567890A' "
|
|
|
|
"== '12345678901234567890123456789012345678901234567890B'"
|
|
|
|
)
|
|
|
|
else:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert line == (
|
2019-10-10 07:12:32 +08:00
|
|
|
"assert '123456789012...901234567890A' "
|
|
|
|
"== '123456789012...901234567890B'"
|
|
|
|
)
|
2019-08-12 09:41:14 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_dont_rewrite_if_hasattr_fails(self, request) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class Y:
|
2019-01-11 03:12:50 +08:00
|
|
|
""" A class whos getattr fails, but not with `AttributeError` """
|
|
|
|
|
|
|
|
def __getattr__(self, attribute_name):
|
|
|
|
raise KeyError()
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def __repr__(self) -> str:
|
2019-01-11 03:12:50 +08:00
|
|
|
return "Y"
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def __init__(self) -> None:
|
2019-01-11 03:12:50 +08:00
|
|
|
self.foo = 3
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f() -> None:
|
|
|
|
assert cls().foo == 2 # type: ignore[name-defined] # noqa: F821
|
2019-01-11 03:12:50 +08:00
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
# XXX: looks like the "where" should also be there in verbose mode?!
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f, {"cls": Y})
|
|
|
|
assert msg is not None
|
|
|
|
lines = msg.splitlines()
|
2019-03-18 10:05:33 +08:00
|
|
|
if request.config.getoption("verbose") > 0:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert lines == ["assert 3 == 2", " +3", " -2"]
|
2019-03-18 10:05:33 +08:00
|
|
|
else:
|
2020-05-01 19:40:17 +08:00
|
|
|
assert lines == [
|
2019-03-18 10:05:33 +08:00
|
|
|
"assert 3 == 2",
|
|
|
|
" + where 3 = Y.foo",
|
|
|
|
" + where Y = cls()",
|
|
|
|
]
|
2019-01-11 03:12:50 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def test_assert_already_has_message(self):
|
|
|
|
def f():
|
|
|
|
assert False, "something bad!"
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-08-24 00:14:25 +08:00
|
|
|
assert getmsg(f) == "AssertionError: something bad!\nassert False"
|
|
|
|
|
|
|
|
def test_assertion_message(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, "The failure message"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*AssertionError*The failure message*", "*assert 1 == 2*"]
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
|
|
|
|
def test_assertion_message_multiline(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, "A multiline\\nfailure message"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*AssertionError*A multiline*", "*failure message*", "*assert 1 == 2*"]
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
|
|
|
|
def test_assertion_message_tuple(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, (1, 2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*AssertionError*%s*" % repr((1, 2)), "*assert 1 == 2*"]
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
|
|
|
|
def test_assertion_message_expr(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, 1 + 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-08-24 00:14:25 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*AssertionError*3*", "*assert 1 == 2*"])
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2014-10-07 07:01:21 +08:00
|
|
|
def test_assertion_message_escape(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-10-07 07:01:21 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, 'To be escaped: %'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-10-07 07:01:21 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*AssertionError: To be escaped: %", "*assert 1 == 2"]
|
|
|
|
)
|
2014-10-07 07:01:21 +08:00
|
|
|
|
2018-08-02 06:09:25 +08:00
|
|
|
def test_assertion_messages_bytes(self, testdir):
|
|
|
|
testdir.makepyfile("def test_bytes_assertion():\n assert False, b'ohai!'\n")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines(["*AssertionError: b'ohai!'", "*assert False"])
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_boolop(self) -> None:
|
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = g = False
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert (False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = True
|
|
|
|
g = False
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert (True and False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f3() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = False
|
|
|
|
g = True
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f3) == "assert (False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f4() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = g = False
|
|
|
|
assert f or g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f4) == "assert (False or False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f5() -> None:
|
2011-06-29 09:21:22 +08:00
|
|
|
f = g = False
|
|
|
|
assert not f and not g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f5, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def x() -> bool:
|
2011-08-30 22:34:21 +08:00
|
|
|
return False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f6() -> None:
|
2011-08-30 22:34:21 +08:00
|
|
|
assert x() and x()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f6, {"x": x})
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert (False)
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = x()"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f7() -> None:
|
2011-08-30 22:34:21 +08:00
|
|
|
assert False or x()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f7, {"x": x})
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert (False or False)
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = x()"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f8() -> None:
|
2011-10-15 04:26:13 +08:00
|
|
|
assert 1 in {} and 2 in {}
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f8) == "assert (1 in {})"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f9() -> None:
|
2011-10-15 04:26:13 +08:00
|
|
|
x = 1
|
|
|
|
y = 2
|
2017-07-17 07:25:08 +08:00
|
|
|
assert x in {1: None} and y in {}
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f9) == "assert (1 in {1: None} and 2 in {})"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f10() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
f = True
|
|
|
|
g = False
|
|
|
|
assert f or g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f10, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f11() -> None:
|
2011-07-11 17:57:47 +08:00
|
|
|
f = g = h = lambda: True
|
|
|
|
assert f() and g() and h()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f11, must_pass=True)
|
2011-07-11 17:57:47 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_short_circuit_evaluation(self) -> None:
|
|
|
|
def f1() -> None:
|
|
|
|
assert True or explode # type: ignore[name-defined] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f1, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-06-29 09:21:22 +08:00
|
|
|
x = 1
|
|
|
|
assert x == 1 or x == 2
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f2, must_pass=True)
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_unary_op(self) -> None:
|
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
x = True
|
|
|
|
assert not x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert not True"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
x = 0
|
|
|
|
assert ~x + 1
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert (~0 + 1)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f3() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
x = 3
|
|
|
|
assert -x + x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f3) == "assert (-3 + 3)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f4() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
x = 0
|
|
|
|
assert +x + x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f4) == "assert (+0 + 0)"
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_binary_op(self) -> None:
|
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
x = 1
|
|
|
|
y = -1
|
|
|
|
assert x + y
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert (1 + -1)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2012-05-04 01:49:30 +08:00
|
|
|
assert not 5 % 4
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert not (5 % 4)"
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_boolop_percent(self) -> None:
|
|
|
|
def f1() -> None:
|
2014-10-27 16:31:33 +08:00
|
|
|
assert 3 % 2 and False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert ((3 % 2) and False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2014-10-27 16:31:33 +08:00
|
|
|
assert False or 4 % 2
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert (False or (4 % 2))"
|
2014-10-13 16:26:18 +08:00
|
|
|
|
2016-02-06 06:30:07 +08:00
|
|
|
def test_at_operator_issue1290(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class Matrix(object):
|
2016-02-06 06:30:07 +08:00
|
|
|
def __init__(self, num):
|
|
|
|
self.num = num
|
|
|
|
def __matmul__(self, other):
|
|
|
|
return self.num * other.num
|
|
|
|
|
|
|
|
def test_multmat_operator():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert Matrix(2) @ Matrix(3) == 6"""
|
|
|
|
)
|
2016-02-06 06:30:07 +08:00
|
|
|
testdir.runpytest().assert_outcomes(passed=1)
|
|
|
|
|
2018-11-18 02:41:44 +08:00
|
|
|
def test_starred_with_side_effect(self, testdir):
|
|
|
|
"""See #4412"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""\
|
|
|
|
def test():
|
|
|
|
f = lambda x: x
|
|
|
|
x = iter([1, 2, 3])
|
|
|
|
assert 2 * next(x) == f(*[next(x)])
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.runpytest().assert_outcomes(passed=1)
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_call(self) -> None:
|
|
|
|
def g(a=42, *args, **kwargs) -> bool:
|
2011-05-19 04:31:10 +08:00
|
|
|
return False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
ns = {"g": g}
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert g()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f1, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g()"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert g(1)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f2, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(1)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f3() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert g(1, 2)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f3, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(1, 2)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f4() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert g(1, g=42)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f4, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(1, g=42)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f5() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert g(1, 3, g=23)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f5, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(1, 3, g=23)"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f6() -> None:
|
2011-07-15 00:46:32 +08:00
|
|
|
seq = [1, 2, 3]
|
|
|
|
assert g(*seq)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f6, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(*[1, 2, 3])"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f7() -> None:
|
2011-07-15 00:45:42 +08:00
|
|
|
x = "a"
|
2017-07-17 07:25:08 +08:00
|
|
|
assert g(**{x: 2})
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f7, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = g(**{'a': 2})"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_attribute(self) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class X:
|
2011-05-19 04:31:10 +08:00
|
|
|
g = 3
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
ns = {"x": X}
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f1() -> None:
|
|
|
|
assert not x.g # type: ignore[name-defined] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f1, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert not 3
|
2011-05-19 04:31:10 +08:00
|
|
|
+ where 3 = x.g"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
|
|
|
x.a = False # type: ignore[name-defined] # noqa: F821
|
|
|
|
assert x.a # type: ignore[name-defined] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f2, ns)
|
2018-05-23 22:48:46 +08:00
|
|
|
== """assert False
|
2016-06-24 18:44:06 +08:00
|
|
|
+ where False = x.a"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_comparisons(self) -> None:
|
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
a, b = range(2)
|
|
|
|
assert b < a
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == """assert 1 < 0"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
a, b, c = range(3)
|
|
|
|
assert a > b > c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == """assert 0 > 1"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f3() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b > c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f3) == """assert 1 > 2"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f4() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b <= c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f4, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f5() -> None:
|
2011-05-20 07:56:48 +08:00
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b
|
|
|
|
assert b < c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
getmsg(f5, must_pass=True)
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
def test_len(self, request):
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
2017-11-04 23:17:20 +08:00
|
|
|
values = list(range(10))
|
|
|
|
assert len(values) == 11
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
msg = getmsg(f)
|
|
|
|
if request.config.getoption("verbose") > 0:
|
2020-02-04 21:38:18 +08:00
|
|
|
assert msg == "assert 10 == 11\n +10\n -11"
|
2019-03-18 10:05:33 +08:00
|
|
|
else:
|
|
|
|
assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])"
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_custom_reprcompare(self, monkeypatch) -> None:
|
|
|
|
def my_reprcompare1(op, left, right) -> str:
|
2011-05-19 04:31:10 +08:00
|
|
|
return "42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
monkeypatch.setattr(util, "_reprcompare", my_reprcompare1)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f1() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert 42 < 3
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f1) == "assert 42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def my_reprcompare2(op, left, right) -> str:
|
2018-08-23 09:30:42 +08:00
|
|
|
return "{} {} {}".format(left, op, right)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
monkeypatch.setattr(util, "_reprcompare", my_reprcompare2)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f2() -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
assert 1 < 3 < 5 <= 4 < 7
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert getmsg(f2) == "assert 5 <= 4"
|
2011-05-25 07:15:08 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_assert_raising__bool__in_comparison(self) -> None:
|
|
|
|
def f() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2020-05-01 16:39:38 +08:00
|
|
|
def __bool__(self):
|
2011-05-25 07:15:08 +08:00
|
|
|
raise ValueError(42)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-25 07:15:08 +08:00
|
|
|
def __lt__(self, other):
|
|
|
|
return A()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-25 07:15:08 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<MY42 object>"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def myany(x) -> bool:
|
2011-05-25 07:15:08 +08:00
|
|
|
return False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-25 07:15:08 +08:00
|
|
|
assert myany(A() < 0)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f)
|
|
|
|
assert msg is not None
|
|
|
|
assert "<MY42 object> < 0" in msg
|
2011-07-13 06:09:14 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_formatchar(self) -> None:
|
|
|
|
def f() -> None:
|
|
|
|
assert "%test" == "test" # type: ignore[comparison-overlap] # noqa: F821
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f)
|
|
|
|
assert msg is not None
|
|
|
|
assert msg.startswith("assert '%test' == 'test'")
|
2011-07-20 10:42:00 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_custom_repr(self, request) -> None:
|
|
|
|
def f() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class Foo:
|
2014-08-19 02:07:38 +08:00
|
|
|
a = 1
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "\n{ \n~ \n}"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-08-19 02:07:38 +08:00
|
|
|
f = Foo()
|
|
|
|
assert 0 == f.a
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f)
|
|
|
|
assert msg is not None
|
|
|
|
lines = util._format_lines([msg])
|
2019-03-18 10:05:33 +08:00
|
|
|
if request.config.getoption("verbose") > 0:
|
2020-02-04 21:38:18 +08:00
|
|
|
assert lines == ["assert 0 == 1\n +0\n -1"]
|
2019-03-18 10:05:33 +08:00
|
|
|
else:
|
|
|
|
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]
|
2014-08-19 02:07:38 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_custom_repr_non_ascii(self) -> None:
|
|
|
|
def f() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
|
|
|
name = "ä"
|
2018-09-20 00:44:26 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name.encode("UTF-8") # only legal in python2
|
|
|
|
|
|
|
|
a = A()
|
|
|
|
assert not a.name
|
|
|
|
|
|
|
|
msg = getmsg(f)
|
2020-05-01 19:40:17 +08:00
|
|
|
assert msg is not None
|
2018-09-20 00:44:26 +08:00
|
|
|
assert "UnicodeDecodeError" not in msg
|
|
|
|
assert "UnicodeEncodeError" not in msg
|
|
|
|
|
2011-07-13 06:09:14 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestRewriteOnImport:
|
2011-07-26 10:40:38 +08:00
|
|
|
def test_pycache_is_a_file(self, testdir):
|
|
|
|
testdir.tmpdir.join("__pycache__").write("Hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-06-08 05:30:10 +08:00
|
|
|
def test_rewritten():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "@py_builtins" in globals()"""
|
|
|
|
)
|
2011-07-26 10:40:38 +08:00
|
|
|
assert testdir.runpytest().ret == 0
|
|
|
|
|
2013-05-29 06:11:12 +08:00
|
|
|
def test_pycache_is_readonly(self, testdir):
|
|
|
|
cache = testdir.tmpdir.mkdir("__pycache__")
|
|
|
|
old_mode = cache.stat().mode
|
|
|
|
cache.chmod(old_mode ^ stat.S_IWRITE)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-06-08 05:30:10 +08:00
|
|
|
def test_rewritten():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "@py_builtins" in globals()"""
|
|
|
|
)
|
2013-05-29 06:11:12 +08:00
|
|
|
try:
|
|
|
|
assert testdir.runpytest().ret == 0
|
|
|
|
finally:
|
|
|
|
cache.chmod(old_mode)
|
|
|
|
|
2011-07-26 10:40:38 +08:00
|
|
|
def test_zipfile(self, testdir):
|
|
|
|
z = testdir.tmpdir.join("myzip.zip")
|
|
|
|
z_fn = str(z)
|
|
|
|
f = zipfile.ZipFile(z_fn, "w")
|
|
|
|
try:
|
|
|
|
f.writestr("test_gum/__init__.py", "")
|
|
|
|
f.writestr("test_gum/test_lizard.py", "")
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
z.chmod(256)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-06-08 05:30:10 +08:00
|
|
|
import sys
|
|
|
|
sys.path.append(%r)
|
2018-05-23 22:48:46 +08:00
|
|
|
import test_gum.test_lizard"""
|
|
|
|
% (z_fn,)
|
|
|
|
)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert testdir.runpytest().ret == ExitCode.NO_TESTS_COLLECTED
|
2011-07-26 10:40:38 +08:00
|
|
|
|
2011-07-13 06:09:14 +08:00
|
|
|
def test_readonly(self, testdir):
|
|
|
|
sub = testdir.mkdir("testing")
|
2011-07-20 11:56:34 +08:00
|
|
|
sub.join("test_readonly.py").write(
|
2018-08-23 10:21:00 +08:00
|
|
|
b"""
|
2011-07-13 06:09:14 +08:00
|
|
|
def test_rewritten():
|
2011-07-20 11:41:58 +08:00
|
|
|
assert "@py_builtins" in globals()
|
2018-08-23 10:21:00 +08:00
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"wb",
|
|
|
|
)
|
2013-05-29 06:11:12 +08:00
|
|
|
old_mode = sub.stat().mode
|
2011-07-13 06:09:14 +08:00
|
|
|
sub.chmod(320)
|
2013-05-29 06:11:12 +08:00
|
|
|
try:
|
|
|
|
assert testdir.runpytest().ret == 0
|
|
|
|
finally:
|
|
|
|
sub.chmod(old_mode)
|
2011-07-14 02:33:54 +08:00
|
|
|
|
|
|
|
def test_dont_write_bytecode(self, testdir, monkeypatch):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-06-08 05:30:10 +08:00
|
|
|
import os
|
|
|
|
def test_no_bytecode():
|
|
|
|
assert "__pycache__" in __cached__
|
|
|
|
assert not os.path.exists(__cached__)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert not os.path.exists(os.path.dirname(__cached__))"""
|
|
|
|
)
|
2011-07-14 02:33:54 +08:00
|
|
|
monkeypatch.setenv("PYTHONDONTWRITEBYTECODE", "1")
|
2015-04-28 17:54:46 +08:00
|
|
|
assert testdir.runpytest_subprocess().ret == 0
|
2016-11-21 00:56:15 +08:00
|
|
|
|
|
|
|
def test_orphaned_pyc_file(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-11-21 00:56:15 +08:00
|
|
|
import orphan
|
|
|
|
def test_it():
|
|
|
|
assert orphan.value == 17
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
orphan="""
|
2016-11-21 00:56:15 +08:00
|
|
|
value = 17
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-11-21 00:56:15 +08:00
|
|
|
py_compile.compile("orphan.py")
|
|
|
|
os.remove("orphan.py")
|
|
|
|
|
|
|
|
# Python 3 puts the .pyc files in a __pycache__ directory, and will
|
|
|
|
# not import from there without source. It will import a .pyc from
|
|
|
|
# the source location though.
|
|
|
|
if not os.path.exists("orphan.pyc"):
|
|
|
|
pycs = glob.glob("__pycache__/orphan.*.pyc")
|
|
|
|
assert len(pycs) == 1
|
|
|
|
os.rename(pycs[0], "orphan.pyc")
|
|
|
|
|
|
|
|
assert testdir.runpytest().ret == 0
|
2011-08-29 22:13:00 +08:00
|
|
|
|
2019-06-25 07:37:07 +08:00
|
|
|
def test_cached_pyc_includes_pytest_version(self, testdir, monkeypatch):
|
|
|
|
"""Avoid stale caches (#1671)"""
|
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_foo="""
|
|
|
|
def test_foo():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
assert result.ret == 0
|
|
|
|
found_names = glob.glob(
|
|
|
|
"__pycache__/*-pytest-{}.pyc".format(pytest.__version__)
|
|
|
|
)
|
|
|
|
assert found_names, "pyc with expected tag not found in names: {}".format(
|
|
|
|
glob.glob("__pycache__/*.pyc")
|
|
|
|
)
|
|
|
|
|
2012-05-22 22:20:58 +08:00
|
|
|
@pytest.mark.skipif('"__pypy__" in sys.modules')
|
2011-08-29 22:13:00 +08:00
|
|
|
def test_pyc_vs_pyo(self, testdir, monkeypatch):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-10-22 17:14:18 +08:00
|
|
|
import pytest
|
|
|
|
def test_optimized():
|
|
|
|
"hello"
|
|
|
|
assert test_optimized.__doc__ is None"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-05-03 01:54:23 +08:00
|
|
|
p = make_numbered_dir(root=Path(testdir.tmpdir), prefix="runpytest-")
|
2011-08-29 22:13:00 +08:00
|
|
|
tmp = "--basetemp=%s" % p
|
|
|
|
monkeypatch.setenv("PYTHONOPTIMIZE", "2")
|
2012-10-22 17:14:18 +08:00
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
2015-04-28 17:54:53 +08:00
|
|
|
assert testdir.runpytest_subprocess(tmp).ret == 0
|
2012-07-07 23:01:44 +08:00
|
|
|
tagged = "test_pyc_vs_pyo." + PYTEST_TAG
|
|
|
|
assert tagged + ".pyo" in os.listdir("__pycache__")
|
2011-08-29 22:13:00 +08:00
|
|
|
monkeypatch.undo()
|
2012-10-22 17:14:18 +08:00
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
2015-04-28 17:54:53 +08:00
|
|
|
assert testdir.runpytest_subprocess(tmp).ret == 1
|
2012-07-07 23:01:44 +08:00
|
|
|
assert tagged + ".pyc" in os.listdir("__pycache__")
|
2011-08-30 12:12:07 +08:00
|
|
|
|
|
|
|
def test_package(self, testdir):
|
|
|
|
pkg = testdir.tmpdir.join("pkg")
|
|
|
|
pkg.mkdir()
|
|
|
|
pkg.join("__init__.py").ensure()
|
2018-05-23 22:48:46 +08:00
|
|
|
pkg.join("test_blah.py").write(
|
|
|
|
"""
|
2011-08-30 12:12:07 +08:00
|
|
|
def test_rewritten():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "@py_builtins" in globals()"""
|
|
|
|
)
|
2011-08-30 12:12:07 +08:00
|
|
|
assert testdir.runpytest().ret == 0
|
2011-09-21 05:53:07 +08:00
|
|
|
|
|
|
|
def test_translate_newlines(self, testdir):
|
|
|
|
content = "def test_rewritten():\r\n assert '@py_builtins' in globals()"
|
|
|
|
b = content.encode("utf-8")
|
|
|
|
testdir.tmpdir.join("test_newlines.py").write(b, "wb")
|
|
|
|
assert testdir.runpytest().ret == 0
|
2013-03-08 23:44:41 +08:00
|
|
|
|
2014-01-22 21:32:22 +08:00
|
|
|
def test_package_without__init__py(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
pkg = testdir.mkdir("a_package_without_init_py")
|
|
|
|
pkg.join("module.py").ensure()
|
2014-01-22 21:32:22 +08:00
|
|
|
testdir.makepyfile("import a_package_without_init_py.module")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert testdir.runpytest().ret == ExitCode.NO_TESTS_COLLECTED
|
2013-08-02 04:11:18 +08:00
|
|
|
|
2018-09-04 07:13:41 +08:00
|
|
|
def test_rewrite_warning(self, testdir):
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
pytest.register_assert_rewrite("_pytest")
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# needs to be a subprocess because pytester explicitly disables this warning
|
|
|
|
result = testdir.runpytest_subprocess()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*Module already imported*: _pytest"])
|
2016-06-22 18:42:11 +08:00
|
|
|
|
2016-08-03 06:16:27 +08:00
|
|
|
def test_rewrite_module_imported_from_conftest(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2016-08-03 06:16:27 +08:00
|
|
|
import test_rewrite_module_imported
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_rewrite_module_imported="""
|
2016-08-03 06:16:27 +08:00
|
|
|
def test_rewritten():
|
|
|
|
assert "@py_builtins" in globals()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-03 06:16:27 +08:00
|
|
|
assert testdir.runpytest_subprocess().ret == 0
|
|
|
|
|
2016-10-19 08:58:31 +08:00
|
|
|
def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
|
|
|
|
"""
|
|
|
|
AssertionRewriteHook should remember rewritten modules so it
|
|
|
|
doesn't give false positives (#2005).
|
|
|
|
"""
|
|
|
|
monkeypatch.syspath_prepend(testdir.tmpdir)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(test_remember_rewritten_modules="")
|
2016-10-19 08:58:31 +08:00
|
|
|
warnings = []
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
2018-12-12 06:02:36 +08:00
|
|
|
monkeypatch.setattr(
|
|
|
|
hook, "_warn_already_imported", lambda code, msg: warnings.append(msg)
|
|
|
|
)
|
2019-06-23 06:02:32 +08:00
|
|
|
spec = hook.find_spec("test_remember_rewritten_modules")
|
2020-05-01 19:40:17 +08:00
|
|
|
assert spec is not None
|
2019-06-23 06:02:32 +08:00
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
hook.exec_module(module)
|
2018-05-23 22:48:46 +08:00
|
|
|
hook.mark_rewrite("test_remember_rewritten_modules")
|
|
|
|
hook.mark_rewrite("test_remember_rewritten_modules")
|
2016-10-19 08:58:31 +08:00
|
|
|
assert warnings == []
|
|
|
|
|
2017-01-12 03:11:56 +08:00
|
|
|
def test_rewrite_warning_using_pytest_plugins(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"conftest.py": "pytest_plugins = ['core', 'gui', 'sci']",
|
|
|
|
"core.py": "",
|
|
|
|
"gui.py": "pytest_plugins = ['core', 'sci']",
|
|
|
|
"sci.py": "pytest_plugins = ['core']",
|
|
|
|
"test_rewrite_warning_pytest_plugins.py": "def test(): pass",
|
|
|
|
}
|
|
|
|
)
|
2016-12-02 01:50:08 +08:00
|
|
|
testdir.chdir()
|
|
|
|
result = testdir.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*= 1 passed in *=*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*pytest-warning summary*")
|
2016-12-02 01:50:08 +08:00
|
|
|
|
2017-01-12 03:11:56 +08:00
|
|
|
def test_rewrite_warning_using_pytest_plugins_env_var(self, testdir, monkeypatch):
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "plugin")
|
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"plugin.py": "",
|
|
|
|
"test_rewrite_warning_using_pytest_plugins_env_var.py": """
|
2017-01-12 03:11:56 +08:00
|
|
|
import plugin
|
|
|
|
pytest_plugins = ['plugin']
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
}
|
|
|
|
)
|
2017-01-12 03:11:56 +08:00
|
|
|
testdir.chdir()
|
|
|
|
result = testdir.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*= 1 passed in *=*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*pytest-warning summary*")
|
2017-01-12 03:11:56 +08:00
|
|
|
|
2016-06-22 18:42:11 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestAssertionRewriteHookDetails:
|
2014-03-28 15:33:12 +08:00
|
|
|
def test_sys_meta_path_munged(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-03-28 16:03:52 +08:00
|
|
|
def test_meta_path():
|
2018-05-23 22:48:46 +08:00
|
|
|
import sys; sys.meta_path = []"""
|
|
|
|
)
|
2014-03-28 15:33:12 +08:00
|
|
|
assert testdir.runpytest().ret == 0
|
|
|
|
|
2020-02-25 10:47:35 +08:00
|
|
|
def test_write_pyc(self, testdir: Testdir, tmpdir, monkeypatch) -> None:
|
2013-08-01 21:43:42 +08:00
|
|
|
from _pytest.assertion.rewrite import _write_pyc
|
|
|
|
from _pytest.assertion import AssertionState
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-02-25 10:47:35 +08:00
|
|
|
config = testdir.parseconfig()
|
2013-08-01 21:43:42 +08:00
|
|
|
state = AssertionState(config, "rewrite")
|
2019-11-07 21:41:26 +08:00
|
|
|
source_path = str(tmpdir.ensure("source.py"))
|
2013-08-01 21:43:42 +08:00
|
|
|
pycpath = tmpdir.join("pyc").strpath
|
2020-05-01 19:40:15 +08:00
|
|
|
co = compile("1", "f.py", "single")
|
|
|
|
assert _write_pyc(state, co, os.stat(source_path), pycpath)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-07 21:41:26 +08:00
|
|
|
if sys.platform == "win32":
|
|
|
|
from contextlib import contextmanager
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-07 21:41:26 +08:00
|
|
|
@contextmanager
|
|
|
|
def atomic_write_failed(fn, mode="r", overwrite=False):
|
2020-03-27 23:40:23 +08:00
|
|
|
e = OSError()
|
2019-11-07 21:41:26 +08:00
|
|
|
e.errno = 10
|
|
|
|
raise e
|
|
|
|
yield
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
_pytest.assertion.rewrite, "atomic_write", atomic_write_failed
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
|
2020-03-27 23:40:23 +08:00
|
|
|
def raise_oserror(*args):
|
|
|
|
raise OSError()
|
2019-11-07 21:41:26 +08:00
|
|
|
|
2020-03-27 23:40:23 +08:00
|
|
|
monkeypatch.setattr("os.rename", raise_oserror)
|
2019-11-07 21:41:26 +08:00
|
|
|
|
2020-05-01 19:40:15 +08:00
|
|
|
assert not _write_pyc(state, co, os.stat(source_path), pycpath)
|
2013-10-10 23:40:31 +08:00
|
|
|
|
|
|
|
def test_resources_provider_for_loader(self, testdir):
|
|
|
|
"""
|
|
|
|
Attempts to load resources from a package should succeed normally,
|
|
|
|
even when the AssertionRewriteHook is used to load the modules.
|
|
|
|
|
|
|
|
See #366 for details.
|
|
|
|
"""
|
|
|
|
pytest.importorskip("pkg_resources")
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.mkpydir("testpkg")
|
2013-10-10 23:40:31 +08:00
|
|
|
contents = {
|
2018-05-23 22:48:46 +08:00
|
|
|
"testpkg/test_pkg": """
|
2013-10-10 23:40:31 +08:00
|
|
|
import pkg_resources
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest.assertion.rewrite import AssertionRewritingHook
|
|
|
|
|
|
|
|
def test_load_resource():
|
|
|
|
assert isinstance(__loader__, AssertionRewritingHook)
|
|
|
|
res = pkg_resources.resource_string(__name__, 'resource.txt')
|
2013-10-11 06:01:56 +08:00
|
|
|
res = res.decode('ascii')
|
2013-10-10 23:40:31 +08:00
|
|
|
assert res == 'Load me please.'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-10-10 23:40:31 +08:00
|
|
|
}
|
|
|
|
testdir.makepyfile(**contents)
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.maketxtfile(**{"testpkg/resource": "Load me please."})
|
2013-10-10 23:40:31 +08:00
|
|
|
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
result.assert_outcomes(passed=1)
|
2014-08-03 05:01:28 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_read_pyc(self, tmp_path: Path) -> None:
|
2014-08-03 05:01:28 +08:00
|
|
|
"""
|
|
|
|
Ensure that the `_read_pyc` can properly deal with corrupted pyc files.
|
|
|
|
In those circumstances it should just give up instead of generating
|
|
|
|
an exception that is propagated to the caller.
|
|
|
|
"""
|
|
|
|
import py_compile
|
|
|
|
from _pytest.assertion.rewrite import _read_pyc
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
source = tmp_path / "source.py"
|
|
|
|
pyc = Path(str(source) + "c")
|
2014-08-03 05:01:28 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
source.write_text("def test(): pass")
|
2014-08-03 05:01:28 +08:00
|
|
|
py_compile.compile(str(source), str(pyc))
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
contents = pyc.read_bytes()
|
2014-08-03 05:01:28 +08:00
|
|
|
strip_bytes = 20 # header is around 8 bytes, strip a little more
|
|
|
|
assert len(contents) > strip_bytes
|
2020-05-01 19:40:17 +08:00
|
|
|
pyc.write_bytes(contents[:strip_bytes])
|
2014-08-03 05:01:28 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
assert _read_pyc(source, pyc) is None # no error
|
2015-03-04 23:21:27 +08:00
|
|
|
|
2020-04-02 21:25:53 +08:00
|
|
|
def test_reload_is_same_and_reloads(self, testdir: Testdir) -> None:
|
|
|
|
"""Reloading a (collected) module after change picks up the change."""
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2015-03-04 23:21:27 +08:00
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-04-02 21:25:53 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
file="""
|
2018-09-23 19:05:55 +08:00
|
|
|
def reloaded():
|
|
|
|
return False
|
|
|
|
|
|
|
|
def rewrite_self():
|
|
|
|
with open(__file__, 'w') as self:
|
|
|
|
self.write('def reloaded(): return True')
|
2020-04-02 21:25:53 +08:00
|
|
|
""",
|
2018-09-23 19:05:55 +08:00
|
|
|
test_fun="""
|
|
|
|
import sys
|
2020-04-02 21:25:53 +08:00
|
|
|
from importlib import reload
|
2018-09-23 19:05:55 +08:00
|
|
|
|
|
|
|
def test_loader():
|
|
|
|
import file
|
|
|
|
assert not file.reloaded()
|
|
|
|
file.rewrite_self()
|
2020-04-02 21:25:53 +08:00
|
|
|
assert sys.modules["file"] is reload(file)
|
2018-09-23 19:05:55 +08:00
|
|
|
assert file.reloaded()
|
2020-04-02 21:25:53 +08:00
|
|
|
""",
|
2018-09-23 19:05:55 +08:00
|
|
|
)
|
2020-04-02 21:25:53 +08:00
|
|
|
result = testdir.runpytest()
|
2018-09-23 19:05:55 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed*"])
|
|
|
|
|
2015-07-12 01:13:43 +08:00
|
|
|
def test_get_data_support(self, testdir):
|
|
|
|
"""Implement optional PEP302 api (#808).
|
|
|
|
"""
|
|
|
|
path = testdir.mkpydir("foo")
|
2018-05-23 22:48:46 +08:00
|
|
|
path.join("test_foo.py").write(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
class Test(object):
|
|
|
|
def test_foo(self):
|
|
|
|
import pkgutil
|
|
|
|
data = pkgutil.get_data('foo.test_foo', 'data.txt')
|
|
|
|
assert data == b'Hey'
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
path.join("data.txt").write("Hey")
|
2015-07-12 01:13:43 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-07-12 01:13:43 +08:00
|
|
|
|
2015-04-30 09:31:12 +08:00
|
|
|
|
|
|
|
def test_issue731(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2015-04-30 09:31:12 +08:00
|
|
|
class LongReprWithBraces(object):
|
|
|
|
def __repr__(self):
|
|
|
|
return 'LongReprWithBraces({' + ('a' * 80) + '}' + ('a' * 120) + ')'
|
|
|
|
|
|
|
|
def some_method(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def test_long_repr():
|
|
|
|
obj = LongReprWithBraces()
|
|
|
|
assert obj.some_method()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-04-30 09:31:12 +08:00
|
|
|
result = testdir.runpytest()
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*unbalanced braces*")
|
2015-04-30 09:31:12 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestIssue925:
|
2016-06-25 23:21:10 +08:00
|
|
|
def test_simple_case(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-25 23:21:10 +08:00
|
|
|
def test_ternary_display():
|
|
|
|
assert (False == False) == False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-25 23:21:10 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*E*assert (False == False) == False"])
|
2016-06-25 23:21:10 +08:00
|
|
|
|
|
|
|
def test_long_case(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-25 23:21:10 +08:00
|
|
|
def test_ternary_display():
|
|
|
|
assert False == (False == True) == True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-25 23:21:10 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*E*assert (False == True) == True"])
|
2016-06-25 23:21:10 +08:00
|
|
|
|
|
|
|
def test_many_brackets(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-06-25 23:21:10 +08:00
|
|
|
def test_ternary_display():
|
|
|
|
assert True == ((False == True) == True)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-25 23:21:10 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*E*assert True == ((False == True) == True)"])
|
2016-06-25 23:21:10 +08:00
|
|
|
|
2016-12-16 22:29:08 +08:00
|
|
|
|
2018-06-26 21:35:27 +08:00
|
|
|
class TestIssue2121:
|
2018-09-01 21:59:21 +08:00
|
|
|
def test_rewrite_python_files_contain_subdirs(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"tests/file.py": """
|
|
|
|
def test_simple_failure():
|
|
|
|
assert 1 + 1 == 3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-09-01 21:59:21 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
python_files = tests/**.py
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-12-16 22:29:08 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"])
|
2018-08-28 07:51:16 +08:00
|
|
|
|
|
|
|
|
2019-04-04 18:53:55 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.maxsize <= (2 ** 31 - 1), reason="Causes OverflowError on 32bit systems"
|
|
|
|
)
|
2019-03-14 19:16:07 +08:00
|
|
|
@pytest.mark.parametrize("offset", [-1, +1])
|
|
|
|
def test_source_mtime_long_long(testdir, offset):
|
|
|
|
"""Support modification dates after 2038 in rewritten files (#4903).
|
|
|
|
|
|
|
|
pytest would crash with:
|
|
|
|
|
|
|
|
fp.write(struct.pack("<ll", mtime, size))
|
|
|
|
E struct.error: argument out of range
|
|
|
|
"""
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
def test(): pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# use unsigned long timestamp which overflows signed long,
|
|
|
|
# which was the cause of the bug
|
|
|
|
# +1 offset also tests masking of 0xFFFFFFFF
|
|
|
|
timestamp = 2 ** 32 + offset
|
|
|
|
os.utime(str(p), (timestamp, timestamp))
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_rewrite_infinite_recursion(testdir, pytestconfig, monkeypatch) -> None:
|
2018-08-28 07:51:16 +08:00
|
|
|
"""Fix infinite recursion when writing pyc files: if an import happens to be triggered when writing the pyc
|
|
|
|
file, this would cause another call to the hook, which would trigger another pyc writing, which could
|
|
|
|
trigger another import, and so on. (#3506)"""
|
2020-05-01 19:40:17 +08:00
|
|
|
from _pytest.assertion import rewrite as rewritemod
|
2018-08-28 07:51:16 +08:00
|
|
|
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile(test_foo="def test_foo(): pass")
|
|
|
|
testdir.makepyfile(test_bar="def test_bar(): pass")
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
original_write_pyc = rewritemod._write_pyc
|
2018-08-28 07:51:16 +08:00
|
|
|
|
|
|
|
write_pyc_called = []
|
|
|
|
|
|
|
|
def spy_write_pyc(*args, **kwargs):
|
|
|
|
# make a note that we have called _write_pyc
|
|
|
|
write_pyc_called.append(True)
|
|
|
|
# try to import a module at this point: we should not try to rewrite this module
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("test_bar") is None
|
2018-08-28 07:51:16 +08:00
|
|
|
return original_write_pyc(*args, **kwargs)
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
monkeypatch.setattr(rewritemod, "_write_pyc", spy_write_pyc)
|
2018-08-28 07:51:16 +08:00
|
|
|
monkeypatch.setattr(sys, "dont_write_bytecode", False)
|
|
|
|
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
2019-06-23 06:02:32 +08:00
|
|
|
spec = hook.find_spec("test_foo")
|
|
|
|
assert spec is not None
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
hook.exec_module(module)
|
2018-08-28 07:51:16 +08:00
|
|
|
assert len(write_pyc_called) == 1
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestEarlyRewriteBailout:
|
2018-09-01 21:59:21 +08:00
|
|
|
@pytest.fixture
|
2020-05-01 19:40:17 +08:00
|
|
|
def hook(self, pytestconfig, monkeypatch, testdir) -> AssertionRewritingHook:
|
2018-09-01 21:59:21 +08:00
|
|
|
"""Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
|
2019-06-23 06:02:32 +08:00
|
|
|
if PathFinder.find_spec has been called.
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
2019-06-23 06:02:32 +08:00
|
|
|
import importlib.machinery
|
2018-09-01 21:59:21 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
self.find_spec_calls = [] # type: List[str]
|
|
|
|
self.initial_paths = set() # type: Set[py.path.local]
|
2018-09-01 21:59:21 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class StubSession:
|
2018-09-01 21:59:21 +08:00
|
|
|
_initialpaths = self.initial_paths
|
|
|
|
|
|
|
|
def isinitpath(self, p):
|
|
|
|
return p in self._initialpaths
|
|
|
|
|
2019-06-23 06:02:32 +08:00
|
|
|
def spy_find_spec(name, path):
|
|
|
|
self.find_spec_calls.append(name)
|
2020-05-01 19:40:17 +08:00
|
|
|
return importlib.machinery.PathFinder.find_spec(name, path) # type: ignore
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
|
|
|
# use default patterns, otherwise we inherit pytest's testing config
|
|
|
|
hook.fnpats[:] = ["test_*.py", "*_test.py"]
|
2019-06-23 06:02:32 +08:00
|
|
|
monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
|
2020-05-01 19:40:17 +08:00
|
|
|
hook.set_session(StubSession()) # type: ignore[arg-type] # noqa: F821
|
2018-09-01 21:59:21 +08:00
|
|
|
testdir.syspathinsert()
|
|
|
|
return hook
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_basic(self, testdir, hook: AssertionRewritingHook) -> None:
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
2019-06-23 06:02:32 +08:00
|
|
|
Ensure we avoid calling PathFinder.find_spec when we know for sure a certain
|
|
|
|
module will not be rewritten to optimize assertion rewriting (#3918).
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(): return 1
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(test_foo="def test_foo(): pass")
|
|
|
|
testdir.makepyfile(bar="def bar(): pass")
|
|
|
|
foobar_path = testdir.makepyfile(foobar="def foobar(): pass")
|
|
|
|
self.initial_paths.add(foobar_path)
|
|
|
|
|
|
|
|
# conftest files should always be rewritten
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("conftest") is not None
|
|
|
|
assert self.find_spec_calls == ["conftest"]
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
# files matching "python_files" mask should always be rewritten
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("test_foo") is not None
|
|
|
|
assert self.find_spec_calls == ["conftest", "test_foo"]
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
# file does not match "python_files": early bailout
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("bar") is None
|
|
|
|
assert self.find_spec_calls == ["conftest", "test_foo"]
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
# file is an initial path (passed on the command-line): should be rewritten
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("foobar") is not None
|
|
|
|
assert self.find_spec_calls == ["conftest", "test_foo", "foobar"]
|
2018-09-01 21:59:21 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_pattern_contains_subdirectories(
|
|
|
|
self, testdir, hook: AssertionRewritingHook
|
|
|
|
) -> None:
|
2018-09-01 21:59:21 +08:00
|
|
|
"""If one of the python_files patterns contain subdirectories ("tests/**.py") we can't bailout early
|
2019-06-23 06:02:32 +08:00
|
|
|
because we need to match with the full path, which can only be found by calling PathFinder.find_spec
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
**{
|
2019-06-28 10:11:20 +08:00
|
|
|
"tests/file.py": """\
|
|
|
|
def test_simple_failure():
|
|
|
|
assert 1 + 1 == 3
|
|
|
|
"""
|
2018-09-01 21:59:21 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
testdir.syspathinsert(p.dirpath())
|
|
|
|
hook.fnpats[:] = ["tests/**.py"]
|
2019-06-23 06:02:32 +08:00
|
|
|
assert hook.find_spec("file") is not None
|
|
|
|
assert self.find_spec_calls == ["file"]
|
2018-09-14 05:50:05 +08:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform.startswith("win32"), reason="cannot remove cwd on Windows"
|
|
|
|
)
|
2019-02-14 05:42:04 +08:00
|
|
|
def test_cwd_changed(self, testdir, monkeypatch):
|
|
|
|
# Setup conditions for py's fspath trying to import pathlib on py34
|
|
|
|
# always (previously triggered via xdist only).
|
|
|
|
# Ref: https://github.com/pytest-dev/py/pull/207
|
2019-03-22 23:20:55 +08:00
|
|
|
monkeypatch.syspath_prepend("")
|
2019-03-01 21:55:09 +08:00
|
|
|
monkeypatch.delitem(sys.modules, "pathlib", raising=False)
|
2019-02-14 05:42:04 +08:00
|
|
|
|
2018-09-14 05:50:05 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
2019-06-28 10:11:20 +08:00
|
|
|
"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
|
|
|
|
""",
|
2018-09-14 05:50:05 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
2019-06-24 22:09:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestAssertionPass:
|
2019-06-27 04:54:24 +08:00
|
|
|
def test_option_default(self, testdir):
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
assert config.getini("enable_assertion_pass_hook") is False
|
|
|
|
|
2019-06-28 10:11:20 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def flag_on(self, testdir):
|
|
|
|
testdir.makeini("[pytest]\nenable_assertion_pass_hook = True\n")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hook_on(self, testdir):
|
2019-06-24 22:09:39 +08:00
|
|
|
testdir.makeconftest(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
2019-06-24 22:09:39 +08:00
|
|
|
def pytest_assertion_pass(item, lineno, orig, expl):
|
|
|
|
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))
|
|
|
|
"""
|
|
|
|
)
|
2019-06-26 23:08:09 +08:00
|
|
|
|
2019-06-28 10:11:20 +08:00
|
|
|
def test_hook_call(self, testdir, flag_on, hook_on):
|
2019-06-24 22:09:39 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
2019-06-24 22:09:39 +08:00
|
|
|
def test_simple():
|
|
|
|
a=1
|
|
|
|
b=2
|
|
|
|
c=3
|
|
|
|
d=0
|
|
|
|
|
|
|
|
assert a+b == c+d
|
2019-06-27 07:46:31 +08:00
|
|
|
|
|
|
|
# cover failing assertions with a message
|
|
|
|
def test_fails():
|
|
|
|
assert False, "assert with message"
|
2019-06-24 22:09:39 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-06-26 23:08:09 +08:00
|
|
|
result = testdir.runpytest()
|
2019-06-24 22:09:39 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2019-06-28 10:11:20 +08:00
|
|
|
"*Assertion Passed: a+b == c+d (1 + 2) == (3 + 0) at line 7*"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_hook_call_with_parens(self, testdir, flag_on, hook_on):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""\
|
|
|
|
def f(): return 1
|
|
|
|
def test():
|
|
|
|
assert f()
|
|
|
|
"""
|
2019-06-24 22:09:39 +08:00
|
|
|
)
|
2019-06-28 10:11:20 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("*Assertion Passed: f() 1")
|
2019-06-24 22:09:39 +08:00
|
|
|
|
2019-06-28 10:11:20 +08:00
|
|
|
def test_hook_not_called_without_hookimpl(self, testdir, monkeypatch, flag_on):
|
2019-06-24 22:09:39 +08:00
|
|
|
"""Assertion pass should not be called (and hence formatting should
|
|
|
|
not occur) if there is no hook declared for pytest_assertion_pass"""
|
|
|
|
|
|
|
|
def raise_on_assertionpass(*_, **__):
|
|
|
|
raise Exception("Assertion passed called when it shouldn't!")
|
|
|
|
|
2019-06-25 16:41:11 +08:00
|
|
|
monkeypatch.setattr(
|
|
|
|
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass
|
|
|
|
)
|
2019-06-24 22:09:39 +08:00
|
|
|
|
2019-06-26 01:49:05 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
2019-06-26 01:49:05 +08:00
|
|
|
def test_simple():
|
|
|
|
a=1
|
|
|
|
b=2
|
|
|
|
c=3
|
|
|
|
d=0
|
|
|
|
|
|
|
|
assert a+b == c+d
|
|
|
|
"""
|
|
|
|
)
|
2019-06-26 23:08:09 +08:00
|
|
|
result = testdir.runpytest()
|
2019-06-26 01:49:05 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
|
|
|
|
|
|
|
def test_hook_not_called_without_cmd_option(self, testdir, monkeypatch):
|
|
|
|
"""Assertion pass should not be called (and hence formatting should
|
|
|
|
not occur) if there is no hook declared for pytest_assertion_pass"""
|
|
|
|
|
|
|
|
def raise_on_assertionpass(*_, **__):
|
|
|
|
raise Exception("Assertion passed called when it shouldn't!")
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass
|
|
|
|
)
|
|
|
|
|
|
|
|
testdir.makeconftest(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
2019-06-26 01:49:05 +08:00
|
|
|
def pytest_assertion_pass(item, lineno, orig, expl):
|
|
|
|
raise Exception("Assertion Passed: {} {} at line {}".format(orig, expl, lineno))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2019-06-24 22:09:39 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
2019-06-24 22:09:39 +08:00
|
|
|
def test_simple():
|
|
|
|
a=1
|
|
|
|
b=2
|
|
|
|
c=3
|
|
|
|
d=0
|
|
|
|
|
|
|
|
assert a+b == c+d
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.assert_outcomes(passed=1)
|
2019-06-28 10:11:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
@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(
|
|
|
|
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
|
2019-08-02 07:55:30 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_try_makedirs(monkeypatch, tmp_path: Path) -> None:
|
2019-09-22 01:38:39 +08:00
|
|
|
from _pytest.assertion.rewrite import try_makedirs
|
2019-08-02 07:55:30 +08:00
|
|
|
|
|
|
|
p = tmp_path / "foo"
|
|
|
|
|
|
|
|
# create
|
2020-05-01 19:40:17 +08:00
|
|
|
assert try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
assert p.is_dir()
|
|
|
|
|
|
|
|
# already exist
|
2020-05-01 19:40:17 +08:00
|
|
|
assert try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
|
|
|
|
# monkeypatch to simulate all error situations
|
2019-09-22 01:38:39 +08:00
|
|
|
def fake_mkdir(p, exist_ok=False, *, exc):
|
2019-08-02 07:55:30 +08:00
|
|
|
assert isinstance(p, str)
|
|
|
|
raise exc
|
|
|
|
|
2019-09-22 01:38:39 +08:00
|
|
|
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=FileNotFoundError()))
|
2020-05-01 19:40:17 +08:00
|
|
|
assert not try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
|
2019-09-22 01:38:39 +08:00
|
|
|
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=NotADirectoryError()))
|
2020-05-01 19:40:17 +08:00
|
|
|
assert not try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
|
2019-09-22 01:38:39 +08:00
|
|
|
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=PermissionError()))
|
2020-05-01 19:40:17 +08:00
|
|
|
assert not try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
|
|
|
|
err = OSError()
|
|
|
|
err.errno = errno.EROFS
|
2019-09-22 01:38:39 +08:00
|
|
|
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err))
|
2020-05-01 19:40:17 +08:00
|
|
|
assert not try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
|
|
|
|
# unhandled OSError should raise
|
|
|
|
err = OSError()
|
|
|
|
err.errno = errno.ECHILD
|
2019-09-22 01:38:39 +08:00
|
|
|
monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err))
|
2019-08-02 07:55:30 +08:00
|
|
|
with pytest.raises(OSError) as exc_info:
|
2020-05-01 19:40:17 +08:00
|
|
|
try_makedirs(p)
|
2019-08-02 07:55:30 +08:00
|
|
|
assert exc_info.value.errno == errno.ECHILD
|
2019-09-19 07:29:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestPyCacheDir:
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"prefix, source, expected",
|
|
|
|
[
|
|
|
|
("c:/tmp/pycs", "d:/projects/src/foo.py", "c:/tmp/pycs/projects/src"),
|
|
|
|
(None, "d:/projects/src/foo.py", "d:/projects/src/__pycache__"),
|
|
|
|
("/tmp/pycs", "/home/projects/src/foo.py", "/tmp/pycs/home/projects/src"),
|
|
|
|
(None, "/home/projects/src/foo.py", "/home/projects/src/__pycache__"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_cache_dir(self, monkeypatch, prefix, source, expected):
|
|
|
|
if prefix:
|
|
|
|
if sys.version_info < (3, 8):
|
|
|
|
pytest.skip("pycache_prefix not available in py<38")
|
|
|
|
monkeypatch.setattr(sys, "pycache_prefix", prefix)
|
|
|
|
|
|
|
|
assert get_cache_dir(Path(source)) == Path(expected)
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 8), reason="pycache_prefix not available in py<38"
|
|
|
|
)
|
|
|
|
def test_sys_pycache_prefix_integration(self, tmp_path, monkeypatch, testdir):
|
|
|
|
"""Integration test for sys.pycache_prefix (#4730)."""
|
|
|
|
pycache_prefix = tmp_path / "my/pycs"
|
|
|
|
monkeypatch.setattr(sys, "pycache_prefix", str(pycache_prefix))
|
|
|
|
monkeypatch.setattr(sys, "dont_write_bytecode", False)
|
|
|
|
|
|
|
|
testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"src/test_foo.py": """
|
|
|
|
import bar
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
"src/bar/__init__.py": "",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
test_foo = Path(testdir.tmpdir) / "src/test_foo.py"
|
|
|
|
bar_init = Path(testdir.tmpdir) / "src/bar/__init__.py"
|
|
|
|
assert test_foo.is_file()
|
|
|
|
assert bar_init.is_file()
|
|
|
|
|
|
|
|
# test file: rewritten, custom pytest cache tag
|
|
|
|
test_foo_pyc = get_cache_dir(test_foo) / ("test_foo" + PYC_TAIL)
|
|
|
|
assert test_foo_pyc.is_file()
|
|
|
|
|
|
|
|
# normal file: not touched by pytest, normal cache tag
|
|
|
|
bar_init_pyc = get_cache_dir(bar_init) / "__init__.{cache_tag}.pyc".format(
|
|
|
|
cache_tag=sys.implementation.cache_tag
|
|
|
|
)
|
|
|
|
assert bar_init_pyc.is_file()
|