| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import re |
| 5 |
import sys |
| 6 |
from types import FrameType |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
from _pytest._code import Code |
| 10 |
from _pytest._code import ExceptionInfo |
| 11 |
from _pytest._code import Frame |
| 12 |
from _pytest._code import Source |
| 13 |
from _pytest._code.code import ExceptionChainRepr |
| 14 |
from _pytest._code.code import ReprFuncArgs |
| 15 |
import pytest |
| 16 |
|
| 17 |
|
| 18 |
def test_ne() -> None: |
| 19 |
code1 = Code(compile('foo = "bar"', "", "exec")) |
| 20 |
assert code1 == code1 |
| 21 |
code2 = Code(compile('foo = "baz"', "", "exec")) |
| 22 |
assert code2 != code1 |
| 23 |
|
| 24 |
|
| 25 |
def test_code_gives_back_name_for_not_existing_file() -> None: |
| 26 |
name = "abc-123" |
| 27 |
co_code = compile("pass\n", name, "exec") |
| 28 |
assert co_code.co_filename == name |
| 29 |
code = Code(co_code) |
| 30 |
assert str(code.path) == name |
| 31 |
assert code.fullsource is None |
| 32 |
|
| 33 |
|
| 34 |
def test_code_from_function_with_class() -> None: |
| 35 |
class A: |
| 36 |
pass |
| 37 |
|
| 38 |
with pytest.raises(TypeError): |
| 39 |
Code.from_function(A) |
| 40 |
|
| 41 |
|
| 42 |
def x() -> None: |
| 43 |
raise NotImplementedError() |
| 44 |
|
| 45 |
|
| 46 |
def test_code_fullsource() -> None: |
| 47 |
code = Code.from_function(x) |
| 48 |
full = code.fullsource |
| 49 |
assert "test_code_fullsource()" in str(full) |
| 50 |
|
| 51 |
|
| 52 |
def test_code_source() -> None: |
| 53 |
code = Code.from_function(x) |
| 54 |
src = code.source() |
| 55 |
expected = """def x() -> None: |
| 56 |
raise NotImplementedError()""" |
| 57 |
assert str(src) == expected |
| 58 |
|
| 59 |
|
| 60 |
def test_frame_getsourcelineno_myself() -> None: |
| 61 |
def func() -> FrameType: |
| 62 |
return sys._getframe(0) |
| 63 |
|
| 64 |
f = Frame(func()) |
| 65 |
source, lineno = f.code.fullsource, f.lineno |
| 66 |
assert source is not None |
| 67 |
assert source[lineno].startswith(" return sys._getframe(0)") |
| 68 |
|
| 69 |
|
| 70 |
def test_getstatement_empty_fullsource() -> None: |
| 71 |
def func() -> FrameType: |
| 72 |
return sys._getframe(0) |
| 73 |
|
| 74 |
f = Frame(func()) |
| 75 |
with mock.patch.object(f.code.__class__, "fullsource", None): |
| 76 |
assert f.statement == Source("") |
| 77 |
|
| 78 |
|
| 79 |
def test_code_from_func() -> None: |
| 80 |
co = Code.from_function(test_frame_getsourcelineno_myself) |
| 81 |
assert co.firstlineno |
| 82 |
assert co.path |
| 83 |
|
| 84 |
|
| 85 |
def test_unicode_handling() -> None: |
| 86 |
value = "ąć".encode() |
| 87 |
|
| 88 |
with pytest.raises(Exception) as excinfo: |
| 89 |
raise Exception(value) |
| 90 |
str(excinfo) |
| 91 |
|
| 92 |
|
| 93 |
def test_code_getargs() -> None: |
| 94 |
def f1(x): |
| 95 |
raise NotImplementedError() |
| 96 |
|
| 97 |
c1 = Code.from_function(f1) |
| 98 |
assert c1.getargs(var=True) == ("x",) |
| 99 |
|
| 100 |
def f2(x, *y): |
| 101 |
raise NotImplementedError() |
| 102 |
|
| 103 |
c2 = Code.from_function(f2) |
| 104 |
assert c2.getargs(var=True) == ("x", "y") |
| 105 |
|
| 106 |
def f3(x, **z): |
| 107 |
raise NotImplementedError() |
| 108 |
|
| 109 |
c3 = Code.from_function(f3) |
| 110 |
assert c3.getargs(var=True) == ("x", "z") |
| 111 |
|
| 112 |
def f4(x, *y, **z): |
| 113 |
raise NotImplementedError() |
| 114 |
|
| 115 |
c4 = Code.from_function(f4) |
| 116 |
assert c4.getargs(var=True) == ("x", "y", "z") |
| 117 |
|
| 118 |
def f5(x, *y, **z): |
| 119 |
a1 = a2 = a3 = a4 = a5 = a6 = 1 # noqa: F841 |
| 120 |
|
| 121 |
c5 = Code.from_function(f5) |
| 122 |
f5(1, 2, 3, z=4) # cover function body |
| 123 |
assert c5.getargs(var=True) == ("x", "y", "z") |
| 124 |
|
| 125 |
def f6(x, *y, kw=1, **z): |
| 126 |
a1 = a2 = a3 = a4 = a5 = a6 = 1 # noqa: F841 |
| 127 |
|
| 128 |
c6 = Code.from_function(f6) |
| 129 |
f6(1, 2, kw=3, z=4) # cover function body |
| 130 |
assert c6.getargs(var=True) == ("x", "kw", "y", "z") |
| 131 |
|
| 132 |
|
| 133 |
def test_frame_getargs() -> None: |
| 134 |
def f1(x) -> FrameType: |
| 135 |
return sys._getframe(0) |
| 136 |
|
| 137 |
fr1 = Frame(f1("a")) |
| 138 |
assert fr1.getargs(var=True) == [("x", "a")] |
| 139 |
|
| 140 |
def f2(x, *y) -> FrameType: |
| 141 |
return sys._getframe(0) |
| 142 |
|
| 143 |
fr2 = Frame(f2("a", "b", "c")) |
| 144 |
assert fr2.getargs(var=True) == [("x", "a"), ("y", ("b", "c"))] |
| 145 |
|
| 146 |
def f3(x, **z) -> FrameType: |
| 147 |
return sys._getframe(0) |
| 148 |
|
| 149 |
fr3 = Frame(f3("a", b="c")) |
| 150 |
assert fr3.getargs(var=True) == [("x", "a"), ("z", {"b": "c"})] |
| 151 |
|
| 152 |
def f4(x, *y, **z) -> FrameType: |
| 153 |
return sys._getframe(0) |
| 154 |
|
| 155 |
fr4 = Frame(f4("a", "b", c="d")) |
| 156 |
assert fr4.getargs(var=True) == [("x", "a"), ("y", ("b",)), ("z", {"c": "d"})] |
| 157 |
|
| 158 |
|
| 159 |
class TestExceptionInfo: |
| 160 |
def test_bad_getsource(self) -> None: |
| 161 |
try: |
| 162 |
if False: |
| 163 |
pass # type: ignore[unreachable] |
| 164 |
else: |
| 165 |
assert False |
| 166 |
except AssertionError: |
| 167 |
exci = ExceptionInfo.from_current() |
| 168 |
assert exci.getrepr() |
| 169 |
|
| 170 |
def test_from_current_with_missing(self) -> None: |
| 171 |
with pytest.raises(AssertionError, match="no current exception"): |
| 172 |
ExceptionInfo.from_current() |
| 173 |
|
| 174 |
|
| 175 |
class TestTracebackEntry: |
| 176 |
def test_getsource(self) -> None: |
| 177 |
try: |
| 178 |
if False: |
| 179 |
pass # type: ignore[unreachable] |
| 180 |
else: |
| 181 |
assert False |
| 182 |
except AssertionError: |
| 183 |
exci = ExceptionInfo.from_current() |
| 184 |
entry = exci.traceback[0] |
| 185 |
source = entry.getsource() |
| 186 |
assert source is not None |
| 187 |
assert len(source) == 6 |
| 188 |
assert "assert False" in source[5] |
| 189 |
|
| 190 |
def test_tb_entry_str(self): |
| 191 |
try: |
| 192 |
assert False |
| 193 |
except AssertionError: |
| 194 |
exci = ExceptionInfo.from_current() |
| 195 |
pattern = r" File '.*test_code.py':\d+ in test_tb_entry_str\n assert False" |
| 196 |
entry = str(exci.traceback[0]) |
| 197 |
assert re.match(pattern, entry) |
| 198 |
|
| 199 |
|
| 200 |
class TestReprFuncArgs: |
| 201 |
def test_not_raise_exception_with_mixed_encoding(self, tw_mock) -> None: |
| 202 |
args = [("unicode_string", "São Paulo"), ("utf8_string", b"S\xc3\xa3o Paulo")] |
| 203 |
|
| 204 |
r = ReprFuncArgs(args) |
| 205 |
r.toterminal(tw_mock) |
| 206 |
|
| 207 |
assert ( |
| 208 |
tw_mock.lines[0] |
| 209 |
== r"unicode_string = São Paulo, utf8_string = b'S\xc3\xa3o Paulo'" |
| 210 |
) |
| 211 |
|
| 212 |
|
| 213 |
def test_ExceptionChainRepr(): |
| 214 |
"""Test ExceptionChainRepr, especially with regard to being hashable.""" |
| 215 |
try: |
| 216 |
raise ValueError() |
| 217 |
except ValueError: |
| 218 |
excinfo1 = ExceptionInfo.from_current() |
| 219 |
excinfo2 = ExceptionInfo.from_current() |
| 220 |
|
| 221 |
repr1 = excinfo1.getrepr() |
| 222 |
repr2 = excinfo2.getrepr() |
| 223 |
assert repr1 != repr2 |
| 224 |
|
| 225 |
assert isinstance(repr1, ExceptionChainRepr) |
| 226 |
assert hash(repr1) != hash(repr2) |
| 227 |
assert repr1 is not excinfo1.getrepr() |