Files
FabledGrimoire/tests/test_measure_duplication.py
bvandeusenandClaude Opus 5.5 914f31359c
CI & Build / Python lint (push) Successful in 5s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / TypeScript typecheck (push) Successful in 57s
CI & Build / integration (push) Successful in 1m19s
CI & Build / Python tests (push) Successful in 2m2s
CI & Build / Build & push image (push) Successful in 15s
docs(6209): the remaining test files — lessons, reply moment and shape, retrieval specs and scopes, rule doors and judgments, services, snippets, search scope; five tests renamed to their property
Docstrings, section headers and assertion messages drop the record number,
milestone or step that introduced the behaviour, and incident accounts are
restated as the present-tense failure shape. A dead pointer to a design doc
that is not in the repo is removed.

Renamed to the property they check:
  test_the_old_shape_would_be_caught -> test_a_rank_then_scope_query_is_caught
  test_a_ledger_repeat_no_longer_produces_that_case_at_all -> test_a_ledger_repeat_is_counted_not_withheld
  test_the_header_no_longer_promises_once_per_session -> test_the_header_explains_the_seen_marker
  test_the_portal_case_no_longer_stamps_one_canon_onto_a_whole_file -> test_one_canon_is_not_stamped_onto_a_whole_file
  test_the_repos_own_prose_no_longer_defines_with -> test_the_repos_own_prose_does_not_define_with
  test_the_original_regression_create_note_with_content -> test_the_real_server_refuses_content_on_create_note

Fixture and example ids stay: "note 5" in a supersession chain, "rule 156"
in an illustrative question, `#2275` as the hex-versus-citation case,
`#3244` as the example of a bare number, fixed dates in date-filter tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 21:44:58 -04:00

129 lines
4.5 KiB
Python

"""The DRY close-out measure.
Stdlib-only and kept in scripts/ so any project can run a scratch copy of it,
so these tests import it by path rather than as a package.
What matters is the after-only list: it is the part of the measure that finds
something a pass could not, so it must name a copy the change CREATED and stay
quiet about one that was already there. A list that repeats old duplication
every time is one nobody reads.
"""
import importlib.util
import pathlib
import shutil
import subprocess
import pytest
_PATH = pathlib.Path(__file__).resolve().parents[1] / "scripts" / "measure_duplication.py"
_spec = importlib.util.spec_from_file_location("measure_duplication", _PATH)
dup = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(dup)
BODY = """
def load(user_id, note_id):
# a comment the measure ignores
if not allowed(user_id, note_id):
raise ValueError("note {} not found".format(note_id))
with session() as s:
row = s.get(Row, note_id)
if row is None:
raise ValueError("not a row")
return row
"""
def _files(**texts):
return [(name, text.encode()) for name, text in texts.items()]
def test_comments_blanks_and_bare_punctuation_are_not_code():
lines = dup.significant_lines('x = 1\n\n// note\n# note\n });\n-- sql note\ny = "two"\n')
assert [t for _, t in lines] == ["x = 1", 'y = "S"']
assert [n for n, _ in lines] == [1, 7]
def test_a_preprocessor_line_is_code_and_a_hash_comment_is_not():
lines = dup.significant_lines("#include <x.h>\n# a comment\n#!/bin/sh\n")
assert [t for _, t in lines] == ["#include <x.h>"]
def test_copies_that_differ_only_in_their_strings_are_one_copy():
other = BODY.replace('"not a row"', '"no such row"')
result = dup.measure(_files(**{"a.py": BODY, "b.py": other}), [], [], window=6)
py = result["py"]
assert py["dup_windows"] > 0
assert py["dup_lines"] == py["lines"]
assert py["share"] == 1.0
def test_unrelated_files_measure_zero():
other = "\n".join(f"v{i} = compute({i})" for i in range(20))
result = dup.measure(_files(**{"a.py": BODY, "b.py": other}), [], [], window=6)
assert result["py"]["dup_lines"] == 0
def test_the_first_matching_group_claims_a_file():
groups = [("tests", ["tests/*"]), ("py", ["*.py"])]
assert dup.group_of("tests/test_x.py", groups) == "tests"
assert dup.group_of("src/x.py", groups) == "py"
assert dup.group_of("README.md", groups) is None
def test_without_groups_only_source_extensions_are_measured():
assert dup.group_of("src/x.go", []) == "go"
assert dup.group_of("docs/x.md", []) is None
assert dup.group_of("Makefile", []) is None
def test_excluded_paths_are_not_measured():
result = dup.measure(
_files(**{"a.py": BODY, "gen/b.py": BODY}), [], ["gen/*"], window=6,
)
assert result["py"]["dup_lines"] == 0
def test_a_copy_the_change_created_is_listed_after_only():
before = dup.measure(_files(**{"a.py": BODY, "c.py": "z = 1\n"}), [], [], window=6)
after = dup.measure(_files(**{"a.py": BODY, "c.py": BODY}), [], [], window=6)
fresh = dup.only_after(before, after)
assert [e["files"] for e in fresh["py"]] == [["a.py", "c.py"]]
assert fresh["py"][0]["at"] == ["a.py:2", "c.py:2"]
def test_a_copy_that_was_already_there_is_not_listed():
both = _files(**{"a.py": BODY, "b.py": BODY})
before = dup.measure(both, [], [], window=6)
after = dup.measure(both, [], [], window=6)
assert dup.only_after(before, after) == {}
def test_the_report_shows_before_and_after_side_by_side():
before = dup.measure(_files(**{"a.py": BODY}), [], [], window=6)
after = dup.measure(_files(**{"a.py": BODY, "b.py": BODY}), [], [], window=6)
text = dup.report(before, after, dup.only_after(before, after), show=5)
assert "0.0% → 100.0%" in text
assert "a.py:2, b.py:2" in text
@pytest.mark.skipif(shutil.which("git") is None, reason="needs git")
def test_a_revision_is_read_without_a_checkout(tmp_path, capsys):
def git(*args):
subprocess.run(
["git", "-C", str(tmp_path), "-c", "user.name=t", "-c", "user.email=t@t", *args],
check=True, capture_output=True,
)
git("init", "-q")
(tmp_path / "a.py").write_text(BODY)
git("add", "a.py")
git("commit", "-qm", "one copy")
(tmp_path / "b.py").write_text(BODY)
git("add", "b.py")
assert dup.main(["--repo", str(tmp_path), "--before", "HEAD"]) == 0
out = capsys.readouterr().out
assert "Duplicated only after: 1 file set(s)" in out
assert "a.py:2, b.py:2" in out