2019-05-28 07:31:52 +08:00
|
|
|
import ast
|
2016-11-21 00:56:15 +08:00
|
|
|
import glob
|
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
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
import py
|
|
|
|
|
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
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.assertion.rewrite import AssertionRewritingHook
|
|
|
|
from _pytest.assertion.rewrite import PYTEST_TAG
|
|
|
|
from _pytest.assertion.rewrite import rewrite_asserts
|
2019-06-07 18:58:51 +08:00
|
|
|
from _pytest.main import ExitCode
|
2011-05-19 04:31:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
def setup_module(mod):
|
2011-05-27 01:01:34 +08:00
|
|
|
mod._old_reprcompare = util._reprcompare
|
2015-11-27 22:43:01 +08:00
|
|
|
_pytest._code._reprcompare = None
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def teardown_module(mod):
|
2011-05-27 01:01:34 +08:00
|
|
|
util._reprcompare = mod._old_reprcompare
|
2011-05-19 04:31:10 +08:00
|
|
|
del mod._old_reprcompare
|
|
|
|
|
|
|
|
|
2011-05-20 05:53:13 +08:00
|
|
|
def rewrite(src):
|
|
|
|
tree = ast.parse(src)
|
|
|
|
rewrite_asserts(tree)
|
|
|
|
return tree
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def getmsg(f, extra_ns=None, must_pass=False):
|
|
|
|
"""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")
|
|
|
|
ns = {}
|
|
|
|
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:
|
|
|
|
func()
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-05-25 06:30:35 +08:00
|
|
|
def test_dont_rewrite(self):
|
|
|
|
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
|
|
|
|
m = rewrite(s)
|
2019-02-06 04:48:18 +08:00
|
|
|
assert len(m.body) == 2
|
|
|
|
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()
|
|
|
|
assert "warnings" not in "".join(result.outlines)
|
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
def test_name(self, request):
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
f = False
|
|
|
|
assert f
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
2013-10-12 21:39:22 +08:00
|
|
|
assert a_global # noqa
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:08 +08:00
|
|
|
assert getmsg(f, {"a_global": False}) == "assert False"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-01-11 01:59:08 +08:00
|
|
|
def f():
|
|
|
|
assert sys == 42
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
verbose = request.config.getoption("verbose")
|
|
|
|
msg = getmsg(f, {"sys": sys})
|
|
|
|
if verbose > 0:
|
|
|
|
assert msg == (
|
|
|
|
"assert <module 'sys' (built-in)> == 42\n"
|
|
|
|
" -<module 'sys' (built-in)>\n"
|
|
|
|
" +42"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
assert msg == "assert sys == 42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-01-11 01:59:08 +08:00
|
|
|
def f():
|
2019-03-18 10:05:33 +08:00
|
|
|
assert cls == 42 # 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
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
msg = getmsg(f, {"cls": X}).splitlines()
|
|
|
|
if verbose > 0:
|
2019-05-28 07:31:52 +08:00
|
|
|
|
|
|
|
assert msg == [
|
|
|
|
"assert <class 'test_...e.<locals>.X'> == 42",
|
|
|
|
" -<class 'test_assertrewrite.TestAssertionRewrite.test_name.<locals>.X'>",
|
|
|
|
" +42",
|
|
|
|
]
|
2019-03-18 10:05:33 +08:00
|
|
|
else:
|
|
|
|
assert msg == ["assert cls == 42"]
|
|
|
|
|
|
|
|
def test_dont_rewrite_if_hasattr_fails(self, request):
|
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()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Y"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.foo = 3
|
|
|
|
|
|
|
|
def f():
|
|
|
|
assert cls().foo == 2 # noqa
|
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
# XXX: looks like the "where" should also be there in verbose mode?!
|
|
|
|
message = getmsg(f, {"cls": Y}).splitlines()
|
|
|
|
if request.config.getoption("verbose") > 0:
|
|
|
|
assert message == ["assert 3 == 2", " -3", " +2"]
|
|
|
|
else:
|
|
|
|
assert message == [
|
|
|
|
"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"])
|
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def test_boolop(self):
|
|
|
|
def f():
|
|
|
|
f = g = False
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
f = True
|
|
|
|
g = False
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (True and False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
f = False
|
|
|
|
g = True
|
|
|
|
assert f and g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
f = g = False
|
|
|
|
assert f or g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (False or False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-06-29 09:21:22 +08:00
|
|
|
def f():
|
|
|
|
f = g = False
|
|
|
|
assert not f and not g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-06-29 09:21:22 +08:00
|
|
|
getmsg(f, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-08-30 22:34:21 +08:00
|
|
|
def x():
|
|
|
|
return False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-08-30 22:34:21 +08:00
|
|
|
def f():
|
|
|
|
assert x() and x()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, {"x": x})
|
|
|
|
== """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
|
|
|
|
2011-08-30 22:34:21 +08:00
|
|
|
def f():
|
|
|
|
assert False or x()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, {"x": x})
|
|
|
|
== """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
|
|
|
|
2011-10-15 04:26:13 +08:00
|
|
|
def f():
|
|
|
|
assert 1 in {} and 2 in {}
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-10-15 04:26:13 +08:00
|
|
|
assert getmsg(f) == "assert (1 in {})"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-10-15 04:26:13 +08:00
|
|
|
def f():
|
|
|
|
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
|
|
|
|
2011-10-15 04:26:13 +08:00
|
|
|
assert getmsg(f) == "assert (1 in {1: None} and 2 in {})"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
f = True
|
|
|
|
g = False
|
|
|
|
assert f or g
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
getmsg(f, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-07-11 17:57:47 +08:00
|
|
|
def f():
|
|
|
|
f = g = h = lambda: True
|
|
|
|
assert f() and g() and h()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-07-11 17:57:47 +08:00
|
|
|
getmsg(f, must_pass=True)
|
|
|
|
|
2017-02-15 23:00:18 +08:00
|
|
|
def test_short_circuit_evaluation(self):
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
2013-10-12 21:39:22 +08:00
|
|
|
assert True or explode # noqa
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
getmsg(f, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-06-29 09:21:22 +08:00
|
|
|
def f():
|
|
|
|
x = 1
|
|
|
|
assert x == 1 or x == 2
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-06-29 09:21:22 +08:00
|
|
|
getmsg(f, must_pass=True)
|
2011-05-19 04:31:10 +08:00
|
|
|
|
|
|
|
def test_unary_op(self):
|
|
|
|
def f():
|
|
|
|
x = True
|
|
|
|
assert not x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert not True"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
x = 0
|
|
|
|
assert ~x + 1
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (~0 + 1)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
x = 3
|
|
|
|
assert -x + x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (-3 + 3)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
x = 0
|
|
|
|
assert +x + x
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (+0 + 0)"
|
|
|
|
|
|
|
|
def test_binary_op(self):
|
|
|
|
def f():
|
|
|
|
x = 1
|
|
|
|
y = -1
|
|
|
|
assert x + y
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert (1 + -1)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2012-05-04 01:49:30 +08:00
|
|
|
def f():
|
|
|
|
assert not 5 % 4
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-05-04 01:49:30 +08:00
|
|
|
assert getmsg(f) == "assert not (5 % 4)"
|
2011-05-19 04:31:10 +08:00
|
|
|
|
2014-10-27 16:31:33 +08:00
|
|
|
def test_boolop_percent(self):
|
2014-10-13 16:26:18 +08:00
|
|
|
def f():
|
2014-10-27 16:31:33 +08:00
|
|
|
assert 3 % 2 and False
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-27 16:31:33 +08:00
|
|
|
assert getmsg(f) == "assert ((3 % 2) and False)"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-13 16:26:18 +08:00
|
|
|
def f():
|
2014-10-27 16:31:33 +08:00
|
|
|
assert False or 4 % 2
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-10-27 16:31:33 +08:00
|
|
|
assert getmsg(f) == "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)
|
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def test_call(self):
|
|
|
|
def g(a=42, *args, **kwargs):
|
|
|
|
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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert g()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert g(1)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert g(1, 2)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert g(1, g=42)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert g(1, 3, g=23)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-07-15 00:46:32 +08:00
|
|
|
def f():
|
|
|
|
seq = [1, 2, 3]
|
|
|
|
assert g(*seq)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-07-15 00:45:42 +08:00
|
|
|
def f():
|
|
|
|
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 (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
|
|
|
def test_attribute(self):
|
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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
2017-07-17 07:25:09 +08:00
|
|
|
assert not x.g # noqa
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
2013-10-12 21:39:22 +08:00
|
|
|
x.a = False # noqa
|
2018-05-23 22:48:46 +08:00
|
|
|
assert x.a # noqa
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
getmsg(f, ns)
|
|
|
|
== """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
|
|
|
|
|
|
|
def test_comparisons(self):
|
|
|
|
def f():
|
|
|
|
a, b = range(2)
|
|
|
|
assert b < a
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == """assert 1 < 0"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
a, b, c = range(3)
|
|
|
|
assert a > b > c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == """assert 0 > 1"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b > c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == """assert 1 > 2"""
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b <= c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
getmsg(f, must_pass=True)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-20 07:56:48 +08:00
|
|
|
def f():
|
|
|
|
a, b, c = range(3)
|
|
|
|
assert a < b
|
|
|
|
assert b < c
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-20 07:56:48 +08:00
|
|
|
getmsg(f, 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:
|
|
|
|
assert msg == "assert 10 == 11\n -10\n +11"
|
|
|
|
else:
|
|
|
|
assert msg == "assert 10 == 11\n + where 10 = len([0, 1, 2, 3, 4, 5, ...])"
|
2011-05-19 04:31:10 +08:00
|
|
|
|
|
|
|
def test_custom_reprcompare(self, monkeypatch):
|
|
|
|
def my_reprcompare(op, left, right):
|
|
|
|
return "42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-27 01:01:34 +08:00
|
|
|
monkeypatch.setattr(util, "_reprcompare", my_reprcompare)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert 42 < 3
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert 42"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def my_reprcompare(op, left, right):
|
2018-08-23 09:30:42 +08:00
|
|
|
return "{} {} {}".format(left, op, right)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-27 01:01:34 +08:00
|
|
|
monkeypatch.setattr(util, "_reprcompare", my_reprcompare)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
def f():
|
|
|
|
assert 1 < 3 < 5 <= 4 < 7
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-05-19 04:31:10 +08:00
|
|
|
assert getmsg(f) == "assert 5 <= 4"
|
2011-05-25 07:15:08 +08:00
|
|
|
|
|
|
|
def test_assert_raising_nonzero_in_comparison(self):
|
|
|
|
def f():
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2011-05-25 07:15:08 +08:00
|
|
|
def __nonzero__(self):
|
|
|
|
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
|
|
|
|
2011-05-25 07:15:08 +08:00
|
|
|
def myany(x):
|
|
|
|
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
|
|
|
|
2011-05-25 07:15:08 +08:00
|
|
|
assert "<MY42 object> < 0" in getmsg(f)
|
2011-07-13 06:09:14 +08:00
|
|
|
|
2011-07-20 10:42:00 +08:00
|
|
|
def test_formatchar(self):
|
|
|
|
def f():
|
|
|
|
assert "%test" == "test"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2011-07-20 10:42:00 +08:00
|
|
|
assert getmsg(f).startswith("assert '%test' == 'test'")
|
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
def test_custom_repr(self, request):
|
2014-08-19 02:07:38 +08:00
|
|
|
def f():
|
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
|
|
|
|
2019-03-18 10:05:33 +08:00
|
|
|
lines = util._format_lines([getmsg(f)])
|
|
|
|
if request.config.getoption("verbose") > 0:
|
|
|
|
assert lines == ["assert 0 == 1\n -0\n +1"]
|
|
|
|
else:
|
|
|
|
assert lines == ["assert 0 == 1\n + where 1 = \\n{ \\n~ \\n}.a"]
|
2014-08-19 02:07:38 +08:00
|
|
|
|
2018-09-20 00:44:26 +08:00
|
|
|
def test_custom_repr_non_ascii(self):
|
|
|
|
def f():
|
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)
|
|
|
|
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
|
|
|
|
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
|
|
|
)
|
|
|
|
p = py.path.local.make_numbered_dir(
|
|
|
|
prefix="runpytest-", keep=None, rootdir=testdir.tmpdir
|
|
|
|
)
|
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)
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
hook.find_module("test_remember_rewritten_modules")
|
|
|
|
hook.load_module("test_remember_rewritten_modules")
|
|
|
|
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 *=*"])
|
|
|
|
assert "pytest-warning summary" not in result.stdout.str()
|
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 *=*"])
|
|
|
|
assert "pytest-warning summary" not in result.stdout.str()
|
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:
|
2013-08-02 04:11:18 +08:00
|
|
|
def test_loader_is_package_false_for_module(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_fun="""
|
2013-08-02 04:11:18 +08:00
|
|
|
def test_loader():
|
|
|
|
assert not __loader__.is_package(__name__)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-08-02 04:11:18 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed*"])
|
2013-08-02 04:11:18 +08:00
|
|
|
|
|
|
|
def test_loader_is_package_true_for_package(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_fun="""
|
2013-08-02 04:11:18 +08:00
|
|
|
def test_loader():
|
|
|
|
assert not __loader__.is_package(__name__)
|
|
|
|
|
|
|
|
def test_fun():
|
|
|
|
assert __loader__.is_package('fun')
|
|
|
|
|
|
|
|
def test_missing():
|
|
|
|
assert not __loader__.is_package('pytest_not_there')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.mkpydir("fun")
|
2013-08-02 04:11:18 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 3 passed*"])
|
2013-08-02 04:11:18 +08:00
|
|
|
|
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
|
|
|
|
|
2013-08-01 21:43:42 +08:00
|
|
|
def test_write_pyc(self, testdir, tmpdir, monkeypatch):
|
|
|
|
from _pytest.assertion.rewrite import _write_pyc
|
|
|
|
from _pytest.assertion import AssertionState
|
2018-04-12 06:41:10 +08:00
|
|
|
import atomicwrites
|
|
|
|
from contextlib import contextmanager
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2013-08-01 21:43:42 +08:00
|
|
|
config = testdir.parseconfig([])
|
|
|
|
state = AssertionState(config, "rewrite")
|
|
|
|
source_path = tmpdir.ensure("source.py")
|
|
|
|
pycpath = tmpdir.join("pyc").strpath
|
2014-09-02 04:51:27 +08:00
|
|
|
assert _write_pyc(state, [1], source_path.stat(), pycpath)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-04-12 06:41:10 +08:00
|
|
|
@contextmanager
|
2018-05-23 22:48:46 +08:00
|
|
|
def atomic_write_failed(fn, mode="r", overwrite=False):
|
2013-08-01 21:43:42 +08:00
|
|
|
e = IOError()
|
|
|
|
e.errno = 10
|
|
|
|
raise e
|
2018-07-09 07:57:18 +08:00
|
|
|
yield
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-04-12 06:41:10 +08:00
|
|
|
monkeypatch.setattr(atomicwrites, "atomic_write", atomic_write_failed)
|
2014-09-02 04:51:27 +08:00
|
|
|
assert not _write_pyc(state, [1], source_path.stat(), 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
|
|
|
|
|
|
|
def test_read_pyc(self, tmpdir):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
source = tmpdir.join("source.py")
|
|
|
|
pyc = source + "c"
|
2014-08-03 05:01:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
source.write("def test(): pass")
|
2014-08-03 05:01:28 +08:00
|
|
|
py_compile.compile(str(source), str(pyc))
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
contents = pyc.read(mode="rb")
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
pyc.write(contents[:strip_bytes], mode="wb")
|
2014-08-03 05:01:28 +08:00
|
|
|
|
|
|
|
assert _read_pyc(source, str(pyc)) is None # no error
|
2015-03-04 23:21:27 +08:00
|
|
|
|
|
|
|
def test_reload_is_same(self, testdir):
|
|
|
|
# A file that will be picked up during collecting.
|
|
|
|
testdir.tmpdir.join("file.py").ensure()
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.tmpdir.join("pytest.ini").write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2015-03-04 23:21:27 +08:00
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2015-03-04 23:21:27 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_fun="""
|
2015-03-04 23:21:27 +08:00
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
from imp import reload
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_loader():
|
|
|
|
import file
|
|
|
|
assert sys.modules["file"] is reload(file)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-s")
|
|
|
|
result.stdout.fnmatch_lines(["* 1 passed*"])
|
2015-04-30 09:31:12 +08:00
|
|
|
|
2018-09-23 19:05:55 +08:00
|
|
|
def test_reload_reloads(self, testdir):
|
|
|
|
"""Reloading a module after change picks up the change."""
|
|
|
|
testdir.tmpdir.join("file.py").write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
|
|
|
def reloaded():
|
|
|
|
return False
|
|
|
|
|
|
|
|
def rewrite_self():
|
|
|
|
with open(__file__, 'w') as self:
|
|
|
|
self.write('def reloaded(): return True')
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
testdir.tmpdir.join("pytest.ini").write(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
python_files = *.py
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_fun="""
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
from imp import reload
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_loader():
|
|
|
|
import file
|
|
|
|
assert not file.reloaded()
|
|
|
|
file.rewrite_self()
|
|
|
|
reload(file)
|
|
|
|
assert file.reloaded()
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-s")
|
|
|
|
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()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "unbalanced braces" not in result.stdout.str()
|
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
|
|
|
|
|
|
|
|
|
2018-08-28 07:51:16 +08:00
|
|
|
def test_rewrite_infinite_recursion(testdir, pytestconfig, monkeypatch):
|
|
|
|
"""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)"""
|
|
|
|
from _pytest.assertion import rewrite
|
|
|
|
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile(test_foo="def test_foo(): pass")
|
|
|
|
testdir.makepyfile(test_bar="def test_bar(): pass")
|
|
|
|
|
|
|
|
original_write_pyc = rewrite._write_pyc
|
|
|
|
|
|
|
|
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
|
|
|
|
assert hook.find_module("test_bar") is None
|
|
|
|
return original_write_pyc(*args, **kwargs)
|
|
|
|
|
|
|
|
monkeypatch.setattr(rewrite, "_write_pyc", spy_write_pyc)
|
|
|
|
monkeypatch.setattr(sys, "dont_write_bytecode", False)
|
|
|
|
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
|
|
|
assert hook.find_module("test_foo") is not None
|
|
|
|
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
|
|
|
|
def hook(self, pytestconfig, monkeypatch, testdir):
|
|
|
|
"""Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
|
|
|
|
if imp.find_module has been called.
|
|
|
|
"""
|
|
|
|
import imp
|
|
|
|
|
|
|
|
self.find_module_calls = []
|
|
|
|
self.initial_paths = set()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def spy_imp_find_module(name, path):
|
|
|
|
self.find_module_calls.append(name)
|
|
|
|
return imp.find_module(name, path)
|
|
|
|
|
|
|
|
hook = AssertionRewritingHook(pytestconfig)
|
|
|
|
# use default patterns, otherwise we inherit pytest's testing config
|
|
|
|
hook.fnpats[:] = ["test_*.py", "*_test.py"]
|
|
|
|
monkeypatch.setattr(hook, "_imp_find_module", spy_imp_find_module)
|
|
|
|
hook.set_session(StubSession())
|
|
|
|
testdir.syspathinsert()
|
|
|
|
return hook
|
|
|
|
|
|
|
|
def test_basic(self, testdir, hook):
|
|
|
|
"""
|
|
|
|
Ensure we avoid calling imp.find_module when we know for sure a certain module will not be rewritten
|
|
|
|
to optimize assertion rewriting (#3918).
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
assert hook.find_module("conftest") is not None
|
|
|
|
assert self.find_module_calls == ["conftest"]
|
|
|
|
|
|
|
|
# files matching "python_files" mask should always be rewritten
|
|
|
|
assert hook.find_module("test_foo") is not None
|
|
|
|
assert self.find_module_calls == ["conftest", "test_foo"]
|
|
|
|
|
|
|
|
# file does not match "python_files": early bailout
|
|
|
|
assert hook.find_module("bar") is None
|
|
|
|
assert self.find_module_calls == ["conftest", "test_foo"]
|
|
|
|
|
|
|
|
# file is an initial path (passed on the command-line): should be rewritten
|
|
|
|
assert hook.find_module("foobar") is not None
|
|
|
|
assert self.find_module_calls == ["conftest", "test_foo", "foobar"]
|
|
|
|
|
|
|
|
def test_pattern_contains_subdirectories(self, testdir, hook):
|
|
|
|
"""If one of the python_files patterns contain subdirectories ("tests/**.py") we can't bailout early
|
|
|
|
because we need to match with the full path, which can only be found by calling imp.find_module.
|
|
|
|
"""
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
**{
|
|
|
|
"tests/file.py": """
|
|
|
|
def test_simple_failure():
|
|
|
|
assert 1 + 1 == 3
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
)
|
|
|
|
testdir.syspathinsert(p.dirpath())
|
|
|
|
hook.fnpats[:] = ["tests/**.py"]
|
|
|
|
assert hook.find_module("file") is not None
|
|
|
|
assert self.find_module_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-02-14 05:42:04 +08:00
|
|
|
"test_setup_nonexisting_cwd.py": """
|
2018-09-14 05:50:05 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
d = tempfile.mkdtemp()
|
|
|
|
os.chdir(d)
|
|
|
|
shutil.rmtree(d)
|
|
|
|
""",
|
2019-02-14 05:42:04 +08:00
|
|
|
"test_test.py": """
|
2018-09-14 05:50:05 +08:00
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|