@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,79 @@
|
|
|
1
|
+
"""Fixture repos for ticket tw/s6-brittleness/12: the default repo with its tests replaced and a scripted git history.
|
|
2
|
+
|
|
3
|
+
Snapshot interface the tests pin (the spec leaves the JSON keys open):
|
|
4
|
+
test_files {<repo-relative test file>: {sources: [repo-relative paths of its mapped source files, sorted],
|
|
5
|
+
brittle: true | false | null (unmeasured)}}
|
|
6
|
+
metrics.brittle_test_files {value: % of mapped test files that are brittle, score}
|
|
7
|
+
commit: null or "" when the target is not a git repository.
|
|
8
|
+
Every body below is straight-line statements inside one function, so the test (not the import) covers each line.
|
|
9
|
+
"""
|
|
10
|
+
import shutil
|
|
11
|
+
import subprocess
|
|
12
|
+
|
|
13
|
+
from fixture_repo import GIT, build_repo, git
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def body(name, n):
|
|
17
|
+
return f"def run():\n" + "".join(f" {name}{i} = {i}\n" for i in range(n))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def commit(repo, message, touch=(), extra=()):
|
|
21
|
+
"""Append a comment to each file in `touch` (never a covered line) and commit; `extra` files are new."""
|
|
22
|
+
for rel in touch:
|
|
23
|
+
with (repo / rel).open("a") as out:
|
|
24
|
+
out.write(f"# {message}\n")
|
|
25
|
+
for rel in extra:
|
|
26
|
+
(repo / rel).parent.mkdir(parents=True, exist_ok=True)
|
|
27
|
+
(repo / rel).write_text(message)
|
|
28
|
+
subprocess.run([*GIT, "add", "-A", "--", *touch, *extra], cwd=repo, check=True, capture_output=True)
|
|
29
|
+
subprocess.run([*GIT, "commit", "-q", "-m", message], cwd=repo, check=True, capture_output=True)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def suite(path, modules, test_file, test_source):
|
|
33
|
+
"""Default repo with its demo modules and tests replaced by `modules` {rel: text} and one test file."""
|
|
34
|
+
repo = build_repo(path)
|
|
35
|
+
for old in [*(repo / "tests").glob("test_*.py"), *(repo / "demo").glob("[!_]*.py")]:
|
|
36
|
+
old.unlink()
|
|
37
|
+
for rel, text in {**modules, test_file: test_source}.items():
|
|
38
|
+
(repo / rel).write_text(text)
|
|
39
|
+
git(repo, "add", "-A")
|
|
40
|
+
git(repo, "commit", "-q", "-m", "suite")
|
|
41
|
+
return repo
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
CLAIMS = "demo/claims.py"
|
|
45
|
+
TEST = "tests/test_claims.py"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def claims_repo(path):
|
|
49
|
+
"""test_claims covers ~82 lines of claims.py and ~18 of other.py (other.py is under 20%, so not mapped)."""
|
|
50
|
+
return suite(
|
|
51
|
+
path,
|
|
52
|
+
{CLAIMS: body("c", 82), "demo/other.py": body("o", 18)},
|
|
53
|
+
TEST,
|
|
54
|
+
"from demo import claims, other\n\n\ndef test_claims():\n claims.run()\n other.run()\n",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def history(repo, both, claims_only):
|
|
59
|
+
"""After the suite commit (which changed both files): `both` more commits changing claims.py and the test, then `claims_only` changing claims.py alone."""
|
|
60
|
+
for i in range(both):
|
|
61
|
+
commit(repo, f"both {i}", [CLAIMS, TEST])
|
|
62
|
+
for i in range(claims_only):
|
|
63
|
+
commit(repo, f"claims {i}", [CLAIMS])
|
|
64
|
+
return repo
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def board_repo(path):
|
|
68
|
+
"""test_board covers 300 lines: 260 in board.py and 40 in config.py (13%)."""
|
|
69
|
+
return suite(
|
|
70
|
+
path,
|
|
71
|
+
{"demo/board.py": body("b", 260), "demo/config.py": body("g", 40)},
|
|
72
|
+
"tests/test_board.py",
|
|
73
|
+
"from demo import board, config\n\n\ndef test_board():\n board.run()\n config.run()\n",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def without_git(repo):
|
|
78
|
+
shutil.rmtree(repo / ".git")
|
|
79
|
+
return repo
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
collect_ignore_glob = ["testdata/*"]
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from fixture_repo import build_repo, weigh
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.fixture(autouse=True, scope="session")
|
|
9
|
+
def no_browser():
|
|
10
|
+
"""The engine opens the dashboard unless --no-open: a no-op BROWSER keeps every test from opening a real tab.
|
|
11
|
+
|
|
12
|
+
Session scope sets it before any module fixture runs the engine."""
|
|
13
|
+
with pytest.MonkeyPatch.context() as patch:
|
|
14
|
+
patch.setenv("BROWSER", "true")
|
|
15
|
+
yield
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture(scope="session")
|
|
19
|
+
def cache(tmp_path_factory):
|
|
20
|
+
return tmp_path_factory.mktemp("overlay-cache")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.fixture
|
|
24
|
+
def home(tmp_path):
|
|
25
|
+
(tmp_path / "home").mkdir()
|
|
26
|
+
return tmp_path / "home"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@pytest.fixture
|
|
30
|
+
def run(tmp_path, home, cache):
|
|
31
|
+
"""Build a repo (kwargs for build_repo, `name` picks the folder) and weigh it; returns (repo, result)."""
|
|
32
|
+
def _run(*flags, name="demo", now=None, **repo_kwargs):
|
|
33
|
+
repo = build_repo(tmp_path / "work" / name, **repo_kwargs)
|
|
34
|
+
return repo, weigh(repo, home, *flags, cache=cache, now=now)
|
|
35
|
+
return _run
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"""Builds fixture target repos from testdata/default_repo and runs weigh.py against them.
|
|
2
|
+
|
|
3
|
+
Snapshot interface the tests pin (the spec leaves the JSON keys open):
|
|
4
|
+
commit (short sha), dirty (bool), pytest_args (str), pytest_version, python_version,
|
|
5
|
+
inventory {tests, cases, test_files, test_lines, source_lines, runtime_seconds}.
|
|
6
|
+
Environment: TEST_WEIGH_IN_NOW=<ISO UTC> freezes the clock used to name snapshots;
|
|
7
|
+
TEST_WEIGH_IN_CACHE_DIR holds the overlay.
|
|
8
|
+
"""
|
|
9
|
+
import functools
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
HERE = Path(__file__).resolve().parent
|
|
18
|
+
WEIGH = HERE.parent / "weigh.py"
|
|
19
|
+
TEMPLATE = HERE / "testdata" / "default_repo"
|
|
20
|
+
FAKECLOCK = (
|
|
21
|
+
"import pytest\n"
|
|
22
|
+
"@pytest.hookimpl(hookwrapper=True)\n"
|
|
23
|
+
"def pytest_runtest_makereport(item, call):\n"
|
|
24
|
+
" rep = (yield).get_result()\n"
|
|
25
|
+
" if rep.when == 'call':\n"
|
|
26
|
+
" rep.duration = 1.125\n"
|
|
27
|
+
)
|
|
28
|
+
GIT = ["git", "-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def git(root, *args):
|
|
32
|
+
return subprocess.run([*GIT, *args], cwd=root, check=True, capture_output=True, text=True).stdout.strip()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _pad(files, total):
|
|
36
|
+
have = sum(len(f.read_text().splitlines()) for f in files)
|
|
37
|
+
with files[0].open("a") as out:
|
|
38
|
+
out.writelines(f"_PAD_{i} = {i}\n" for i in range(total - have))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@functools.cache
|
|
42
|
+
def uv():
|
|
43
|
+
"""uv for the fixtures and the engine: TEST_WEIGH_IN_UV, then PATH, then the pinned uv that the skill's launcher downloads (CI has no uv on PATH)."""
|
|
44
|
+
found = os.environ.get("TEST_WEIGH_IN_UV") or shutil.which("uv")
|
|
45
|
+
if found:
|
|
46
|
+
return found
|
|
47
|
+
subprocess.run(["sh", str(HERE.parent / "run.sh"), "--help"], check=True, capture_output=True)
|
|
48
|
+
cache = os.environ.get("TEST_WEIGH_IN_CACHE_DIR") or Path(os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache") / "test-weigh-in"
|
|
49
|
+
return str(next(Path(cache).glob("uv/*/*/uv")))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _venv(repo, pytest_version):
|
|
53
|
+
py = repo / ".venv" / "bin" / "python"
|
|
54
|
+
subprocess.run([uv(), "venv", "-q", "--python", "3.12", str(repo / ".venv")], check=True)
|
|
55
|
+
subprocess.run([uv(), "pip", "install", "-q", "--python", str(py), f"pytest=={pytest_version}"], check=True)
|
|
56
|
+
site = next((repo / ".venv" / "lib").glob("python*/site-packages"))
|
|
57
|
+
(site / "fakeclock.py").write_text(FAKECLOCK)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def build_repo(path, origin="git@github.com:jesdi/demo.git", pytest_version="8.3.5", test_lines=None, source_lines=None):
|
|
61
|
+
path = Path(path)
|
|
62
|
+
shutil.copytree(TEMPLATE, path)
|
|
63
|
+
if test_lines:
|
|
64
|
+
_pad(sorted((path / "tests").glob("test_*.py")), test_lines)
|
|
65
|
+
if source_lines:
|
|
66
|
+
_pad([path / "demo" / "board.py", path / "demo" / "config.py"], source_lines)
|
|
67
|
+
_venv(path, pytest_version)
|
|
68
|
+
git(path, "init", "-q", "-b", "main")
|
|
69
|
+
if origin:
|
|
70
|
+
git(path, "remote", "add", "origin", origin)
|
|
71
|
+
git(path, "add", "-A")
|
|
72
|
+
git(path, "commit", "-q", "-m", "init")
|
|
73
|
+
return path
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def short_sha(repo):
|
|
77
|
+
return git(repo, "rev-parse", "--short", "HEAD")
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def site_files(repo):
|
|
81
|
+
site = next((repo / ".venv" / "lib").glob("python*/site-packages"))
|
|
82
|
+
return sorted(str(p.relative_to(site)) for p in site.rglob("*"))
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def weigh(repo, home, *flags, cache, now=None):
|
|
86
|
+
env = {k: v for k, v in os.environ.items() if k not in ("VIRTUAL_ENV", "PYTHONPATH")}
|
|
87
|
+
env.update(HOME=str(home), TEST_WEIGH_IN_CACHE_DIR=str(cache), TEST_WEIGH_IN_UV=uv())
|
|
88
|
+
env.setdefault("UV_CACHE_DIR", subprocess.run([uv(), "cache", "dir"], capture_output=True, text=True).stdout.strip())
|
|
89
|
+
if now:
|
|
90
|
+
env["TEST_WEIGH_IN_NOW"] = now
|
|
91
|
+
return subprocess.run([sys.executable, str(WEIGH), str(repo), *flags], env=env, capture_output=True, text=True)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def snapshots(home, key):
|
|
95
|
+
return sorted((Path(home) / ".test-weigh-in" / key).glob("*.json"))
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def load(path):
|
|
99
|
+
return json.loads(Path(path).read_text())
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Fixture repos for ticket tw/s7-mutation/14, the default repo with its sources and tests replaced.
|
|
2
|
+
|
|
3
|
+
Every mutated function has exactly the mutants named here (mutmut 3.8.1: one for a `return x <op> y`, two per `a = b <op> c`).
|
|
4
|
+
|
|
5
|
+
sample_repo demo/parser.py parse(t, u): six `name = prev + u` lines, 12 mutants on 6 lines; noop(): 1 mutant
|
|
6
|
+
tests test_parse_ok and test_parse_edge each kill every mutant of parse; test_noop kills noop's
|
|
7
|
+
matrix_repo demo/ops.py seven one-mutant functions: add, sub, mul (x * y: with y == 1 the mutant x / y survives),
|
|
8
|
+
pack, unpack, wrap, unwrap
|
|
9
|
+
tests test_a: asserts add, sub and mul: kills the add and sub mutants, not mul's
|
|
10
|
+
test_b: asserts add and sub: kills the same two
|
|
11
|
+
test_session_roundtrip: calls pack, unpack, wrap, unwrap and asserts `is not None`: kills none
|
|
12
|
+
shift_repo demo/shift.py shift(x, y): 1 mutant
|
|
13
|
+
tests test_parse_invalid and test_shift_other both kill it
|
|
14
|
+
"""
|
|
15
|
+
from fixture_repo import build_repo, git
|
|
16
|
+
|
|
17
|
+
PARSER = "def parse(t, u):\n" + "".join(f" {n} = {p} + u\n" for n, p in zip("abcdef", "tabcde")) + " return f\n\n\ndef noop():\n return 7\n"
|
|
18
|
+
SAMPLE_TESTS = '''from demo import parser
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_parse_ok():
|
|
22
|
+
assert parser.parse(1, 2) == 13
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_parse_edge():
|
|
26
|
+
assert parser.parse(0, 1) == 6
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_noop():
|
|
30
|
+
assert parser.noop() == 7
|
|
31
|
+
'''
|
|
32
|
+
OPS = "".join(f"def {n}(x, y):\n return x {op} y\n\n\n" for n, op in
|
|
33
|
+
(("add", "+"), ("sub", "-"), ("mul", "*"), ("pack", "+"), ("unpack", "-"), ("wrap", "*"), ("unwrap", "/"))).rstrip() + "\n"
|
|
34
|
+
MATRIX_TESTS = '''from demo import ops
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_a():
|
|
38
|
+
assert ops.add(1, 2) == 3
|
|
39
|
+
assert ops.sub(3, 1) == 2
|
|
40
|
+
assert ops.mul(3, 1) == 3
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_b():
|
|
44
|
+
assert ops.add(2, 2) == 4
|
|
45
|
+
assert ops.sub(5, 1) == 4
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_session_roundtrip():
|
|
49
|
+
assert ops.pack(1, 2) is not None
|
|
50
|
+
assert ops.unpack(1, 2) is not None
|
|
51
|
+
assert ops.wrap(1, 2) is not None
|
|
52
|
+
assert ops.unwrap(1, 2) is not None
|
|
53
|
+
'''
|
|
54
|
+
SHIFT = "def shift(x, y):\n return x + y\n"
|
|
55
|
+
SHIFT_TESTS = '''from demo import shift
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_parse_invalid():
|
|
59
|
+
assert shift.shift(1, 2) == 3
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_shift_other():
|
|
63
|
+
assert shift.shift(2, 2) == 4
|
|
64
|
+
'''
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def replaced(path, source, tests):
|
|
68
|
+
repo = build_repo(path)
|
|
69
|
+
for old in [*(repo / "tests").glob("test_*.py"), *(repo / "demo").glob("[!_]*.py")]:
|
|
70
|
+
old.unlink()
|
|
71
|
+
(repo / "demo" / source[0]).write_text(source[1])
|
|
72
|
+
(repo / "tests" / tests[0]).write_text(tests[1])
|
|
73
|
+
git(repo, "add", "-A")
|
|
74
|
+
git(repo, "commit", "-q", "-m", "fixture")
|
|
75
|
+
return repo
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def sample_repo(path):
|
|
79
|
+
return replaced(path, ("parser.py", PARSER), ("test_parser.py", SAMPLE_TESTS))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def matrix_repo(path):
|
|
83
|
+
return replaced(path, ("ops.py", OPS), ("test_ops.py", MATRIX_TESTS))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def shift_repo(path):
|
|
87
|
+
return replaced(path, ("shift.py", SHIFT), ("test_shift.py", SHIFT_TESTS))
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Fixture suites for ticket tw/s5-smells/10, built on the default repo with its tests replaced by a controlled set.
|
|
2
|
+
|
|
3
|
+
Every suite has exactly 40 tests, all passing unless a docstring says otherwise. The filler tests are structurally
|
|
4
|
+
distinct from each other (test_fill_NN asserts `board.render(1) == "1"` plus NN empty-string additions), so they never
|
|
5
|
+
form a duplicate-body cluster and carry none of the six smells. test_pick covers both arms of demo/pick.py.
|
|
6
|
+
|
|
7
|
+
smell_suite: tests/test_maint.py test_flow_2 (2 act-then-assert cycles), test_flow_3 (3), test_parse_all (for over a
|
|
8
|
+
literal list), test_load (try/except Exception: pass)
|
|
9
|
+
tests/test_guest.py test_a (opens tmp_path / "x.json"), test_b (opens "~/.cache/app.json", a file
|
|
10
|
+
committed at that relative path)
|
|
11
|
+
tests/test_sleep.py test_a (time.sleep(0)), test_b (time.sleep(2))
|
|
12
|
+
cluster_suite: tests/test_intent.py test_intent_1..4 (differ only in a string literal and a variable name) and
|
|
13
|
+
test_cousin (same shape, calls str instead of board.render: it must not join the cluster)
|
|
14
|
+
summary_suite: tests/test_dups.py 12 clones; tests/test_retry.py test_retry (flaky: passes plain, fails under coverage)
|
|
15
|
+
perfect_suite: fillers and test_pick only: 0% hollow, 100% branch coverage, 0% duplicate clusters
|
|
16
|
+
clone_suite: 40 clones: 100% duplicate-body cluster, 100% without unique coverage
|
|
17
|
+
"""
|
|
18
|
+
import re
|
|
19
|
+
|
|
20
|
+
from fixture_repo import build_repo, git
|
|
21
|
+
|
|
22
|
+
PICK = "def pick(x):\n if x:\n return 1\n return 2\n"
|
|
23
|
+
PICK_TEST = "def test_pick():\n assert pick.pick(True) + pick.pick(False) == 3\n"
|
|
24
|
+
HEADER = "from demo import board, pick\n\n\n"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def filler(k):
|
|
28
|
+
return f'def test_fill_{k:02d}():\n assert board.render(1) == "1"' + ' + ""' * k + "\n"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def clone(i):
|
|
32
|
+
return f'def test_clone_{i:02d}():\n v{i} = board.render(1)\n assert v{i} in "1 {i}"\n'
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def suite(path, files, total=40):
|
|
36
|
+
repo = build_repo(path)
|
|
37
|
+
for old in (repo / "tests").glob("test_*.py"):
|
|
38
|
+
old.unlink()
|
|
39
|
+
(repo / "demo" / "config.py").unlink()
|
|
40
|
+
(repo / "demo" / "pick.py").write_text(PICK)
|
|
41
|
+
own = 0
|
|
42
|
+
for rel, text in files.items():
|
|
43
|
+
(repo / rel).parent.mkdir(parents=True, exist_ok=True)
|
|
44
|
+
(repo / rel).write_text(text)
|
|
45
|
+
own += len(re.findall(r"^def test_", text, re.M))
|
|
46
|
+
fillers = [filler(k) for k in range(total - own - 1)]
|
|
47
|
+
(repo / "tests" / "test_fill.py").write_text(HEADER + "\n\n".join(fillers + [PICK_TEST]))
|
|
48
|
+
git(repo, "add", "-A")
|
|
49
|
+
git(repo, "commit", "-q", "-m", "suite")
|
|
50
|
+
return repo
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
MAINT = '''from demo import board
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_flow_2():
|
|
57
|
+
first = board.render(1)
|
|
58
|
+
assert first == "1"
|
|
59
|
+
second = board.render(2)
|
|
60
|
+
assert second == "2"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_flow_3():
|
|
64
|
+
first = board.render(1)
|
|
65
|
+
assert first == "1"
|
|
66
|
+
second = board.render(2)
|
|
67
|
+
assert second == "2"
|
|
68
|
+
third = board.render(3)
|
|
69
|
+
assert third == "3"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_parse_all():
|
|
73
|
+
for s in ["a", "b"]:
|
|
74
|
+
assert board.render(s) == s
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def test_load():
|
|
78
|
+
try:
|
|
79
|
+
board.render(1)
|
|
80
|
+
except Exception:
|
|
81
|
+
pass
|
|
82
|
+
'''
|
|
83
|
+
GUEST = '''import json
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_a(tmp_path):
|
|
87
|
+
path = tmp_path / "x.json"
|
|
88
|
+
path.write_text("{}")
|
|
89
|
+
with open(path) as handle:
|
|
90
|
+
assert json.load(handle) == {}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_b():
|
|
94
|
+
with open("~/.cache/app.json") as handle:
|
|
95
|
+
assert json.load(handle) == {}
|
|
96
|
+
'''
|
|
97
|
+
SLEEP = '''import time
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_a():
|
|
101
|
+
time.sleep(0)
|
|
102
|
+
assert time.monotonic() > 0
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_b():
|
|
106
|
+
time.sleep(2)
|
|
107
|
+
assert time.monotonic() > 0
|
|
108
|
+
'''
|
|
109
|
+
INTENT = "from demo import board\n\n\n" + "\n\n".join(
|
|
110
|
+
f'def test_intent_{i}():\n {name} = board.render(1)\n assert {name} in "1 {lit}"\n'
|
|
111
|
+
for i, name, lit in ((1, "first", "a"), (2, "second", "b"), (3, "third", "c"), (4, "fourth", "d"))
|
|
112
|
+
) + '\n\ndef test_cousin():\n fifth = str(1)\n assert fifth in "1 e"\n'
|
|
113
|
+
RETRY = 'import sys\n\n\ndef test_retry():\n assert "coverage" not in sys.modules\n'
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def smell_suite(path):
|
|
117
|
+
repo = suite(path, {"tests/test_maint.py": MAINT, "tests/test_guest.py": GUEST, "tests/test_sleep.py": SLEEP})
|
|
118
|
+
(repo / "~" / ".cache").mkdir(parents=True)
|
|
119
|
+
(repo / "~" / ".cache" / "app.json").write_text("{}")
|
|
120
|
+
git(repo, "add", "-A")
|
|
121
|
+
git(repo, "commit", "-q", "-m", "guest file")
|
|
122
|
+
return repo
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def cluster_suite(path):
|
|
126
|
+
return suite(path, {"tests/test_intent.py": INTENT})
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def summary_suite(path):
|
|
130
|
+
dups = "from demo import board\n\n\n" + "\n\n".join(clone(i) for i in range(12))
|
|
131
|
+
return suite(path, {"tests/test_dups.py": dups, "tests/test_retry.py": RETRY})
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def perfect_suite(path):
|
|
135
|
+
return suite(path, {})
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def clone_suite(path):
|
|
139
|
+
return suite(path, {"tests/test_clone.py": "from demo import board\n\n\n" + "\n\n".join(clone(i) for i in range(39))})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Ticket tw/s4-coverage/08: render dashboard.html from a fixture snapshot (no repo run): `map_fixture.py <out-dir>`.
|
|
2
|
+
|
|
3
|
+
Ten passing tests over demo/a.py. test_intent_1 and its twins test_twin_1..3 cover lines 1-10 (similarity 1.0);
|
|
4
|
+
test_other_1..4 cover lines 1-4 (similarity 0.4 with each of those four: 4 shared of 10 in the union).
|
|
5
|
+
test_edge_at covers lines 1-6 (similarity exactly 0.6 with test_intent_1), test_edge_below lines 1-5 (0.5 with it).
|
|
6
|
+
Overlaps (Jaccard >= 0.9) holds the 6 pairs among test_intent_1 and its twins and the 6 among test_other_1..4, each 1.0;
|
|
7
|
+
no pair with an edge test reaches 0.9 (at most 5 of 6 lines).
|
|
8
|
+
Writes the fixture snapshot to <out-dir>/snapshot.json and the page to <out-dir>/dashboard.html.
|
|
9
|
+
"""
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
15
|
+
|
|
16
|
+
from weigh_dashboard import write_dashboard
|
|
17
|
+
from weigh_rules import load
|
|
18
|
+
from weigh_score import feedback_values, score
|
|
19
|
+
|
|
20
|
+
WIDE = list(range(1, 11))
|
|
21
|
+
NARROW = list(range(1, 5))
|
|
22
|
+
COVERAGE = {
|
|
23
|
+
"tests/test_intent.py::test_intent_1": WIDE,
|
|
24
|
+
"tests/test_twin.py::test_twin_1": WIDE,
|
|
25
|
+
"tests/test_twin.py::test_twin_2": WIDE,
|
|
26
|
+
"tests/test_twin.py::test_twin_3": WIDE,
|
|
27
|
+
"tests/test_other.py::test_other_1": NARROW,
|
|
28
|
+
"tests/test_other.py::test_other_2": NARROW,
|
|
29
|
+
"tests/test_other.py::test_other_3": NARROW,
|
|
30
|
+
"tests/test_other.py::test_other_4": NARROW,
|
|
31
|
+
"tests/test_edge.py::test_edge_at": list(range(1, 7)),
|
|
32
|
+
"tests/test_edge.py::test_edge_below": list(range(1, 6)),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def jaccard(a, b):
|
|
37
|
+
return len(set(a) & set(b)) / len(set(a) | set(b))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def build(out):
|
|
41
|
+
rules = load()
|
|
42
|
+
tests = {n: {"outcome": "passed", "duration": 0.05 * (i + 1), "coverage": {"demo/a.py": lines}, "unique_coverage": 0, "flaky": False, "smells": [], "lines": 2}
|
|
43
|
+
for i, (n, lines) in enumerate(COVERAGE.items())}
|
|
44
|
+
metrics, pillars = score(rules, {**feedback_values(tests, 1.0), "no_unique_coverage": 100.0})
|
|
45
|
+
names = list(COVERAGE)
|
|
46
|
+
pairs = [{"tests": [a, b], "jaccard": j} for i, a in enumerate(names) for b in names[i + 1:]
|
|
47
|
+
if (j := jaccard(COVERAGE[a], COVERAGE[b])) >= 0.9]
|
|
48
|
+
data = {"commit": "abc1234", "dirty": False, "inventory": {"tests": 10, "cases": 10, "test_files": 3, "test_lines": 24, "source_lines": 10, "runtime_seconds": 1.0},
|
|
49
|
+
"metrics": metrics, "pillars": pillars, "tests": tests, "overlaps": pairs}
|
|
50
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
51
|
+
(out / "snapshot.json").write_text(json.dumps(data))
|
|
52
|
+
write_dashboard(out, data, rules)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if __name__ == "__main__":
|
|
56
|
+
build(Path(sys.argv[1]))
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Fixture suite for ticket tw/s5-smells/11: the default repo's tests replaced by a controlled set of exactly 40 tests.
|
|
2
|
+
|
|
3
|
+
pt_suite: tests/test_pt.py test_raises pytest.raises(Exception) without match (PT011): the only smell
|
|
4
|
+
test_board uses the fixture board_rows, declared `@pytest.fixture()` (PT001, style-only)
|
|
5
|
+
test_uses_helper calls expect_failure(), a helper outside any test that holds a PT011
|
|
6
|
+
ruff.toml the target repo's own ruff configuration: selects PT001 and ignores PT011. It must change
|
|
7
|
+
nothing: PT011 still counts and PT001 still does not.
|
|
8
|
+
Every other test is a filler with no smell.
|
|
9
|
+
"""
|
|
10
|
+
from fixture_repo import git
|
|
11
|
+
from maint_repo import suite
|
|
12
|
+
|
|
13
|
+
PT = '''import pytest
|
|
14
|
+
|
|
15
|
+
from demo import board
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.fixture()
|
|
19
|
+
def board_rows():
|
|
20
|
+
return [1]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def expect_failure():
|
|
24
|
+
with pytest.raises(Exception):
|
|
25
|
+
int("x")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_raises():
|
|
29
|
+
with pytest.raises(Exception):
|
|
30
|
+
board.render(int("x"))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_board(board_rows):
|
|
34
|
+
assert board.render(board_rows[0]) == "1"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_uses_helper():
|
|
38
|
+
expect_failure()
|
|
39
|
+
assert board.render(2) == "2"
|
|
40
|
+
'''
|
|
41
|
+
RUFF = '[lint]\nselect = ["PT001"]\nignore = ["PT011"]\n'
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def pt_suite(path):
|
|
45
|
+
repo = suite(path, {"tests/test_pt.py": PT, "ruff.toml": RUFF})
|
|
46
|
+
return repo
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Fixture suites that are not green, built on the default repo (40 tests), for the coverage-run tests.
|
|
2
|
+
|
|
3
|
+
red_suite: 3 of 40 tests fail (test_board_3, test_board_4, test_pick_false). test_pick_false is the only test that
|
|
4
|
+
takes the false arm of demo/pick.py, so a suite coverage that counts failing tests would read 100 and not 50.
|
|
5
|
+
skipped_suite: 4 of 40 tests are skipped.
|
|
6
|
+
"""
|
|
7
|
+
from fixture_repo import build_repo, git
|
|
8
|
+
|
|
9
|
+
FAILING = ("tests/test_board.py::test_board_3", "tests/test_board.py::test_board_4", "tests/test_render.py::test_pick_false")
|
|
10
|
+
SKIPPED = ("tests/test_config.py::test_config_2", "tests/test_config.py::test_config_3",
|
|
11
|
+
"tests/test_config.py::test_config_4", "tests/test_render.py::test_render_2")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _commit(repo):
|
|
15
|
+
git(repo, "add", "-A")
|
|
16
|
+
git(repo, "commit", "-q", "-m", "suite")
|
|
17
|
+
return repo
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _edit(repo, rel, old, new):
|
|
21
|
+
path = repo / rel
|
|
22
|
+
path.write_text(path.read_text().replace(old, new))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def red_suite(path):
|
|
26
|
+
repo = build_repo(path)
|
|
27
|
+
(repo / "demo" / "pick.py").write_text("def pick(x):\n if x:\n return 1\n return 2\n")
|
|
28
|
+
render = (repo / "tests" / "test_render.py").read_text()
|
|
29
|
+
kept = render[render.index("def test_render_2"):]
|
|
30
|
+
(repo / "tests" / "test_render.py").write_text(
|
|
31
|
+
"import pytest\nfrom demo import board, config, pick\n"
|
|
32
|
+
"def test_pick_true():\n assert pick.pick(True) == 1\n"
|
|
33
|
+
"def test_pick_false():\n assert pick.pick(False) == 1\n" + kept)
|
|
34
|
+
for n in (3, 4):
|
|
35
|
+
_edit(repo, "tests/test_board.py", f'def test_board_{n}():\n assert board.render({n}) == "{n}"',
|
|
36
|
+
f"def test_board_{n}():\n assert board.render({n}) == 'x'")
|
|
37
|
+
return _commit(repo)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def skipped_suite(path):
|
|
41
|
+
repo = build_repo(path)
|
|
42
|
+
for rel, n in (("test_config", 2), ("test_config", 3), ("test_config", 4), ("test_render", 2)):
|
|
43
|
+
_edit(repo, f"tests/{rel}.py", f"def {rel}_{n}():", f"@pytest.mark.skip\ndef {rel}_{n}():")
|
|
44
|
+
return _commit(repo)
|