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
|
2020-11-09 21:07:51 +08:00
|
|
|
import marshal
|
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-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2021-03-03 08:47:37 +08:00
|
|
|
from typing import cast
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Dict
|
2022-02-08 08:29:01 +08:00
|
|
|
from typing import Generator
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import List
|
|
|
|
from typing import Mapping
|
|
|
|
from typing import Optional
|
|
|
|
from typing import Set
|
2022-02-08 08:29:01 +08:00
|
|
|
from unittest import mock
|
2020-05-01 19:40:17 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2021-03-03 08:47:37 +08:00
|
|
|
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
|
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
|
2021-03-03 08:47:37 +08:00
|
|
|
from _pytest.assertion.rewrite import _get_maxsize_for_saferepr
|
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
|
2021-03-03 08:47:37 +08:00
|
|
|
from _pytest.config import Config
|
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
|
2020-10-29 15:54:34 +08:00
|
|
|
from _pytest.pytester import Pytester
|
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."""
|
2020-10-27 22:07:03 +08:00
|
|
|
src = "\n".join(_pytest._code.Code.from_function(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-10-06 09:13:05 +08:00
|
|
|
ns: 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-07-10 14:44:14 +08:00
|
|
|
func() # type: ignore[operator]
|
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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_place_initial_imports(self) -> None:
|
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
|
|
|
|
2021-10-05 14:52:09 +08:00
|
|
|
def test_location_is_set(self) -> None:
|
|
|
|
s = textwrap.dedent(
|
|
|
|
"""
|
|
|
|
|
|
|
|
assert False, (
|
|
|
|
|
|
|
|
"Ouch"
|
|
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
m = rewrite(s)
|
|
|
|
for node in m.body:
|
|
|
|
if isinstance(node, ast.Import):
|
|
|
|
continue
|
|
|
|
for n in [node, *ast.iter_child_nodes(node)]:
|
|
|
|
assert n.lineno == 3
|
|
|
|
assert n.col_offset == 0
|
|
|
|
if sys.version_info >= (3, 8):
|
|
|
|
assert n.end_lineno == 6
|
|
|
|
assert n.end_col_offset == 3
|
|
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_dont_rewrite_plugin(self, pytester: Pytester) -> None:
|
2017-12-13 09:39:10 +08:00
|
|
|
contents = {
|
|
|
|
"conftest.py": "pytest_plugins = 'plugin'; import plugin",
|
|
|
|
"plugin.py": "'PYTEST_DONT_REWRITE'",
|
|
|
|
"test_foo.py": "def test_foo(): pass",
|
|
|
|
}
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(**contents)
|
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrites_plugin_as_a_package(self, pytester: Pytester) -> None:
|
|
|
|
pkgdir = pytester.mkpydir("plugin")
|
|
|
|
pkgdir.joinpath("__init__.py").write_text(
|
2019-06-23 06:02:32 +08:00
|
|
|
"import pytest\n"
|
|
|
|
"@pytest.fixture\n"
|
|
|
|
"def special_asserter():\n"
|
|
|
|
" def special_assert(x, y):\n"
|
|
|
|
" assert x == y\n"
|
|
|
|
" return special_assert\n"
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makeconftest('pytest_plugins = ["plugin"]')
|
|
|
|
pytester.makepyfile("def test(special_asserter): special_asserter(1, 2)\n")
|
|
|
|
result = pytester.runpytest()
|
2019-06-23 06:02:32 +08:00
|
|
|
result.stdout.fnmatch_lines(["*assert 1 == 2*"])
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_honors_pep_235(self, pytester: Pytester, monkeypatch) -> None:
|
2019-06-23 06:02:32 +08:00
|
|
|
# note: couldn't make it fail on macos with a single `sys.path` entry
|
|
|
|
# note: these modules are named `test_*` to trigger rewriting
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(test_y="x = 1")
|
|
|
|
xdir = pytester.mkdir("x")
|
|
|
|
pytester.mkpydir(str(xdir.joinpath("test_Y")))
|
|
|
|
xdir.joinpath("test_Y").joinpath("__init__.py").write_text("x = 2")
|
|
|
|
pytester.makepyfile(
|
2019-06-23 06:02:32 +08:00
|
|
|
"import test_y\n"
|
|
|
|
"import test_Y\n"
|
|
|
|
"def test():\n"
|
|
|
|
" assert test_y.x == 1\n"
|
|
|
|
" assert test_Y.x == 2\n"
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
monkeypatch.syspath_prepend(str(xdir))
|
|
|
|
pytester.runpytest().assert_outcomes(passed=1)
|
2019-06-23 06:02:32 +08:00
|
|
|
|
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:
|
2020-07-10 14:44:14 +08:00
|
|
|
assert sys == 42 # type: ignore[comparison-overlap]
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
msg = getmsg(f4, {"sys": sys})
|
2022-03-15 04:27:52 +08:00
|
|
|
assert msg == "assert sys == 42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def f5() -> None:
|
2020-07-10 14:44:14 +08:00
|
|
|
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()
|
2022-03-15 04:27:52 +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:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""A class whose getattr fails, but not with `AttributeError`."""
|
2019-01-11 03:12:50 +08:00
|
|
|
|
|
|
|
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()
|
2022-03-15 04:27:52 +08:00
|
|
|
assert lines == [
|
|
|
|
"assert 3 == 2",
|
|
|
|
" + where 3 = Y.foo",
|
|
|
|
" + where Y = cls()",
|
|
|
|
]
|
2019-01-11 03:12:50 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assert_already_has_message(self) -> None:
|
2011-05-19 04:31:10 +08:00
|
|
|
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"
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_message(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2014-08-24 00:14:25 +08:00
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_message_multiline(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2014-08-24 00:14:25 +08:00
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_message_tuple(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, (1, 2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2014-08-24 00:14:25 +08:00
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_message_expr(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-08-24 00:14:25 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1 == 2, 1 + 2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2014-08-24 00:14:25 +08:00
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_message_escape(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2014-10-07 07:01:21 +08:00
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_assertion_messages_bytes(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("def test_bytes_assertion():\n assert False, b'ohai!'\n")
|
|
|
|
result = pytester.runpytest()
|
2018-08-02 06:09:25 +08:00
|
|
|
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:
|
2021-01-30 02:40:43 +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:
|
2020-08-04 00:15:21 +08:00
|
|
|
assert True or explode # type: ignore[name-defined,unreachable] # 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:
|
2021-01-30 02:40:43 +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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_at_operator_issue1290(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.runpytest().assert_outcomes(passed=1)
|
2016-02-06 06:30:07 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_starred_with_side_effect(self, pytester: Pytester) -> None:
|
2018-11-18 02:41:44 +08:00
|
|
|
"""See #4412"""
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-11-18 02:41:44 +08:00
|
|
|
"""\
|
|
|
|
def test():
|
|
|
|
f = lambda x: x
|
|
|
|
x = iter([1, 2, 3])
|
|
|
|
assert 2 * next(x) == f(*[next(x)])
|
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.runpytest().assert_outcomes(passed=1)
|
2018-11-18 02:41:44 +08:00
|
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_len(self, request) -> None:
|
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)
|
2022-03-15 04:27:52 +08:00
|
|
|
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:
|
2020-10-03 04:16:22 +08:00
|
|
|
return f"{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:
|
2020-07-10 14:44:14 +08:00
|
|
|
assert "%test" == "test" # type: ignore[comparison-overlap]
|
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])
|
2022-03-15 04:27:52 +08:00
|
|
|
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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_pycache_is_a_file(self, pytester: Pytester) -> None:
|
|
|
|
pytester.path.joinpath("__pycache__").write_text("Hello")
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-06-08 05:30:10 +08:00
|
|
|
def test_rewritten():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "@py_builtins" in globals()"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2011-07-26 10:40:38 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_pycache_is_readonly(self, pytester: Pytester) -> None:
|
|
|
|
cache = pytester.mkdir("__pycache__")
|
|
|
|
old_mode = cache.stat().st_mode
|
2013-05-29 06:11:12 +08:00
|
|
|
cache.chmod(old_mode ^ stat.S_IWRITE)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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:
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2013-05-29 06:11:12 +08:00
|
|
|
finally:
|
|
|
|
cache.chmod(old_mode)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_zipfile(self, pytester: Pytester) -> None:
|
|
|
|
z = pytester.path.joinpath("myzip.zip")
|
2011-07-26 10:40:38 +08:00
|
|
|
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)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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,)
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == ExitCode.NO_TESTS_COLLECTED
|
2011-07-26 10:40:38 +08:00
|
|
|
|
2021-10-09 21:54:44 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 9),
|
|
|
|
reason="importlib.resources.files was introduced in 3.9",
|
|
|
|
)
|
|
|
|
def test_load_resource_via_files_with_rewrite(self, pytester: Pytester) -> None:
|
|
|
|
example = pytester.path.joinpath("demo") / "example"
|
|
|
|
init = pytester.path.joinpath("demo") / "__init__.py"
|
|
|
|
pytester.makepyfile(
|
|
|
|
**{
|
|
|
|
"demo/__init__.py": """
|
|
|
|
from importlib.resources import files
|
|
|
|
|
|
|
|
def load():
|
|
|
|
return files(__name__)
|
|
|
|
""",
|
|
|
|
"test_load": f"""
|
|
|
|
pytest_plugins = ["demo"]
|
|
|
|
|
|
|
|
def test_load():
|
|
|
|
from demo import load
|
|
|
|
found = {{str(i) for i in load().iterdir() if i.name != "__pycache__"}}
|
|
|
|
assert found == {{{str(example)!r}, {str(init)!r}}}
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
example.mkdir()
|
|
|
|
|
|
|
|
assert pytester.runpytest("-vv").ret == ExitCode.OK
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_readonly(self, pytester: Pytester) -> None:
|
|
|
|
sub = pytester.mkdir("testing")
|
|
|
|
sub.joinpath("test_readonly.py").write_bytes(
|
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
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
old_mode = sub.stat().st_mode
|
2011-07-13 06:09:14 +08:00
|
|
|
sub.chmod(320)
|
2013-05-29 06:11:12 +08:00
|
|
|
try:
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2013-05-29 06:11:12 +08:00
|
|
|
finally:
|
|
|
|
sub.chmod(old_mode)
|
2011-07-14 02:33:54 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_dont_write_bytecode(self, pytester: Pytester, monkeypatch) -> None:
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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")
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest_subprocess().ret == 0
|
2016-11-21 00:56:15 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_orphaned_pyc_file(self, pytester: Pytester, monkeypatch) -> None:
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
|
|
|
monkeypatch.setattr(sys, "pycache_prefix", None, raising=False)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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")
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2011-08-29 22:13:00 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_cached_pyc_includes_pytest_version(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2019-06-25 07:37:07 +08:00
|
|
|
"""Avoid stale caches (#1671)"""
|
|
|
|
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-25 07:37:07 +08:00
|
|
|
test_foo="""
|
|
|
|
def test_foo():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-06-25 07:37:07 +08:00
|
|
|
assert result.ret == 0
|
2020-10-03 04:16:22 +08:00
|
|
|
found_names = glob.glob(f"__pycache__/*-pytest-{pytest.__version__}.pyc")
|
2019-06-25 07:37:07 +08:00
|
|
|
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')
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_pyc_vs_pyo(self, pytester: Pytester, monkeypatch) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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-10-29 15:54:34 +08:00
|
|
|
p = make_numbered_dir(root=Path(pytester.path), 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)
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.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)
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_package(self, pytester: Pytester) -> None:
|
|
|
|
pkg = pytester.path.joinpath("pkg")
|
2011-08-30 12:12:07 +08:00
|
|
|
pkg.mkdir()
|
2020-10-29 15:54:34 +08:00
|
|
|
pkg.joinpath("__init__.py")
|
|
|
|
pkg.joinpath("test_blah.py").write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-08-30 12:12:07 +08:00
|
|
|
def test_rewritten():
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "@py_builtins" in globals()"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2011-09-21 05:53:07 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_translate_newlines(self, pytester: Pytester) -> None:
|
2011-09-21 05:53:07 +08:00
|
|
|
content = "def test_rewritten():\r\n assert '@py_builtins' in globals()"
|
|
|
|
b = content.encode("utf-8")
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.path.joinpath("test_newlines.py").write_bytes(b)
|
|
|
|
assert pytester.runpytest().ret == 0
|
2013-03-08 23:44:41 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_package_without__init__py(self, pytester: Pytester) -> None:
|
|
|
|
pkg = pytester.mkdir("a_package_without_init_py")
|
|
|
|
pkg.joinpath("module.py").touch()
|
|
|
|
pytester.makepyfile("import a_package_without_init_py.module")
|
|
|
|
assert pytester.runpytest().ret == ExitCode.NO_TESTS_COLLECTED
|
2013-08-02 04:11:18 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_warning(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-09-04 07:13:41 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
pytest.register_assert_rewrite("_pytest")
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# needs to be a subprocess because pytester explicitly disables this warning
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_module_imported_from_conftest(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-08-03 06:16:27 +08:00
|
|
|
import test_rewrite_module_imported
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest_subprocess().ret == 0
|
2016-08-03 06:16:27 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_remember_rewritten_modules(
|
|
|
|
self, pytestconfig, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`AssertionRewriteHook` should remember rewritten modules so it
|
|
|
|
doesn't give false positives (#2005)."""
|
2020-10-29 15:54:34 +08:00
|
|
|
monkeypatch.syspath_prepend(pytester.path)
|
|
|
|
pytester.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 == []
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_warning_using_pytest_plugins(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"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",
|
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.chdir()
|
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_warning_using_pytest_plugins_env_var(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "plugin")
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"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
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.chdir()
|
|
|
|
result = pytester.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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_sys_meta_path_munged(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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 = []"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
assert pytester.runpytest().ret == 0
|
2014-03-28 15:33:12 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_write_pyc(self, pytester: Pytester, tmp_path, 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-10-29 15:54:34 +08:00
|
|
|
config = pytester.parseconfig()
|
2013-08-01 21:43:42 +08:00
|
|
|
state = AssertionState(config, "rewrite")
|
2020-10-29 15:54:34 +08:00
|
|
|
tmp_path.joinpath("source.py").touch()
|
|
|
|
source_path = str(tmp_path)
|
|
|
|
pycpath = tmp_path.joinpath("pyc")
|
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
|
2022-03-17 03:34:40 +08:00
|
|
|
yield # type:ignore[unreachable]
|
2019-11-07 21:41:26 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_resources_provider_for_loader(self, pytester: Pytester) -> None:
|
2013-10-10 23:40:31 +08:00
|
|
|
"""
|
|
|
|
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")
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.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
|
|
|
}
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(**contents)
|
|
|
|
pytester.maketxtfile(**{"testpkg/resource": "Load me please."})
|
2013-10-10 23:40:31 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2015-04-28 17:54:46 +08:00
|
|
|
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()
|
2020-11-09 21:07:51 +08:00
|
|
|
strip_bytes = 20 # header is around 16 bytes, strip a little more
|
2014-08-03 05:01:28 +08:00
|
|
|
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
|
|
|
|
2022-01-19 00:46:06 +08:00
|
|
|
def test_read_pyc_success(self, tmp_path: Path, pytester: Pytester) -> None:
|
|
|
|
"""
|
|
|
|
Ensure that the _rewrite_test() -> _write_pyc() produces a pyc file
|
|
|
|
that can be properly read with _read_pyc()
|
|
|
|
"""
|
|
|
|
from _pytest.assertion import AssertionState
|
|
|
|
from _pytest.assertion.rewrite import _read_pyc
|
|
|
|
from _pytest.assertion.rewrite import _rewrite_test
|
|
|
|
from _pytest.assertion.rewrite import _write_pyc
|
|
|
|
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
state = AssertionState(config, "rewrite")
|
|
|
|
|
|
|
|
fn = tmp_path / "source.py"
|
|
|
|
pyc = Path(str(fn) + "c")
|
|
|
|
|
|
|
|
fn.write_text("def test(): assert True")
|
|
|
|
|
|
|
|
source_stat, co = _rewrite_test(fn, config)
|
|
|
|
_write_pyc(state, co, source_stat, pyc)
|
|
|
|
assert _read_pyc(fn, pyc, state.trace) is not None
|
|
|
|
|
2020-11-09 21:07:51 +08:00
|
|
|
def test_read_pyc_more_invalid(self, tmp_path: Path) -> None:
|
|
|
|
from _pytest.assertion.rewrite import _read_pyc
|
|
|
|
|
|
|
|
source = tmp_path / "source.py"
|
|
|
|
pyc = tmp_path / "source.pyc"
|
|
|
|
|
|
|
|
source_bytes = b"def test(): pass\n"
|
|
|
|
source.write_bytes(source_bytes)
|
|
|
|
|
|
|
|
magic = importlib.util.MAGIC_NUMBER
|
|
|
|
|
|
|
|
flags = b"\x00\x00\x00\x00"
|
|
|
|
|
|
|
|
mtime = b"\x58\x3c\xb0\x5f"
|
|
|
|
mtime_int = int.from_bytes(mtime, "little")
|
|
|
|
os.utime(source, (mtime_int, mtime_int))
|
|
|
|
|
|
|
|
size = len(source_bytes).to_bytes(4, "little")
|
|
|
|
|
|
|
|
code = marshal.dumps(compile(source_bytes, str(source), "exec"))
|
|
|
|
|
|
|
|
# Good header.
|
|
|
|
pyc.write_bytes(magic + flags + mtime + size + code)
|
|
|
|
assert _read_pyc(source, pyc, print) is not None
|
|
|
|
|
|
|
|
# Too short.
|
|
|
|
pyc.write_bytes(magic + flags + mtime)
|
|
|
|
assert _read_pyc(source, pyc, print) is None
|
|
|
|
|
|
|
|
# Bad magic.
|
|
|
|
pyc.write_bytes(b"\x12\x34\x56\x78" + flags + mtime + size + code)
|
|
|
|
assert _read_pyc(source, pyc, print) is None
|
|
|
|
|
|
|
|
# Unsupported flags.
|
|
|
|
pyc.write_bytes(magic + b"\x00\xff\x00\x00" + mtime + size + code)
|
|
|
|
assert _read_pyc(source, pyc, print) is None
|
|
|
|
|
|
|
|
# Bad mtime.
|
|
|
|
pyc.write_bytes(magic + flags + b"\x58\x3d\xb0\x5f" + size + code)
|
|
|
|
assert _read_pyc(source, pyc, print) is None
|
|
|
|
|
|
|
|
# Bad size.
|
|
|
|
pyc.write_bytes(magic + flags + mtime + b"\x99\x00\x00\x00" + code)
|
|
|
|
assert _read_pyc(source, pyc, print) is None
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_reload_is_same_and_reloads(self, pytester: Pytester) -> None:
|
2020-04-02 21:25:53 +08:00
|
|
|
"""Reloading a (collected) module after change picks up the change."""
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makeini(
|
2020-04-02 21:25:53 +08:00
|
|
|
"""
|
2015-03-04 23:21:27 +08:00
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2020-04-02 21:25:53 +08:00
|
|
|
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-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2018-09-23 19:05:55 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed*"])
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_get_data_support(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Implement optional PEP302 api (#808)."""
|
2020-10-29 15:54:34 +08:00
|
|
|
path = pytester.mkpydir("foo")
|
|
|
|
path.joinpath("test_foo.py").write_text(
|
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
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
path.joinpath("data.txt").write_text("Hey")
|
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_issue731(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_simple_case(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-06-25 23:21:10 +08:00
|
|
|
def test_ternary_display():
|
|
|
|
assert (False == False) == False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_long_case(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_many_brackets(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_python_files_contain_subdirs(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-09-01 21:59:21 +08:00
|
|
|
**{
|
|
|
|
"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
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makeini(
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
python_files = tests/**.py
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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(
|
2022-02-01 06:20:16 +08:00
|
|
|
sys.maxsize <= (2**31 - 1), reason="Causes OverflowError on 32bit systems"
|
2019-04-04 18:53:55 +08:00
|
|
|
)
|
2019-03-14 19:16:07 +08:00
|
|
|
@pytest.mark.parametrize("offset", [-1, +1])
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_source_mtime_long_long(pytester: Pytester, offset) -> None:
|
2019-03-14 19:16:07 +08:00
|
|
|
"""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
|
|
|
|
"""
|
2020-10-29 15:54:34 +08:00
|
|
|
p = pytester.makepyfile(
|
2019-03-14 19:16:07 +08:00
|
|
|
"""
|
|
|
|
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
|
2022-02-01 06:20:16 +08:00
|
|
|
timestamp = 2**32 + offset
|
2019-03-14 19:16:07 +08:00
|
|
|
os.utime(str(p), (timestamp, timestamp))
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-14 19:16:07 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_rewrite_infinite_recursion(
|
|
|
|
pytester: Pytester, 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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(test_foo="def test_foo(): pass")
|
|
|
|
pytester.makepyfile(test_bar="def test_bar(): pass")
|
2018-08-28 07:51:16 +08:00
|
|
|
|
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-10-29 15:54:34 +08:00
|
|
|
def hook(
|
|
|
|
self, pytestconfig, monkeypatch, pytester: Pytester
|
2022-02-08 08:29:01 +08:00
|
|
|
) -> Generator[AssertionRewritingHook, None, None]:
|
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-10-06 09:13:05 +08:00
|
|
|
self.find_spec_calls: List[str] = []
|
2020-12-14 21:54:59 +08:00
|
|
|
self.initial_paths: Set[Path] = set()
|
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-06-09 02:21:58 +08:00
|
|
|
return importlib.machinery.PathFinder.find_spec(name, path)
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
|
|
|
# use default patterns, otherwise we inherit pytest's testing config
|
2022-02-08 08:29:01 +08:00
|
|
|
with mock.patch.object(hook, "fnpats", ["test_*.py", "*_test.py"]):
|
|
|
|
monkeypatch.setattr(hook, "_find_spec", spy_find_spec)
|
|
|
|
hook.set_session(StubSession()) # type: ignore[arg-type]
|
|
|
|
pytester.syspathinsert()
|
|
|
|
yield hook
|
2018-09-01 21:59:21 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_basic(self, pytester: Pytester, 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
|
|
|
"""
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makeconftest(
|
2018-09-01 21:59:21 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(): return 1
|
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(test_foo="def test_foo(): pass")
|
|
|
|
pytester.makepyfile(bar="def bar(): pass")
|
|
|
|
foobar_path = pytester.makepyfile(foobar="def foobar(): pass")
|
2020-12-14 21:54:59 +08:00
|
|
|
self.initial_paths.add(foobar_path)
|
2018-09-01 21:59:21 +08:00
|
|
|
|
|
|
|
# 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(
|
2020-10-29 15:54:34 +08:00
|
|
|
self, pytester: Pytester, hook: AssertionRewritingHook
|
2020-05-01 19:40:17 +08:00
|
|
|
) -> 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
|
|
|
"""
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-09-01 21:59:21 +08:00
|
|
|
**{
|
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
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.syspathinsert("tests")
|
2022-02-08 08:29:01 +08:00
|
|
|
with mock.patch.object(hook, "fnpats", ["tests/**.py"]):
|
|
|
|
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"
|
|
|
|
)
|
2021-08-24 18:11:06 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform.startswith("sunos5"), reason="cannot remove cwd on Solaris"
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_cwd_changed(self, pytester: Pytester, monkeypatch) -> None:
|
2019-02-14 05:42:04 +08:00
|
|
|
# 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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2018-09-14 05:50:05 +08:00
|
|
|
**{
|
2019-06-28 10:11:20 +08:00
|
|
|
"test_setup_nonexisting_cwd.py": """\
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
2021-03-09 11:12:08 +08:00
|
|
|
with tempfile.TemporaryDirectory() as d:
|
|
|
|
os.chdir(d)
|
2019-06-28 10:11:20 +08:00
|
|
|
""",
|
|
|
|
"test_test.py": """\
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
""",
|
2018-09-14 05:50:05 +08:00
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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:
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_option_default(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2019-06-27 04:54:24 +08:00
|
|
|
assert config.getini("enable_assertion_pass_hook") is False
|
|
|
|
|
2019-06-28 10:11:20 +08:00
|
|
|
@pytest.fixture
|
2020-10-29 15:54:34 +08:00
|
|
|
def flag_on(self, pytester: Pytester):
|
|
|
|
pytester.makeini("[pytest]\nenable_assertion_pass_hook = True\n")
|
2019-06-28 10:11:20 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
2020-10-29 15:54:34 +08:00
|
|
|
def hook_on(self, pytester: Pytester):
|
|
|
|
pytester.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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_hook_call(self, pytester: Pytester, flag_on, hook_on) -> None:
|
|
|
|
pytester.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
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.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*"
|
|
|
|
)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_hook_call_with_parens(self, pytester: Pytester, flag_on, hook_on) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-28 10:11:20 +08:00
|
|
|
"""\
|
|
|
|
def f(): return 1
|
|
|
|
def test():
|
|
|
|
assert f()
|
|
|
|
"""
|
2019-06-24 22:09:39 +08:00
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-28 10:11:20 +08:00
|
|
|
result.stdout.fnmatch_lines("*Assertion Passed: f() 1")
|
2019-06-24 22:09:39 +08:00
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_hook_not_called_without_hookimpl(
|
|
|
|
self, pytester: Pytester, monkeypatch, flag_on
|
|
|
|
) -> None:
|
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
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.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
|
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-26 01:49:05 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_hook_not_called_without_cmd_option(
|
|
|
|
self, pytester: Pytester, monkeypatch
|
|
|
|
) -> None:
|
2019-06-26 01:49:05 +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!")
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
_pytest.assertion.rewrite, "_call_assertion_pass", raise_on_assertionpass
|
|
|
|
)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.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))
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.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
|
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-24 22:09:39 +08:00
|
|
|
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
|
|
|
|
),
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_get_assertion_exprs(src, expected) -> None:
|
2019-06-28 10:11:20 +08:00
|
|
|
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):
|
2021-10-09 18:34:13 +08:00
|
|
|
assert isinstance(p, Path)
|
2019-08-02 07:55:30 +08:00
|
|
|
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__"),
|
|
|
|
],
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_get_cache_dir(self, monkeypatch, prefix, source, expected) -> None:
|
2020-10-25 07:21:47 +08:00
|
|
|
monkeypatch.delenv("PYTHONPYCACHEPREFIX", raising=False)
|
|
|
|
|
|
|
|
if prefix is not None and sys.version_info < (3, 8):
|
|
|
|
pytest.skip("pycache_prefix not available in py<38")
|
|
|
|
monkeypatch.setattr(sys, "pycache_prefix", prefix, raising=False)
|
2019-09-19 07:29:24 +08:00
|
|
|
|
|
|
|
assert get_cache_dir(Path(source)) == Path(expected)
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 8), reason="pycache_prefix not available in py<38"
|
|
|
|
)
|
2021-11-19 22:22:08 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info[:2] == (3, 9) and sys.platform.startswith("win"),
|
|
|
|
reason="#9298",
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
def test_sys_pycache_prefix_integration(
|
|
|
|
self, tmp_path, monkeypatch, pytester: Pytester
|
|
|
|
) -> None:
|
2019-09-19 07:29:24 +08:00
|
|
|
"""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)
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
pytester.makepyfile(
|
2019-09-19 07:29:24 +08:00
|
|
|
**{
|
|
|
|
"src/test_foo.py": """
|
|
|
|
import bar
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
"src/bar/__init__.py": "",
|
|
|
|
}
|
|
|
|
)
|
2020-10-29 15:54:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-09-19 07:29:24 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-10-29 15:54:34 +08:00
|
|
|
test_foo = pytester.path.joinpath("src/test_foo.py")
|
|
|
|
bar_init = pytester.path.joinpath("src/bar/__init__.py")
|
2019-09-19 07:29:24 +08:00
|
|
|
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()
|
2021-03-03 08:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestReprSizeVerbosity:
|
|
|
|
"""
|
|
|
|
Check that verbosity also controls the string length threshold to shorten it using
|
|
|
|
ellipsis.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"verbose, expected_size",
|
|
|
|
[
|
|
|
|
(0, DEFAULT_REPR_MAX_SIZE),
|
|
|
|
(1, DEFAULT_REPR_MAX_SIZE * 10),
|
|
|
|
(2, None),
|
|
|
|
(3, None),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_maxsize_for_saferepr(self, verbose: int, expected_size) -> None:
|
|
|
|
class FakeConfig:
|
|
|
|
def getoption(self, name: str) -> int:
|
|
|
|
assert name == "verbose"
|
|
|
|
return verbose
|
|
|
|
|
|
|
|
config = FakeConfig()
|
|
|
|
assert _get_maxsize_for_saferepr(cast(Config, config)) == expected_size
|
|
|
|
|
|
|
|
def create_test_file(self, pytester: Pytester, size: int) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
f"""
|
|
|
|
def test_very_long_string():
|
|
|
|
text = "x" * {size}
|
|
|
|
assert "hello world" in text
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_default_verbosity(self, pytester: Pytester) -> None:
|
|
|
|
self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*xxx...xxx*"])
|
|
|
|
|
|
|
|
def test_increased_verbosity(self, pytester: Pytester) -> None:
|
|
|
|
self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE)
|
|
|
|
result = pytester.runpytest("-v")
|
|
|
|
result.stdout.no_fnmatch_line("*xxx...xxx*")
|
|
|
|
|
|
|
|
def test_max_increased_verbosity(self, pytester: Pytester) -> None:
|
|
|
|
self.create_test_file(pytester, DEFAULT_REPR_MAX_SIZE * 10)
|
|
|
|
result = pytester.runpytest("-vv")
|
|
|
|
result.stdout.no_fnmatch_line("*xxx...xxx*")
|