1"""Select the tests a CI point runs: a test declares where it runs.
3Registered through livery-workshop's ``pytest11`` entry point, so every
4pytest this venv starts carries it. A test without a marker runs at
5the default points, the gate and the merge; ``only_at`` names the
6points a test runs at and nowhere else; ``also_at`` adds points to
7the default ones. The point comes from the job runner's environment
8(``WORKSHOP_POINT``, set for every entry it spawns) or from
9``--workshop-point`` on the command line, and is the gate when
10neither says. Tests outside the point's selection are deselected,
11never skipped, so they leave no noise in the summary. A point beyond
12the default ones that selects no test at all is green: the nightly
13runs the whole check with its own tests selected in, and a workspace
14that declares none has nothing to run there, which is not a failure.
15"""
27#: The points a test runs at when it names none.
30#: Every point a marker may name.
35 """Add ``--workshop-point``, the point to select for on a local run."""
36 parser.addoption(
37 "--workshop-point",
38 default=None,
39 help="select the tests of this CI point (gate, merge, nightly, release);"
40 f" {POINT_VARIABLE} in the environment says the same",
41 )
45 """Register the two markers, so an unregistered-marker warning never fires."""
46 config.addinivalue_line(
47 "markers", "only_at(*points): the test runs at the named CI points only"
48 )
49 config.addinivalue_line(
50 "markers",
51 "also_at(*points): the test runs at the named CI points as well as the"
52 " default ones (gate, merge)",
53 )
57 """The point this run selects for: the option, else the environment, else gate."""
58 option = config.getoption("--workshop-point", default=None)
59 point = str(option or os.environ.get(POINT_VARIABLE) or "gate")
60 if point not in POINTS:
61 raise pytest.UsageError(
62 f"{point!r} is not a CI point; the points are {', '.join(sorted(POINTS))}"
63 )
64 return point
68 """The points *item* runs at, from its markers.
70 Raises:
71 pytest.UsageError: When a marker names no point or an unknown one.
72 """
73 only = item.get_closest_marker("only_at")
74 also = item.get_closest_marker("also_at")
75 for marker in (only, also):
76 if marker is None:
77 continue
78 named = {str(point) for point in marker.args}
79 if not named:
80 raise pytest.UsageError(f"{item.nodeid}: {marker.name} names no point")
81 unknown = sorted(named - POINTS)
82 if unknown:
83 raise pytest.UsageError(
84 f"{item.nodeid}: {marker.name} names {', '.join(unknown)}, not a"
85 f" point; the points are {', '.join(sorted(POINTS))}"
86 )
87 if only is not None:
88 return frozenset(str(point) for point in only.args)
89 if also is not None:
90 return DEFAULT_POINTS | frozenset(str(point) for point in also.args)
91 return DEFAULT_POINTS
95 config: pytest.Config, items: list[pytest.Item]
96) -> None:
97 """Keep the tests of the current point; deselect the rest."""
98 point = current_point(config)
99 kept: list[pytest.Item] = []
100 dropped: list[pytest.Item] = []
101 for item in items:
102 (kept if point in points_of(item) else dropped).append(item)
103 if dropped:
104 config.hook.pytest_deselected(items=dropped)
105 items[:] = kept
109 session: pytest.Session, exitstatus: int | pytest.ExitCode
110) -> None:
111 """Turn "no tests collected" into green at a point beyond the default ones.
113 pytest exits 5 when nothing ran. At the gate and the merge that
114 stands, since a workspace without tests is not proved by an
115 empty run. At the nightly or the release point an empty selection
116 is the declared state: no test asked for the point, so the point
117 has nothing to run and says so.
118 """
119 if exitstatus != pytest.ExitCode.NO_TESTS_COLLECTED:
120 return
121 point = current_point(session.config)
122 if point in DEFAULT_POINTS:
123 return
124 session.exitstatus = pytest.ExitCode.OK
125 reporter = session.config.pluginmanager.get_plugin("terminalreporter")
126 if reporter is not None:126 ↛ exitline 126 didn't return from function 'pytest_sessionfinish' because the condition on line 126 was always true
127 reporter.write_line(
128 f"no test declares the {point} point: nothing to run there, green"
129 )