Skip to content

Commit e8ba6ef

Browse files
committed
feat(tck): fail a run whose canonical features outgrew a reservation
A reserved tag is a name held open for scenarios that do not exist yet, and it is only ever temporary: the specification writes them, the tag starts gating something, and the capability becomes declarable. Until this package follows, TckConfig refuses to let anyone declare it -- so the new scenarios skip, for a capability no adopter is allowed to claim, and the report shows a gap the provider may not have. Appendix F calls that the unclaimable capability, and it has no local symptom: a handful of extra skips in a run that is otherwise green. @targeting was reserved until spec revision 26362f85 gave it three scenarios, so this is not hypothetical. This package already checked the property -- test_declaration asserted that no packaged feature file carries a reserved tag -- but a self-test is read by whoever changes this package, and a reservation expires somewhere else. An adoption that re-pinned the assets and ran the suite saw nothing. So the check moves into the plugin, where it fails an adopter's run, and the self-test keeps the converse half: every declarable capability must be carried by some canonical scenario, which catches a tag added to the enum and never wired to anything. extensions.canonical_tags() reads the tags off the packaged feature files. Tag lines only, which matters more than it sounds: events.feature mentions @caching in a comment, saying where those scenarios will go once they exist, so a scan that read whole files would fail every adoption over a sentence. Recursive, because the shape of that directory is the specification's to change. capability.expired_reservations() is the pure comparison, matching JavaScript's expiredReservations, and neither name is exported from the package -- the adopter-facing answer is RESERVED_CAPABILITIES. The tags come from the packaged assets rather than from what was collected, for two reasons. Only the canonical set can expire a reservation: an adopter's own feature reaching for a reserved tag is a mistake in that file, not news about the specification. And a narrowed run cannot then select its way past the check. What is read off the collection is whether the session runs the conformance suite at all -- the plugin is installed for every pytest run in the environment, and an unrelated test suite has no business failing over the contents of these feature files. Signed-off-by: Simon Schrottner <simon.schrottner@flagsmith.com>
1 parent 9a228a8 commit e8ba6ef

5 files changed

Lines changed: 278 additions & 31 deletions

File tree

‎tools/openfeature-tck/README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,18 @@ reason, back when both were reserved.
335335
mirror of the mistake the set exists to prevent — a capability that can be verified, refused the
336336
chance — so `@targeting` moved out of it the moment the specification gave it three.
337337

338+
**A reservation this package has outgrown fails the run.** The reservation expires in the
339+
specification repository and `RESERVED_CAPABILITIES` lives here, so on every run the plugin reads the
340+
tags the packaged feature files actually carry and refuses to continue if one of them is still listed
341+
as reserved. Without that, re-pinning the assets onto a revision that gave `@caching` scenarios would
342+
skip them — for a capability `TckConfig` refuses to let anyone declare — and the only visible trace
343+
would be a few more skips. Appendix F calls that the unclaimable capability. The fix when it fires is
344+
to take the tag out of `RESERVED_CAPABILITIES` and decide, per adoption, whether to declare it.
345+
346+
Only the canonical set can expire a reservation. A feature file of your own reaching for a reserved
347+
tag is a mistake in that file, not news about the specification, so the check reads the packaged
348+
assets — which also means a `-k` or `--deselect` cannot narrow the run past it.
349+
338350
`@numeric-coercion` deserves a note, because it is the one capability here that **the specification
339351
does not define**. OpenFeature has a single numeric type on purpose — `number` is "a numeric value of
340352
unspecified type or size", and languages **may** differentiate between integers and floats "as idioms

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/capability.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import typing
56
from enum import Enum
67

78
__all__ = ["Capability"]
@@ -363,3 +364,28 @@ def capability_for_tag(tag: str) -> Capability | None:
363364
capability means reading them back.
364365
"""
365366
return _BY_TAG.get(tag)
367+
368+
369+
def expired_reservations(tags: typing.Iterable[str]) -> tuple[Capability, ...]:
370+
"""Reserved capabilities that the tags handed in turn out to carry.
371+
372+
A non-empty answer means :data:`RESERVED_CAPABILITIES` is out of date: the
373+
scenarios the tag was being held open for now exist, so the capability can
374+
be verified and an adoption should be allowed -- and required -- to say
375+
whether it has it.
376+
377+
Leaving the reservation in place instead is the mirror of declaring an
378+
unverified capability, and it is the quieter mistake of the two. Declaring
379+
a reserved capability is refused, so nobody can claim it; the new scenarios
380+
are therefore skipped for a capability an adopter has no way to declare,
381+
and the report says a gap exists where the provider may well have none.
382+
Appendix F names that the unclaimable capability.
383+
384+
Read off the feature files rather than compared against a second list,
385+
because a reservation expires in the specification repository while this
386+
set lives here. Deduplicated and ordered by tag: the tags arrive from every
387+
scenario of every feature file, and one carried twice is not two expiries.
388+
"""
389+
carried = set(tags)
390+
expired = (c for c in RESERVED_CAPABILITIES if c.tag in carried)
391+
return tuple(sorted(expired, key=lambda capability: capability.tag))

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/extensions.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import importlib.resources
5151
import inspect
52+
import re
5253
import typing
5354
from pathlib import Path
5455

@@ -57,6 +58,7 @@
5758
"EXTENSIONS_DIRECTORY",
5859
"EXTENSIONS_URI_PREFIX",
5960
"canonical_root",
61+
"canonical_tags",
6062
"collision_problem",
6163
"extension_root",
6264
"feature_paths",
@@ -69,6 +71,9 @@
6971

7072
_PACKAGE = "openfeature.contrib.tools.tck"
7173

74+
_TAG = re.compile(r"@[\w-]+")
75+
"""One Gherkin tag, as it appears on a tag line."""
76+
7277
CANONICAL_DIRECTORY = "gherkin"
7378
"""The packaged directory the canonical feature files live in.
7479
@@ -185,6 +190,41 @@ def canonical_root() -> Path | None:
185190
return None
186191

187192

193+
def canonical_tags() -> frozenset[str]:
194+
"""Every Gherkin tag the packaged canonical feature files carry.
195+
196+
What a reservation is checked against: a tag is reserved because no
197+
canonical scenario carries it, and this is the set that says whether that
198+
is still true. See :func:`~.capability.expired_reservations`.
199+
200+
**Tag lines only**, which is what tells a tag apart from the same word
201+
written in prose. ``events.feature`` mentions ``@caching`` in a comment,
202+
saying where those scenarios will go once they exist, so a scan that read
203+
the whole file would report the reservation as expired on the strength of a
204+
sentence about it -- and every adoption would then fail on a sentence.
205+
206+
Recursive, because the shape of the canonical directory is the
207+
specification's to change: a flat scan would answer "no tags" for a file
208+
one directory down, which is silent under-collection, the failure mode the
209+
rest of this module exists to stop.
210+
211+
Empty when the assets are not reachable as files -- an installation from a
212+
zipimport, say. Everything built on this then degrades to "cannot tell",
213+
which is the honest answer and never a false accusation.
214+
"""
215+
root = canonical_root()
216+
if root is None or not root.is_dir():
217+
return frozenset()
218+
219+
tags: set[str] = set()
220+
for feature in sorted(root.rglob("*.feature")):
221+
for line in feature.read_text(encoding="utf-8").splitlines():
222+
stripped = line.strip()
223+
if stripped.startswith("@"):
224+
tags.update(_TAG.findall(stripped))
225+
return frozenset(tags)
226+
227+
188228
def is_canonical(path: Path) -> bool:
189229
"""Whether a feature file is one of the packaged canonical ones."""
190230
canonical = canonical_root()

‎tools/openfeature-tck/src/openfeature/contrib/tools/tck/plugin.py‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""The pytest plugin: capability gating, scenario state, and the shared step vocabulary.
1+
"""The pytest plugin: capability gating, scenario state, and the step vocabulary.
22
33
Registered through the ``pytest11`` entry point, so installing this package is
44
all it takes for the step definitions to be available. pytest-bdd resolves steps
@@ -10,19 +10,26 @@
1010
in their own ``conftest.py`` is resolved by the same fixture lookup as one this
1111
plugin ships, so their scenarios need no glue and no second harness. See
1212
:mod:`~.extensions`.
13+
14+
It is also where the two things a run must refuse to do quietly are checked:
15+
skipping a scenario whose capability was not declared happens loudly, with the
16+
reason, and a reservation the canonical assets have outgrown fails the run
17+
outright rather than skipping scenarios for a capability nobody may claim.
1318
"""
1419

1520
from __future__ import annotations
1621

1722
import typing
23+
from pathlib import Path
1824

1925
import pytest
2026

2127
from openfeature import api
2228

23-
from .capability import Capability, capability_for_marker
29+
from .capability import Capability, capability_for_marker, expired_reservations
2430
from .compose import ComposeBackend, RunningBackend, run_compose_backend
2531
from .config import TckConfig
32+
from .extensions import canonical_tags, is_canonical
2633
from .state import TckState
2734

2835
# The step modules are registered as plugins in their own right, not merely
@@ -52,6 +59,63 @@ def pytest_configure(config: pytest.Config) -> None:
5259
)
5360

5461

62+
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
63+
"""Fail the run if a canonical feature carries a tag still called reserved.
64+
65+
A reservation is a name held open for scenarios that do not exist yet, and
66+
it is only ever temporary: the specification writes them, the tag starts
67+
gating something, and the capability becomes declarable. Until this package
68+
follows, declaring it is refused -- so those scenarios are skipped for a
69+
capability an adopter cannot claim, and the report shows a gap the provider
70+
may not have. Appendix F calls that the unclaimable capability, and it has
71+
no local symptom at all, which is why it is checked rather than watched
72+
for: ``@targeting`` was reserved until spec revision ``26362f85`` gave it
73+
three scenarios.
74+
75+
Refused rather than worked around. Dropping the reservation here instead
76+
would let a run declare a capability against a package that does not know
77+
the tag exists, and the point of the check is that a human re-reads
78+
:data:`~.capability.RESERVED_CAPABILITIES` against the specification.
79+
80+
Only the canonical set can expire a reservation. An adopter's own feature
81+
reaching for a reserved tag is a mistake in that file rather than news
82+
about the specification, so the tags come from the packaged assets and not
83+
from what was collected -- which also means a narrowed run cannot select
84+
its way past the check. What *is* read off the collection is whether this
85+
session runs the conformance suite at all: the plugin is installed for
86+
every pytest run in the environment, and an unrelated test suite has no
87+
business failing over the contents of these feature files.
88+
"""
89+
if not any(_is_canonical_scenario(item) for item in items):
90+
return
91+
92+
expired = expired_reservations(canonical_tags())
93+
if not expired:
94+
return
95+
96+
named = " ".join(capability.tag for capability in expired)
97+
raise pytest.UsageError(
98+
f"reserved capabilities {named} are carried by the canonical feature "
99+
f"files, so the scenarios they were held open for now exist. Remove "
100+
f"them from RESERVED_CAPABILITIES, so an adoption can declare them and "
101+
f"be held to them -- until then those scenarios are skipped for a "
102+
f"capability nobody is allowed to claim."
103+
)
104+
105+
106+
def _is_canonical_scenario(item: pytest.Item) -> bool:
107+
"""Whether a collected node is a scenario from the packaged feature files.
108+
109+
``__scenario__`` is what pytest-bdd hangs on the function it generates, and
110+
it is readable at collection without running a fixture. The feature file is
111+
then matched by path rather than by the uri it reports, so the check does
112+
not rest on the derivation in :mod:`~.extensions`.
113+
"""
114+
scenario = getattr(getattr(item, "function", None), "__scenario__", None)
115+
filename = getattr(getattr(scenario, "feature", None), "filename", None)
116+
return bool(filename) and is_canonical(Path(str(filename)))
117+
118+
55119
@pytest.fixture(scope="session")
56120
def tck_backend(compose_backend: ComposeBackend) -> typing.Iterator[RunningBackend]:
57121
"""The Compose stack under test, started once for the whole session.

0 commit comments

Comments
 (0)