2020-12-19 20:06:17 +08:00
|
|
|
from pathlib import Path
|
2020-12-14 21:54:59 +08:00
|
|
|
from typing import cast
|
2020-10-24 07:09:28 +08:00
|
|
|
from typing import List
|
2020-11-21 21:49:17 +08:00
|
|
|
from typing import Type
|
2020-10-24 07:09:28 +08:00
|
|
|
|
2017-10-23 20:26:42 +08:00
|
|
|
import pytest
|
2017-10-24 00:49:49 +08:00
|
|
|
from _pytest import nodes
|
2021-06-24 17:45:32 +08:00
|
|
|
from _pytest.compat import legacy_path
|
2020-10-31 04:34:05 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-11-21 21:49:17 +08:00
|
|
|
from _pytest.warning_types import PytestWarning
|
2017-10-23 20:26:42 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-10-24 07:09:28 +08:00
|
|
|
("nodeid", "expected"),
|
2018-05-23 22:48:46 +08:00
|
|
|
(
|
2020-10-24 07:09:28 +08:00
|
|
|
("", [""]),
|
|
|
|
("a", ["", "a"]),
|
|
|
|
("aa/b", ["", "aa", "aa/b"]),
|
|
|
|
("a/b/c", ["", "a", "a/b", "a/b/c"]),
|
|
|
|
("a/bbb/c::D", ["", "a", "a/bbb", "a/bbb/c", "a/bbb/c::D"]),
|
|
|
|
("a/b/c::D::eee", ["", "a", "a/b", "a/b/c", "a/b/c::D", "a/b/c::D::eee"]),
|
|
|
|
("::xx", ["", "::xx"]),
|
2021-04-29 22:02:43 +08:00
|
|
|
# / only considered until first ::
|
|
|
|
("a/b/c::D/d::e", ["", "a", "a/b", "a/b/c", "a/b/c::D/d", "a/b/c::D/d::e"]),
|
2020-10-24 07:09:28 +08:00
|
|
|
# : alone is not a separator.
|
|
|
|
("a/b::D:e:f::g", ["", "a", "a/b", "a/b::D:e:f", "a/b::D:e:f::g"]),
|
2021-04-29 22:02:43 +08:00
|
|
|
# / not considered if a part of a test name
|
|
|
|
("a/b::c/d::e[/test]", ["", "a", "a/b", "a/b::c/d", "a/b::c/d::e[/test]"]),
|
2018-05-23 22:48:46 +08:00
|
|
|
),
|
|
|
|
)
|
2020-10-24 07:09:28 +08:00
|
|
|
def test_iterparentnodeids(nodeid: str, expected: List[str]) -> None:
|
|
|
|
result = list(nodes.iterparentnodeids(nodeid))
|
|
|
|
assert result == expected
|
2018-09-05 01:20:42 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_node_from_parent_disallowed_arguments() -> None:
|
2020-01-01 03:20:28 +08:00
|
|
|
with pytest.raises(TypeError, match="session is"):
|
2020-07-10 14:44:14 +08:00
|
|
|
nodes.Node.from_parent(None, session=None) # type: ignore[arg-type]
|
2020-01-01 03:20:28 +08:00
|
|
|
with pytest.raises(TypeError, match="config is"):
|
2020-07-10 14:44:14 +08:00
|
|
|
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type]
|
2020-01-01 03:20:28 +08:00
|
|
|
|
|
|
|
|
2021-06-24 17:45:32 +08:00
|
|
|
def test_subclassing_both_item_and_collector_deprecated(
|
|
|
|
request, tmp_path: Path
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Verifies we warn on diamond inheritance
|
|
|
|
as well as correctly managing legacy inheritance ctors with missing args
|
|
|
|
as found in plugins
|
|
|
|
"""
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
PytestWarning,
|
|
|
|
match=(
|
|
|
|
"(?m)SoWrong is an Item subclass and should not be a collector, however its bases File are collectors.\n"
|
|
|
|
"Please split the Collectors and the Item into separate node types.\n.*"
|
|
|
|
),
|
|
|
|
):
|
|
|
|
|
|
|
|
class SoWrong(nodes.File, nodes.Item):
|
|
|
|
def __init__(self, fspath, parent):
|
|
|
|
"""Legacy ctor with legacy call # don't wana see"""
|
|
|
|
super().__init__(fspath, parent)
|
|
|
|
|
|
|
|
with pytest.warns(
|
|
|
|
PytestWarning, match=".*SoWrong.* not using a cooperative constructor.*"
|
|
|
|
):
|
|
|
|
SoWrong.from_parent(
|
|
|
|
request.session, fspath=legacy_path(tmp_path / "broken.txt")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-21 21:49:17 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"warn_type, msg", [(DeprecationWarning, "deprecated"), (PytestWarning, "pytest")]
|
|
|
|
)
|
|
|
|
def test_node_warn_is_no_longer_only_pytest_warnings(
|
|
|
|
pytester: Pytester, warn_type: Type[Warning], msg: str
|
|
|
|
) -> None:
|
|
|
|
items = pytester.getitems(
|
|
|
|
"""
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
with pytest.warns(warn_type, match=msg):
|
|
|
|
items[0].warn(warn_type(msg))
|
|
|
|
|
|
|
|
|
|
|
|
def test_node_warning_enforces_warning_types(pytester: Pytester) -> None:
|
2020-10-31 04:34:05 +08:00
|
|
|
items = pytester.getitems(
|
2018-09-05 01:20:42 +08:00
|
|
|
"""
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-11-21 21:49:17 +08:00
|
|
|
with pytest.raises(
|
|
|
|
ValueError, match="warning must be an instance of Warning or subclass"
|
|
|
|
):
|
|
|
|
items[0].warn(Exception("ok")) # type: ignore[arg-type]
|
2018-11-09 03:35:53 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test__check_initialpaths_for_relpath() -> None:
|
2018-11-09 03:35:53 +08:00
|
|
|
"""Ensure that it handles dirs, and does not always use dirname."""
|
2020-12-19 20:06:17 +08:00
|
|
|
cwd = Path.cwd()
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
class FakeSession1:
|
2020-12-19 20:06:17 +08:00
|
|
|
_initialpaths = frozenset({cwd})
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2020-12-14 21:54:59 +08:00
|
|
|
session = cast(pytest.Session, FakeSession1)
|
|
|
|
|
2021-03-15 22:01:58 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(session, cwd) == ""
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2020-12-19 20:06:17 +08:00
|
|
|
sub = cwd / "file"
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
class FakeSession2:
|
2020-12-19 20:06:17 +08:00
|
|
|
_initialpaths = frozenset({cwd})
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2020-12-14 21:54:59 +08:00
|
|
|
session = cast(pytest.Session, FakeSession2)
|
|
|
|
|
2021-03-15 22:01:58 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(session, sub) == "file"
|
2018-11-09 03:35:53 +08:00
|
|
|
|
2021-06-22 19:25:52 +08:00
|
|
|
outside = Path("/outside-this-does-not-exist")
|
2020-12-14 21:54:59 +08:00
|
|
|
assert nodes._check_initialpaths_for_relpath(session, outside) is None
|
2020-05-16 22:36:22 +08:00
|
|
|
|
|
|
|
|
2020-10-31 04:34:05 +08:00
|
|
|
def test_failure_with_changed_cwd(pytester: Pytester) -> None:
|
2020-05-16 22:36:22 +08:00
|
|
|
"""
|
|
|
|
Test failure lines should use absolute paths if cwd has changed since
|
|
|
|
invocation, so the path is correct (#6428).
|
|
|
|
"""
|
2020-10-31 04:34:05 +08:00
|
|
|
p = pytester.makepyfile(
|
2020-05-16 22:36:22 +08:00
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def private_dir():
|
|
|
|
out_dir = 'ddd'
|
|
|
|
os.mkdir(out_dir)
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(out_dir)
|
|
|
|
yield out_dir
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
def test_show_wrong_path(private_dir):
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-10-31 04:34:05 +08:00
|
|
|
result = pytester.runpytest()
|
2020-05-16 22:36:22 +08:00
|
|
|
result.stdout.fnmatch_lines([str(p) + ":*: AssertionError", "*1 failed in *"])
|