2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
2019-11-16 05:02:55 +08:00
|
|
|
from types import FrameType
|
2019-05-28 07:31:52 +08:00
|
|
|
from unittest import mock
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2020-01-29 08:20:45 +08:00
|
|
|
from _pytest._code import Code
|
|
|
|
from _pytest._code import ExceptionInfo
|
|
|
|
from _pytest._code import Frame
|
2020-04-03 06:56:53 +08:00
|
|
|
from _pytest._code.code import ExceptionChainRepr
|
2020-01-29 08:20:45 +08:00
|
|
|
from _pytest._code.code import ReprFuncArgs
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_ne() -> None:
|
2020-01-29 08:20:45 +08:00
|
|
|
code1 = Code(compile('foo = "bar"', "", "exec"))
|
2015-11-27 22:43:01 +08:00
|
|
|
assert code1 == code1
|
2020-01-29 08:20:45 +08:00
|
|
|
code2 = Code(compile('foo = "baz"', "", "exec"))
|
2015-11-27 22:43:01 +08:00
|
|
|
assert code2 != code1
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_gives_back_name_for_not_existing_file() -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
name = "abc-123"
|
|
|
|
co_code = compile("pass\n", name, "exec")
|
2015-11-27 22:43:01 +08:00
|
|
|
assert co_code.co_filename == name
|
2020-01-29 08:20:45 +08:00
|
|
|
code = Code(co_code)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(code.path) == name
|
|
|
|
assert code.fullsource is None
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_with_class() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
pytest.raises(TypeError, Code, A)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def x() -> None:
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_fullsource() -> None:
|
2020-01-29 08:20:45 +08:00
|
|
|
code = Code(x)
|
2015-11-27 22:43:01 +08:00
|
|
|
full = code.fullsource
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "test_code_fullsource()" in str(full)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_source() -> None:
|
2020-01-29 08:20:45 +08:00
|
|
|
code = Code(x)
|
2015-11-27 22:43:01 +08:00
|
|
|
src = code.source()
|
2019-11-16 05:02:55 +08:00
|
|
|
expected = """def x() -> None:
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()"""
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(src) == expected
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_frame_getsourcelineno_myself() -> None:
|
|
|
|
def func() -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
f = Frame(func())
|
2015-11-27 22:43:01 +08:00
|
|
|
source, lineno = f.code.fullsource, f.lineno
|
2019-11-16 05:02:55 +08:00
|
|
|
assert source is not None
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source[lineno].startswith(" return sys._getframe(0)")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatement_empty_fullsource() -> None:
|
|
|
|
def func() -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
f = Frame(func())
|
2018-08-24 00:06:17 +08:00
|
|
|
with mock.patch.object(f.code.__class__, "fullsource", None):
|
|
|
|
assert f.statement == ""
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_from_func() -> None:
|
2020-01-29 08:20:45 +08:00
|
|
|
co = Code(test_frame_getsourcelineno_myself)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert co.firstlineno
|
|
|
|
assert co.path
|
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_unicode_handling() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
value = "ąć".encode()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def f() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
raise Exception(value)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(Exception, f)
|
2019-06-03 06:32:00 +08:00
|
|
|
str(excinfo)
|
2016-03-06 03:58:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_getargs() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
def f1(x):
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
c1 = Code(f1)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert c1.getargs(var=True) == ("x",)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f2(x, *y):
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
c2 = Code(f2)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert c2.getargs(var=True) == ("x", "y")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f3(x, **z):
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
c3 = Code(f3)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert c3.getargs(var=True) == ("x", "z")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f4(x, *y, **z):
|
2018-08-27 07:13:22 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
c4 = Code(f4)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert c4.getargs(var=True) == ("x", "y", "z")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_frame_getargs() -> None:
|
|
|
|
def f1(x) -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
fr1 = Frame(f1("a"))
|
2018-05-23 22:48:46 +08:00
|
|
|
assert fr1.getargs(var=True) == [("x", "a")]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def f2(x, *y) -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
fr2 = Frame(f2("a", "b", "c"))
|
2018-05-23 22:48:46 +08:00
|
|
|
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def f3(x, **z) -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
fr3 = Frame(f3("a", b="c"))
|
2018-05-23 22:48:46 +08:00
|
|
|
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def f4(x, *y, **z) -> FrameType:
|
2015-11-27 22:43:01 +08:00
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-01-29 08:20:45 +08:00
|
|
|
fr4 = Frame(f4("a", "b", c="d"))
|
2018-05-23 22:48:46 +08:00
|
|
|
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestExceptionInfo:
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_bad_getsource(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
2017-07-17 07:25:10 +08:00
|
|
|
if False:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False
|
2015-11-27 22:43:01 +08:00
|
|
|
except AssertionError:
|
2020-01-29 08:20:45 +08:00
|
|
|
exci = ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
assert exci.getrepr()
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_from_current_with_missing(self) -> None:
|
2019-03-23 07:29:36 +08:00
|
|
|
with pytest.raises(AssertionError, match="no current exception"):
|
2020-01-29 08:20:45 +08:00
|
|
|
ExceptionInfo.from_current()
|
2019-03-23 07:29:36 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTracebackEntry:
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getsource(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
2017-07-17 07:25:10 +08:00
|
|
|
if False:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert False
|
2015-11-27 22:43:01 +08:00
|
|
|
except AssertionError:
|
2020-01-29 08:20:45 +08:00
|
|
|
exci = ExceptionInfo.from_current()
|
2015-11-27 22:43:01 +08:00
|
|
|
entry = exci.traceback[0]
|
|
|
|
source = entry.getsource()
|
2019-11-16 05:02:55 +08:00
|
|
|
assert source is not None
|
2017-07-17 07:25:10 +08:00
|
|
|
assert len(source) == 6
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "assert False" in source[5]
|
2017-08-31 03:06:12 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestReprFuncArgs:
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")]
|
2017-08-31 03:06:12 +08:00
|
|
|
|
|
|
|
r = ReprFuncArgs(args)
|
2019-08-26 22:32:57 +08:00
|
|
|
r.toterminal(tw_mock)
|
2019-05-28 07:31:52 +08:00
|
|
|
|
|
|
|
assert (
|
2019-08-26 22:32:57 +08:00
|
|
|
tw_mock.lines[0]
|
2019-05-28 07:31:52 +08:00
|
|
|
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'"
|
|
|
|
)
|
2020-04-03 06:56:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_ExceptionChainRepr():
|
|
|
|
"""Test ExceptionChainRepr, especially with regard to being hashable."""
|
|
|
|
try:
|
|
|
|
raise ValueError()
|
|
|
|
except ValueError:
|
|
|
|
excinfo1 = ExceptionInfo.from_current()
|
|
|
|
excinfo2 = ExceptionInfo.from_current()
|
|
|
|
|
|
|
|
repr1 = excinfo1.getrepr()
|
|
|
|
repr2 = excinfo2.getrepr()
|
|
|
|
assert repr1 != repr2
|
|
|
|
|
|
|
|
assert isinstance(repr1, ExceptionChainRepr)
|
|
|
|
assert hash(repr1) != hash(repr2)
|
|
|
|
assert repr1 is not excinfo1.getrepr()
|