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.
This commit is contained in:
Abdessamad Derraz committed 2026-11-06 14:57:48 +01:00
1 parent de8f20f409
commit 8b55ab45a5
2 files changed
+26

No files matched your search

+12
View File
@@ -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}"
+14
View File
@@ -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