Skip to content

The agent that grows with you

tests: reject unknown platforms() specs at collection

ethernetb0e6dd7dc72b
diff --git a/tests/conftest.py b/tests/conftest.py
index 11505d1e110..76fae1adc3c 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1358,11 +1358,18 @@ def _host_matches_platforms(conditions, arch=None, arch_negate=False):
specs = [str(c).strip().lower() for c in conditions if str(c).strip()]
if not specs:
return True, "platforms() with no specs matches every host"
+ # An unknown spec is a collection error, never a skip: a typo like
+ # platforms("linx") would otherwise drop the test on every host while
+ # both lanes stay green — the exact failure the gate exists to catch.
+ unknown = [spec for spec in specs if spec.removeprefix("not ").strip() not in _PLATFORM_ALIASES]
+ if unknown:
+ raise pytest.UsageError(
+ f"platforms(): unknown spec(s) {', '.join(map(repr, unknown))} — valid: "
+ f"{', '.join(sorted(_PLATFORM_ALIASES))}, each optionally prefixed with 'not '"
+ )
for spec in specs:
negate = spec.startswith("not ")
leaf = spec[4:].strip() if negate else spec
- if leaf not in _PLATFORM_ALIASES:
- return False, f"platforms(): unknown spec {spec!r}"
wanted = _PLATFORM_ALIASES[leaf]
matched = (not wanted) or host in wanted
if negate:
@@ -1391,11 +1398,14 @@ def _platforms_gate_reason(item):
for mark in item.iter_markers("platforms"):
kwargs = dict(mark.kwargs)
conds = list(mark.args)
- ok, reason = _host_matches_platforms(
- conds,
- arch=kwargs.pop("arch", None),
- arch_negate=kwargs.pop("arch_negate", False),
- )
+ try:
+ ok, reason = _host_matches_platforms(
+ conds,
+ arch=kwargs.pop("arch", None),
+ arch_negate=kwargs.pop("arch_negate", False),
+ )
+ except pytest.UsageError as exc:
+ raise pytest.UsageError(f"{item.nodeid}: {exc}") from None
if kwargs:
raise pytest.UsageError(
f"{item.nodeid}: platforms() got unexpected keyword(s) "
diff --git a/tests/test_os_marker_gating.py b/tests/test_os_marker_gating.py
index e87fa5d28a3..7cde9aa197f 100644
--- a/tests/test_os_marker_gating.py
+++ b/tests/test_os_marker_gating.py
@@ -7,8 +7,16 @@ import sys
import pytest
+_INVALID_MARKERS = {
+ "stacked": "@pytest.mark.platforms('any')",
+ "keyword": "@pytest.mark.platforms('any', bogus=True)",
+ "typo": "@pytest.mark.platforms('linx')",
+}
+
+
@pytest.mark.parametrize("invalid,message", [("", ""), ("stacked", "at most one platforms()"),
- ("keyword", "unexpected keyword")])
+ ("keyword", "unexpected keyword"),
+ ("typo", "unknown spec")])
def test_native_collection_witnesses(tmp_path, invalid, message):
root = Path(__file__).resolve().parents[1]
host = {"linux": "linux", "darwin": "macos", "win32": "windows"}[sys.platform]
@@ -25,7 +33,7 @@ def test_native_collection_witnesses(tmp_path, invalid, message):
suite += f"{marker}\ndef test_{name}():\n Path({name!r}).touch()\n"
(tmp_path / "test_valid.py").write_text(suite, encoding="utf-8")
if invalid:
- marker = "@pytest.mark.platforms('any')" if invalid == "stacked" else "@pytest.mark.platforms('any', bogus=True)"
+ marker = _INVALID_MARKERS[invalid]
module_mark = "pytestmark = pytest.mark.platforms('any')\n" if invalid == "stacked" else ""
(tmp_path / "test_bad.py").write_text(
f"import pytest\n{module_mark}{marker}\ndef test_bad():\n raise AssertionError('must reject collection')\n",
diff --git a/tests/test_platforms_marker.py b/tests/test_platforms_marker.py
index 6df5309527e..f36952191bf 100644
--- a/tests/test_platforms_marker.py
+++ b/tests/test_platforms_marker.py
@@ -11,14 +11,21 @@ from tests.conftest import _host_matches_platforms, _platform_machine
(("posix",), {"linux", "darwin"}), (("not macos",), {"linux", "win32"}),
(("not windows",), {"linux", "darwin"}), (("linux", "macos"), {"linux", "darwin"}),
(("any",), {"linux", "darwin", "win32"}), ((), {"linux", "darwin", "win32"}),
- (("linux", "amiga"), {"linux"}), (("linux", "win32host-mismatch"), {"linux"}),
- (("amiga",), set()), (("not amiga",), set()),
])
def test_native_spec_table(specs, hosts):
ok, reason = _host_matches_platforms(specs)
assert ok is (sys.platform in hosts), reason
- if specs in (("amiga",), ("not amiga",)):
- assert "unknown spec" in reason
+
+
+ ("amiga",), ("not amiga",), ("linx",),
+ # A matching spec never excuses a misspelt sibling: the typo would drop the
+ # test on the host it was meant for while this host stays green.
+ ("linux", "amiga"), ("darwin", "linux"), ("win32", "posix"),
+])
+def test_unknown_spec_is_a_collection_error_not_a_skip(specs):
+ with pytest.raises(pytest.UsageError, match="unknown spec"):
+ _host_matches_platforms(specs)
@pytest.mark.parametrize("negate", [False, True])