@jesdi/skills 0.0.80 → 0.0.82
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-prune/SKILL.md +4 -2
- package/skills/test-prune/prune_strengthen.py +24 -12
- package/skills/test-prune/tests/test_prune_guards.py +1 -1
- package/skills/test-prune/tests/test_prune_inputs.py +10 -5
- package/skills/test-prune/tests/test_prune_strengthen.py +34 -12
- 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 +156 -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 +70 -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 +134 -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 +13 -7
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""`--mutation` through weigh.py: the score over all mutants, and recovery from a run SIGKILLed while mutmut's own
|
|
2
|
+
processes keep running.
|
|
3
|
+
|
|
4
|
+
Snapshot interface: metrics.mutation_score {value: percent of all mutants killed, score}.
|
|
5
|
+
"""
|
|
6
|
+
import os
|
|
7
|
+
import signal
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
12
|
+
from fixture_repo import HERE, load, snapshots, weigh
|
|
13
|
+
from kill_repo import replaced
|
|
14
|
+
from test_mutation_score import SLOW, commit, start, wait_for_mutmut, worktrees
|
|
15
|
+
|
|
16
|
+
sys.path.insert(0, str(HERE.parent))
|
|
17
|
+
|
|
18
|
+
KEY = "github.com/jesdi/demo"
|
|
19
|
+
# add's mutant is killed, mul's survives (x / y with y == 1), orphan's has no covering test: 1 of 3 killed
|
|
20
|
+
SCORED = "def add(x, y):\n return x + y\n\n\ndef mul(x, y):\n return x * y\n\n\ndef orphan(x, y):\n return x - y\n"
|
|
21
|
+
SCORED_TESTS = "from demo import scored\n\n\ndef test_ops():\n assert scored.add(1, 2) == 3\n assert scored.mul(3, 1) == 3\n"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_score_is_over_all_mutants_killed_surviving_and_uncovered(tmp_path, home, cache):
|
|
25
|
+
from weigh_rules import load as load_rules
|
|
26
|
+
from weigh_score import metric_score
|
|
27
|
+
|
|
28
|
+
repo = replaced(tmp_path / "demo", ("scored.py", SCORED), ("test_scored.py", SCORED_TESTS))
|
|
29
|
+
result = weigh(repo, home, "--mutation", "--no-open", cache=cache)
|
|
30
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
31
|
+
metric = load(snapshots(home, KEY)[-1])["metrics"]["mutation_score"]
|
|
32
|
+
assert metric["value"] == pytest.approx(100 / 3)
|
|
33
|
+
rule = load_rules()["pillars"]["regression"]["metrics"]["mutation_score"]
|
|
34
|
+
assert metric["score"] == pytest.approx(metric_score(100 / 3, rule["target"], rule["bad"], rule.get("log", False)))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def kill_group(pid):
|
|
38
|
+
try:
|
|
39
|
+
os.killpg(pid, signal.SIGKILL)
|
|
40
|
+
except ProcessLookupError:
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_run_sigkilled_while_mutmut_runs_is_cleaned_up_by_the_next_run(tmp_path, home, cache):
|
|
45
|
+
from fixture_repo import build_repo
|
|
46
|
+
|
|
47
|
+
repo = build_repo(tmp_path / "demo")
|
|
48
|
+
commit(repo, {"tests/test_slow.py": SLOW})
|
|
49
|
+
first = start(repo, home, cache)
|
|
50
|
+
second = None
|
|
51
|
+
try:
|
|
52
|
+
wait_for_mutmut(repo, first)
|
|
53
|
+
first.kill() # weigh.py alone: mutmut and its workers live on, as after a real SIGKILL
|
|
54
|
+
first.wait()
|
|
55
|
+
stale = [l.removeprefix("worktree ") for l in worktrees(repo)[1:]]
|
|
56
|
+
assert len(stale) == 1
|
|
57
|
+
second = start(repo, home, cache)
|
|
58
|
+
fresh = wait_for_mutmut(repo, second, ignore=stale)
|
|
59
|
+
assert [l.removeprefix("worktree ") for l in worktrees(repo)[1:]] == [fresh]
|
|
60
|
+
assert not os.path.exists(stale[0])
|
|
61
|
+
assert second.wait(timeout=900) == 0
|
|
62
|
+
finally:
|
|
63
|
+
kill_group(first.pid)
|
|
64
|
+
if second:
|
|
65
|
+
kill_group(second.pid)
|
|
66
|
+
second.wait()
|
|
67
|
+
assert len(worktrees(repo)) == 1
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Sampling candidates, the mutation settings in the snapshot, and kills after a hung matrix run.
|
|
2
|
+
|
|
3
|
+
Snapshot interface: mutation_settings {mutation: bool, matrix_per_function: N or "all"}; mutation.matrix as in
|
|
4
|
+
test_kill_matrix.py, a row (empty when no test covers the function) for every sampled mutant.
|
|
5
|
+
"""
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from fixture_repo import HERE, load, snapshots, weigh
|
|
11
|
+
from kill_repo import replaced
|
|
12
|
+
|
|
13
|
+
sys.path.insert(0, str(HERE.parent))
|
|
14
|
+
|
|
15
|
+
KEY = "github.com/jesdi/demo"
|
|
16
|
+
SOURCE = "def add(x, y):\n return x + y\n\n\ndef orphan(x, y):\n return x - y\n"
|
|
17
|
+
TESTS = "from demo import calc\n\n\ndef test_add():\n assert calc.add(1, 2) == 3\n"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@pytest.fixture(scope="module")
|
|
21
|
+
def runs(tmp_path_factory, cache):
|
|
22
|
+
home = tmp_path_factory.mktemp("sampling-home")
|
|
23
|
+
repo = replaced(tmp_path_factory.mktemp("sampling") / "demo", ("calc.py", SOURCE), ("test_calc.py", TESTS))
|
|
24
|
+
snaps = {}
|
|
25
|
+
for now, flags in (("2026-01-01T00:00:00Z", ()), ("2026-01-02T00:00:00Z", ("--matrix-per-function", "all")),
|
|
26
|
+
("2026-01-03T00:00:00Z", None)):
|
|
27
|
+
result = weigh(repo, home, "--no-open", *(("--mutation", *flags) if flags is not None else ()), cache=cache, now=now)
|
|
28
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
29
|
+
snaps[flags] = load(snapshots(home, KEY)[-1])
|
|
30
|
+
return snaps
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest.mark.parametrize("flags", [(), ("--matrix-per-function", "all")])
|
|
34
|
+
def test_a_function_no_test_covers_is_sampled_with_an_empty_row(runs, flags):
|
|
35
|
+
matrix = runs[flags]["mutation"]["matrix"]
|
|
36
|
+
orphan = [m for m in matrix if ".x_orphan__mutmut_" in m]
|
|
37
|
+
assert orphan and all(matrix[m] == [] for m in orphan)
|
|
38
|
+
assert any(".x_add__mutmut_" in m and matrix[m] for m in matrix)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_the_snapshot_holds_the_mutation_settings(runs):
|
|
42
|
+
assert runs[()]["mutation_settings"] == {"mutation": True, "matrix_per_function": 3}
|
|
43
|
+
assert runs[("--matrix-per-function", "all")]["mutation_settings"] == {"mutation": True, "matrix_per_function": "all"}
|
|
44
|
+
assert runs[None]["mutation_settings"] == {"mutation": False, "matrix_per_function": 3}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_a_hung_run_credits_only_the_tests_seen_to_fail_or_hang(monkeypatch):
|
|
48
|
+
import weigh_mutation
|
|
49
|
+
|
|
50
|
+
alone = {"t_fail": {"t_fail"}, "t_hang": None, "t_pass": set()}
|
|
51
|
+
monkeypatch.setattr(weigh_mutation, "_failing",
|
|
52
|
+
lambda python, wt, env, mutant, tests, timeout: alone[tests[0]] if len(tests) == 1 else None)
|
|
53
|
+
assert weigh_mutation._kills(None, None, {}, "m", ["t_hang", "t_fail", "t_pass"], 1) == ["t_fail", "t_hang"]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""`--mutation` against fixture repos: refusals, a clean run in a temporary worktree, interruption, and the target left untouched."""
|
|
2
|
+
import os
|
|
3
|
+
import signal
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from fixture_repo import WEIGH, build_repo, git, snapshots, uv
|
|
11
|
+
|
|
12
|
+
KEY = "github.com/jesdi/demo"
|
|
13
|
+
SLOW = "import time\ndef test_slow():\n time.sleep(3)\n"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def marker_conftest(marker):
|
|
17
|
+
return f"def pytest_sessionstart(session):\n open({str(marker)!r}, 'a').write('x')\n"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def commit(repo, files):
|
|
21
|
+
for rel, text in files.items():
|
|
22
|
+
(repo / rel).write_text(text)
|
|
23
|
+
git(repo, "add", "-A")
|
|
24
|
+
git(repo, "commit", "-q", "-m", "more")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def env_for(home, cache):
|
|
28
|
+
env = {k: v for k, v in os.environ.items() if k not in ("VIRTUAL_ENV", "PYTHONPATH")}
|
|
29
|
+
env.update(HOME=str(home), TEST_WEIGH_IN_CACHE_DIR=str(cache), TEST_WEIGH_IN_UV=uv(), BROWSER="true")
|
|
30
|
+
return env
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def mutate(repo, home, cache):
|
|
34
|
+
return subprocess.run([sys.executable, str(WEIGH), str(repo), "--mutation", "--no-open"],
|
|
35
|
+
env=env_for(home, cache), capture_output=True, text=True, timeout=900)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def worktrees(repo):
|
|
39
|
+
return [l for l in git(repo, "worktree", "list", "--porcelain").splitlines() if l.startswith("worktree ")]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def start(repo, home, cache):
|
|
43
|
+
return subprocess.Popen([sys.executable, str(WEIGH), str(repo), "--mutation", "--no-open"], env=env_for(home, cache),
|
|
44
|
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def wait_for_mutmut(repo, proc, ignore=()):
|
|
48
|
+
"""Poll until a temporary worktree of the repo holds a mutants/ directory, i.e. mutmut is running."""
|
|
49
|
+
deadline = time.time() + 300
|
|
50
|
+
while time.time() < deadline:
|
|
51
|
+
assert proc.poll() is None, "the run ended before mutmut started"
|
|
52
|
+
for line in worktrees(repo)[1:]:
|
|
53
|
+
path = line.removeprefix("worktree ")
|
|
54
|
+
if path not in ignore and os.path.isdir(os.path.join(path, "mutants")):
|
|
55
|
+
return path
|
|
56
|
+
time.sleep(0.1)
|
|
57
|
+
pytest.fail("mutmut never started")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def reap(proc):
|
|
61
|
+
try:
|
|
62
|
+
os.killpg(proc.pid, signal.SIGKILL)
|
|
63
|
+
except ProcessLookupError:
|
|
64
|
+
pass
|
|
65
|
+
proc.wait()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def refused(result, name):
|
|
69
|
+
out = result.stdout + result.stderr
|
|
70
|
+
assert result.returncode != 0, out
|
|
71
|
+
assert name in out, out
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_red_suite_is_refused_naming_the_failing_test(tmp_path, home, cache):
|
|
75
|
+
repo = build_repo(tmp_path / "demo")
|
|
76
|
+
commit(repo, {"tests/test_claim.py": "def test_claim_flow():\n assert False\n"})
|
|
77
|
+
refused(mutate(repo, home, cache), "test_claim_flow")
|
|
78
|
+
assert snapshots(home, KEY) == []
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@pytest.mark.parametrize("dirty, name", [
|
|
82
|
+
({"demo/board.py": "def render(n):\n return str(n) + ''\n"}, "board.py"),
|
|
83
|
+
({"tests/test_new.py": "def test_new():\n assert True\n"}, "tests/test_new.py"),
|
|
84
|
+
])
|
|
85
|
+
def test_dirty_tree_is_refused_before_any_test_runs(tmp_path, home, cache, dirty, name):
|
|
86
|
+
marker = tmp_path / "ran"
|
|
87
|
+
repo = build_repo(tmp_path / "demo")
|
|
88
|
+
commit(repo, {"tests/conftest.py": marker_conftest(marker)})
|
|
89
|
+
for rel, text in dirty.items():
|
|
90
|
+
(repo / rel).write_text(text)
|
|
91
|
+
refused(mutate(repo, home, cache), name)
|
|
92
|
+
assert not marker.exists()
|
|
93
|
+
assert snapshots(home, KEY) == []
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_clean_run_leaves_the_target_untouched(tmp_path, home, cache):
|
|
97
|
+
repo = build_repo(tmp_path / "demo")
|
|
98
|
+
config = (repo / ".git" / "config").read_bytes()
|
|
99
|
+
result = mutate(repo, home, cache)
|
|
100
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
101
|
+
assert not (repo / "mutants").exists()
|
|
102
|
+
assert len(worktrees(repo)) == 1
|
|
103
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
104
|
+
assert (repo / ".git" / "config").read_bytes() == config
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def test_killed_run_is_cleaned_up_by_the_next_run(tmp_path, home, cache):
|
|
108
|
+
repo = build_repo(tmp_path / "demo")
|
|
109
|
+
commit(repo, {"tests/test_slow.py": SLOW})
|
|
110
|
+
proc = start(repo, home, cache)
|
|
111
|
+
try:
|
|
112
|
+
wait_for_mutmut(repo, proc)
|
|
113
|
+
proc.kill()
|
|
114
|
+
finally:
|
|
115
|
+
reap(proc)
|
|
116
|
+
stale = [l.removeprefix("worktree ") for l in worktrees(repo)[1:]]
|
|
117
|
+
assert len(stale) == 1
|
|
118
|
+
second = start(repo, home, cache)
|
|
119
|
+
try:
|
|
120
|
+
fresh = wait_for_mutmut(repo, second, ignore=stale)
|
|
121
|
+
# the second mutation is under way: the interrupted run's worktree is already gone, registration and directory
|
|
122
|
+
assert [l.removeprefix("worktree ") for l in worktrees(repo)[1:]] == [fresh]
|
|
123
|
+
assert not os.path.exists(stale[0])
|
|
124
|
+
assert second.wait(timeout=900) == 0
|
|
125
|
+
finally:
|
|
126
|
+
reap(second)
|
|
127
|
+
assert len(worktrees(repo)) == 1
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_ctrl_c_removes_the_worktree(tmp_path, home, cache):
|
|
131
|
+
repo = build_repo(tmp_path / "demo")
|
|
132
|
+
commit(repo, {"tests/test_slow.py": SLOW})
|
|
133
|
+
proc = start(repo, home, cache)
|
|
134
|
+
try:
|
|
135
|
+
wait_for_mutmut(repo, proc)
|
|
136
|
+
os.killpg(proc.pid, signal.SIGINT)
|
|
137
|
+
assert proc.wait(timeout=120) != 0
|
|
138
|
+
finally:
|
|
139
|
+
reap(proc)
|
|
140
|
+
assert len(worktrees(repo)) == 1
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""A refused `--mutation` run still removes the worktree an interrupted run left behind."""
|
|
2
|
+
import subprocess
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from fixture_repo import build_repo, git
|
|
7
|
+
from test_mutation_score import commit, mutate, worktrees
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def plant_stale(repo, cache):
|
|
11
|
+
"""A worktree registered in the repo, under the cache, named after a pid that no longer runs."""
|
|
12
|
+
dead = subprocess.Popen(["true"])
|
|
13
|
+
dead.wait()
|
|
14
|
+
wt = cache / "worktrees" / f"{dead.pid}-stale"
|
|
15
|
+
git(repo, "worktree", "add", "-q", "--detach", str(wt), "HEAD")
|
|
16
|
+
return wt
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest.mark.parametrize("break_it", [
|
|
20
|
+
lambda repo: (repo / "demo" / "board.py").write_text("dirty\n"),
|
|
21
|
+
lambda repo: commit(repo, {"tests/test_claim.py": "def test_claim_flow():\n assert False\n"}),
|
|
22
|
+
], ids=["dirty", "red"])
|
|
23
|
+
def test_refused_run_removes_the_stale_worktree(tmp_path, home, cache, break_it):
|
|
24
|
+
repo = build_repo(tmp_path / "demo")
|
|
25
|
+
wt = plant_stale(repo, cache)
|
|
26
|
+
break_it(repo)
|
|
27
|
+
result = mutate(repo, home, cache)
|
|
28
|
+
assert result.returncode == 1, result.stdout + result.stderr
|
|
29
|
+
assert not wt.exists()
|
|
30
|
+
assert len(worktrees(repo)) == 1
|
|
@@ -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,156 @@
|
|
|
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"])
|
|
123
|
+
def test_file_writing_pytest_args_write_outside_the_repo(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
|
+
def test_file_writers_configured_by_the_repo_write_outside_it(tmp_path, home, cache):
|
|
131
|
+
repo = build_repo(tmp_path / "demo")
|
|
132
|
+
pyproject = repo / "pyproject.toml"
|
|
133
|
+
pyproject.write_text(pyproject.read_text().replace('"-p fakeclock"', '"-p fakeclock --junitxml=report.xml"')
|
|
134
|
+
+ 'log_file = "report.log"\n')
|
|
135
|
+
git(repo, "commit", "-q", "-am", "writers")
|
|
136
|
+
result = weigh(repo, home, cache=cache)
|
|
137
|
+
assert result.returncode == 0, result.stdout + result.stderr
|
|
138
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_debug_is_refused_before_it_writes(tmp_path, home, cache):
|
|
142
|
+
repo = build_repo(tmp_path / "demo")
|
|
143
|
+
result = weigh(repo, home, "--pytest-args=--debug", cache=cache)
|
|
144
|
+
assert result.returncode != 0
|
|
145
|
+
assert "writes files" in result.stderr
|
|
146
|
+
assert git(repo, "status", "--porcelain") == ""
|
|
147
|
+
assert snapshots(home, KEY) == []
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def test_a_run_that_changes_the_repo_anyway_is_refused(tmp_path, home, cache):
|
|
151
|
+
repo = committed(tmp_path, {"tests/test_leak.py": "from pathlib import Path\n\n\ndef test_leak():\n"
|
|
152
|
+
" Path('leak.txt').write_text('x')\n"})
|
|
153
|
+
result = weigh(repo, home, cache=cache)
|
|
154
|
+
assert result.returncode != 0
|
|
155
|
+
assert "changed the target repo" in result.stderr and "leak.txt" in result.stderr
|
|
156
|
+
assert snapshots(home, KEY) == []
|
|
@@ -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}
|