From 8b55ab45a55e2e545e16b10de44a4a92e7b855da Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:57:48 +0100 Subject: [PATCH] fix: suggest the database beside a mistyped --db --db databse.json, with database.json in the same folder, was told to run generate_db --force --output databse.json: a rescan of the whole collection into a second file under the typo. load_database now names the closest .json in that folder first, and gives the rebuild command only when none is close. --- scripts/common.py | 12 ++++++++++++ tests/test_audit_regressions.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/scripts/common.py b/scripts/common.py index 80eb59564..3fdccc704 100644 --- a/scripts/common.py +++ b/scripts/common.py @@ -73,6 +73,18 @@ def load_database(db_path: str) -> dict: with open(db_path) as f: return json.load(f) except FileNotFoundError: + # A mistyped --db beside the real database was told to rescan the + # whole collection into a second file under the typo. + import difflib # noqa: PLC0415 + + typed = Path(db_path) + close = difflib.get_close_matches( + typed.name, [p.name for p in typed.parent.glob("*.json")], n=1, cutoff=0.8 + ) + if close: + raise DatabaseUnreadable( + f"{db_path} not found. Did you mean {typed.with_name(close[0])}?" + ) from None raise DatabaseUnreadable( f"{db_path} not found. Build it with: python scripts/generate_db.py " f"--force --bios-dir bios --output {db_path}" diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index e5a26871f..f2664c89f 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -6060,6 +6060,20 @@ class TheRepairCommandWritesTheFileNamed(unittest.TestCase): load_database(target) self.assertIn(f"--output {target}", str(caught.exception)) + def test_a_typo_beside_the_database_names_the_database(self): + """--db databse.json beside database.json was told to rescan the whole + collection into a second file named after the typo.""" + from scripts.common import DatabaseUnreadable, load_database + + with tempfile.TemporaryDirectory(dir=TMP_ROOT) as directory: + (Path(directory) / "database.json").write_text("{}") + typo = Path(directory) / "databse.json" + with self.assertRaises(DatabaseUnreadable) as caught: + load_database(str(typo)) + message = str(caught.exception) + self.assertIn(f"Did you mean {typo.with_name('database.json')}?", message) + self.assertNotIn("generate_db.py", message) + class VerifyPacksRefusesWhatItCannotDo(unittest.TestCase): """The --verify-packs refusals lived in the --all-variants checks, which