From df0a5c3ee4d619798c6220210facbf35411a0070 Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 8 Aug 2026 14:38:17 +0530 Subject: [PATCH] refactor(doctor): reuse backup's size formatter for database listings _format_db_size reimplemented human-readable size formatting two imports away from backup._format_size, which doctor already leans on for _QUICK_STATE_FILES. Delegate and keep only the stat-failure wrap. Sizes now scale units (KB/GB) instead of pinning everything to MB. --- hermes_cli/doctor.py | 8 ++++++-- tests/hermes_cli/test_doctor_journal_modes.py | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/hermes_cli/doctor.py b/hermes_cli/doctor.py index 3e35b67b6e96c..b6dd56c8780dc 100644 --- a/hermes_cli/doctor.py +++ b/hermes_cli/doctor.py @@ -142,11 +142,15 @@ def _read_journal_mode(db_path: Path) -> tuple[str | None, str | None]: def _format_db_size(db_path: Path) -> str: + # backup.py owns human-readable size formatting; reuse it (as with + # _QUICK_STATE_FILES above) and keep only the stat-failure wrap here. + from hermes_cli.backup import _format_size + try: - mb = db_path.stat().st_size / 1_048_576 + nbytes = db_path.stat().st_size except OSError: return "size unknown" - return f"{mb:.1f} MB" if mb >= 0.1 else "<0.1 MB" + return _format_size(nbytes) def _report_database_journal_modes( diff --git a/tests/hermes_cli/test_doctor_journal_modes.py b/tests/hermes_cli/test_doctor_journal_modes.py index 674d7023fecf5..acc56ff0ea2a7 100644 --- a/tests/hermes_cli/test_doctor_journal_modes.py +++ b/tests/hermes_cli/test_doctor_journal_modes.py @@ -8,6 +8,7 @@ read-only engine open creates -wal/-shm sidecar files next to a WAL database. """ import os +import re import sqlite3 import pytest @@ -257,7 +258,8 @@ class TestSizeAndRepairHint: _make_db(db, journal_mode="WAL") doctor._report_database_journal_modes(tmp_path, VULNERABLE) out = capsys.readouterr().out - assert "MB)" in out + # _format_size picks the unit (a fresh test DB is KB-scale). + assert re.search(r"\(\d[\d.]* [KMGT]?B\)", out) assert "To clear the exposure:" in out def test_no_repair_hint_when_nothing_is_exposed(self, tmp_path, capsys):