|
1 | 1 | # mypy: allow-untyped-defs |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +from collections.abc import Iterator |
4 | 5 | import os |
5 | 6 | import sys |
| 7 | +from typing import cast |
6 | 8 | from unittest import mock |
7 | 9 |
|
| 10 | +from _pytest.config import Config |
8 | 11 | from _pytest.config import ExitCode |
| 12 | +from _pytest.config import RegisteredMarker |
| 13 | +from _pytest.config import UsageError |
| 14 | +from _pytest.mark import _validate_marker_names |
9 | 15 | from _pytest.mark import MarkGenerator |
| 16 | +from _pytest.mark.expression import Expression |
10 | 17 | from _pytest.mark.structures import _EmptyParameterSetMark |
11 | 18 | from _pytest.mark.structures import EMPTY_PARAMETERSET_OPTION |
12 | 19 | from _pytest.nodes import Collector |
@@ -219,6 +226,114 @@ def test_hello(): |
219 | 226 | ) |
220 | 227 |
|
221 | 228 |
|
| 229 | +class TestValidateMarkerNames: |
| 230 | + """Tests for _validate_marker_names (issue #2781).""" |
| 231 | + |
| 232 | + class FakeConfig: |
| 233 | + def __init__( |
| 234 | + self, |
| 235 | + markers: list[str], |
| 236 | + strict_markers: bool | None = None, |
| 237 | + strict: bool = False, |
| 238 | + ) -> None: |
| 239 | + self._ini: dict[str, list[str] | bool | None] = { |
| 240 | + "markers": markers, |
| 241 | + "strict_markers": strict_markers, |
| 242 | + "strict": strict, |
| 243 | + } |
| 244 | + |
| 245 | + def getini(self, name: str) -> list[str] | bool | None: |
| 246 | + return self._ini[name] |
| 247 | + |
| 248 | + def _iter_registered_markers(self) -> Iterator[RegisteredMarker]: |
| 249 | + yield from Config._iter_registered_markers(cast(Config, self)) |
| 250 | + |
| 251 | + def _make_config( |
| 252 | + self, |
| 253 | + strict_markers: bool | None = None, |
| 254 | + strict: bool = False, |
| 255 | + ) -> Config: |
| 256 | + return cast( |
| 257 | + Config, |
| 258 | + self.FakeConfig( |
| 259 | + markers=["registered: a registered marker"], |
| 260 | + strict_markers=strict_markers, |
| 261 | + strict=strict, |
| 262 | + ), |
| 263 | + ) |
| 264 | + |
| 265 | + def test_unknown_marker_with_strict_markers(self) -> None: |
| 266 | + expr = Expression.compile("unknown_marker") |
| 267 | + |
| 268 | + with pytest.raises(UsageError, match=r"Unknown marker.*unknown_marker"): |
| 269 | + _validate_marker_names(expr, self._make_config(strict_markers=True)) |
| 270 | + |
| 271 | + def test_unknown_marker_with_strict(self) -> None: |
| 272 | + expr = Expression.compile("unknown_marker") |
| 273 | + |
| 274 | + with pytest.raises(UsageError, match=r"Unknown marker.*unknown_marker"): |
| 275 | + _validate_marker_names(expr, self._make_config(strict=True)) |
| 276 | + |
| 277 | + def test_registered_marker_passes(self) -> None: |
| 278 | + expr = Expression.compile("registered") |
| 279 | + |
| 280 | + _validate_marker_names(expr, self._make_config(strict_markers=True)) |
| 281 | + |
| 282 | + def test_no_validation_without_strict(self) -> None: |
| 283 | + expr = Expression.compile("any_marker") |
| 284 | + |
| 285 | + _validate_marker_names(expr, self._make_config()) |
| 286 | + |
| 287 | + |
| 288 | +@pytest.fixture |
| 289 | +def markexpr_pytester(pytester: Pytester) -> Pytester: |
| 290 | + pytester.makeini( |
| 291 | + """ |
| 292 | + [pytest] |
| 293 | + markers = |
| 294 | + registered: a registered marker |
| 295 | + """ |
| 296 | + ) |
| 297 | + pytester.makepyfile( |
| 298 | + """ |
| 299 | + import pytest |
| 300 | +
|
| 301 | + @pytest.mark.registered |
| 302 | + def test_registered(): |
| 303 | + pass |
| 304 | +
|
| 305 | + def test_plain(): |
| 306 | + pass |
| 307 | + """ |
| 308 | + ) |
| 309 | + return pytester |
| 310 | + |
| 311 | + |
| 312 | +@pytest.mark.parametrize("option", ["--strict-markers", "--strict"]) |
| 313 | +def test_strict_prohibits_unregistered_markers_in_markexpr( |
| 314 | + markexpr_pytester: Pytester, option: str |
| 315 | +) -> None: |
| 316 | + result = markexpr_pytester.runpytest(option, "-m", "registered or unregisteredmark") |
| 317 | + assert result.ret == ExitCode.USAGE_ERROR |
| 318 | + result.stderr.fnmatch_lines( |
| 319 | + ["*Unknown marker(s) in '-m' expression: unregisteredmark*"] |
| 320 | + ) |
| 321 | + |
| 322 | + |
| 323 | +def test_strict_allows_registered_markers_in_markexpr( |
| 324 | + markexpr_pytester: Pytester, |
| 325 | +) -> None: |
| 326 | + result = markexpr_pytester.runpytest("--strict-markers", "-m", "registered") |
| 327 | + result.assert_outcomes(passed=1, deselected=1) |
| 328 | + |
| 329 | + |
| 330 | +def test_unregistered_markers_in_markexpr_allowed_without_strict( |
| 331 | + markexpr_pytester: Pytester, |
| 332 | +) -> None: |
| 333 | + result = markexpr_pytester.runpytest("-m", "unregisteredmark") |
| 334 | + result.assert_outcomes(deselected=2) |
| 335 | + |
| 336 | + |
222 | 337 | @pytest.mark.parametrize( |
223 | 338 | ("expr", "expected_passed"), |
224 | 339 | [ |
|
0 commit comments