2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2015-11-27 22:43:01 +08:00
|
|
|
# flake8: noqa
|
|
|
|
# disable flake check on this file because some constructs are strange
|
|
|
|
# or redundant on purpose and can't be disable on a line-by-line basis
|
2017-12-27 11:47:26 +08:00
|
|
|
import inspect
|
2020-07-02 01:20:12 +08:00
|
|
|
import linecache
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
2020-07-02 01:20:07 +08:00
|
|
|
import textwrap
|
2020-12-19 20:11:00 +08:00
|
|
|
from pathlib import Path
|
2019-11-16 05:02:55 +08:00
|
|
|
from typing import Any
|
|
|
|
from typing import Dict
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import pytest
|
2020-10-27 22:11:39 +08:00
|
|
|
from _pytest._code import Code
|
|
|
|
from _pytest._code import Frame
|
|
|
|
from _pytest._code import getfslineno
|
2020-10-27 22:07:03 +08:00
|
|
|
from _pytest._code import Source
|
2021-01-02 00:55:03 +08:00
|
|
|
from _pytest.pathlib import import_path
|
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_source_str_function() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
x = Source("3")
|
|
|
|
assert str(x) == "3"
|
|
|
|
|
|
|
|
x = Source(" 3")
|
|
|
|
assert str(x) == "3"
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
x = Source(
|
|
|
|
"""
|
2015-11-27 22:43:01 +08:00
|
|
|
3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(x) == "\n3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_source_from_function() -> None:
|
2020-10-27 22:11:39 +08:00
|
|
|
source = Source(test_source_str_function)
|
2019-11-16 05:02:55 +08:00
|
|
|
assert str(source).startswith("def test_source_str_function() -> None:")
|
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_source_from_method() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestClass:
|
2015-11-27 22:43:01 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-10-27 22:11:39 +08:00
|
|
|
source = Source(TestClass().test_method)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert source.lines == ["def test_method(self):", " pass"]
|
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_source_from_lines() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
lines = ["a \n", "b\n", "c"]
|
2020-10-27 22:11:39 +08:00
|
|
|
source = Source(lines)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert source.lines == ["a ", "b", "c"]
|
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_source_from_inner_function() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
2020-07-04 17:12:52 +08:00
|
|
|
raise NotImplementedError()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-10-27 22:11:39 +08:00
|
|
|
source = Source(f)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source).startswith("def f():")
|
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_source_strips() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = Source("")
|
|
|
|
assert source == Source()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(source) == ""
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.strip() == source
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_source_strip_multiline() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = Source()
|
|
|
|
source.lines = ["", " hello", " "]
|
|
|
|
source2 = source.strip()
|
|
|
|
assert source2.lines == [" hello"]
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestAccesses:
|
2019-11-16 05:02:55 +08:00
|
|
|
def setup_class(self) -> None:
|
2019-11-07 18:43:59 +08:00
|
|
|
self.source = Source(
|
|
|
|
"""\
|
|
|
|
def f(x):
|
|
|
|
pass
|
|
|
|
def g(x):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getrange(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
x = self.source[0:2]
|
|
|
|
assert len(x.lines) == 2
|
|
|
|
assert str(x) == "def f(x):\n pass"
|
|
|
|
|
2019-11-25 23:20:54 +08:00
|
|
|
def test_getrange_step_not_supported(self) -> None:
|
|
|
|
with pytest.raises(IndexError, match=r"step"):
|
|
|
|
self.source[::2]
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getline(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
x = self.source[0]
|
|
|
|
assert x == "def f(x):"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_len(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(self.source) == 4
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_iter(self) -> None:
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [x for x in self.source]
|
|
|
|
assert len(values) == 4
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-07-02 01:20:12 +08:00
|
|
|
class TestSourceParsing:
|
2019-11-16 05:02:55 +08:00
|
|
|
def setup_class(self) -> None:
|
2019-11-07 18:43:59 +08:00
|
|
|
self.source = Source(
|
|
|
|
"""\
|
|
|
|
def f(x):
|
|
|
|
assert (x ==
|
|
|
|
3 +
|
|
|
|
4)
|
|
|
|
"""
|
|
|
|
).strip()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatement(self) -> None:
|
2017-07-17 07:25:09 +08:00
|
|
|
# print str(self.source)
|
2015-11-27 22:43:01 +08:00
|
|
|
ass = str(self.source[1:])
|
|
|
|
for i in range(1, 4):
|
2017-07-17 07:25:09 +08:00
|
|
|
# print "trying start in line %r" % self.source[i]
|
2015-11-27 22:43:01 +08:00
|
|
|
s = self.source.getstatement(i)
|
2018-05-23 22:48:46 +08:00
|
|
|
# x = s.deindent()
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(s) == ass
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_triple_quoted(self) -> None:
|
2017-07-17 07:25:09 +08:00
|
|
|
# print str(self.source)
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""hello('''
|
|
|
|
''')"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
s = source.getstatement(0)
|
2020-07-02 01:20:10 +08:00
|
|
|
assert s == source
|
2015-11-27 22:43:01 +08:00
|
|
|
s = source.getstatement(1)
|
2020-07-02 01:20:10 +08:00
|
|
|
assert s == source
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_within_constructs(self) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except SomeThing:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
42
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 7
|
|
|
|
# check all lineno's that could occur in a traceback
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert source.getstatementrange(0) == (0, 7)
|
|
|
|
# assert source.getstatementrange(1) == (1, 5)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.getstatementrange(2) == (2, 3)
|
|
|
|
assert source.getstatementrange(3) == (3, 4)
|
|
|
|
assert source.getstatementrange(4) == (4, 5)
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert source.getstatementrange(5) == (0, 7)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert source.getstatementrange(6) == (6, 7)
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_bug(self) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
x = (
|
|
|
|
y +
|
|
|
|
z)
|
|
|
|
except:
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 6
|
|
|
|
assert source.getstatementrange(2) == (1, 4)
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_bug2(self) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
assert (
|
|
|
|
33
|
|
|
|
==
|
|
|
|
[
|
|
|
|
X(3,
|
|
|
|
b=1, c=2
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert len(source) == 9
|
|
|
|
assert source.getstatementrange(5) == (0, 9)
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_ast_issue58(self) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = Source(
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
def test_some():
|
|
|
|
for a in [a for a in
|
|
|
|
CAUSE_ERROR]: pass
|
|
|
|
|
|
|
|
x = 3
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert getstatement(2, source).lines == source.lines[2:3]
|
|
|
|
assert getstatement(3, source).lines == source.lines[3:4]
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_out_of_bounds_py3(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = Source("if xxx:\n from .collections import something")
|
|
|
|
r = source.getstatementrange(1)
|
2017-07-17 07:25:08 +08:00
|
|
|
assert r == (1, 2)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstatementrange_with_syntaxerror_issue7(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = Source(":")
|
|
|
|
pytest.raises(SyntaxError, lambda: source.getstatementrange(0))
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstartingblock_singleline() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2019-11-16 05:02:55 +08:00
|
|
|
def __init__(self, *args) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
frame = sys._getframe(1)
|
2020-10-27 22:11:39 +08:00
|
|
|
self.source = Frame(frame).statement
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
x = A("x", "y")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [i for i in x.source.lines if i.strip()]
|
|
|
|
assert len(values) == 1
|
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_getline_finally() -> None:
|
|
|
|
def c() -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
pass
|
|
|
|
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
teardown = None
|
|
|
|
try:
|
2019-11-16 05:02:55 +08:00
|
|
|
c(1) # type: ignore
|
2018-11-23 02:05:10 +08:00
|
|
|
finally:
|
|
|
|
if teardown:
|
2020-08-04 00:15:21 +08:00
|
|
|
teardown() # type: ignore[unreachable]
|
2015-11-27 22:43:01 +08:00
|
|
|
source = excinfo.traceback[-1].statement
|
2019-11-16 05:02:55 +08:00
|
|
|
assert str(source).strip() == "c(1) # type: ignore"
|
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_getfuncsource_dynamic() -> None:
|
2020-07-02 01:20:12 +08:00
|
|
|
def f():
|
2020-07-04 17:12:52 +08:00
|
|
|
raise NotImplementedError()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-07-02 01:20:12 +08:00
|
|
|
def g():
|
2020-07-04 17:12:52 +08:00
|
|
|
pass # pragma: no cover
|
2020-07-02 01:20:12 +08:00
|
|
|
|
2020-10-27 22:11:39 +08:00
|
|
|
f_source = Source(f)
|
|
|
|
g_source = Source(g)
|
2020-07-04 17:12:52 +08:00
|
|
|
assert str(f_source).strip() == "def f():\n raise NotImplementedError()"
|
|
|
|
assert str(g_source).strip() == "def g():\n pass # pragma: no cover"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getfuncsource_with_multine_string() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
2018-05-23 22:48:46 +08:00
|
|
|
c = """while True:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
|
2018-10-03 07:13:30 +08:00
|
|
|
expected = '''\
|
|
|
|
def f():
|
|
|
|
c = """while True:
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
'''
|
2020-10-27 22:11:39 +08:00
|
|
|
assert str(Source(f)) == expected.rstrip()
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_deindent() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.source import deindent as deindent
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
assert deindent(["\tfoo", "\tbar"]) == ["foo", "bar"]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-10-04 01:01:06 +08:00
|
|
|
source = """\
|
2015-11-27 22:43:01 +08:00
|
|
|
def f():
|
|
|
|
def g():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
lines = deindent(source.splitlines())
|
2018-10-04 01:01:06 +08:00
|
|
|
assert lines == ["def f():", " def g():", " pass"]
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2021-02-21 02:01:42 +08:00
|
|
|
def test_source_of_class_at_eof_without_newline(_sys_snapshot, tmp_path: Path) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
# this test fails because the implicit inspect.getsource(A) below
|
|
|
|
# does not return the "x = 1" last line.
|
2020-10-27 22:11:39 +08:00
|
|
|
source = Source(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2020-10-27 22:07:03 +08:00
|
|
|
class A:
|
2015-11-27 22:43:01 +08:00
|
|
|
def method(self):
|
|
|
|
x = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2021-01-02 00:55:03 +08:00
|
|
|
path = tmp_path.joinpath("a.py")
|
2023-06-20 19:55:40 +08:00
|
|
|
path.write_text(str(source), encoding="utf-8")
|
2024-03-02 22:46:54 +08:00
|
|
|
mod: Any = import_path(path, root=tmp_path, consider_namespace_packages=False)
|
2021-01-02 00:55:03 +08:00
|
|
|
s2 = Source(mod.A)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(source).strip() == str(s2).strip()
|
|
|
|
|
2017-07-17 07:25:06 +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
|
|
|
|
2020-07-02 01:20:13 +08:00
|
|
|
def test_source_fallback() -> None:
|
|
|
|
src = Source(x)
|
2015-11-27 22:43:01 +08:00
|
|
|
expected = """def x():
|
|
|
|
pass"""
|
2020-07-02 01:20:10 +08:00
|
|
|
assert str(src) == expected
|
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_findsource_fallback() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.source import findsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
src, lineno = findsource(x)
|
2019-11-16 05:02:55 +08:00
|
|
|
assert src is not None
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "test_findsource_simple" in str(src)
|
|
|
|
assert src[lineno] == " def x():"
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-07-02 01:20:12 +08:00
|
|
|
def test_findsource(monkeypatch) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.source import findsource
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-07-02 01:20:12 +08:00
|
|
|
filename = "<pytest-test_findsource>"
|
|
|
|
lines = ["if 1:\n", " def x():\n", " pass\n"]
|
|
|
|
co = compile("".join(lines), filename, "exec")
|
|
|
|
|
2021-12-22 09:42:32 +08:00
|
|
|
monkeypatch.setitem(linecache.cache, filename, (1, None, lines, filename))
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
src, lineno = findsource(co)
|
2019-11-16 05:02:55 +08:00
|
|
|
assert src is not None
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "if 1:" in str(src)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
d: Dict[str, Any] = {}
|
2015-11-27 22:43:01 +08:00
|
|
|
eval(co, d)
|
2018-05-23 22:48:46 +08:00
|
|
|
src, lineno = findsource(d["x"])
|
2019-11-16 05:02:55 +08:00
|
|
|
assert src is not None
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "if 1:" in str(src)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert src[lineno] == " def x():"
|
|
|
|
|
|
|
|
|
2020-01-30 05:43:22 +08:00
|
|
|
def test_getfslineno() -> None:
|
|
|
|
def f(x) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
fspath, lineno = getfslineno(f)
|
|
|
|
|
2020-12-19 20:11:00 +08:00
|
|
|
assert isinstance(fspath, Path)
|
|
|
|
assert fspath.name == "test_source.py"
|
2020-01-30 05:43:22 +08:00
|
|
|
assert lineno == f.__code__.co_firstlineno - 1 # see findsource
|
|
|
|
|
|
|
|
class A:
|
|
|
|
pass
|
|
|
|
|
|
|
|
fspath, lineno = getfslineno(A)
|
|
|
|
|
|
|
|
_, A_lineno = inspect.findsource(A)
|
2020-12-19 20:11:00 +08:00
|
|
|
assert isinstance(fspath, Path)
|
|
|
|
assert fspath.name == "test_source.py"
|
2020-01-30 05:43:22 +08:00
|
|
|
assert lineno == A_lineno
|
|
|
|
|
|
|
|
assert getfslineno(3) == ("", -1)
|
|
|
|
|
|
|
|
class B:
|
|
|
|
pass
|
|
|
|
|
2020-05-07 20:26:55 +08:00
|
|
|
B.__name__ = B.__qualname__ = "B2"
|
2020-01-30 05:43:22 +08:00
|
|
|
assert getfslineno(B)[1] == -1
|
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_code_of_object_instance_with_call() -> 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-10-27 22:11:39 +08:00
|
|
|
pytest.raises(TypeError, lambda: Source(A()))
|
2017-07-17 07:25:06 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class WithCall:
|
2019-11-16 05:02:55 +08:00
|
|
|
def __call__(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
|
|
|
|
2020-10-27 22:07:03 +08:00
|
|
|
code = Code.from_function(WithCall())
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "pass" in str(code.source())
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Hello:
|
2019-11-16 05:02:55 +08:00
|
|
|
def __call__(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-10-27 22:07:03 +08:00
|
|
|
pytest.raises(TypeError, lambda: Code.from_function(Hello))
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def getstatement(lineno: int, source) -> Source:
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.source import getstatementrange_ast
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-10-27 22:11:39 +08:00
|
|
|
src = Source(source)
|
2019-11-16 05:02:55 +08:00
|
|
|
ast, start, end = getstatementrange_ast(lineno, src)
|
|
|
|
return src[start:end]
|
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_oneline() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(0, "raise ValueError")
|
|
|
|
assert str(source) == "raise ValueError"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_comment_and_no_newline_at_end() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
from _pytest._code.source import getstatementrange_ast
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
source = Source(
|
|
|
|
[
|
|
|
|
"def test_basic_complex():",
|
|
|
|
" assert 1 == 2",
|
|
|
|
"# vim: filetype=pyopencl:fdm=marker",
|
|
|
|
]
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
ast, start, end = getstatementrange_ast(1, source)
|
|
|
|
assert end == 2
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_oneline_and_comment() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(0, "raise ValueError\n#hello")
|
|
|
|
assert str(source) == "raise ValueError"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_comments() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = '''def test():
|
|
|
|
"comment 1"
|
|
|
|
x = 1
|
|
|
|
# comment 2
|
|
|
|
# comment 3
|
|
|
|
|
|
|
|
assert False
|
|
|
|
|
|
|
|
"""
|
|
|
|
comment 4
|
|
|
|
"""
|
|
|
|
'''
|
2017-07-17 07:25:08 +08:00
|
|
|
for line in range(2, 6):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(getstatement(line, source)) == " x = 1"
|
2023-07-01 05:55:42 +08:00
|
|
|
for line in range(6, 8):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert str(getstatement(line, source)) == " assert False"
|
2023-07-01 05:55:42 +08:00
|
|
|
for line in range(8, 10):
|
2019-02-16 09:34:31 +08:00
|
|
|
assert str(getstatement(line, source)) == '"""\ncomment 4\n"""'
|
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_comment_in_statement() -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = """test(foo=1,
|
2015-11-27 22:43:01 +08:00
|
|
|
# comment 1
|
|
|
|
bar=2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-07-17 07:25:08 +08:00
|
|
|
for line in range(1, 3):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert (
|
|
|
|
str(getstatement(line, source))
|
|
|
|
== "test(foo=1,\n # comment 1\n bar=2)"
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-02-29 03:32:33 +08:00
|
|
|
def test_source_with_decorator() -> None:
|
|
|
|
"""Test behavior with Source / Code().source with regard to decorators."""
|
|
|
|
from _pytest.compat import get_real_func
|
|
|
|
|
|
|
|
@pytest.mark.foo
|
|
|
|
def deco_mark():
|
2020-02-29 03:45:02 +08:00
|
|
|
assert False
|
2020-02-29 03:32:33 +08:00
|
|
|
|
|
|
|
src = inspect.getsource(deco_mark)
|
2020-07-02 01:20:07 +08:00
|
|
|
assert textwrap.indent(str(Source(deco_mark)), " ") + "\n" == src
|
2020-02-29 03:32:33 +08:00
|
|
|
assert src.startswith(" @pytest.mark.foo")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def deco_fixture():
|
2020-02-29 03:45:02 +08:00
|
|
|
assert False
|
2020-02-29 03:32:33 +08:00
|
|
|
|
|
|
|
src = inspect.getsource(deco_fixture)
|
2020-02-29 03:45:02 +08:00
|
|
|
assert src == " @pytest.fixture\n def deco_fixture():\n assert False\n"
|
2021-12-27 20:23:15 +08:00
|
|
|
# currently Source does not unwrap decorators, testing the
|
2020-05-20 06:19:53 +08:00
|
|
|
# existing behavior here for explicitness, but perhaps we should revisit/change this
|
|
|
|
# in the future
|
2020-02-29 03:32:33 +08:00
|
|
|
assert str(Source(deco_fixture)).startswith("@functools.wraps(function)")
|
2020-07-02 01:20:07 +08:00
|
|
|
assert (
|
|
|
|
textwrap.indent(str(Source(get_real_func(deco_fixture))), " ") + "\n" == src
|
|
|
|
)
|
2020-02-29 03:32:33 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_single_line_else() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(1, "if False: 2\nelse: 3")
|
|
|
|
assert str(source) == "else: 3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_single_line_finally() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(1, "try: 1\nfinally: 3")
|
|
|
|
assert str(source) == "finally: 3"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_issue55() -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = (
|
|
|
|
"def round_trip(dinp):\n assert 1 == dinp\n"
|
|
|
|
'def test_rt():\n round_trip("""\n""")\n'
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
s = getstatement(3, source)
|
|
|
|
assert str(s) == ' round_trip("""\n""")'
|
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_multiline() -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
source = getstatement(
|
|
|
|
0,
|
|
|
|
"""\
|
2015-11-27 22:43:01 +08:00
|
|
|
raise ValueError(
|
|
|
|
23
|
|
|
|
)
|
|
|
|
x = 3
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2015-11-27 22:43:01 +08:00
|
|
|
assert str(source) == "raise ValueError(\n 23\n)"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTry:
|
2019-11-16 05:02:55 +08:00
|
|
|
def setup_class(self) -> None:
|
2019-11-07 18:43:59 +08:00
|
|
|
self.source = """\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
except Something:
|
|
|
|
raise IndexError(1)
|
|
|
|
else:
|
|
|
|
raise KeyError()
|
|
|
|
"""
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_body(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " raise ValueError"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_except_line(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(2, self.source)
|
|
|
|
assert str(source) == "except Something:"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_except_body(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " raise IndexError(1)"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_else(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(5, self.source)
|
|
|
|
assert str(source) == " raise KeyError()"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestTryFinally:
|
2019-11-16 05:02:55 +08:00
|
|
|
def setup_class(self) -> None:
|
2019-11-07 18:43:59 +08:00
|
|
|
self.source = """\
|
2015-11-27 22:43:01 +08:00
|
|
|
try:
|
|
|
|
raise ValueError
|
|
|
|
finally:
|
|
|
|
raise IndexError(1)
|
|
|
|
"""
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_body(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " raise ValueError"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_finally(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " raise IndexError(1)"
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestIf:
|
2019-11-16 05:02:55 +08:00
|
|
|
def setup_class(self) -> None:
|
2019-11-07 18:43:59 +08:00
|
|
|
self.source = """\
|
2015-11-27 22:43:01 +08:00
|
|
|
if 1:
|
|
|
|
y = 3
|
|
|
|
elif False:
|
|
|
|
y = 5
|
|
|
|
else:
|
|
|
|
y = 7
|
|
|
|
"""
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_body(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(1, self.source)
|
|
|
|
assert str(source) == " y = 3"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_elif_clause(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(2, self.source)
|
|
|
|
assert str(source) == "elif False:"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_elif(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(3, self.source)
|
|
|
|
assert str(source) == " y = 5"
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_else(self) -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = getstatement(5, self.source)
|
|
|
|
assert str(source) == " y = 7"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_semicolon() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
s = """\
|
|
|
|
hello ; pytest.skip()
|
|
|
|
"""
|
|
|
|
source = getstatement(0, s)
|
|
|
|
assert str(source) == s.strip()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_def_online() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
s = """\
|
|
|
|
def func(): raise ValueError(42)
|
|
|
|
|
|
|
|
def something():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
source = getstatement(0, s)
|
|
|
|
assert str(source) == "def func(): raise ValueError(42)"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2021-12-07 17:31:03 +08:00
|
|
|
def test_decorator() -> None:
|
|
|
|
s = """\
|
|
|
|
def foo(f):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@foo
|
|
|
|
def bar():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
source = getstatement(3, s)
|
|
|
|
assert "@foo" in str(source)
|
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def XXX_test_expression_multiline() -> None:
|
2015-11-27 22:43:01 +08:00
|
|
|
source = """\
|
|
|
|
something
|
|
|
|
'''
|
|
|
|
'''"""
|
|
|
|
result = getstatement(1, source)
|
|
|
|
assert str(result) == "'''\n'''"
|
2018-07-09 08:05:01 +08:00
|
|
|
|
|
|
|
|
2019-11-16 05:02:55 +08:00
|
|
|
def test_getstartingblock_multiline() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2018-07-09 08:05:01 +08:00
|
|
|
def __init__(self, *args):
|
|
|
|
frame = sys._getframe(1)
|
2020-10-27 22:11:39 +08:00
|
|
|
self.source = Frame(frame).statement
|
2018-07-09 08:05:01 +08:00
|
|
|
|
|
|
|
# fmt: off
|
|
|
|
x = A('x',
|
|
|
|
'y'
|
|
|
|
,
|
|
|
|
'z')
|
|
|
|
# fmt: on
|
|
|
|
values = [i for i in x.source.lines if i.strip()]
|
|
|
|
assert len(values) == 4
|