| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from pathlib import Path |
| 5 |
import re |
| 6 |
import warnings |
| 7 |
|
| 8 |
from _pytest import nodes |
| 9 |
from _pytest.outcomes import OutcomeException |
| 10 |
from _pytest.pytester import Pytester |
| 11 |
from _pytest.warning_types import PytestWarning |
| 12 |
import pytest |
| 13 |
|
| 14 |
|
| 15 |
def test_node_from_parent_disallowed_arguments() -> None: |
| 16 |
with pytest.raises(TypeError, match="session is"): |
| 17 |
nodes.Node.from_parent(None, session=None) # type: ignore[arg-type] |
| 18 |
with pytest.raises(TypeError, match="config is"): |
| 19 |
nodes.Node.from_parent(None, config=None) # type: ignore[arg-type] |
| 20 |
|
| 21 |
|
| 22 |
def test_node_direct_construction_deprecated() -> None: |
| 23 |
with pytest.raises( |
| 24 |
OutcomeException, |
| 25 |
match=( |
| 26 |
r"Direct construction of _pytest\.nodes\.Node has been deprecated, please " |
| 27 |
r"use _pytest\.nodes\.Node\.from_parent.\nSee " |
| 28 |
r"https://docs\.pytest\.org/en/stable/deprecations\.html#node-construction-changed-to-node-from-parent" |
| 29 |
r" for more details\." |
| 30 |
), |
| 31 |
): |
| 32 |
nodes.Node(None, session=None) # type: ignore[arg-type] |
| 33 |
|
| 34 |
|
| 35 |
def test_subclassing_both_item_and_collector_deprecated( |
| 36 |
request, tmp_path: Path |
| 37 |
) -> None: |
| 38 |
"""Verifies we warn on diamond inheritance from both Item and Collector.""" |
| 39 |
# We do not expect any warnings messages to be issued during class definition. |
| 40 |
with warnings.catch_warnings(): |
| 41 |
warnings.simplefilter("error") |
| 42 |
|
| 43 |
class SoWrong(nodes.Item, nodes.File): |
| 44 |
def __init__(self, parent: nodes.Collector, path: Path) -> None: |
| 45 |
super().__init__(name="broken", parent=parent, path=path) |
| 46 |
|
| 47 |
def collect(self): |
| 48 |
raise NotImplementedError() |
| 49 |
|
| 50 |
def runtest(self): |
| 51 |
raise NotImplementedError() |
| 52 |
|
| 53 |
with pytest.warns(PytestWarning) as rec: |
| 54 |
SoWrong.from_parent(request.session, path=tmp_path / "broken.txt") |
| 55 |
messages = [str(x.message) for x in rec] |
| 56 |
assert any( |
| 57 |
re.search(".*SoWrong.* not using a cooperative constructor.*", x) |
| 58 |
for x in messages |
| 59 |
) |
| 60 |
assert any( |
| 61 |
re.search("(?m)SoWrong .* should not be a collector", x) for x in messages |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize( |
| 66 |
"warn_type, msg", [(DeprecationWarning, "deprecated"), (PytestWarning, "pytest")] |
| 67 |
) |
| 68 |
def test_node_warn_is_no_longer_only_pytest_warnings( |
| 69 |
pytester: Pytester, warn_type: type[Warning], msg: str |
| 70 |
) -> None: |
| 71 |
items = pytester.getitems( |
| 72 |
""" |
| 73 |
def test(): |
| 74 |
pass |
| 75 |
""" |
| 76 |
) |
| 77 |
with pytest.warns(warn_type, match=msg): |
| 78 |
items[0].warn(warn_type(msg)) |
| 79 |
|
| 80 |
|
| 81 |
def test_node_warning_enforces_warning_types(pytester: Pytester) -> None: |
| 82 |
items = pytester.getitems( |
| 83 |
""" |
| 84 |
def test(): |
| 85 |
pass |
| 86 |
""" |
| 87 |
) |
| 88 |
with pytest.raises( |
| 89 |
ValueError, match="warning must be an instance of Warning or subclass" |
| 90 |
): |
| 91 |
items[0].warn(Exception("ok")) # type: ignore[arg-type] |
| 92 |
|
| 93 |
|
| 94 |
class TestNormSep: |
| 95 |
"""Tests for the norm_sep helper function.""" |
| 96 |
|
| 97 |
def test_forward_slashes_unchanged(self) -> None: |
| 98 |
"""Forward slashes pass through unchanged.""" |
| 99 |
assert nodes.norm_sep("a/b/c") == "a/b/c" |
| 100 |
|
| 101 |
def test_backslashes_converted(self) -> None: |
| 102 |
"""Backslashes are converted to forward slashes.""" |
| 103 |
assert nodes.norm_sep("a\\b\\c") == "a/b/c" |
| 104 |
|
| 105 |
def test_mixed_separators(self) -> None: |
| 106 |
"""Mixed separators are all normalized to forward slashes.""" |
| 107 |
assert nodes.norm_sep("a\\b/c\\d") == "a/b/c/d" |
| 108 |
|
| 109 |
def test_pathlike_input(self, tmp_path: Path) -> None: |
| 110 |
"""PathLike objects are converted to string with normalized separators.""" |
| 111 |
# Create a path and verify it's normalized |
| 112 |
result = nodes.norm_sep(tmp_path / "subdir" / "file.py") |
| 113 |
assert "\\" not in result |
| 114 |
assert "subdir/file.py" in result |
| 115 |
|
| 116 |
def test_empty_string(self) -> None: |
| 117 |
"""Empty string returns empty string.""" |
| 118 |
assert nodes.norm_sep("") == "" |
| 119 |
|
| 120 |
def test_windows_absolute_path(self) -> None: |
| 121 |
"""Windows absolute paths have backslashes converted.""" |
| 122 |
assert nodes.norm_sep("C:\\Users\\test\\project") == "C:/Users/test/project" |
| 123 |
|
| 124 |
|
| 125 |
def test__check_initialpaths_for_relpath() -> None: |
| 126 |
"""Ensure that it handles dirs, and does not always use dirname.""" |
| 127 |
cwd = Path.cwd() |
| 128 |
|
| 129 |
initial_paths = frozenset({cwd}) |
| 130 |
|
| 131 |
assert nodes._check_initialpaths_for_relpath(initial_paths, cwd) == "" |
| 132 |
|
| 133 |
sub = cwd / "file" |
| 134 |
assert nodes._check_initialpaths_for_relpath(initial_paths, sub) == "file" |
| 135 |
|
| 136 |
outside = Path("/outside-this-does-not-exist") |
| 137 |
assert nodes._check_initialpaths_for_relpath(initial_paths, outside) is None |
| 138 |
|
| 139 |
|
| 140 |
def test_failure_with_changed_cwd(pytester: Pytester) -> None: |
| 141 |
""" |
| 142 |
Test failure lines should use absolute paths if cwd has changed since |
| 143 |
invocation, so the path is correct (#6428). |
| 144 |
""" |
| 145 |
p = pytester.makepyfile( |
| 146 |
""" |
| 147 |
import os |
| 148 |
import pytest |
| 149 |
|
| 150 |
@pytest.fixture |
| 151 |
def private_dir(): |
| 152 |
out_dir = 'ddd' |
| 153 |
os.mkdir(out_dir) |
| 154 |
old_dir = os.getcwd() |
| 155 |
os.chdir(out_dir) |
| 156 |
yield out_dir |
| 157 |
os.chdir(old_dir) |
| 158 |
|
| 159 |
def test_show_wrong_path(private_dir): |
| 160 |
assert False |
| 161 |
""" |
| 162 |
) |
| 163 |
result = pytester.runpytest() |
| 164 |
result.stdout.fnmatch_lines([str(p) + ":*: AssertionError", "*1 failed in *"]) |