summaryrefslogtreecommitdiffstats
path: root/third_party/python/py/testing/log/test_warning.py
blob: a460c319e8727c692cfcb0b567141bbd883c26fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import sys
from distutils.version import LooseVersion

import pytest

import py

mypath = py.path.local(__file__).new(ext=".py")


win = sys.platform.startswith('win')
pytestmark = pytest.mark.skipif(win and LooseVersion(pytest.__version__) >= LooseVersion('3.1'),
                                reason='apiwarn is not compatible with pytest >= 3.1 (#162)')


@pytest.mark.xfail
def test_forwarding_to_warnings_module():
    pytest.deprecated_call(py.log._apiwarn, "1.3", "..")

def test_apiwarn_functional(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", stacklevel=1)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_apiwarn_functional).co_firstlineno + 2
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1

def test_stacklevel(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel=2)
    # 3
    # 4
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel).co_firstlineno + 6
    warning = str(err)
    assert warning.find(":%s" % lno) != -1

def test_stacklevel_initpkg_with_resolve(testdir, recwarn):
    testdir.makepyfile(modabc="""
        import py
        def f():
            py.log._apiwarn("x", "some", stacklevel="apipkg123")
    """)
    testdir.makepyfile(apipkg123="""
        def __getattr__():
            import modabc
            modabc.f()
    """)
    p = testdir.makepyfile("""
        import apipkg123
        apipkg123.__getattr__()
    """)
    capture = py.io.StdCapture()
    p.pyimport()
    out, err = capture.reset()
    warning = str(err)
    loc = 'test_stacklevel_initpkg_with_resolve.py:2'
    assert warning.find(loc) != -1

def test_stacklevel_initpkg_no_resolve(recwarn):
    def f():
        py.log._apiwarn("x", "some", stacklevel="apipkg")
    capture = py.io.StdCapture()
    f()
    out, err = capture.reset()
    lno = py.code.getrawcode(test_stacklevel_initpkg_no_resolve).co_firstlineno + 2
    warning = str(err)
    assert warning.find(":%s" % lno) != -1


def test_function(recwarn):
    capture = py.io.StdCapture()
    py.log._apiwarn("x.y.z", "something", function=test_function)
    out, err = capture.reset()
    py.builtin.print_("out", out)
    py.builtin.print_("err", err)
    assert err.find("x.y.z") != -1
    lno = py.code.getrawcode(test_function).co_firstlineno
    exp = "%s:%s" % (mypath, lno)
    assert err.find(exp) != -1