2017-08-31 03:06:12 +08:00
|
|
|
# coding: utf-8
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import py
|
|
|
|
import pytest
|
2017-08-31 03:06:12 +08:00
|
|
|
from test_excinfo import TWMock
|
2018-05-24 00:12:04 +08:00
|
|
|
from six import text_type
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_ne():
|
2018-05-23 22:48:46 +08:00
|
|
|
code1 = _pytest._code.Code(compile('foo = "bar"', "", "exec"))
|
2015-11-27 22:43:01 +08:00
|
|
|
assert code1 == code1
|
2018-05-23 22:48:46 +08:00
|
|
|
code2 = _pytest._code.Code(compile('foo = "baz"', "", "exec"))
|
2015-11-27 22:43:01 +08:00
|
|
|
assert code2 != code1
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_gives_back_name_for_not_existing_file():
|
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
|
|
|
|
code = _pytest._code.Code(co_code)
|
|
|
|
assert str(code.path) == name
|
|
|
|
assert code.fullsource is None
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_with_class():
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
pytest.raises(TypeError, "_pytest._code.Code(A)")
|
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
if True:
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def x():
|
|
|
|
pass
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_fullsource():
|
|
|
|
code = _pytest._code.Code(x)
|
|
|
|
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
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_source():
|
|
|
|
code = _pytest._code.Code(x)
|
|
|
|
src = code.source()
|
|
|
|
expected = """def x():
|
|
|
|
pass"""
|
|
|
|
assert str(src) == expected
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_frame_getsourcelineno_myself():
|
|
|
|
def func():
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
f = func()
|
|
|
|
f = _pytest._code.Frame(f)
|
|
|
|
source, lineno = f.code.fullsource, f.lineno
|
|
|
|
assert source[lineno].startswith(" return sys._getframe(0)")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getstatement_empty_fullsource():
|
|
|
|
def func():
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
f = func()
|
|
|
|
f = _pytest._code.Frame(f)
|
|
|
|
prop = f.code.__class__.fullsource
|
|
|
|
try:
|
|
|
|
f.code.__class__.fullsource = None
|
|
|
|
assert f.statement == _pytest._code.Source("")
|
|
|
|
finally:
|
|
|
|
f.code.__class__.fullsource = prop
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_from_func():
|
|
|
|
co = _pytest._code.Code(test_frame_getsourcelineno_myself)
|
|
|
|
assert co.firstlineno
|
|
|
|
assert co.path
|
|
|
|
|
|
|
|
|
|
|
|
def test_unicode_handling():
|
2018-05-23 22:48:46 +08:00
|
|
|
value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
raise Exception(value)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = pytest.raises(Exception, f)
|
|
|
|
str(excinfo)
|
|
|
|
if sys.version_info[0] < 3:
|
2018-05-24 00:12:04 +08:00
|
|
|
text_type(excinfo)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2016-03-06 03:58:44 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(sys.version_info[0] >= 3, reason="python 2 only issue")
|
2016-03-06 03:58:44 +08:00
|
|
|
def test_unicode_handling_syntax_error():
|
2018-05-23 22:48:46 +08:00
|
|
|
value = py.builtin._totext("\xc4\x85\xc4\x87\n", "utf-8").encode("utf8")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-03-06 03:58:44 +08:00
|
|
|
def f():
|
2018-05-23 22:48:46 +08:00
|
|
|
raise SyntaxError("invalid syntax", (None, 1, 3, value))
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-03-06 03:58:44 +08:00
|
|
|
excinfo = pytest.raises(Exception, f)
|
|
|
|
str(excinfo)
|
|
|
|
if sys.version_info[0] < 3:
|
2018-05-24 00:12:04 +08:00
|
|
|
text_type(excinfo)
|
2016-03-06 03:58:44 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_code_getargs():
|
|
|
|
def f1(x):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
c1 = _pytest._code.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):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
c2 = _pytest._code.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):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
c3 = _pytest._code.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):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
c4 = _pytest._code.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
|
|
|
|
|
|
|
|
|
|
|
def test_frame_getargs():
|
|
|
|
def f1(x):
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
fr1 = _pytest._code.Frame(f1("a"))
|
|
|
|
assert fr1.getargs(var=True) == [("x", "a")]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f2(x, *y):
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
fr2 = _pytest._code.Frame(f2("a", "b", "c"))
|
|
|
|
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f3(x, **z):
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
fr3 = _pytest._code.Frame(f3("a", b="c"))
|
|
|
|
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def f4(x, *y, **z):
|
|
|
|
return sys._getframe(0)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
fr4 = _pytest._code.Frame(f4("a", "b", c="d"))
|
|
|
|
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestExceptionInfo(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_bad_getsource(self):
|
|
|
|
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:
|
|
|
|
exci = _pytest._code.ExceptionInfo()
|
|
|
|
assert exci.getrepr()
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestTracebackEntry(object):
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_getsource(self):
|
|
|
|
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:
|
|
|
|
exci = _pytest._code.ExceptionInfo()
|
|
|
|
entry = exci.traceback[0]
|
|
|
|
source = entry.getsource()
|
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
|
|
|
|
|
|
|
|
|
|
|
class TestReprFuncArgs(object):
|
|
|
|
def test_not_raise_exception_with_mixed_encoding(self):
|
|
|
|
from _pytest._code.code import ReprFuncArgs
|
|
|
|
|
|
|
|
tw = TWMock()
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
args = [("unicode_string", u"São Paulo"), ("utf8_string", "S\xc3\xa3o Paulo")]
|
2017-08-31 03:06:12 +08:00
|
|
|
|
|
|
|
r = ReprFuncArgs(args)
|
|
|
|
r.toterminal(tw)
|
|
|
|
if sys.version_info[0] >= 3:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert tw.lines[0] == "unicode_string = São Paulo, utf8_string = São Paulo"
|
2017-08-31 03:06:12 +08:00
|
|
|
else:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert tw.lines[0] == "unicode_string = São Paulo, utf8_string = São Paulo"
|