pytest-admission 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_admission-0.1.0/.github/workflows/release.yml +49 -0
- pytest_admission-0.1.0/.github/workflows/test.yml +30 -0
- pytest_admission-0.1.0/.gitignore +3 -0
- pytest_admission-0.1.0/LICENSE +21 -0
- pytest_admission-0.1.0/PKG-INFO +238 -0
- pytest_admission-0.1.0/README.md +201 -0
- pytest_admission-0.1.0/pyproject.toml +31 -0
- pytest_admission-0.1.0/src/pytest_admission/__init__.py +3 -0
- pytest_admission-0.1.0/src/pytest_admission/cli.py +72 -0
- pytest_admission-0.1.0/src/pytest_admission/config.py +185 -0
- pytest_admission-0.1.0/src/pytest_admission/contract.py +145 -0
- pytest_admission-0.1.0/src/pytest_admission/hand_kept.py +46 -0
- pytest_admission-0.1.0/src/pytest_admission/plugin.py +93 -0
- pytest_admission-0.1.0/src/pytest_admission/record.py +88 -0
- pytest_admission-0.1.0/src/pytest_admission/refusal.py +52 -0
- pytest_admission-0.1.0/src/pytest_admission/selection.py +80 -0
- pytest_admission-0.1.0/src/pytest_admission/walk.py +164 -0
- pytest_admission-0.1.0/src/pytest_admission/workflow_args.py +76 -0
- pytest_admission-0.1.0/tests/conftest.py +67 -0
- pytest_admission-0.1.0/tests/test_cli.py +151 -0
- pytest_admission-0.1.0/tests/test_config.py +74 -0
- pytest_admission-0.1.0/tests/test_contract.py +168 -0
- pytest_admission-0.1.0/tests/test_hand_kept_lists.py +81 -0
- pytest_admission-0.1.0/tests/test_no_project_identifier.py +12 -0
- pytest_admission-0.1.0/tests/test_record.py +99 -0
- pytest_admission-0.1.0/tests/test_refusal.py +113 -0
- pytest_admission-0.1.0/tests/test_selection.py +197 -0
- pytest_admission-0.1.0/tests/test_walk.py +177 -0
- pytest_admission-0.1.0/tests/test_walk_review.py +41 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ['v*']
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-python@v5
|
|
16
|
+
with:
|
|
17
|
+
python-version: '3.12'
|
|
18
|
+
- name: Check release version
|
|
19
|
+
run: |
|
|
20
|
+
python - <<'PY'
|
|
21
|
+
import os
|
|
22
|
+
import tomllib
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
version = tomllib.loads(Path('pyproject.toml').read_text())['project']['version']
|
|
25
|
+
if os.environ['GITHUB_REF_NAME'] != f'v{version}':
|
|
26
|
+
raise SystemExit('Release tag does not match package version')
|
|
27
|
+
PY
|
|
28
|
+
- run: python -m pip install build
|
|
29
|
+
- run: python -m build
|
|
30
|
+
- uses: actions/upload-artifact@v4
|
|
31
|
+
with:
|
|
32
|
+
name: distributions
|
|
33
|
+
path: dist/*
|
|
34
|
+
if-no-files-found: error
|
|
35
|
+
publish:
|
|
36
|
+
needs: build
|
|
37
|
+
runs-on: ubuntu-latest
|
|
38
|
+
environment:
|
|
39
|
+
name: pypi
|
|
40
|
+
url: https://pypi.org/p/pytest-admission
|
|
41
|
+
permissions:
|
|
42
|
+
id-token: write
|
|
43
|
+
steps:
|
|
44
|
+
- uses: actions/download-artifact@v4
|
|
45
|
+
with:
|
|
46
|
+
name: distributions
|
|
47
|
+
path: dist
|
|
48
|
+
- name: Publish to PyPI
|
|
49
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
strategy:
|
|
11
|
+
fail-fast: false
|
|
12
|
+
matrix:
|
|
13
|
+
python: ['3.11', '3.12']
|
|
14
|
+
pytest: ['lowest', 'highest']
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: ${{ matrix.python }}
|
|
20
|
+
- name: Install package and selected pytest endpoint
|
|
21
|
+
shell: bash
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install -e '.[test]'
|
|
24
|
+
if [[ '${{ matrix.pytest }}' == 'lowest' ]]; then
|
|
25
|
+
python -m pip install 'pytest==8.3.4'
|
|
26
|
+
else
|
|
27
|
+
python -m pip install --upgrade 'pytest>=8.3.4,<10'
|
|
28
|
+
fi
|
|
29
|
+
- name: Run plugin tests (including pytester subprocess sessions)
|
|
30
|
+
run: python -m pytest -q
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Daniel Lassiter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pytest-admission
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Project-configured admission checks for pytest
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 Daniel Lassiter
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Classifier: Framework :: Pytest
|
|
28
|
+
Classifier: Programming Language :: Python :: 3
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
31
|
+
Requires-Python: >=3.11
|
|
32
|
+
Requires-Dist: pytest<10,>=8.3.4
|
|
33
|
+
Requires-Dist: pyyaml>=6
|
|
34
|
+
Provides-Extra: test
|
|
35
|
+
Requires-Dist: pytest<10,>=8.3.4; extra == 'test'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# pytest-admission
|
|
39
|
+
|
|
40
|
+
Project-configured admission for pytest. Requires Python 3.11+ and pytest
|
|
41
|
+
8.3.4 through 9.x. Licensed under the MIT license in `LICENSE`.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
python -m pip install pytest-admission
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The `pytest11` entry point loads the plugin automatically. For a source checkout:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
python -m pip install -e '.[test]'
|
|
53
|
+
python -m pytest -q
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
CI runs Python 3.11 and 3.12 with both pytest 8.3.4 and the newest available
|
|
57
|
+
pytest below 10. Acceptance tests use isolated `pytester` subprocess sessions
|
|
58
|
+
and a file-writing stand-in, not a solver or compiler.
|
|
59
|
+
|
|
60
|
+
## Admission command
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
pytest-admission check --base <review-base>
|
|
64
|
+
# Source-checkout alternative:
|
|
65
|
+
python -m pytest_admission.cli check --base <review-base>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The command collects the full project once, never running test bodies or
|
|
69
|
+
fixtures. Configured workflow sources still have their own collection-only
|
|
70
|
+
subprocesses. It ignores pytest `addopts` and `PYTEST_ADDOPTS` so they cannot
|
|
71
|
+
narrow the population. Each failing check prints one line prefixed with its
|
|
72
|
+
arm (`selection`, `contract`, or `simulation`); collection/configuration errors
|
|
73
|
+
also fail closed. Exit status is 0 on a clean collection and 1 on failure.
|
|
74
|
+
`--base` defaults to `HEAD`; `--root <directory>` selects another project.
|
|
75
|
+
The existing `pytest-admission record` command remains the sole record writer.
|
|
76
|
+
|
|
77
|
+
## Release
|
|
78
|
+
|
|
79
|
+
Version 0.1.0 is released by pushing tag `v0.1.0` after configuring the
|
|
80
|
+
repository's PyPI trusted publisher for `.github/workflows/release.yml` and
|
|
81
|
+
the GitHub `pypi` environment. The workflow builds the wheel and source
|
|
82
|
+
distribution without publishing credentials, checks the tag against package
|
|
83
|
+
metadata, then publishes through PyPI's trusted-publishing action. Tagging
|
|
84
|
+
and configuring/approving publication are maintainer actions.
|
|
85
|
+
|
|
86
|
+
## Declare launch sinks
|
|
87
|
+
|
|
88
|
+
Put configuration in the `pyproject.toml` at pytest's root directory:
|
|
89
|
+
|
|
90
|
+
```toml
|
|
91
|
+
[tool.pytest-admission]
|
|
92
|
+
sinks = ["myproject.launch:compile_model", "myproject.runner:Runner.launch"]
|
|
93
|
+
|
|
94
|
+
[tool.pytest-admission.relax]
|
|
95
|
+
env = "PROJECT_BUILD_VENUE"
|
|
96
|
+
values = ["toolchain"]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Each sink is `importable.module:qualified.callable`; dotted attributes after the
|
|
100
|
+
colon can name methods. Declare the callable at the entry point used by the
|
|
101
|
+
project, not a copied alias. Target modules must be importable at session
|
|
102
|
+
configuration time (use an installed package or pytest's `pythonpath` setting).
|
|
103
|
+
Importing a target module must not itself launch work.
|
|
104
|
+
|
|
105
|
+
Before collection, `pytest_configure` imports and resolves every declared sink,
|
|
106
|
+
then replaces each with a callable that fails with
|
|
107
|
+
`pytest-admission: refused sink <configured name>`. No original call is made.
|
|
108
|
+
A call from a fixture fails during setup. Originals are restored when pytest
|
|
109
|
+
cleans up its configuration, including failed sessions.
|
|
110
|
+
|
|
111
|
+
The refusal is armed by default in every session. Only an exact, case-sensitive
|
|
112
|
+
match between the configured environment variable and one of `values` leaves
|
|
113
|
+
the original callables untouched. Missing, empty, misspelt, and other
|
|
114
|
+
unrecognised tokens do not relax it. Without `relax`, sinks remain armed.
|
|
115
|
+
The plugin is a test guard, not a sandbox: it does not intercept aliases that
|
|
116
|
+
were already copied before configuration or launches outside declared sinks.
|
|
117
|
+
|
|
118
|
+
No table (or no `sinks`) means no declared sinks. Configuration accepts only
|
|
119
|
+
`sinks`, `relax`, `project_roots`, `simulation_list`, `registry`, `record`,
|
|
120
|
+
`jobs`, `launchers`, and `hand_kept_lists`;
|
|
121
|
+
`relax` accepts only `env` and `values`. Unknown keys,
|
|
122
|
+
malformed tables, malformed callable names, non-string or duplicate array
|
|
123
|
+
entries, missing relaxation fields, and unresolvable/non-callable sinks fail
|
|
124
|
+
configuration with an error naming the field or sink. `sinks` may be empty;
|
|
125
|
+
`relax.values` must contain at least one non-empty string. Environment variable
|
|
126
|
+
names use letters, digits and underscores and cannot start with a digit.
|
|
127
|
+
|
|
128
|
+
## Collection reach analysis
|
|
129
|
+
|
|
130
|
+
`project_roots = ["."]` names directories relative to the pytest root (the
|
|
131
|
+
default is the whole project). `simulation_list = ["tests/test_model.py::test_run"]`
|
|
132
|
+
lists admitted simulation-bearing node IDs; shell-style patterns are accepted,
|
|
133
|
+
including for parametrized IDs. The default list is empty. Being listed does
|
|
134
|
+
not relax runtime refusal: only the venue token does that.
|
|
135
|
+
|
|
136
|
+
Collection walks test bodies and every fixture in each item's closure, following
|
|
137
|
+
statically resolved Python calls inside those roots. Reaching a declared sink,
|
|
138
|
+
or an ambiguous call beneath a declared sink module, places a test in the
|
|
139
|
+
`simulation-bearing` tier; otherwise it is `fast`. Unlisted simulation-bearing
|
|
140
|
+
tests fail collection admission. Dynamic string-based `getattr` calls are not
|
|
141
|
+
resolved: they can remain fast and still be refused at runtime.
|
|
142
|
+
|
|
143
|
+
Every session writes `.pytest_cache/admission/<session-id>.json`, keyed by node
|
|
144
|
+
ID, with `tier`, `sinks`, `routes` (call chains), and `unresolved_calls`. The
|
|
145
|
+
artifact is written even when admission fails. Tier is also exposed as the
|
|
146
|
+
`admission_tier` item user property. The walk does not execute user calls and is
|
|
147
|
+
not a proof of absence of simulation; runtime refusal remains independent.
|
|
148
|
+
|
|
149
|
+
## Contract admission
|
|
150
|
+
|
|
151
|
+
Enable the contract check by configuring both project-relative paths:
|
|
152
|
+
|
|
153
|
+
```toml
|
|
154
|
+
[tool.pytest-admission]
|
|
155
|
+
registry = "tests/contracts.toml"
|
|
156
|
+
record = "tests/admission_record.json"
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The registry is a TOML `contracts` table keyed by contract name (its values
|
|
160
|
+
may hold descriptions), or `contracts = ["name", ...]`. An empty file is an
|
|
161
|
+
empty registry. Mark each covered test with `@pytest.mark.contract("name")`.
|
|
162
|
+
Unknown names, multiple markers on one item, malformed markers, and registered
|
|
163
|
+
names with no collected item fail admission.
|
|
164
|
+
|
|
165
|
+
Run the whole collection with `pytest --base <revision>`. Git's committed
|
|
166
|
+
`<revision>...HEAD` diff defines added or edited tests; their function spans
|
|
167
|
+
include decorators. Unmarked changed definitions fail. `--base` defaults to
|
|
168
|
+
`HEAD` for an unchanged-tree run; pass the review base to check a change.
|
|
169
|
+
The pytest project root may be a subdirectory of the Git repository; changed
|
|
170
|
+
paths are resolved from the repository toplevel, not the project root.
|
|
171
|
+
The unmarked count counts collected items, including each parametrized case,
|
|
172
|
+
and must equal the record's `unmarked_tests`. It may not rise above the base
|
|
173
|
+
record, and removing an unmarked item requires lowering the record in the
|
|
174
|
+
same change. A zero record permits no unmarked item.
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
pytest-admission record
|
|
178
|
+
# Source-checkout alternative:
|
|
179
|
+
python -m pytest_admission.record record --root .
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
This command is the only record writer. It collects without running test
|
|
183
|
+
bodies or fixtures, initializes a missing record, or lowers an existing count.
|
|
184
|
+
It refuses an increase and leaves the file untouched on collection or
|
|
185
|
+
admission errors. JSON `gaps` (the selection-coverage record) is preserved.
|
|
186
|
+
Ordinary pytest sessions never rewrite the admission record. Commit the
|
|
187
|
+
record together with the tests that lower it.
|
|
188
|
+
|
|
189
|
+
## Selection coverage
|
|
190
|
+
|
|
191
|
+
Declare up to six logical jobs, each with one or more workflow sources:
|
|
192
|
+
|
|
193
|
+
```toml
|
|
194
|
+
[[tool.pytest-admission.jobs]]
|
|
195
|
+
name = "fast"
|
|
196
|
+
capabilities = ["network"]
|
|
197
|
+
sources = [{ workflow = ".github/workflows/tests.yml", job = "unit" }]
|
|
198
|
+
|
|
199
|
+
[[tool.pytest-admission.hand_kept_lists]]
|
|
200
|
+
path = "tests/allowlist.toml"
|
|
201
|
+
format = "toml"
|
|
202
|
+
field = "tests.nodes"
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Each source reads its pytest arguments from that workflow job's `run` steps.
|
|
206
|
+
Literal `pytest`, `python -m pytest`, and prefixed commands such as `uv run
|
|
207
|
+
pytest` are supported. Workflow/job/step working directories and simple `cd`
|
|
208
|
+
commands are respected. Shell/workflow expressions in pytest arguments fail
|
|
209
|
+
closed; resolve those selections into literal commands. Workflow shell, test
|
|
210
|
+
bodies and launcher scripts are never executed by the check; collection does
|
|
211
|
+
import tests and conftests. Every source is collected in a fresh subprocess
|
|
212
|
+
with its paths, markers, deselections and plugin options intact.
|
|
213
|
+
File-descriptor redirects such as `2>&1`, including before `| tee log`, do not
|
|
214
|
+
become positional pytest arguments.
|
|
215
|
+
Only an unquoted number directly adjacent to `<` or `>` is an fd prefix.
|
|
216
|
+
Numeric pytest arguments separated from the redirect, such as `-n 4 > log`
|
|
217
|
+
or `--maxfail 2 > log`, are preserved.
|
|
218
|
+
|
|
219
|
+
Set `launchers = ["scripts/collect_launcher.py"]` in the main admission table
|
|
220
|
+
for a command whose pytest arguments follow `--`. Sources within one logical
|
|
221
|
+
job are unioned, then all logical jobs are unioned. Fewer than six jobs permits
|
|
222
|
+
only gaps already in the **HEAD-committed** admission record's JSON `gaps`;
|
|
223
|
+
six jobs permits no gaps. A second source is not another job. The check does
|
|
224
|
+
not rewrite the record, and jobs require the configured registry/record pair.
|
|
225
|
+
|
|
226
|
+
An unconditional `skip` or literal-true `skipif` fails collection.
|
|
227
|
+
Conditions may be positional or supplied as `condition=`; false boolean and
|
|
228
|
+
string-expression conditions are not classified as unconditional skips.
|
|
229
|
+
A capability skip uses `@pytest.mark.skipif(condition, capability="network", reason="...")`;
|
|
230
|
+
at least one configured logical job must declare that capability.
|
|
231
|
+
|
|
232
|
+
List formats are `lines` (one ID per nonblank line, `#` comments permitted),
|
|
233
|
+
`toml` (a dotted `field` selecting a string array), or `python` (`field` naming
|
|
234
|
+
one module-level literal tuple/list, read by AST without importing it).
|
|
235
|
+
Entries must resolve to collected items; pytest parent selectors, including
|
|
236
|
+
unparametrized function IDs, are accepted. Missing positional workflow node
|
|
237
|
+
selectors also fail source collection. All checks use the full population
|
|
238
|
+
passed to admission, so invoke admission without `-k`, `-m`, or path narrowing.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# pytest-admission
|
|
2
|
+
|
|
3
|
+
Project-configured admission for pytest. Requires Python 3.11+ and pytest
|
|
4
|
+
8.3.4 through 9.x. Licensed under the MIT license in `LICENSE`.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
python -m pip install pytest-admission
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The `pytest11` entry point loads the plugin automatically. For a source checkout:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
python -m pip install -e '.[test]'
|
|
16
|
+
python -m pytest -q
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
CI runs Python 3.11 and 3.12 with both pytest 8.3.4 and the newest available
|
|
20
|
+
pytest below 10. Acceptance tests use isolated `pytester` subprocess sessions
|
|
21
|
+
and a file-writing stand-in, not a solver or compiler.
|
|
22
|
+
|
|
23
|
+
## Admission command
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
pytest-admission check --base <review-base>
|
|
27
|
+
# Source-checkout alternative:
|
|
28
|
+
python -m pytest_admission.cli check --base <review-base>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The command collects the full project once, never running test bodies or
|
|
32
|
+
fixtures. Configured workflow sources still have their own collection-only
|
|
33
|
+
subprocesses. It ignores pytest `addopts` and `PYTEST_ADDOPTS` so they cannot
|
|
34
|
+
narrow the population. Each failing check prints one line prefixed with its
|
|
35
|
+
arm (`selection`, `contract`, or `simulation`); collection/configuration errors
|
|
36
|
+
also fail closed. Exit status is 0 on a clean collection and 1 on failure.
|
|
37
|
+
`--base` defaults to `HEAD`; `--root <directory>` selects another project.
|
|
38
|
+
The existing `pytest-admission record` command remains the sole record writer.
|
|
39
|
+
|
|
40
|
+
## Release
|
|
41
|
+
|
|
42
|
+
Version 0.1.0 is released by pushing tag `v0.1.0` after configuring the
|
|
43
|
+
repository's PyPI trusted publisher for `.github/workflows/release.yml` and
|
|
44
|
+
the GitHub `pypi` environment. The workflow builds the wheel and source
|
|
45
|
+
distribution without publishing credentials, checks the tag against package
|
|
46
|
+
metadata, then publishes through PyPI's trusted-publishing action. Tagging
|
|
47
|
+
and configuring/approving publication are maintainer actions.
|
|
48
|
+
|
|
49
|
+
## Declare launch sinks
|
|
50
|
+
|
|
51
|
+
Put configuration in the `pyproject.toml` at pytest's root directory:
|
|
52
|
+
|
|
53
|
+
```toml
|
|
54
|
+
[tool.pytest-admission]
|
|
55
|
+
sinks = ["myproject.launch:compile_model", "myproject.runner:Runner.launch"]
|
|
56
|
+
|
|
57
|
+
[tool.pytest-admission.relax]
|
|
58
|
+
env = "PROJECT_BUILD_VENUE"
|
|
59
|
+
values = ["toolchain"]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Each sink is `importable.module:qualified.callable`; dotted attributes after the
|
|
63
|
+
colon can name methods. Declare the callable at the entry point used by the
|
|
64
|
+
project, not a copied alias. Target modules must be importable at session
|
|
65
|
+
configuration time (use an installed package or pytest's `pythonpath` setting).
|
|
66
|
+
Importing a target module must not itself launch work.
|
|
67
|
+
|
|
68
|
+
Before collection, `pytest_configure` imports and resolves every declared sink,
|
|
69
|
+
then replaces each with a callable that fails with
|
|
70
|
+
`pytest-admission: refused sink <configured name>`. No original call is made.
|
|
71
|
+
A call from a fixture fails during setup. Originals are restored when pytest
|
|
72
|
+
cleans up its configuration, including failed sessions.
|
|
73
|
+
|
|
74
|
+
The refusal is armed by default in every session. Only an exact, case-sensitive
|
|
75
|
+
match between the configured environment variable and one of `values` leaves
|
|
76
|
+
the original callables untouched. Missing, empty, misspelt, and other
|
|
77
|
+
unrecognised tokens do not relax it. Without `relax`, sinks remain armed.
|
|
78
|
+
The plugin is a test guard, not a sandbox: it does not intercept aliases that
|
|
79
|
+
were already copied before configuration or launches outside declared sinks.
|
|
80
|
+
|
|
81
|
+
No table (or no `sinks`) means no declared sinks. Configuration accepts only
|
|
82
|
+
`sinks`, `relax`, `project_roots`, `simulation_list`, `registry`, `record`,
|
|
83
|
+
`jobs`, `launchers`, and `hand_kept_lists`;
|
|
84
|
+
`relax` accepts only `env` and `values`. Unknown keys,
|
|
85
|
+
malformed tables, malformed callable names, non-string or duplicate array
|
|
86
|
+
entries, missing relaxation fields, and unresolvable/non-callable sinks fail
|
|
87
|
+
configuration with an error naming the field or sink. `sinks` may be empty;
|
|
88
|
+
`relax.values` must contain at least one non-empty string. Environment variable
|
|
89
|
+
names use letters, digits and underscores and cannot start with a digit.
|
|
90
|
+
|
|
91
|
+
## Collection reach analysis
|
|
92
|
+
|
|
93
|
+
`project_roots = ["."]` names directories relative to the pytest root (the
|
|
94
|
+
default is the whole project). `simulation_list = ["tests/test_model.py::test_run"]`
|
|
95
|
+
lists admitted simulation-bearing node IDs; shell-style patterns are accepted,
|
|
96
|
+
including for parametrized IDs. The default list is empty. Being listed does
|
|
97
|
+
not relax runtime refusal: only the venue token does that.
|
|
98
|
+
|
|
99
|
+
Collection walks test bodies and every fixture in each item's closure, following
|
|
100
|
+
statically resolved Python calls inside those roots. Reaching a declared sink,
|
|
101
|
+
or an ambiguous call beneath a declared sink module, places a test in the
|
|
102
|
+
`simulation-bearing` tier; otherwise it is `fast`. Unlisted simulation-bearing
|
|
103
|
+
tests fail collection admission. Dynamic string-based `getattr` calls are not
|
|
104
|
+
resolved: they can remain fast and still be refused at runtime.
|
|
105
|
+
|
|
106
|
+
Every session writes `.pytest_cache/admission/<session-id>.json`, keyed by node
|
|
107
|
+
ID, with `tier`, `sinks`, `routes` (call chains), and `unresolved_calls`. The
|
|
108
|
+
artifact is written even when admission fails. Tier is also exposed as the
|
|
109
|
+
`admission_tier` item user property. The walk does not execute user calls and is
|
|
110
|
+
not a proof of absence of simulation; runtime refusal remains independent.
|
|
111
|
+
|
|
112
|
+
## Contract admission
|
|
113
|
+
|
|
114
|
+
Enable the contract check by configuring both project-relative paths:
|
|
115
|
+
|
|
116
|
+
```toml
|
|
117
|
+
[tool.pytest-admission]
|
|
118
|
+
registry = "tests/contracts.toml"
|
|
119
|
+
record = "tests/admission_record.json"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The registry is a TOML `contracts` table keyed by contract name (its values
|
|
123
|
+
may hold descriptions), or `contracts = ["name", ...]`. An empty file is an
|
|
124
|
+
empty registry. Mark each covered test with `@pytest.mark.contract("name")`.
|
|
125
|
+
Unknown names, multiple markers on one item, malformed markers, and registered
|
|
126
|
+
names with no collected item fail admission.
|
|
127
|
+
|
|
128
|
+
Run the whole collection with `pytest --base <revision>`. Git's committed
|
|
129
|
+
`<revision>...HEAD` diff defines added or edited tests; their function spans
|
|
130
|
+
include decorators. Unmarked changed definitions fail. `--base` defaults to
|
|
131
|
+
`HEAD` for an unchanged-tree run; pass the review base to check a change.
|
|
132
|
+
The pytest project root may be a subdirectory of the Git repository; changed
|
|
133
|
+
paths are resolved from the repository toplevel, not the project root.
|
|
134
|
+
The unmarked count counts collected items, including each parametrized case,
|
|
135
|
+
and must equal the record's `unmarked_tests`. It may not rise above the base
|
|
136
|
+
record, and removing an unmarked item requires lowering the record in the
|
|
137
|
+
same change. A zero record permits no unmarked item.
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
pytest-admission record
|
|
141
|
+
# Source-checkout alternative:
|
|
142
|
+
python -m pytest_admission.record record --root .
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
This command is the only record writer. It collects without running test
|
|
146
|
+
bodies or fixtures, initializes a missing record, or lowers an existing count.
|
|
147
|
+
It refuses an increase and leaves the file untouched on collection or
|
|
148
|
+
admission errors. JSON `gaps` (the selection-coverage record) is preserved.
|
|
149
|
+
Ordinary pytest sessions never rewrite the admission record. Commit the
|
|
150
|
+
record together with the tests that lower it.
|
|
151
|
+
|
|
152
|
+
## Selection coverage
|
|
153
|
+
|
|
154
|
+
Declare up to six logical jobs, each with one or more workflow sources:
|
|
155
|
+
|
|
156
|
+
```toml
|
|
157
|
+
[[tool.pytest-admission.jobs]]
|
|
158
|
+
name = "fast"
|
|
159
|
+
capabilities = ["network"]
|
|
160
|
+
sources = [{ workflow = ".github/workflows/tests.yml", job = "unit" }]
|
|
161
|
+
|
|
162
|
+
[[tool.pytest-admission.hand_kept_lists]]
|
|
163
|
+
path = "tests/allowlist.toml"
|
|
164
|
+
format = "toml"
|
|
165
|
+
field = "tests.nodes"
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Each source reads its pytest arguments from that workflow job's `run` steps.
|
|
169
|
+
Literal `pytest`, `python -m pytest`, and prefixed commands such as `uv run
|
|
170
|
+
pytest` are supported. Workflow/job/step working directories and simple `cd`
|
|
171
|
+
commands are respected. Shell/workflow expressions in pytest arguments fail
|
|
172
|
+
closed; resolve those selections into literal commands. Workflow shell, test
|
|
173
|
+
bodies and launcher scripts are never executed by the check; collection does
|
|
174
|
+
import tests and conftests. Every source is collected in a fresh subprocess
|
|
175
|
+
with its paths, markers, deselections and plugin options intact.
|
|
176
|
+
File-descriptor redirects such as `2>&1`, including before `| tee log`, do not
|
|
177
|
+
become positional pytest arguments.
|
|
178
|
+
Only an unquoted number directly adjacent to `<` or `>` is an fd prefix.
|
|
179
|
+
Numeric pytest arguments separated from the redirect, such as `-n 4 > log`
|
|
180
|
+
or `--maxfail 2 > log`, are preserved.
|
|
181
|
+
|
|
182
|
+
Set `launchers = ["scripts/collect_launcher.py"]` in the main admission table
|
|
183
|
+
for a command whose pytest arguments follow `--`. Sources within one logical
|
|
184
|
+
job are unioned, then all logical jobs are unioned. Fewer than six jobs permits
|
|
185
|
+
only gaps already in the **HEAD-committed** admission record's JSON `gaps`;
|
|
186
|
+
six jobs permits no gaps. A second source is not another job. The check does
|
|
187
|
+
not rewrite the record, and jobs require the configured registry/record pair.
|
|
188
|
+
|
|
189
|
+
An unconditional `skip` or literal-true `skipif` fails collection.
|
|
190
|
+
Conditions may be positional or supplied as `condition=`; false boolean and
|
|
191
|
+
string-expression conditions are not classified as unconditional skips.
|
|
192
|
+
A capability skip uses `@pytest.mark.skipif(condition, capability="network", reason="...")`;
|
|
193
|
+
at least one configured logical job must declare that capability.
|
|
194
|
+
|
|
195
|
+
List formats are `lines` (one ID per nonblank line, `#` comments permitted),
|
|
196
|
+
`toml` (a dotted `field` selecting a string array), or `python` (`field` naming
|
|
197
|
+
one module-level literal tuple/list, read by AST without importing it).
|
|
198
|
+
Entries must resolve to collected items; pytest parent selectors, including
|
|
199
|
+
unparametrized function IDs, are accepted. Missing positional workflow node
|
|
200
|
+
selectors also fail source collection. All checks use the full population
|
|
201
|
+
passed to admission, so invoke admission without `-k`, `-m`, or path narrowing.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pytest-admission"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Project-configured admission checks for pytest"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
dependencies = ["pytest>=8.3.4,<10", "PyYAML>=6"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Framework :: Pytest",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
test = ["pytest>=8.3.4,<10"]
|
|
22
|
+
|
|
23
|
+
[project.entry-points.pytest11]
|
|
24
|
+
admission = "pytest_admission.plugin"
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
pytest-admission = "pytest_admission.cli:main"
|
|
28
|
+
|
|
29
|
+
[tool.pytest.ini_options]
|
|
30
|
+
testpaths = ["tests"]
|
|
31
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""Collection-only admission command and the existing record writer."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
from contextlib import redirect_stderr, redirect_stdout
|
|
5
|
+
from io import StringIO
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from pytest_admission import plugin, record
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CheckCollection:
|
|
14
|
+
def __init__(self):
|
|
15
|
+
self.failures = []
|
|
16
|
+
|
|
17
|
+
@pytest.hookimpl(tryfirst=True)
|
|
18
|
+
def pytest_configure(self, config):
|
|
19
|
+
config.stash[plugin.CHECK_FAILURES] = self.failures
|
|
20
|
+
|
|
21
|
+
def pytest_collectreport(self, report):
|
|
22
|
+
if report.failed:
|
|
23
|
+
self.failures.append(("collection", " ".join(str(report.longrepr).split())))
|
|
24
|
+
|
|
25
|
+
def pytest_internalerror(self, excrepr):
|
|
26
|
+
self.failures.append(("internal", " ".join(str(excrepr).split())))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def check(root: Path, base: str) -> int:
|
|
30
|
+
collector = CheckCollection()
|
|
31
|
+
captured = StringIO()
|
|
32
|
+
# Ignore project addopts and PYTEST_ADDOPTS: neither may narrow the census.
|
|
33
|
+
from unittest.mock import patch
|
|
34
|
+
import os
|
|
35
|
+
|
|
36
|
+
with patch.dict(os.environ, {"PYTEST_ADDOPTS": ""}), redirect_stdout(captured), redirect_stderr(captured):
|
|
37
|
+
status = pytest.main(
|
|
38
|
+
["--collect-only", "-p", "no:terminal", "-o", "addopts=",
|
|
39
|
+
"--rootdir", str(root), "--base", base, str(root)],
|
|
40
|
+
plugins=[collector] if record._autoloads_plugin() else [collector, plugin],
|
|
41
|
+
)
|
|
42
|
+
for arm, message in collector.failures:
|
|
43
|
+
lines = message.splitlines()
|
|
44
|
+
if lines and lines[0].startswith("pytest-admission:") and len(lines) > 1:
|
|
45
|
+
lines = lines[1:]
|
|
46
|
+
for line in lines:
|
|
47
|
+
if line.strip() and not line.startswith("reach artifact:"):
|
|
48
|
+
print(f"[{arm}] {line.strip()}")
|
|
49
|
+
if status not in (pytest.ExitCode.OK, pytest.ExitCode.NO_TESTS_COLLECTED):
|
|
50
|
+
if not collector.failures:
|
|
51
|
+
detail = " ".join(captured.getvalue().split()) or f"pytest exited {int(status)}"
|
|
52
|
+
print(f"[collection] {detail}")
|
|
53
|
+
return 1
|
|
54
|
+
return int(bool(collector.failures))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def main(argv: list[str] | None = None) -> int:
|
|
58
|
+
parser = argparse.ArgumentParser(prog="pytest-admission")
|
|
59
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
60
|
+
check_parser = commands.add_parser("check", help="Check the whole collection without running tests")
|
|
61
|
+
check_parser.add_argument("--base", default="HEAD", help="Review base revision (default: HEAD)")
|
|
62
|
+
check_parser.add_argument("--root", type=Path, default=Path.cwd())
|
|
63
|
+
record_parser = commands.add_parser("record", help="Initialize or lower the admission record")
|
|
64
|
+
record_parser.add_argument("--root", type=Path, default=Path.cwd())
|
|
65
|
+
args = parser.parse_args(argv)
|
|
66
|
+
if args.command == "record":
|
|
67
|
+
return record.main(["record", "--root", str(args.root)])
|
|
68
|
+
return check(args.root.resolve(), args.base)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == "__main__":
|
|
72
|
+
raise SystemExit(main())
|