@jesdi/skills 0.0.81 → 0.0.83
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.
- package/package.json +2 -1
- package/skills/crap-gate/launcher.sh +104 -0
- package/skills/crap-gate/run.sh +15 -75
- package/skills/crap-gate/test_launcher.py +39 -0
- package/skills/implement-spec/launcher.sh +104 -0
- package/skills/implement-spec/run.sh +10 -89
- package/skills/implement-spec/tests/test_launcher.py +138 -0
- package/skills/test-weigh-in/SKILL.md +16 -0
- package/skills/test-weigh-in/dashboard.html +112 -0
- package/skills/test-weigh-in/launcher.sh +104 -0
- package/skills/test-weigh-in/rules.toml +134 -0
- package/skills/test-weigh-in/run.sh +21 -0
- package/skills/test-weigh-in/tests/brittle_repo.py +79 -0
- package/skills/test-weigh-in/tests/conftest.py +35 -0
- package/skills/test-weigh-in/tests/fixture_repo.py +99 -0
- package/skills/test-weigh-in/tests/kill_repo.py +87 -0
- package/skills/test-weigh-in/tests/maint_repo.py +139 -0
- package/skills/test-weigh-in/tests/map_fixture.py +56 -0
- package/skills/test-weigh-in/tests/pt_repo.py +46 -0
- package/skills/test-weigh-in/tests/red_repo.py +44 -0
- package/skills/test-weigh-in/tests/rule_repo.py +208 -0
- package/skills/test-weigh-in/tests/rules_fixture.py +65 -0
- package/skills/test-weigh-in/tests/smell_repo.py +138 -0
- package/skills/test-weigh-in/tests/test_brittleness.py +75 -0
- package/skills/test-weigh-in/tests/test_brittleness_history.py +168 -0
- package/skills/test-weigh-in/tests/test_coverage_run.py +201 -0
- package/skills/test-weigh-in/tests/test_coverage_value.py +76 -0
- package/skills/test-weigh-in/tests/test_dashboard_map.py +20 -0
- package/skills/test-weigh-in/tests/test_interpreters.py +115 -0
- package/skills/test-weigh-in/tests/test_kill_matrix.py +126 -0
- package/skills/test-weigh-in/tests/test_maintainability_smells.py +113 -0
- package/skills/test-weigh-in/tests/test_mutation_recovery.py +67 -0
- package/skills/test-weigh-in/tests/test_mutation_sampling.py +53 -0
- package/skills/test-weigh-in/tests/test_mutation_score.py +140 -0
- package/skills/test-weigh-in/tests/test_mutation_stale.py +30 -0
- package/skills/test-weigh-in/tests/test_mutmut_contract.py +43 -0
- package/skills/test-weigh-in/tests/test_ruff_pt_smells.py +45 -0
- package/skills/test-weigh-in/tests/test_runs.py +175 -0
- package/skills/test-weigh-in/tests/test_score.py +26 -0
- package/skills/test-weigh-in/tests/test_scoring.py +154 -0
- package/skills/test-weigh-in/tests/test_smell_rules.py +96 -0
- package/skills/test-weigh-in/tests/test_smells.py +71 -0
- package/skills/test-weigh-in/tests/test_walking_skeleton.py +91 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/demo/__init__.py +0 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/demo/board.py +2 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/demo/config.py +2 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/pyproject.toml +8 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_board.py +19 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_config.py +19 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_parser.py +19 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_render.py +19 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_report.py +17 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_session.py +19 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_store.py +17 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/tests/test_sync.py +17 -0
- package/skills/test-weigh-in/tests/testdata/default_repo/uv.lock +1 -0
- package/skills/test-weigh-in/weigh.py +133 -0
- package/skills/test-weigh-in/weigh_coverage.py +154 -0
- package/skills/test-weigh-in/weigh_dashboard.py +72 -0
- package/skills/test-weigh-in/weigh_env.py +112 -0
- package/skills/test-weigh-in/weigh_git.py +119 -0
- package/skills/test-weigh-in/weigh_inventory.py +49 -0
- package/skills/test-weigh-in/weigh_mutation.py +190 -0
- package/skills/test-weigh-in/weigh_pytest_plugin.py +56 -0
- package/skills/test-weigh-in/weigh_ruff.py +22 -0
- package/skills/test-weigh-in/weigh_rules.py +65 -0
- package/skills/test-weigh-in/weigh_runs.py +173 -0
- package/skills/test-weigh-in/weigh_score.py +80 -0
- package/skills/test-weigh-in/weigh_smells.py +355 -0
- package/skills/test-weigh-in/weigh_snapshot.py +49 -0
- package/skills-manifest.json +11 -5
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""The pinned mutmut still exposes what the kill matrix relies on: the mutant-selection variable and the covering-tests data.
|
|
2
|
+
|
|
3
|
+
The pin is `weigh_env.MUTMUT`, a requirement string such as "mutmut==3.8.1" (the closure is resolved by uv from it).
|
|
4
|
+
"""
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
from fixture_repo import HERE, uv
|
|
11
|
+
|
|
12
|
+
sys.path.insert(0, str(HERE.parent))
|
|
13
|
+
|
|
14
|
+
SELECTION_VARIABLE = "MUTANT_UNDER_TEST"
|
|
15
|
+
COVERING_DATA = "tests_by_mangled_function_name"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_pinned_mutmut_exposes_what_the_kill_matrix_relies_on(tmp_path):
|
|
19
|
+
import weigh_env
|
|
20
|
+
|
|
21
|
+
pin = getattr(weigh_env, "MUTMUT", None)
|
|
22
|
+
assert pin and pin.startswith("mutmut=="), f"weigh_env.MUTMUT must pin mutmut exactly, got {pin!r}"
|
|
23
|
+
proj = tmp_path / "proj"
|
|
24
|
+
(proj / "pkg").mkdir(parents=True)
|
|
25
|
+
(proj / "pkg" / "__init__.py").write_text("")
|
|
26
|
+
(proj / "pkg" / "calc.py").write_text("def add(a, b):\n return a + b\n")
|
|
27
|
+
(proj / "tests").mkdir()
|
|
28
|
+
(proj / "tests" / "test_calc.py").write_text("from pkg.calc import add\ndef test_add():\n assert add(1, 2) == 3\n")
|
|
29
|
+
(proj / "pyproject.toml").write_text(
|
|
30
|
+
'[project]\nname = "pkg"\nversion = "0"\n[tool.mutmut]\npaths_to_mutate = ["pkg/"]\ntests_dir = ["tests/"]\n'
|
|
31
|
+
'[tool.pytest.ini_options]\npythonpath = ["."]\n')
|
|
32
|
+
py = proj / ".venv" / "bin" / "python"
|
|
33
|
+
subprocess.run([uv(), "venv", "-q", "--python", "3.12", str(proj / ".venv")], check=True)
|
|
34
|
+
subprocess.run([uv(), "pip", "install", "-q", "--python", str(py), pin, "pytest"], check=True)
|
|
35
|
+
run = subprocess.run([str(proj / ".venv" / "bin" / "mutmut"), "run"], cwd=proj, capture_output=True, text=True)
|
|
36
|
+
assert run.returncode == 0, run.stdout + run.stderr
|
|
37
|
+
stats = json.loads((proj / "mutants" / "mutmut-stats.json").read_text())
|
|
38
|
+
assert COVERING_DATA in stats, f"pinned mutmut no longer exposes {COVERING_DATA}"
|
|
39
|
+
(func,) = stats[COVERING_DATA]
|
|
40
|
+
env = {**os.environ, SELECTION_VARIABLE: func + "__mutmut_1"}
|
|
41
|
+
picked = subprocess.run([str(py), "-m", "pytest", "-q", "-p", "no:cacheprovider"], cwd=proj / "mutants", env=env,
|
|
42
|
+
capture_output=True, text=True)
|
|
43
|
+
assert picked.returncode != 0, f"pinned mutmut no longer exposes {SELECTION_VARIABLE}: selecting a mutant did not change behavior"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Ticket tw/s5-smells/11: ruff's PT rules as maintainability smells, through weigh.py.
|
|
2
|
+
|
|
3
|
+
Interface the tests pin (the spec leaves the JSON keys open): a test's `smells` list holds the ruff rule code ("PT011")
|
|
4
|
+
for each PT violation inside that test; metrics.smelly_tests.value is the percent of all tests with at least one
|
|
5
|
+
maintainability smell. The target repo's ruff.toml (select PT001, ignore PT011) must not change which rules run.
|
|
6
|
+
"""
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from fixture_repo import load, snapshots, weigh
|
|
10
|
+
from pt_repo import pt_suite
|
|
11
|
+
|
|
12
|
+
KEY = "github.com/jesdi/demo"
|
|
13
|
+
T = "tests/test_pt.py::"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture(scope="module")
|
|
17
|
+
def snap(tmp_path_factory, cache):
|
|
18
|
+
root = tmp_path_factory.mktemp("pt")
|
|
19
|
+
(root / "home").mkdir()
|
|
20
|
+
result = weigh(pt_suite(root / "work" / "demo"), root / "home", cache=cache)
|
|
21
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
22
|
+
return load(snapshots(root / "home", KEY)[-1])
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_pytest_raises_without_match_lists_pt011_and_counts_as_smelly(snap):
|
|
26
|
+
assert snap["inventory"]["tests"] == 40
|
|
27
|
+
assert snap["tests"][T + "test_raises"]["smells"] == ["PT011"]
|
|
28
|
+
assert snap["metrics"]["smelly_tests"]["value"] == pytest.approx(100 / 40)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def ran(snap):
|
|
32
|
+
"""The PT rules ran at all: a negative claim below only proves something when test_raises carries PT011."""
|
|
33
|
+
assert "PT011" in snap["tests"][T + "test_raises"]["smells"]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_a_style_only_pt_rule_on_a_fixture_decorator_is_no_smell(snap):
|
|
37
|
+
ran(snap)
|
|
38
|
+
assert snap["tests"][T + "test_board"]["smells"] == []
|
|
39
|
+
assert "PT001" not in {s for t in snap["tests"].values() for s in t["smells"]}
|
|
40
|
+
assert snap["metrics"]["smelly_tests"]["value"] == pytest.approx(100 / 40)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_a_violation_in_a_helper_outside_any_test_counts_for_no_test(snap):
|
|
44
|
+
ran(snap)
|
|
45
|
+
assert snap["tests"][T + "test_uses_helper"]["smells"] == []
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from fixture_repo import build_repo, git, load, snapshots, weigh
|
|
5
|
+
|
|
6
|
+
KEY = "github.com/jesdi/demo"
|
|
7
|
+
MIXED = '''import pytest
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.fixture
|
|
11
|
+
def skip_on_teardown():
|
|
12
|
+
yield
|
|
13
|
+
pytest.skip("teardown")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture
|
|
17
|
+
def broken():
|
|
18
|
+
raise RuntimeError("setup")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.mark.parametrize("n", [1, 2, 3])
|
|
22
|
+
def test_parse(n):
|
|
23
|
+
assert n != 2
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.mark.parametrize("n", [1, 2])
|
|
27
|
+
def test_skipped(n):
|
|
28
|
+
pytest.skip("not here")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@pytest.mark.xfail
|
|
32
|
+
def test_expected_failure():
|
|
33
|
+
assert False
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_errors(broken):
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_fails_then_skips_in_teardown(skip_on_teardown):
|
|
41
|
+
assert False
|
|
42
|
+
'''
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def committed(tmp_path, files, **repo_kwargs):
|
|
46
|
+
"""The default repo with extra committed files."""
|
|
47
|
+
repo = build_repo(tmp_path / "demo", **repo_kwargs)
|
|
48
|
+
for rel, text in files.items():
|
|
49
|
+
(repo / rel).parent.mkdir(parents=True, exist_ok=True)
|
|
50
|
+
(repo / rel).write_text(text)
|
|
51
|
+
git(repo, "add", "-A")
|
|
52
|
+
git(repo, "commit", "-q", "--allow-empty", "-m", "extra")
|
|
53
|
+
return repo
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_cases_aggregate_into_tests_with_their_outcomes(tmp_path, home, cache):
|
|
57
|
+
repo = committed(tmp_path, {"tests/test_mixed.py": MIXED})
|
|
58
|
+
result = weigh(repo, home, "--pytest-args", "tests/test_mixed.py", cache=cache)
|
|
59
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
60
|
+
(snap,) = snapshots(home, KEY)
|
|
61
|
+
tests = {node.split("::")[1]: {k: t[k] for k in ("outcome", "duration", "cases")} for node, t in load(snap)["tests"].items()}
|
|
62
|
+
assert tests == {
|
|
63
|
+
"test_parse": {"outcome": "failed", "duration": pytest.approx(3.375), "cases": 3},
|
|
64
|
+
"test_skipped": {"outcome": "skipped", "duration": pytest.approx(2.25), "cases": 2},
|
|
65
|
+
"test_expected_failure": {"outcome": "passed", "duration": pytest.approx(1.125), "cases": 1},
|
|
66
|
+
"test_errors": {"outcome": "failed", "duration": 0.0, "cases": 1},
|
|
67
|
+
"test_fails_then_skips_in_teardown": {"outcome": "failed", "duration": pytest.approx(1.125), "cases": 1},
|
|
68
|
+
}
|
|
69
|
+
assert load(snap)["inventory"]["cases"] == 8
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@pytest.mark.parametrize("files, args, says", [
|
|
73
|
+
({"tests/test_broken.py": "def test_x(:\n"}, "", "SyntaxError"),
|
|
74
|
+
({}, "tests/test_missing.py", "not found"),
|
|
75
|
+
])
|
|
76
|
+
def test_a_run_that_did_not_execute_the_tests_writes_no_snapshot(tmp_path, home, cache, files, args, says):
|
|
77
|
+
repo = committed(tmp_path, files)
|
|
78
|
+
result = weigh(repo, home, "--pytest-args", args, cache=cache)
|
|
79
|
+
assert result.returncode != 0
|
|
80
|
+
assert "without running the tests" in result.stderr and says in result.stderr
|
|
81
|
+
assert snapshots(home, KEY) == []
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
COVERAGE_ONLY_FAILURE = '''import sys
|
|
85
|
+
|
|
86
|
+
if "coverage" in sys.modules:
|
|
87
|
+
raise RuntimeError("collection fails under coverage")
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_ok():
|
|
91
|
+
pass
|
|
92
|
+
'''
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_a_coverage_run_that_did_not_execute_the_tests_writes_no_snapshot(tmp_path, home, cache):
|
|
96
|
+
repo = committed(tmp_path, {"tests/test_coverage_only.py": COVERAGE_ONLY_FAILURE})
|
|
97
|
+
result = weigh(repo, home, cache=cache)
|
|
98
|
+
assert result.returncode != 0
|
|
99
|
+
assert "under coverage" in result.stderr and "collection fails under coverage" in result.stderr
|
|
100
|
+
assert snapshots(home, KEY) == []
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def test_python_files_with_spaces_are_counted(tmp_path, home, cache):
|
|
104
|
+
repo = committed(tmp_path, {"demo/space name.py": "A = 1\nB = 2\n"})
|
|
105
|
+
result = weigh(repo, home, cache=cache)
|
|
106
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
107
|
+
(snap,) = snapshots(home, KEY)
|
|
108
|
+
assert load(snap)["inventory"]["source_lines"] == 2 + sum(
|
|
109
|
+
len(p.read_text().splitlines()) for p in (repo / "demo").glob("*.py") if p.name != "space name.py")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@pytest.mark.parametrize("origin", ["git@github.com:../../escaped.git", "git@github.com:jesdi/./demo.git"])
|
|
113
|
+
def test_an_origin_that_climbs_out_of_the_snapshot_dir_is_refused(tmp_path, home, cache, origin):
|
|
114
|
+
repo = build_repo(tmp_path / "demo", origin=origin)
|
|
115
|
+
result = weigh(repo, home, cache=cache)
|
|
116
|
+
assert result.returncode != 0
|
|
117
|
+
assert origin in result.stderr
|
|
118
|
+
assert [p.name for p in home.iterdir()] == []
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@pytest.mark.parametrize("args", ["--junitxml=report.xml", "-q --junit-xml report.xml", "--log-file=report.log",
|
|
122
|
+
"--basetemp=tmp", "--debug"])
|
|
123
|
+
def test_file_writing_pytest_args_leave_the_repo_as_it_was(tmp_path, home, cache, args):
|
|
124
|
+
repo = build_repo(tmp_path / "demo")
|
|
125
|
+
result = weigh(repo, home, f"--pytest-args={args}", cache=cache)
|
|
126
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
127
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@pytest.mark.parametrize("addopts", ["--junitxml=report.xml", "--debug"])
|
|
131
|
+
def test_file_writers_configured_by_the_repo_leave_it_as_it_was(tmp_path, home, cache, addopts):
|
|
132
|
+
repo = build_repo(tmp_path / "demo")
|
|
133
|
+
pyproject = repo / "pyproject.toml"
|
|
134
|
+
pyproject.write_text(pyproject.read_text().replace('"-p fakeclock"', f'"-p fakeclock {addopts}"')
|
|
135
|
+
+ 'log_file = "report.log"\n')
|
|
136
|
+
git(repo, "commit", "-q", "-am", "writers")
|
|
137
|
+
result = weigh(repo, home, cache=cache)
|
|
138
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
139
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_a_test_that_writes_where_it_runs_leaves_the_repo_as_it_was(tmp_path, home, cache):
|
|
143
|
+
repo = committed(tmp_path, {"tests/test_leak.py": "from pathlib import Path\n\n\ndef test_leak():\n"
|
|
144
|
+
" Path('leak.txt').write_text('x')\n"})
|
|
145
|
+
result = weigh(repo, home, cache=cache)
|
|
146
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
147
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
148
|
+
assert "tests/test_leak.py::test_leak" in load(snapshots(home, KEY)[0])["tests"]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
@pytest.mark.parametrize("target", ["demo/board.py", "out.log"])
|
|
152
|
+
def test_a_run_that_changes_the_repo_anyway_is_refused(tmp_path, home, cache, target):
|
|
153
|
+
"""Written by absolute path, past the copy: an already-dirty tracked file or an ignored one, both invisible to git status."""
|
|
154
|
+
repo = tmp_path / "demo"
|
|
155
|
+
committed(tmp_path, {".gitignore": "*.log\n", "tests/test_leak.py": f"def test_leak():\n"
|
|
156
|
+
f" with open({str(repo / target)!r}, 'a') as f:\n f.write('# leaked\\n')\n"})
|
|
157
|
+
with (repo / "demo/board.py").open("a") as f:
|
|
158
|
+
f.write("# dirty before the run\n")
|
|
159
|
+
result = weigh(repo, home, cache=cache)
|
|
160
|
+
assert result.returncode != 0
|
|
161
|
+
assert "changed the target repo" in result.stderr and target in result.stderr
|
|
162
|
+
assert snapshots(home, KEY) == []
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def test_source_imported_from_the_repo_itself_is_measured(tmp_path, home, cache):
|
|
166
|
+
"""An editable install points at the repo, not the copy the run uses: its code still counts, under its repo path."""
|
|
167
|
+
repo = committed(tmp_path, {"src/editable/__init__.py": "def double(n):\n return 2 * n\n",
|
|
168
|
+
"tests/test_editable.py": "from editable import double\n\n\ndef test_double():\n"
|
|
169
|
+
" assert double(2) == 4\n"})
|
|
170
|
+
site = next((repo / ".venv" / "lib").glob("python*/site-packages"))
|
|
171
|
+
(site / "editable.pth").write_text(str(repo / "src") + "\n") # the venv ignores itself: the tree stays clean
|
|
172
|
+
result = weigh(repo, home, cache=cache)
|
|
173
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
174
|
+
test = load(snapshots(home, KEY)[0])["tests"]["tests/test_editable.py::test_double"]
|
|
175
|
+
assert test["coverage"] == {"src/editable/__init__.py": [2]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
7
|
+
|
|
8
|
+
from weigh_rules import load # noqa: E402
|
|
9
|
+
from weigh_score import feedback_values, score # noqa: E402
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_tests_over_10s_is_a_percentage_of_all_tests():
|
|
13
|
+
tests = {f"t{i}": {"duration": 20 if i == 0 else 0.1} for i in range(200)}
|
|
14
|
+
assert feedback_values(tests, 30)["tests_over_10s"] == pytest.approx(0.5)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_without_mutation_regression_uses_the_partial_weights():
|
|
18
|
+
# coverage 90 scores 90 and 10% hollow tests score 10: .60 * 90 + .40 * 10
|
|
19
|
+
metrics, pillars = score(load(), {"branch_coverage": 90, "hollow_tests": 10})
|
|
20
|
+
assert pillars["regression"] == {"score": pytest.approx(58), "partial": True}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_a_pillar_without_partial_weights_keeps_its_weights():
|
|
24
|
+
values = {"total_runtime": 60, "p95_duration": 2, "slowest_decile_share": 40, "tests_over_10s": 2}
|
|
25
|
+
_, pillars = score(load(), values)
|
|
26
|
+
assert pillars["feedback"] == {"score": pytest.approx(.4 * 90 + .3 * 10 + .15 * 90 + .15 * 10), "partial": False}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Ticket tw/s3-skeleton/06: rules.toml scoring, the Fast feedback pillar, stdout and --no-open.
|
|
2
|
+
|
|
3
|
+
The anchor tests pin the logistic curve on total runtime and on branch coverage (branch arcs only,
|
|
4
|
+
not statements plus branches).
|
|
5
|
+
Interface: see rules_fixture.py. TEST_WEIGH_IN_RULES=<path> replaces the skill's rules.toml.
|
|
6
|
+
"""
|
|
7
|
+
import os
|
|
8
|
+
import stat
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
from fixture_repo import FAKECLOCK, build_repo, load, snapshots, weigh as _weigh
|
|
14
|
+
from rules_fixture import sha256, write_rules
|
|
15
|
+
|
|
16
|
+
KEY = "github.com/jesdi/demo"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def weigh(repo, home, *flags, cache, rules=None, now=None):
|
|
20
|
+
if rules:
|
|
21
|
+
os.environ["TEST_WEIGH_IN_RULES"] = str(rules)
|
|
22
|
+
try:
|
|
23
|
+
return _weigh(repo, home, *flags, cache=cache, now=now)
|
|
24
|
+
finally:
|
|
25
|
+
os.environ.pop("TEST_WEIGH_IN_RULES", None)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def set_test_duration(repo, seconds):
|
|
29
|
+
"""The 40 tests of the default repo take `seconds` each: total runtime is 40 * seconds."""
|
|
30
|
+
site = next((repo / ".venv" / "lib").glob("python*/site-packages"))
|
|
31
|
+
(site / "fakeclock.py").write_text(FAKECLOCK.replace("1.125", repr(seconds)))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def last_snapshot(home):
|
|
35
|
+
return load(snapshots(home, KEY)[-1])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_anchors_score_90_and_10(tmp_path, home, cache):
|
|
39
|
+
repo = build_repo(tmp_path / "demo") # 40 tests x 1.125 s = 45 s
|
|
40
|
+
at_target = write_rules(tmp_path / "target.toml", total_runtime=dict(target=45, bad=450))
|
|
41
|
+
at_bad = write_rules(tmp_path / "bad.toml", total_runtime=dict(target=450, bad=45))
|
|
42
|
+
r1 = weigh(repo, home, cache=cache, rules=at_target, now="2026-01-01T00:00:00Z")
|
|
43
|
+
assert r1.returncode == 0, r1.stdout + r1.stderr
|
|
44
|
+
r2 = weigh(repo, home, cache=cache, rules=at_bad, now="2026-01-01T00:00:01Z")
|
|
45
|
+
assert r2.returncode == 0, r2.stdout + r2.stderr
|
|
46
|
+
first, second = (load(p)["metrics"]["total_runtime"] for p in snapshots(home, KEY))
|
|
47
|
+
assert first["value"] == pytest.approx(45)
|
|
48
|
+
assert first["score"] == pytest.approx(90, abs=0.01)
|
|
49
|
+
assert second["score"] == pytest.approx(10, abs=0.01)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def add_half_covered_branch(repo):
|
|
53
|
+
"""A module with one `if` (2 branch arcs, 4 statements) and a test taking only the true arm: 50% branch coverage."""
|
|
54
|
+
(repo / "demo" / "pick.py").write_text("def pick(x):\n if x:\n return 1\n return 2\n")
|
|
55
|
+
(repo / "tests" / "test_pick.py").write_text("from demo import pick\n\n\ndef test_pick_true():\n assert pick.pick(True) == 1\n")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_branch_coverage_counts_branch_arcs_only_and_hits_the_anchors(tmp_path, home, cache):
|
|
59
|
+
repo = build_repo(tmp_path / "demo")
|
|
60
|
+
add_half_covered_branch(repo)
|
|
61
|
+
at_target = write_rules(tmp_path / "target.toml", branch_coverage=dict(target=50, bad=10))
|
|
62
|
+
at_bad = write_rules(tmp_path / "bad.toml", branch_coverage=dict(target=90, bad=50))
|
|
63
|
+
for rules, now in ((at_target, "2026-01-01T00:00:00Z"), (at_bad, "2026-01-01T00:00:01Z")):
|
|
64
|
+
result = weigh(repo, home, cache=cache, rules=rules, now=now)
|
|
65
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
66
|
+
first, second = (load(p)["metrics"]["branch_coverage"] for p in snapshots(home, KEY))
|
|
67
|
+
assert first["value"] == pytest.approx(50)
|
|
68
|
+
assert first["score"] == pytest.approx(90, abs=0.01)
|
|
69
|
+
assert second["score"] == pytest.approx(10, abs=0.01)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_faster_runtime_scores_higher_even_beyond_the_bad_anchor(tmp_path, home, cache):
|
|
73
|
+
repo = build_repo(tmp_path / "demo")
|
|
74
|
+
set_test_duration(repo, 30) # 1,200 s, past the 600 s bad anchor
|
|
75
|
+
r1 = weigh(repo, home, cache=cache, now="2026-01-01T00:00:00Z")
|
|
76
|
+
assert r1.returncode == 0, r1.stdout + r1.stderr
|
|
77
|
+
set_test_duration(repo, 22.5) # 900 s
|
|
78
|
+
r2 = weigh(repo, home, cache=cache, now="2026-01-01T00:00:01Z")
|
|
79
|
+
assert r2.returncode == 0, r2.stdout + r2.stderr
|
|
80
|
+
first, second = (load(p) for p in snapshots(home, KEY))
|
|
81
|
+
assert first["inventory"]["runtime_seconds"] == pytest.approx(1200)
|
|
82
|
+
assert second["inventory"]["runtime_seconds"] == pytest.approx(900)
|
|
83
|
+
assert second["pillars"]["feedback"]["score"] > first["pillars"]["feedback"]["score"]
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_fast_feedback_weights_not_summing_to_one_stop_the_run(tmp_path, home, cache):
|
|
87
|
+
repo = build_repo(tmp_path / "demo")
|
|
88
|
+
rules = write_rules(tmp_path / "rules.toml", tests_over_10s=dict(weight=.10)) # .40 .30 .15 .10
|
|
89
|
+
result = weigh(repo, home, cache=cache, rules=rules)
|
|
90
|
+
assert result.returncode == 2
|
|
91
|
+
assert "Fast feedback" in result.stdout + result.stderr
|
|
92
|
+
assert snapshots(home, KEY) == []
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_equal_anchors_stop_the_run_naming_the_metric(tmp_path, home, cache):
|
|
96
|
+
repo = build_repo(tmp_path / "demo")
|
|
97
|
+
rules = write_rules(tmp_path / "rules.toml", branch_coverage=dict(target=80, bad=80))
|
|
98
|
+
result = weigh(repo, home, cache=cache, rules=rules)
|
|
99
|
+
assert result.returncode == 2
|
|
100
|
+
assert "branch coverage" in (result.stdout + result.stderr).lower()
|
|
101
|
+
assert snapshots(home, KEY) == []
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_edited_rules_keep_the_version_and_change_the_hash(tmp_path, home, cache):
|
|
105
|
+
repo = build_repo(tmp_path / "demo")
|
|
106
|
+
before = write_rules(tmp_path / "before.toml", branch_coverage=dict(bad=50))
|
|
107
|
+
after = write_rules(tmp_path / "after.toml", branch_coverage=dict(bad=60))
|
|
108
|
+
for rules, now in ((before, "2026-01-01T00:00:00Z"), (after, "2026-01-01T00:00:01Z")):
|
|
109
|
+
result = weigh(repo, home, cache=cache, rules=rules, now=now)
|
|
110
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
111
|
+
first, second = (load(p)["rules"] for p in snapshots(home, KEY))
|
|
112
|
+
assert (first["version"], second["version"]) == (1, 1)
|
|
113
|
+
assert first["sha256"] == sha256(before)
|
|
114
|
+
assert second["sha256"] == sha256(after)
|
|
115
|
+
assert first["sha256"] != second["sha256"]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_shipped_rules_are_version_1_and_hashed(run, home):
|
|
119
|
+
_, result = run()
|
|
120
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
121
|
+
rules = last_snapshot(home)["rules"]
|
|
122
|
+
shipped = Path(__file__).resolve().parent.parent / "rules.toml"
|
|
123
|
+
assert rules["version"] == 1
|
|
124
|
+
assert rules["sha256"] == sha256(shipped)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
@pytest.fixture
|
|
128
|
+
def browser_log(tmp_path, monkeypatch):
|
|
129
|
+
"""Fake browser launchers first on PATH: each one appends its name to the log when run."""
|
|
130
|
+
bin_dir, log = tmp_path / "fakebin", tmp_path / "opened.log"
|
|
131
|
+
bin_dir.mkdir()
|
|
132
|
+
for name in ("open", "xdg-open", "osascript", "sensible-browser", "x-www-browser", "gio", "wslview"):
|
|
133
|
+
script = bin_dir / name
|
|
134
|
+
script.write_text(f'#!/bin/sh\necho {name} "$@" >> "{log}"\n')
|
|
135
|
+
script.chmod(script.stat().st_mode | stat.S_IEXEC)
|
|
136
|
+
monkeypatch.setenv("PATH", f"{bin_dir}{os.pathsep}{os.environ['PATH']}")
|
|
137
|
+
monkeypatch.setenv("BROWSER", str(bin_dir / "open"))
|
|
138
|
+
return log
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_no_open_opens_no_browser_and_prints_the_dashboard_path(run, home, browser_log):
|
|
142
|
+
_, result = run("--no-open")
|
|
143
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
144
|
+
dashboard = home / ".test-weigh-in" / KEY / "dashboard.html"
|
|
145
|
+
assert dashboard.is_file()
|
|
146
|
+
assert str(dashboard) in result.stdout
|
|
147
|
+
assert not browser_log.exists()
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_without_no_open_the_dashboard_is_opened(run, home, browser_log):
|
|
151
|
+
_, result = run()
|
|
152
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
153
|
+
assert str(home / ".test-weigh-in" / KEY / "dashboard.html") in result.stdout
|
|
154
|
+
assert browser_log.exists()
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""The boundaries of the smell rules (spec requirement 18), through weigh.py; the suite is described in rule_repo.py."""
|
|
2
|
+
import json
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from fixture_repo import load, weigh
|
|
8
|
+
from rule_repo import rules_suite
|
|
9
|
+
|
|
10
|
+
KEY = "github.com/jesdi/demo"
|
|
11
|
+
T = "tests/test_rules.py::"
|
|
12
|
+
M = "tests/test_maint_rules.py::"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture(scope="module")
|
|
16
|
+
def home(tmp_path_factory, cache):
|
|
17
|
+
root = tmp_path_factory.mktemp("rules")
|
|
18
|
+
(root / "home").mkdir()
|
|
19
|
+
result = weigh(rules_suite(root / "work" / "demo"), root / "home", "--no-open", cache=cache)
|
|
20
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
21
|
+
return root / "home" / ".test-weigh-in" / KEY
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@pytest.fixture(scope="module")
|
|
25
|
+
def snap(home):
|
|
26
|
+
return load(sorted(home.glob("*.json"))[-1])
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.fixture(scope="module")
|
|
30
|
+
def page(home):
|
|
31
|
+
"""The data the dashboard renders, as weigh.py wrote it into the page."""
|
|
32
|
+
return json.loads(re.search(r"const data = (.*);\n", (home / "dashboard.html").read_text()).group(1))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def worst(page, key):
|
|
36
|
+
return {w["test"]: w["reason"] for w in next(p for p in page["pillars"] if p["key"] == key)["worst"]}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def smelly(snap, smell):
|
|
40
|
+
return {n.removeprefix(T) for n, t in snap["tests"].items() if smell in t["smells"]}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_check_and_expect_helpers_are_assertions(snap):
|
|
44
|
+
assert snap["tests"][T + "test_check"]["outcome"] == "passed"
|
|
45
|
+
assert smelly(snap, "No assertion") == {"TestA::test_board", "TestOther::test_inherited", "TestDiamond::test_diamond"}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_call_count_and_call_args_assertions_are_interaction_only_and_one_beside_a_result_check_is_not(snap):
|
|
49
|
+
assert smelly(snap, "Interaction-only") == {"test_call_count", "test_call_args"}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_four_monkeypatch_setattr_calls_are_mock_heavy_and_three_are_not(snap):
|
|
53
|
+
assert smelly(snap, "Mock-heavy") == {"test_setattr_4"}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_a_private_name_read_through_a_conftest_module_is_not_private_access(snap):
|
|
57
|
+
assert smelly(snap, "Private access") == {"test_module_private", "test_local_private"}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_same_named_methods_of_two_classes_are_checked_apart(snap):
|
|
61
|
+
a, b = snap["tests"][T + "TestA::test_board"], snap["tests"][T + "TestB::test_board"]
|
|
62
|
+
assert "No assertion" in a["smells"] and "No assertion" not in b["smells"]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_an_inherited_test_is_its_base_class_method_not_a_same_named_one(snap):
|
|
66
|
+
child = snap["tests"][T + "TestChild::test_inherited"]
|
|
67
|
+
assert child["outcome"] == "passed"
|
|
68
|
+
assert "No assertion" not in child["smells"] and child["lines"] == 2
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def maint(snap, smell):
|
|
72
|
+
return {n.removeprefix(M) for n, t in snap["tests"].items() if n.startswith(M) and smell in t["smells"]}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_a_literal_path_held_in_a_variable_is_a_mystery_guest(snap):
|
|
76
|
+
assert maint(snap, "Mystery guest") == {"test_guest_name", "test_guest_path"}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_a_check_helper_closes_an_act_then_assert_cycle(snap):
|
|
80
|
+
assert maint(snap, "Eager test") == {"test_eager_helpers"}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_a_handler_that_calls_a_check_helper_does_not_swallow(snap):
|
|
84
|
+
assert snap["tests"][M + "test_swallow_checked"]["outcome"] == "passed"
|
|
85
|
+
assert not maint(snap, "Swallowed exception")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def test_a_long_test_without_smells_ranks_among_the_worst_maintainability_tests(snap, page):
|
|
89
|
+
assert snap["tests"][M + "test_long"]["smells"] == []
|
|
90
|
+
assert worst(page, "maintainability")[M + "test_long"] == "54 lines"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_slow_tests_without_a_real_sleep_rank_among_the_worst_feedback_tests(page):
|
|
94
|
+
reasons = worst(page, "feedback")
|
|
95
|
+
assert len(reasons) == 10
|
|
96
|
+
assert all(re.fullmatch(r"\d+\.\d\d s", r) for r in reasons.values())
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Ticket tw/s5-smells/09: No assertion, Interaction-only, Mock-heavy and Private access, through weigh.py.
|
|
2
|
+
|
|
3
|
+
Interface the tests pin (the spec leaves the JSON keys open): each entry of the snapshot's `tests` has `smells`, a list
|
|
4
|
+
of the smell names "No assertion", "Interaction-only", "Mock-heavy", "Private access"; the metrics hollow_tests,
|
|
5
|
+
mock_heavy, interaction_only and private_access hold the percentage of all tests (0-100) that have the smell.
|
|
6
|
+
"""
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from fixture_repo import load, snapshots, weigh
|
|
10
|
+
from smell_repo import smelly_suite
|
|
11
|
+
|
|
12
|
+
KEY = "github.com/jesdi/demo"
|
|
13
|
+
T = "tests/test_smells.py::"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.fixture(scope="module")
|
|
17
|
+
def snap(tmp_path_factory, cache):
|
|
18
|
+
root = tmp_path_factory.mktemp("smells")
|
|
19
|
+
(root / "home").mkdir()
|
|
20
|
+
repo = smelly_suite(root / "work" / "demo")
|
|
21
|
+
result = weigh(repo, root / "home", cache=cache)
|
|
22
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
23
|
+
return load(snapshots(root / "home", KEY)[-1])
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def smelly(snap, smell):
|
|
27
|
+
return {n for n, t in snap["tests"].items() if smell in t.get("smells", [])}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def names(*tests):
|
|
31
|
+
return {T + t for t in tests}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_a_test_without_assertion_has_the_smell_and_is_hollow(snap):
|
|
35
|
+
assert T + "test_board_renders" in smelly(snap, "No assertion")
|
|
36
|
+
assert snap["metrics"]["hollow_tests"]["value"] == pytest.approx(100 * 4 / len(snap["tests"]))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_failing_erroring_and_skipped_tests_are_checked_and_counted(snap):
|
|
40
|
+
assert {T + "test_fail_hollow", T + "test_error_hollow", T + "test_skipped_hollow"} <= smelly(snap, "No assertion")
|
|
41
|
+
assert smelly(snap, "No assertion") == names("test_board_renders", "test_fail_hollow", "test_error_hollow", "test_skipped_hollow")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_a_custom_assertion_helper_from_conftest_is_an_assertion(snap):
|
|
45
|
+
assert "smells" in snap["tests"][T + "test_board"]
|
|
46
|
+
assert T + "test_board" not in smelly(snap, "No assertion")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_pytest_raises_is_an_assertion(snap):
|
|
50
|
+
assert "smells" in snap["tests"][T + "test_invalid"]
|
|
51
|
+
assert T + "test_invalid" not in smelly(snap, "No assertion")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_pytest_warns_and_an_assertion_in_a_nested_helper_are_assertions(snap):
|
|
55
|
+
assert "smells" in snap["tests"][T + "test_warns"]
|
|
56
|
+
assert not {T + "test_warns", T + "test_nested_helper"} & smelly(snap, "No assertion")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_more_than_three_mock_constructs_is_mock_heavy(snap):
|
|
60
|
+
assert smelly(snap, "Mock-heavy") == names("test_b_heavy")
|
|
61
|
+
assert snap["metrics"]["mock_heavy"]["value"] == pytest.approx(100 / len(snap["tests"]))
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_asserting_only_on_a_mock_is_interaction_only(snap):
|
|
65
|
+
assert smelly(snap, "Interaction-only") == names("test_notify")
|
|
66
|
+
assert snap["metrics"]["interaction_only"]["value"] == pytest.approx(100 / len(snap["tests"]))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def test_importing_a_private_name_is_private_access_but_dunder_and_conftest_names_are_not(snap):
|
|
70
|
+
assert smelly(snap, "Private access") == names("test_b_private")
|
|
71
|
+
assert snap["metrics"]["private_access"]["value"] == pytest.approx(100 / len(snap["tests"]))
|