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.
This commit is contained in:
kshitij 2026-08-08 14:38:17 +05:30
parent 39db9d1114
commit df0a5c3ee4
2 changed files with 9 additions and 3 deletions

View File

@ -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(

View File

@ -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):