Skip to content

Commit 3cae143

Browse files
committed
refactor: remove dead code and reduce test duplication
- Remove vestigial _lock_loader_for() and unused Callable/TYPE_CHECKING imports from cli.py - Extract _make_summary() and _write_report_files() helpers in test_diff.py - Add timestamp constants to eliminate repeated string literals
1 parent 54bbfc7 commit 3cae143

2 files changed

Lines changed: 75 additions & 132 deletions

File tree

src/ftready/cli.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import logging
66
import sys
77
from pathlib import Path
8-
from typing import TYPE_CHECKING
98

109
import rich_click as click
1110

@@ -16,9 +15,6 @@
1615
from ftready.report import generate_report
1716
from ftready.scraper import fetch_ftchecker_db
1817

19-
if TYPE_CHECKING:
20-
from collections.abc import Callable
21-
2218
_logger = logging.getLogger(__name__)
2319

2420
_LOCK_FILE_NAMES = ("uv.lock", "poetry.lock", "pdm.lock")
@@ -33,13 +29,6 @@ def _find_lock_file(project_dir: Path) -> Path | None:
3329
return None
3430

3531

36-
def _lock_loader_for(
37-
lock_path: Path, # noqa: ARG001
38-
) -> Callable[[Path, set[str]], dict[str, str]]:
39-
"""Return the lock-file parser (all formats share the same ``[[package]]`` structure)."""
40-
return load_lockfile_dependencies
41-
42-
4332
def _resolve_deps(
4433
*,
4534
requirements: Path | None,
@@ -82,8 +71,7 @@ def _resolve_deps(
8271
msg = f"{lock_path} not found."
8372
raise click.UsageError(msg)
8473
_logger.info("[ftready] Reading all deps from %s …", lock_path)
85-
loader = _lock_loader_for(lock_path)
86-
deps = loader(lock_path, direct_names)
74+
deps = load_lockfile_dependencies(lock_path, direct_names)
8775
transitive = len(deps) - len(direct_names)
8876
_logger.info(
8977
"[ftready] Found %d packages in lock file (%d direct, %d transitive).",

tests/test_diff.py

Lines changed: 74 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from ftready.cli import main
1111
from ftready.diff import DiffSummary, PackageDiff, diff_reports, format_diff, format_diff_json
1212

13+
_OLD_TS = "2025-01-01T00:00:00+00:00"
14+
_NEW_TS = "2025-02-01T00:00:00+00:00"
15+
1316

1417
def _make_report(
1518
packages: list[dict],
@@ -18,7 +21,7 @@ def _make_report(
1821
ok_314: int = 0,
1922
fail_313: int = 0,
2023
fail_314: int = 0,
21-
generated_at: str = "2025-01-01T00:00:00+00:00",
24+
generated_at: str = _OLD_TS,
2225
) -> dict:
2326
return {
2427
"generated_at": generated_at,
@@ -49,82 +52,88 @@ def _pkg(name: str, s313: str = "Success", s314: str = "Success") -> dict:
4952
}
5053

5154

55+
def _make_summary(
56+
packages: list[PackageDiff],
57+
*,
58+
old_ok_313: int = 0,
59+
new_ok_313: int = 0,
60+
old_ok_314: int = 0,
61+
new_ok_314: int = 0,
62+
) -> DiffSummary:
63+
"""Build a DiffSummary with default timestamps."""
64+
return DiffSummary(
65+
old_generated_at="2025-01-01",
66+
new_generated_at="2025-02-01",
67+
packages=packages,
68+
old_ok_313=old_ok_313,
69+
new_ok_313=new_ok_313,
70+
old_ok_314=old_ok_314,
71+
new_ok_314=new_ok_314,
72+
)
73+
74+
75+
def _write_report_files(
76+
tmp_path: Path,
77+
old: dict,
78+
new: dict,
79+
) -> tuple[Path, Path]:
80+
"""Write old/new JSON reports to tmp_path and return their paths."""
81+
old_file = tmp_path / "old.json"
82+
new_file = tmp_path / "new.json"
83+
old_file.write_text(json.dumps(old))
84+
new_file.write_text(json.dumps(new))
85+
return old_file, new_file
86+
87+
5288
class TestDiffReports:
5389
def test_no_changes(self):
5490
old = _make_report([_pkg("numpy")], ok_313=1, ok_314=1)
55-
new = _make_report([_pkg("numpy")], ok_313=1, ok_314=1, generated_at="2025-02-01T00:00:00+00:00")
91+
new = _make_report([_pkg("numpy")], ok_313=1, ok_314=1, generated_at=_NEW_TS)
5692
summary = diff_reports(old, new)
5793
assert len(summary.changed) == 0
5894

5995
def test_status_change_detected(self):
60-
old = _make_report([_pkg("numpy", "Not tested", "Not tested")], ok_313=0, ok_314=0)
61-
new = _make_report(
62-
[_pkg("numpy", "Success", "Success")],
63-
ok_313=1,
64-
ok_314=1,
65-
generated_at="2025-02-01T00:00:00+00:00",
66-
)
96+
old = _make_report([_pkg("numpy", "Not tested", "Not tested")])
97+
new = _make_report([_pkg("numpy", "Success", "Success")], ok_313=1, ok_314=1, generated_at=_NEW_TS)
6798
summary = diff_reports(old, new)
6899
assert len(summary.changed) == 1
69100
assert summary.changed[0].name == "numpy"
70101
assert summary.changed[0].new_313t == "Success"
71102

72103
def test_added_package(self):
73104
old = _make_report([_pkg("numpy")])
74-
new = _make_report(
75-
[_pkg("numpy"), _pkg("pandas")],
76-
ok_313=2,
77-
ok_314=2,
78-
generated_at="2025-02-01T00:00:00+00:00",
79-
)
105+
new = _make_report([_pkg("numpy"), _pkg("pandas")], ok_313=2, ok_314=2, generated_at=_NEW_TS)
80106
summary = diff_reports(old, new)
81107
added = [p for p in summary.changed if p.added]
82108
assert len(added) == 1
83109
assert added[0].name == "pandas"
84110

85111
def test_removed_package(self):
86112
old = _make_report([_pkg("numpy"), _pkg("pandas")], ok_313=2)
87-
new = _make_report([_pkg("numpy")], ok_313=1, generated_at="2025-02-01T00:00:00+00:00")
113+
new = _make_report([_pkg("numpy")], ok_313=1, generated_at=_NEW_TS)
88114
summary = diff_reports(old, new)
89115
removed = [p for p in summary.changed if p.removed]
90116
assert len(removed) == 1
91117
assert removed[0].name == "pandas"
92118

93119
def test_improved_property(self):
94120
old = _make_report([_pkg("numpy", "Not tested", "Not tested")])
95-
new = _make_report(
96-
[_pkg("numpy", "Success", "Not tested")],
97-
ok_313=1,
98-
generated_at="2025-02-01T00:00:00+00:00",
99-
)
121+
new = _make_report([_pkg("numpy", "Success", "Not tested")], ok_313=1, generated_at=_NEW_TS)
100122
summary = diff_reports(old, new)
101123
assert len(summary.improved) == 1
102124
assert summary.improved[0].name == "numpy"
103125

104126
def test_regressed_property(self):
105127
old = _make_report([_pkg("numpy", "Success", "Success")], ok_313=1, ok_314=1)
106-
new = _make_report(
107-
[_pkg("numpy", "Failed", "Success")],
108-
ok_314=1,
109-
fail_313=1,
110-
generated_at="2025-02-01T00:00:00+00:00",
111-
)
128+
new = _make_report([_pkg("numpy", "Failed", "Success")], ok_314=1, fail_313=1, generated_at=_NEW_TS)
112129
summary = diff_reports(old, new)
113130
assert len(summary.regressed) == 1
114131
assert summary.regressed[0].name == "numpy"
115132

116133
def test_reads_from_files(self, tmp_path: Path):
117134
old = _make_report([_pkg("numpy", "Not tested", "Not tested")])
118-
new = _make_report(
119-
[_pkg("numpy", "Success", "Success")],
120-
ok_313=1,
121-
ok_314=1,
122-
generated_at="2025-02-01T00:00:00+00:00",
123-
)
124-
old_file = tmp_path / "old.json"
125-
new_file = tmp_path / "new.json"
126-
old_file.write_text(json.dumps(old))
127-
new_file.write_text(json.dumps(new))
135+
new = _make_report([_pkg("numpy", "Success", "Success")], ok_313=1, ok_314=1, generated_at=_NEW_TS)
136+
old_file, new_file = _write_report_files(tmp_path, old, new)
128137
summary = diff_reports(str(old_file), str(new_file))
129138
assert len(summary.changed) == 1
130139

@@ -149,83 +158,53 @@ def test_changed_when_removed(self):
149158

150159
class TestFormatDiff:
151160
def test_no_changes_message(self):
152-
summary = DiffSummary(
153-
old_generated_at="2025-01-01",
154-
new_generated_at="2025-02-01",
155-
packages=[PackageDiff("numpy", "Success", "Success", "Success", "Success")],
156-
old_ok_313=1,
157-
new_ok_313=1,
158-
old_ok_314=1,
159-
new_ok_314=1,
161+
summary = _make_summary(
162+
[PackageDiff("numpy", "Success", "Success", "Success", "Success")],
163+
old_ok_313=1, new_ok_313=1, old_ok_314=1, new_ok_314=1,
160164
)
161165
output = format_diff(summary)
162166
assert "No changes" in output
163167

164168
def test_improved_section_shown(self):
165-
summary = DiffSummary(
166-
old_generated_at="2025-01-01",
167-
new_generated_at="2025-02-01",
168-
packages=[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Not tested")],
169-
old_ok_313=0,
169+
summary = _make_summary(
170+
[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Not tested")],
170171
new_ok_313=1,
171-
old_ok_314=0,
172-
new_ok_314=0,
173172
)
174173
output = format_diff(summary)
175174
assert "Improved" in output
176175
assert "numpy" in output
177176

178177
def test_regressed_section_shown(self):
179-
summary = DiffSummary(
180-
old_generated_at="2025-01-01",
181-
new_generated_at="2025-02-01",
182-
packages=[PackageDiff("numpy", "Success", "Failed", "Success", "Success")],
183-
old_ok_313=1,
184-
new_ok_313=0,
185-
old_ok_314=1,
186-
new_ok_314=1,
178+
summary = _make_summary(
179+
[PackageDiff("numpy", "Success", "Failed", "Success", "Success")],
180+
old_ok_313=1, old_ok_314=1, new_ok_314=1,
187181
)
188182
output = format_diff(summary)
189183
assert "Regressed" in output
190184
assert "numpy" in output
191185

192186
def test_added_section_shown(self):
193-
summary = DiffSummary(
194-
old_generated_at="2025-01-01",
195-
new_generated_at="2025-02-01",
196-
packages=[PackageDiff("pandas", "", "Success", "", "Success", added=True)],
197-
old_ok_313=0,
198-
new_ok_313=1,
199-
old_ok_314=0,
200-
new_ok_314=1,
187+
summary = _make_summary(
188+
[PackageDiff("pandas", "", "Success", "", "Success", added=True)],
189+
new_ok_313=1, new_ok_314=1,
201190
)
202191
output = format_diff(summary)
203192
assert "Added" in output
204193
assert "pandas" in output
205194

206195
def test_removed_section_shown(self):
207-
summary = DiffSummary(
208-
old_generated_at="2025-01-01",
209-
new_generated_at="2025-02-01",
210-
packages=[PackageDiff("pandas", "Success", "", "Success", "", removed=True)],
211-
old_ok_313=1,
212-
new_ok_313=0,
213-
old_ok_314=1,
214-
new_ok_314=0,
196+
summary = _make_summary(
197+
[PackageDiff("pandas", "Success", "", "Success", "", removed=True)],
198+
old_ok_313=1, old_ok_314=1,
215199
)
216200
output = format_diff(summary)
217201
assert "Removed" in output
218202
assert "pandas" in output
219203

220204
def test_delta_counts_shown(self):
221-
summary = DiffSummary(
222-
old_generated_at="2025-01-01",
223-
new_generated_at="2025-02-01",
224-
packages=[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Success")],
225-
old_ok_313=0,
226-
new_ok_313=1,
227-
old_ok_314=0,
228-
new_ok_314=1,
205+
summary = _make_summary(
206+
[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Success")],
207+
new_ok_313=1, new_ok_314=1,
229208
)
230209
output = format_diff(summary)
231210
assert "+1" in output
@@ -234,14 +213,9 @@ def test_delta_counts_shown(self):
234213

235214
class TestFormatDiffJson:
236215
def test_valid_json(self):
237-
summary = DiffSummary(
238-
old_generated_at="2025-01-01",
239-
new_generated_at="2025-02-01",
240-
packages=[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Not tested")],
241-
old_ok_313=0,
216+
summary = _make_summary(
217+
[PackageDiff("numpy", "Not tested", "Success", "Not tested", "Not tested")],
242218
new_ok_313=1,
243-
old_ok_314=0,
244-
new_ok_314=0,
245219
)
246220
output = format_diff_json(summary)
247221
data = json.loads(output)
@@ -250,17 +224,12 @@ def test_valid_json(self):
250224
assert data["summary"]["delta_313"] == 1
251225

252226
def test_json_only_includes_changed(self):
253-
summary = DiffSummary(
254-
old_generated_at="2025-01-01",
255-
new_generated_at="2025-02-01",
256-
packages=[
257-
PackageDiff("numpy", "Success", "Success", "Success", "Success"), # unchanged
258-
PackageDiff("pandas", "Not tested", "Success", "Not tested", "Not tested"), # changed
227+
summary = _make_summary(
228+
[
229+
PackageDiff("numpy", "Success", "Success", "Success", "Success"),
230+
PackageDiff("pandas", "Not tested", "Success", "Not tested", "Not tested"),
259231
],
260-
old_ok_313=1,
261-
new_ok_313=2,
262-
old_ok_314=1,
263-
new_ok_314=1,
232+
old_ok_313=1, new_ok_313=2, old_ok_314=1, new_ok_314=1,
264233
)
265234
output = format_diff_json(summary)
266235
data = json.loads(output)
@@ -271,16 +240,8 @@ def test_json_only_includes_changed(self):
271240
class TestDiffCLI:
272241
def test_diff_command(self, tmp_path: Path):
273242
old = _make_report([_pkg("numpy", "Not tested", "Not tested")])
274-
new = _make_report(
275-
[_pkg("numpy", "Success", "Success")],
276-
ok_313=1,
277-
ok_314=1,
278-
generated_at="2025-02-01T00:00:00+00:00",
279-
)
280-
old_file = tmp_path / "old.json"
281-
new_file = tmp_path / "new.json"
282-
old_file.write_text(json.dumps(old))
283-
new_file.write_text(json.dumps(new))
243+
new = _make_report([_pkg("numpy", "Success", "Success")], ok_313=1, ok_314=1, generated_at=_NEW_TS)
244+
old_file, new_file = _write_report_files(tmp_path, old, new)
284245
runner = CliRunner()
285246
result = runner.invoke(main, ["diff", str(old_file), str(new_file)])
286247
assert result.exit_code == 0
@@ -290,10 +251,7 @@ def test_diff_command(self, tmp_path: Path):
290251
def test_diff_json_output(self, tmp_path: Path):
291252
old = _make_report([_pkg("numpy", "Not tested", "Not tested")])
292253
new = _make_report([_pkg("numpy", "Success", "Success")], ok_313=1, ok_314=1)
293-
old_file = tmp_path / "old.json"
294-
new_file = tmp_path / "new.json"
295-
old_file.write_text(json.dumps(old))
296-
new_file.write_text(json.dumps(new))
254+
old_file, new_file = _write_report_files(tmp_path, old, new)
297255
runner = CliRunner()
298256
result = runner.invoke(main, ["diff", str(old_file), str(new_file), "--format", "json"])
299257
assert result.exit_code == 0
@@ -303,11 +261,8 @@ def test_diff_json_output(self, tmp_path: Path):
303261
def test_diff_output_to_file(self, tmp_path: Path):
304262
old = _make_report([_pkg("numpy")])
305263
new = _make_report([_pkg("numpy")])
306-
old_file = tmp_path / "old.json"
307-
new_file = tmp_path / "new.json"
264+
old_file, new_file = _write_report_files(tmp_path, old, new)
308265
out_file = tmp_path / "diff.txt"
309-
old_file.write_text(json.dumps(old))
310-
new_file.write_text(json.dumps(new))
311266
runner = CliRunner()
312267
result = runner.invoke(main, ["diff", str(old_file), str(new_file), "--output", str(out_file)])
313268
assert result.exit_code == 0

0 commit comments

Comments
 (0)