xpt 0.1.1__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.
- xpt-0.1.1/LICENSE +21 -0
- xpt-0.1.1/PKG-INFO +69 -0
- xpt-0.1.1/README.md +53 -0
- xpt-0.1.1/pyproject.toml +78 -0
- xpt-0.1.1/pyproject.toml.orig +57 -0
- xpt-0.1.1/src/xpt/__init__.py +3 -0
- xpt-0.1.1/src/xpt/__main__.py +3 -0
- xpt-0.1.1/src/xpt/accept.py +129 -0
- xpt-0.1.1/src/xpt/api.py +43 -0
- xpt-0.1.1/src/xpt/cli.py +198 -0
- xpt-0.1.1/src/xpt/decisions.py +128 -0
- xpt-0.1.1/src/xpt/diff.py +101 -0
- xpt-0.1.1/src/xpt/expect.py +62 -0
- xpt-0.1.1/src/xpt/failure.py +167 -0
- xpt-0.1.1/src/xpt/group.py +157 -0
- xpt-0.1.1/src/xpt/layout.py +70 -0
- xpt-0.1.1/src/xpt/literal.py +22 -0
- xpt-0.1.1/src/xpt/path.py +47 -0
- xpt-0.1.1/src/xpt/py.typed +0 -0
- xpt-0.1.1/src/xpt/pytest_plugin.py +231 -0
- xpt-0.1.1/src/xpt/record.py +307 -0
- xpt-0.1.1/src/xpt/render.py +175 -0
- xpt-0.1.1/src/xpt/report.py +67 -0
- xpt-0.1.1/src/xpt/rewrite.py +280 -0
- xpt-0.1.1/src/xpt/serialize.py +90 -0
- xpt-0.1.1/src/xpt/site.py +215 -0
- xpt-0.1.1/src/xpt/source.py +166 -0
- xpt-0.1.1/src/xpt/summary.py +276 -0
- xpt-0.1.1/src/xpt/terminal.py +247 -0
- xpt-0.1.1/src/xpt/value.py +161 -0
xpt-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Isaiah Harville
|
|
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.
|
xpt-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xpt
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Inline expect tests for Python, built for coding agents and the people reviewing their work.
|
|
5
|
+
Author: Isaiah Harville
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 1 - Planning
|
|
9
|
+
Classifier: Framework :: Pytest
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Software Development :: Testing
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Project-URL: Documentation, https://harville-labs.github.io/xpt/
|
|
14
|
+
Project-URL: Repository, https://github.com/Harville-Labs/xpt
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# xpt
|
|
18
|
+
|
|
19
|
+
Inline expect tests for Python, built so coding agents can work with them cheaply and safely.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from xpt import expect
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_parser():
|
|
26
|
+
expect(parse("x + y"))
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
With xpt installed, `pytest` prints one short block per failure and a footer with the next command, in place of pytest's header, dots, and tracebacks:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
FAILED tests/test_parser.py::test_parser tests/test_parser.py:14
|
|
33
|
+
> expect(parse(source), tree)
|
|
34
|
+
a81f mismatch
|
|
35
|
+
value.operator: "addition" -> "subtraction"
|
|
36
|
+
1 changed, 42 unchanged
|
|
37
|
+
1 failed, 12 passed in 0.31s. Next: xpt accept a81f
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`-v` raises the limits, `-vv` adds the traceback, and `--xpt-json PATH` writes the whole report as JSON. `--xpt-report=pytest`, or `XPT_REPORT=pytest`, keeps pytest's own output.
|
|
41
|
+
|
|
42
|
+
The report needs pytest 8.0 or later and is tested on every minor release from 8.0 to 9.2, with pytest-xdist 3.5 to 3.8. It replaces only pytest's header, progress, failure sections, default short summary, and final line. Everything else pytest and other plugins print still prints, and the exit code is always pytest's. Options that ask for pytest's own failure output (`--tb` other than `auto`, `-l`, `--full-trace`) and runs that don't run tests (`--co`, `--fixtures`, `--markers`, `--setup-plan`) keep pytest's report. `-r` with anything but the default keeps pytest's short summary beside the xpt report. pytest-sugar, when it is on, keeps its own report. If the xpt report can't install or render, or pytest is older than 8.0, the run prints pytest's report and one line starting with `xpt:` that says why.
|
|
43
|
+
|
|
44
|
+
A run records what `expect` saw under `.xpt/`. Then:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
xpt failures # each failed call site with its diff, and the next command
|
|
48
|
+
xpt show a81f # one call site, with its function and tests
|
|
49
|
+
xpt accept a81f # write the actual value into the test source
|
|
50
|
+
xpt accept --all # every acceptable call site that was not rejected
|
|
51
|
+
xpt reject a81f # the actual value is a bug; stop offering it for acceptance
|
|
52
|
+
xpt runs # recorded runs, newest first
|
|
53
|
+
xpt clean # delete .xpt/
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`pytest --expect-create` runs the tests, then fills every missing expectation the same way `xpt accept --missing` does. It writes after the session ends, never during it, and never replaces an existing expected value. The footer names what it wrote and suggests `xpt accept` only for what remains:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
2 failed in 0.25s. Filled 1 missing expectation: test_demo.py:4. Next: xpt accept 3526
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The run still fails, because the filled expectations were empty when their tests ran. Run the tests again to see them pass.
|
|
63
|
+
|
|
64
|
+
`xpt accept` writes without running the tests again, and refuses if that source changed since the run. `xpt reject` records in `.xpt/decisions.jsonl` that a call site's actual value is a real bug. `xpt failures` and the pytest footer then mark that call site `rejected` and stop suggesting it, until a run records a different value there. Every command takes `--json`. Exit codes are 0 for done or nothing to do, 1 when failures remain or an accept or reject was refused, and 2 for a usage error. `python -m xpt` works too, and `xpt.api` is the same surface for tools.
|
|
65
|
+
|
|
66
|
+
Status: alpha. The documentation is at https://harville-labs.github.io/xpt/, and [llms.txt](https://harville-labs.github.io/xpt/llms.txt) indexes it as Markdown for language models. Read [docs/design.md](docs/design.md) for the design and roadmap, and [docs/research.md](docs/research.md) for how expecttest, inline-snapshot, syrupy, and ApprovalTests compare. Plans are tracked as GitHub issues.
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
[bench/](bench/README.md) measures whether agents fix failing suites with fewer tokens and no more wrong accepts using xpt's report than with pytest's own output.
|
xpt-0.1.1/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# xpt
|
|
2
|
+
|
|
3
|
+
Inline expect tests for Python, built so coding agents can work with them cheaply and safely.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from xpt import expect
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def test_parser():
|
|
10
|
+
expect(parse("x + y"))
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
With xpt installed, `pytest` prints one short block per failure and a footer with the next command, in place of pytest's header, dots, and tracebacks:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
FAILED tests/test_parser.py::test_parser tests/test_parser.py:14
|
|
17
|
+
> expect(parse(source), tree)
|
|
18
|
+
a81f mismatch
|
|
19
|
+
value.operator: "addition" -> "subtraction"
|
|
20
|
+
1 changed, 42 unchanged
|
|
21
|
+
1 failed, 12 passed in 0.31s. Next: xpt accept a81f
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`-v` raises the limits, `-vv` adds the traceback, and `--xpt-json PATH` writes the whole report as JSON. `--xpt-report=pytest`, or `XPT_REPORT=pytest`, keeps pytest's own output.
|
|
25
|
+
|
|
26
|
+
The report needs pytest 8.0 or later and is tested on every minor release from 8.0 to 9.2, with pytest-xdist 3.5 to 3.8. It replaces only pytest's header, progress, failure sections, default short summary, and final line. Everything else pytest and other plugins print still prints, and the exit code is always pytest's. Options that ask for pytest's own failure output (`--tb` other than `auto`, `-l`, `--full-trace`) and runs that don't run tests (`--co`, `--fixtures`, `--markers`, `--setup-plan`) keep pytest's report. `-r` with anything but the default keeps pytest's short summary beside the xpt report. pytest-sugar, when it is on, keeps its own report. If the xpt report can't install or render, or pytest is older than 8.0, the run prints pytest's report and one line starting with `xpt:` that says why.
|
|
27
|
+
|
|
28
|
+
A run records what `expect` saw under `.xpt/`. Then:
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
xpt failures # each failed call site with its diff, and the next command
|
|
32
|
+
xpt show a81f # one call site, with its function and tests
|
|
33
|
+
xpt accept a81f # write the actual value into the test source
|
|
34
|
+
xpt accept --all # every acceptable call site that was not rejected
|
|
35
|
+
xpt reject a81f # the actual value is a bug; stop offering it for acceptance
|
|
36
|
+
xpt runs # recorded runs, newest first
|
|
37
|
+
xpt clean # delete .xpt/
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`pytest --expect-create` runs the tests, then fills every missing expectation the same way `xpt accept --missing` does. It writes after the session ends, never during it, and never replaces an existing expected value. The footer names what it wrote and suggests `xpt accept` only for what remains:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
2 failed in 0.25s. Filled 1 missing expectation: test_demo.py:4. Next: xpt accept 3526
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The run still fails, because the filled expectations were empty when their tests ran. Run the tests again to see them pass.
|
|
47
|
+
|
|
48
|
+
`xpt accept` writes without running the tests again, and refuses if that source changed since the run. `xpt reject` records in `.xpt/decisions.jsonl` that a call site's actual value is a real bug. `xpt failures` and the pytest footer then mark that call site `rejected` and stop suggesting it, until a run records a different value there. Every command takes `--json`. Exit codes are 0 for done or nothing to do, 1 when failures remain or an accept or reject was refused, and 2 for a usage error. `python -m xpt` works too, and `xpt.api` is the same surface for tools.
|
|
49
|
+
|
|
50
|
+
Status: alpha. The documentation is at https://harville-labs.github.io/xpt/, and [llms.txt](https://harville-labs.github.io/xpt/llms.txt) indexes it as Markdown for language models. Read [docs/design.md](docs/design.md) for the design and roadmap, and [docs/research.md](docs/research.md) for how expecttest, inline-snapshot, syrupy, and ApprovalTests compare. Plans are tracked as GitHub issues.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
[bench/](bench/README.md) measures whether agents fix failing suites with fewer tokens and no more wrong accepts using xpt's report than with pytest's own output.
|
xpt-0.1.1/pyproject.toml
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "xpt"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "Inline expect tests for Python, built for coding agents and the people reviewing their work."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = []
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 1 - Planning",
|
|
12
|
+
"Framework :: Pytest",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Topic :: Software Development :: Testing",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[[project.authors]]
|
|
18
|
+
name = "Isaiah Harville"
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Documentation = "https://harville-labs.github.io/xpt/"
|
|
22
|
+
Repository = "https://github.com/Harville-Labs/xpt"
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
xpt = "xpt.cli:main"
|
|
26
|
+
|
|
27
|
+
[project.entry-points.pytest11]
|
|
28
|
+
xpt = "xpt.pytest_plugin"
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
32
|
+
build-backend = "uv_build"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=9",
|
|
37
|
+
"ruff>=0.14",
|
|
38
|
+
"ty>=0.0.1a0",
|
|
39
|
+
"hypothesis>=6",
|
|
40
|
+
"pytest-xdist>=3.8.0",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[tool.ruff]
|
|
44
|
+
line-length = 100
|
|
45
|
+
extend-exclude = [
|
|
46
|
+
"docs/prototypes",
|
|
47
|
+
"bench",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[tool.ruff.lint]
|
|
51
|
+
select = [
|
|
52
|
+
"E",
|
|
53
|
+
"F",
|
|
54
|
+
"W",
|
|
55
|
+
"I",
|
|
56
|
+
"UP",
|
|
57
|
+
"B",
|
|
58
|
+
"SIM",
|
|
59
|
+
"RUF",
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
[tool.ty.src]
|
|
63
|
+
exclude = [
|
|
64
|
+
"docs/prototypes",
|
|
65
|
+
"bench",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ty.environment]
|
|
69
|
+
extra-paths = ["tests"]
|
|
70
|
+
|
|
71
|
+
[tool.pytest.ini_options]
|
|
72
|
+
testpaths = ["tests"]
|
|
73
|
+
pythonpath = ["tests"]
|
|
74
|
+
addopts = [
|
|
75
|
+
"--strict-markers",
|
|
76
|
+
"-p",
|
|
77
|
+
"no:cacheprovider",
|
|
78
|
+
]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "xpt"
|
|
3
|
+
version = "0.1.1"
|
|
4
|
+
description = "Inline expect tests for Python, built for coding agents and the people reviewing their work."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [{ name = "Isaiah Harville" }]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 1 - Planning",
|
|
13
|
+
"Framework :: Pytest",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Topic :: Software Development :: Testing",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Documentation = "https://harville-labs.github.io/xpt/"
|
|
20
|
+
Repository = "https://github.com/Harville-Labs/xpt"
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
xpt = "xpt.cli:main"
|
|
24
|
+
|
|
25
|
+
[project.entry-points.pytest11]
|
|
26
|
+
xpt = "xpt.pytest_plugin"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["uv_build>=0.13.6,<0.14.0"]
|
|
30
|
+
build-backend = "uv_build"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=9",
|
|
35
|
+
"ruff>=0.14",
|
|
36
|
+
"ty>=0.0.1a0",
|
|
37
|
+
"hypothesis>=6",
|
|
38
|
+
"pytest-xdist>=3.8.0",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 100
|
|
43
|
+
extend-exclude = ["docs/prototypes", "bench"]
|
|
44
|
+
|
|
45
|
+
[tool.ruff.lint]
|
|
46
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF"]
|
|
47
|
+
|
|
48
|
+
[tool.ty.src]
|
|
49
|
+
exclude = ["docs/prototypes", "bench"]
|
|
50
|
+
|
|
51
|
+
[tool.ty.environment]
|
|
52
|
+
extra-paths = ["tests"]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|
|
56
|
+
pythonpath = ["tests"]
|
|
57
|
+
addopts = ["--strict-markers", "-p", "no:cacheprovider"]
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import secrets
|
|
5
|
+
import shutil
|
|
6
|
+
from collections import defaultdict
|
|
7
|
+
from collections.abc import Generator, Iterable, Sequence
|
|
8
|
+
from contextlib import contextmanager
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from xpt.group import Group
|
|
13
|
+
from xpt.record import state_directory
|
|
14
|
+
from xpt.rewrite import Outcome, rewrite
|
|
15
|
+
from xpt.site import Unlocatable
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class AcceptResult:
|
|
20
|
+
"""One outcome per group, in the order given, and the project paths that were rewritten."""
|
|
21
|
+
|
|
22
|
+
outcomes: tuple[Outcome, ...]
|
|
23
|
+
changed: tuple[str, ...]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AcceptError(Exception):
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def accept(root: Path, groups: Iterable[Group], *, dry_run: bool = False) -> AcceptResult:
|
|
31
|
+
"""Write each acceptable group's actual value into its source file as the expected value.
|
|
32
|
+
|
|
33
|
+
A group is written only when its expected argument's text still matches the run. Each
|
|
34
|
+
file is rewritten in one pass to a temporary file and moved into place, under an
|
|
35
|
+
exclusive `.xpt/accept.lock`. A dry run reports the same result and writes nothing.
|
|
36
|
+
"""
|
|
37
|
+
given = tuple(groups)
|
|
38
|
+
outcomes: dict[int, Outcome] = {}
|
|
39
|
+
by_path: dict[str, list[int]] = defaultdict(list)
|
|
40
|
+
for i, g in enumerate(given):
|
|
41
|
+
site = g.site
|
|
42
|
+
if not g.acceptable or isinstance(site, Unlocatable):
|
|
43
|
+
outcomes[i] = Outcome(g, "skipped", g.detail)
|
|
44
|
+
else:
|
|
45
|
+
by_path[site.path].append(i)
|
|
46
|
+
changed = []
|
|
47
|
+
with lock(state_directory(root) / "accept.lock"):
|
|
48
|
+
for path, indexes in sorted(by_path.items()):
|
|
49
|
+
done = _accept_file(root, path, [given[i] for i in indexes], dry_run)
|
|
50
|
+
outcomes.update(zip(indexes, done.outcomes, strict=True))
|
|
51
|
+
if done.changed:
|
|
52
|
+
changed.append(path)
|
|
53
|
+
return AcceptResult(tuple(outcomes[i] for i in range(len(given))), tuple(changed))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def create(root: Path, groups: Sequence[Group]) -> AcceptResult:
|
|
57
|
+
"""Accept missing groups the way `pytest --expect-create` does after the session.
|
|
58
|
+
|
|
59
|
+
A held lock skips every group with the lock's message instead of raising, so the session
|
|
60
|
+
still ends with its report.
|
|
61
|
+
"""
|
|
62
|
+
if not groups:
|
|
63
|
+
return AcceptResult((), ())
|
|
64
|
+
try:
|
|
65
|
+
return accept(root, groups)
|
|
66
|
+
except AcceptError as error:
|
|
67
|
+
return AcceptResult(tuple(Outcome(g, "skipped", str(error)) for g in groups), ())
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class _FileResult:
|
|
72
|
+
outcomes: tuple[Outcome, ...]
|
|
73
|
+
changed: bool
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _accept_file(root: Path, path: str, groups: list[Group], dry_run: bool) -> _FileResult:
|
|
77
|
+
def skipped(reason: str) -> _FileResult:
|
|
78
|
+
return _FileResult(tuple(Outcome(g, "skipped", reason) for g in groups), False)
|
|
79
|
+
|
|
80
|
+
file = (root / path).resolve()
|
|
81
|
+
if not file.is_relative_to(root.resolve()) or not file.is_file():
|
|
82
|
+
return skipped(
|
|
83
|
+
f"{path} is not a file under {root.as_posix()}. Run accept from the project root."
|
|
84
|
+
)
|
|
85
|
+
try:
|
|
86
|
+
data = file.read_bytes()
|
|
87
|
+
except OSError as error:
|
|
88
|
+
return skipped(f"{path} could not be read: {error.strerror}.")
|
|
89
|
+
planned = rewrite(path, data, groups)
|
|
90
|
+
if planned.data == data:
|
|
91
|
+
return _FileResult(planned.outcomes, False)
|
|
92
|
+
if dry_run:
|
|
93
|
+
return _FileResult(planned.outcomes, True)
|
|
94
|
+
try:
|
|
95
|
+
_replace(file, planned.data)
|
|
96
|
+
except OSError as error:
|
|
97
|
+
return skipped(f"{path} could not be written: {error.strerror}. It was left unchanged.")
|
|
98
|
+
return _FileResult(planned.outcomes, True)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _replace(file: Path, data: bytes) -> None:
|
|
102
|
+
temp = file.with_name(f".{file.name}.{secrets.token_hex(4)}.tmp")
|
|
103
|
+
try:
|
|
104
|
+
temp.write_bytes(data)
|
|
105
|
+
shutil.copymode(file, temp)
|
|
106
|
+
os.replace(temp, file)
|
|
107
|
+
finally:
|
|
108
|
+
temp.unlink(missing_ok=True)
|
|
109
|
+
# A same-size edit in the same second passes CPython's and pytest's mtime-and-size pyc check.
|
|
110
|
+
for cached in (file.parent / "__pycache__").glob(f"{file.stem}.*.pyc"):
|
|
111
|
+
cached.unlink(missing_ok=True)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@contextmanager
|
|
115
|
+
def lock(lock: Path) -> Generator[None]:
|
|
116
|
+
try:
|
|
117
|
+
fd = os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
|
118
|
+
except FileExistsError:
|
|
119
|
+
shown = lock.as_posix()
|
|
120
|
+
raise AcceptError(
|
|
121
|
+
f"{shown} exists, so another xpt command is writing to .xpt/. "
|
|
122
|
+
f"If none is, delete {shown} and run the command again."
|
|
123
|
+
) from None
|
|
124
|
+
try:
|
|
125
|
+
os.write(fd, str(os.getpid()).encode("ascii"))
|
|
126
|
+
os.close(fd)
|
|
127
|
+
yield
|
|
128
|
+
finally:
|
|
129
|
+
lock.unlink(missing_ok=True)
|
xpt-0.1.1/src/xpt/api.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""The programmatic surface of xpt for tools: read a run, group it, accept or reject groups."""
|
|
2
|
+
|
|
3
|
+
from xpt.accept import AcceptError, AcceptResult, accept
|
|
4
|
+
from xpt.decisions import Rejection, RejectOutcome, RejectStatus, load_rejections, mark, reject
|
|
5
|
+
from xpt.diff import Change, Diff, diff
|
|
6
|
+
from xpt.group import Group, GroupStatus, group
|
|
7
|
+
from xpt.path import format_path
|
|
8
|
+
from xpt.record import Observation, RecordError, Status, latest_run, load_run, run_ids
|
|
9
|
+
from xpt.rewrite import Outcome, OutcomeStatus
|
|
10
|
+
from xpt.site import CallSite, Unlocatable
|
|
11
|
+
from xpt.value import Value, from_json, to_json
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"AcceptError",
|
|
15
|
+
"AcceptResult",
|
|
16
|
+
"CallSite",
|
|
17
|
+
"Change",
|
|
18
|
+
"Diff",
|
|
19
|
+
"Group",
|
|
20
|
+
"GroupStatus",
|
|
21
|
+
"Observation",
|
|
22
|
+
"Outcome",
|
|
23
|
+
"OutcomeStatus",
|
|
24
|
+
"RecordError",
|
|
25
|
+
"RejectOutcome",
|
|
26
|
+
"RejectStatus",
|
|
27
|
+
"Rejection",
|
|
28
|
+
"Status",
|
|
29
|
+
"Unlocatable",
|
|
30
|
+
"Value",
|
|
31
|
+
"accept",
|
|
32
|
+
"diff",
|
|
33
|
+
"format_path",
|
|
34
|
+
"from_json",
|
|
35
|
+
"group",
|
|
36
|
+
"latest_run",
|
|
37
|
+
"load_rejections",
|
|
38
|
+
"load_run",
|
|
39
|
+
"mark",
|
|
40
|
+
"reject",
|
|
41
|
+
"run_ids",
|
|
42
|
+
"to_json",
|
|
43
|
+
]
|
xpt-0.1.1/src/xpt/cli.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import io
|
|
5
|
+
import json
|
|
6
|
+
import shutil
|
|
7
|
+
import sys
|
|
8
|
+
from collections.abc import Callable, Sequence
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import NoReturn
|
|
12
|
+
|
|
13
|
+
from xpt import decisions, render
|
|
14
|
+
from xpt.accept import AcceptError, AcceptResult, accept, lock
|
|
15
|
+
from xpt.group import Group, group, missing
|
|
16
|
+
from xpt.record import (
|
|
17
|
+
DIRECTORY,
|
|
18
|
+
SCHEMA_VERSION,
|
|
19
|
+
RecordError,
|
|
20
|
+
latest_run,
|
|
21
|
+
load_run,
|
|
22
|
+
run_ids,
|
|
23
|
+
)
|
|
24
|
+
from xpt.value import Json
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class UsageError(Exception):
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, slots=True)
|
|
32
|
+
class Output:
|
|
33
|
+
"""A command's result in both forms, and its exit code."""
|
|
34
|
+
|
|
35
|
+
code: int
|
|
36
|
+
lines: list[str]
|
|
37
|
+
data: dict[str, Json]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
Command = Callable[[argparse.Namespace, Path], Output]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class _Run:
|
|
45
|
+
id: str
|
|
46
|
+
groups: tuple[Group, ...]
|
|
47
|
+
run_option: str
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _load(root: Path, run: str | None) -> _Run:
|
|
51
|
+
groups = decisions.mark(group(load_run(root, run)), decisions.load_rejections(root))
|
|
52
|
+
return _Run(run or latest_run(root), groups, f" --run {run}" if run else "")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _pick(run: _Run, ids: Sequence[str]) -> list[Group]:
|
|
56
|
+
by_id = {g.id: g for g in run.groups if g.id is not None}
|
|
57
|
+
unknown = [i for i in ids if i not in by_id]
|
|
58
|
+
if unknown:
|
|
59
|
+
raise UsageError(
|
|
60
|
+
f"no group {', '.join(unknown)} in run {run.id}. Run xpt failures to list the ids."
|
|
61
|
+
)
|
|
62
|
+
return [by_id[i] for i in dict.fromkeys(ids)]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def failures(args: argparse.Namespace, root: Path) -> Output:
|
|
66
|
+
run = _load(root, args.run)
|
|
67
|
+
return Output(
|
|
68
|
+
1 if run.groups else 0,
|
|
69
|
+
render.failures_lines(run.id, run.groups, run.run_option),
|
|
70
|
+
{"run": run.id, "groups": [render.group_json(g) for g in run.groups]},
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def show(args: argparse.Namespace, root: Path) -> Output:
|
|
75
|
+
run = _load(root, args.run)
|
|
76
|
+
[g] = _pick(run, [args.id])
|
|
77
|
+
return Output(
|
|
78
|
+
0,
|
|
79
|
+
render.show_lines(g, args.full, run.run_option),
|
|
80
|
+
{"run": run.id, "group": render.group_json(g)},
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def accept_command(args: argparse.Namespace, root: Path) -> Output:
|
|
85
|
+
chosen = [bool(args.ids), args.all, args.missing].count(True)
|
|
86
|
+
if chosen == 0:
|
|
87
|
+
raise UsageError(
|
|
88
|
+
"xpt accept needs ids, --missing, or --all. Nothing was accepted. "
|
|
89
|
+
"Run xpt failures to list the ids."
|
|
90
|
+
)
|
|
91
|
+
if chosen > 1:
|
|
92
|
+
raise UsageError("give ids, --missing, or --all, not more than one.")
|
|
93
|
+
run = _load(root, args.run)
|
|
94
|
+
if args.all:
|
|
95
|
+
groups, target = [g for g in run.groups if not g.rejected], "--all"
|
|
96
|
+
elif args.missing:
|
|
97
|
+
groups, target = missing(run.groups), "--missing"
|
|
98
|
+
else:
|
|
99
|
+
groups, target = _pick(run, args.ids), " ".join(args.ids)
|
|
100
|
+
data: dict[str, Json] = {"run": run.id, "dry_run": args.dry_run}
|
|
101
|
+
if not groups:
|
|
102
|
+
empty = render.accept_json(AcceptResult((), ()))
|
|
103
|
+
return Output(0, [f"Nothing to accept in run {run.id}."], data | empty)
|
|
104
|
+
result = accept(root, groups, dry_run=args.dry_run)
|
|
105
|
+
written = all(o.status in ("accepted", "already_accepted") for o in result.outcomes)
|
|
106
|
+
retry = f"xpt accept{run.run_option} {target}"
|
|
107
|
+
return Output(
|
|
108
|
+
0 if written else 1,
|
|
109
|
+
render.accept_lines(result, args.dry_run, retry),
|
|
110
|
+
data | render.accept_json(result),
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def reject(args: argparse.Namespace, root: Path) -> Output:
|
|
115
|
+
run = _load(root, args.run)
|
|
116
|
+
outcomes = decisions.reject(root, run.id, _pick(run, args.ids))
|
|
117
|
+
return Output(
|
|
118
|
+
1 if any(o.status == "skipped" for o in outcomes) else 0,
|
|
119
|
+
render.reject_lines(outcomes),
|
|
120
|
+
{"run": run.id, "outcomes": [render.outcome_json(o) for o in outcomes]},
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def runs(args: argparse.Namespace, root: Path) -> Output:
|
|
125
|
+
recorded = run_ids(root)
|
|
126
|
+
latest = latest_run(root) if (root / DIRECTORY / "latest").is_file() else None
|
|
127
|
+
every = sorted({*recorded, *([latest] if latest else [])}, reverse=True)
|
|
128
|
+
rows = [(r, r == latest, len(group(load_run(root, r))) if r in recorded else 0) for r in every]
|
|
129
|
+
return Output(
|
|
130
|
+
0,
|
|
131
|
+
render.runs_lines(rows),
|
|
132
|
+
{"runs": [{"id": r, "latest": latest, "failures": n} for r, latest, n in rows]},
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def clean(args: argparse.Namespace, root: Path) -> Output:
|
|
137
|
+
directory = root / DIRECTORY
|
|
138
|
+
if not directory.is_dir():
|
|
139
|
+
return Output(0, ["Nothing to clean, .xpt/ does not exist."], {"removed": False})
|
|
140
|
+
with lock(directory / "accept.lock"):
|
|
141
|
+
shutil.rmtree(directory)
|
|
142
|
+
return Output(0, ["Removed .xpt/."], {"removed": True})
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class _Parser(argparse.ArgumentParser):
|
|
146
|
+
def error(self, message: str) -> NoReturn:
|
|
147
|
+
raise UsageError(f"{message}. Run {self.prog} --help for usage.")
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _parser() -> argparse.ArgumentParser:
|
|
151
|
+
common = _Parser(add_help=False)
|
|
152
|
+
common.add_argument("--json", action="store_true", help="print one JSON object")
|
|
153
|
+
common.add_argument("--root", type=Path, default=Path(), help="the project root (default: .)")
|
|
154
|
+
picked = _Parser(add_help=False)
|
|
155
|
+
picked.add_argument("--run", help="a run id from xpt runs (default: the latest run)")
|
|
156
|
+
|
|
157
|
+
parser = _Parser(
|
|
158
|
+
prog="xpt", description="Review, accept, and reject recorded expect() failures."
|
|
159
|
+
)
|
|
160
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
161
|
+
|
|
162
|
+
def add(name: str, handler: Command, help: str, *parents: _Parser) -> argparse.ArgumentParser:
|
|
163
|
+
sub = commands.add_parser(name, help=help, parents=[common, *parents])
|
|
164
|
+
sub.set_defaults(handler=handler)
|
|
165
|
+
return sub
|
|
166
|
+
|
|
167
|
+
add("failures", failures, "list the failed call sites of a run, with their diffs", picked)
|
|
168
|
+
sub = add("show", show, "show one call site", picked)
|
|
169
|
+
sub.add_argument("id")
|
|
170
|
+
sub.add_argument("--full", action="store_true", help="lift the change and length limits")
|
|
171
|
+
sub = add("accept", accept_command, "write actual values into the test source", picked)
|
|
172
|
+
sub.add_argument("ids", nargs="*", metavar="id")
|
|
173
|
+
sub.add_argument("--all", action="store_true", help="every acceptable call site")
|
|
174
|
+
sub.add_argument("--missing", action="store_true", help="only empty expectations")
|
|
175
|
+
sub.add_argument("--dry-run", action="store_true", help="report and write nothing")
|
|
176
|
+
sub = add("reject", reject, "record actual values as bugs, never to be offered", picked)
|
|
177
|
+
sub.add_argument("ids", nargs="+", metavar="id")
|
|
178
|
+
add("runs", runs, "list recorded runs")
|
|
179
|
+
add("clean", clean, "delete .xpt/ and every recorded value")
|
|
180
|
+
return parser
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
184
|
+
for stream in (sys.stdout, sys.stderr):
|
|
185
|
+
if isinstance(stream, io.TextIOWrapper):
|
|
186
|
+
stream.reconfigure(encoding="utf-8")
|
|
187
|
+
try:
|
|
188
|
+
args = _parser().parse_args(argv)
|
|
189
|
+
output: Output = args.handler(args, args.root)
|
|
190
|
+
except (UsageError, RecordError, AcceptError) as error:
|
|
191
|
+
print(f"xpt: error: {error}", file=sys.stderr)
|
|
192
|
+
return 2
|
|
193
|
+
if args.json:
|
|
194
|
+
payload = {"schema_version": SCHEMA_VERSION, "command": args.command, **output.data}
|
|
195
|
+
print(json.dumps(payload, ensure_ascii=False, allow_nan=False))
|
|
196
|
+
else:
|
|
197
|
+
print("\n".join(output.lines))
|
|
198
|
+
return output.code
|