Exemplo n.º 1
0
def test_getfslineno() -> None:
    def f(x) -> None:
        raise NotImplementedError()

    fspath, lineno = getfslineno(f)

    assert isinstance(fspath, py.path.local)
    assert fspath.basename == "test_source.py"
    assert lineno == f.__code__.co_firstlineno - 1  # see findsource

    class A:
        pass

    fspath, lineno = getfslineno(A)

    _, A_lineno = inspect.findsource(A)
    assert isinstance(fspath, py.path.local)
    assert fspath.basename == "test_source.py"
    assert lineno == A_lineno

    assert getfslineno(3) == ("", -1)

    class B:
        pass

    B.__name__ = B.__qualname__ = "B2"
    assert getfslineno(B)[1] == -1
Exemplo n.º 2
0
def test_getfslineno():
    from _pytest._code import getfslineno

    def f(x):
        pass

    fspath, lineno = getfslineno(f)

    assert fspath.basename == "test_source.py"
    assert lineno == _pytest._code.getrawcode(f).co_firstlineno - 1  # see findsource

    class A(object):
        pass

    fspath, lineno = getfslineno(A)

    _, A_lineno = inspect.findsource(A)
    assert fspath.basename == "test_source.py"
    assert lineno == A_lineno

    assert getfslineno(3) == ("", -1)

    class B(object):
        pass

    B.__name__ = "B2"
    assert getfslineno(B)[1] == -1
Exemplo n.º 3
0
def test_getfslineno() -> None:
    from _pytest._code import getfslineno

    def f(x) -> None:
        pass

    fspath, lineno = getfslineno(f)

    assert isinstance(fspath, py.path.local)
    assert fspath.basename == "test_source.py"
    assert lineno == f.__code__.co_firstlineno - 1  # see findsource

    class A:
        pass

    fspath, lineno = getfslineno(A)

    _, A_lineno = inspect.findsource(A)
    assert fspath.basename == "test_source.py"
    assert lineno == A_lineno

    assert getfslineno(3) == ("", -1)

    class B:
        pass

    B.__name__ = "B2"
    assert getfslineno(B)[1] == -1
Exemplo n.º 4
0
def test_getfslineno():
    from _pytest._code import getfslineno

    def f(x):
        pass

    fspath, lineno = getfslineno(f)

    assert fspath.basename == "test_source.py"
    assert lineno == _pytest._code.getrawcode(f).co_firstlineno - 1  # see findsource

    class A(object):
        pass

    fspath, lineno = getfslineno(A)

    _, A_lineno = inspect.findsource(A)
    assert fspath.basename == "test_source.py"
    assert lineno == A_lineno

    assert getfslineno(3) == ("", -1)

    class B(object):
        pass

    B.__name__ = "B2"
    assert getfslineno(B)[1] == -1
Exemplo n.º 5
0
def test_wrapped_getfslineno() -> None:
    def func():
        pass

    def wrap(f):
        func.__wrapped__ = f  # type: ignore
        func.patchings = ["qwe"]  # type: ignore
        return func

    @wrap
    def wrapped_func(x, y, z):
        pass

    fs, lineno = getfslineno(wrapped_func)
    fs2, lineno2 = getfslineno(wrap)
    assert lineno > lineno2, "getfslineno does not unwrap correctly"
Exemplo n.º 6
0
def test_getfslineno() -> None:
    def f(x) -> None:
        raise NotImplementedError()

    fspath, lineno = getfslineno(f)

    assert isinstance(fspath, py.path.local)
    assert fspath.basename == "test_source.py"
    assert lineno == f.__code__.co_firstlineno - 1  # see findsource

    class A:
        pass

    fspath, lineno = getfslineno(A)

    _, A_lineno = inspect.findsource(A)
    assert isinstance(fspath, py.path.local)
    assert fspath.basename == "test_source.py"
    assert lineno == A_lineno

    assert getfslineno(3) == ("", -1)

    class B:
        pass

    B.__name__ = "B2"
    assert getfslineno(B)[1] == -1

    co = compile("...", "", "eval")
    assert co.co_filename == ""

    if hasattr(sys, "pypy_version_info"):
        assert getfslineno(co) == ("", -1)
    else:
        assert getfslineno(co) == ("", 0)
Exemplo n.º 7
0
def get_fslocation_from_item(node: "Node") -> Tuple[Union[str, Path], Optional[int]]:
    """Try to extract the actual location from a node, depending on available attributes:

    * "location": a pair (path, lineno)
    * "obj": a Python object that the node wraps.
    * "fspath": just a path

    :rtype: A tuple of (str|Path, int) with filename and line number.
    """
    # See Item.location.
    location: Optional[Tuple[str, Optional[int], str]] = getattr(node, "location", None)
    if location is not None:
        return location[:2]
    obj = getattr(node, "obj", None)
    if obj is not None:
        return getfslineno(obj)
    return getattr(node, "fspath", "unknown location"), -1
Exemplo n.º 8
0
def get_fslocation_from_item(
    item: "Item", ) -> Tuple[Union[str, py.path.local], Optional[int]]:
    """Tries to extract the actual location from an item, depending on available attributes:

    * "fslocation": a pair (path, lineno)
    * "obj": a Python object that the item wraps.
    * "fspath": just a path

    :rtype: a tuple of (str|LocalPath, int) with filename and line number.
    """
    try:
        return item.location[:2]
    except AttributeError:
        pass
    obj = getattr(item, "obj", None)
    if obj is not None:
        return getfslineno(obj)
    return getattr(item, "fspath", "unknown location"), -1
Exemplo n.º 9
0
def report_process_crash(item, result):
    from _pytest._code import getfslineno

    path, lineno = getfslineno(item)
    info = "%s:%s: running the test CRASHED with signal %d" % (
        path,
        lineno,
        result.signal,
    )
    from _pytest import runner

    # pytest >= 4.1
    has_from_call = getattr(runner.CallInfo, "from_call", None) is not None
    if has_from_call:
        call = runner.CallInfo.from_call(lambda: 0 / 0, "???")
    else:
        call = runner.CallInfo(lambda: 0 / 0, "???")
    call.excinfo = info
    rep = runner.pytest_runtest_makereport(item, call)
    if result.out:
        rep.sections.append(("captured stdout", result.out))
    if result.err:
        rep.sections.append(("captured stderr", result.err))

    xfail_marker = item.get_closest_marker("xfail")
    if not xfail_marker:
        return rep

    rep.outcome = "skipped"
    rep.wasxfail = ("reason: {xfail_reason}; "
                    "pytest-forked reason: {crash_info}".format(
                        xfail_reason=xfail_marker.kwargs["reason"],
                        crash_info=info,
                    ))
    warnings.warn(
        "pytest-forked xfail support is incomplete at the moment and may "
        "output a misleading reason message",
        RuntimeWarning,
    )

    return rep