From 3da67a2f567f60902110ea8ea8a984a136ca8a9a Mon Sep 17 00:00:00 2001 From: Joshua-Ward1 Date: Thu, 22 Jan 2026 11:26:44 -0500 Subject: [PATCH] Fix disk-space boundary logic and add regression tests --- src/sysforge/checks/core.py | 4 +++- src/sysforge/cli.py | 3 +++ tests/test_checks.py | 12 ++++++++++++ tests/test_cli.py | 9 +++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/sysforge/checks/core.py b/src/sysforge/checks/core.py index 2d980e6..6271046 100644 --- a/src/sysforge/checks/core.py +++ b/src/sysforge/checks/core.py @@ -17,10 +17,12 @@ def run(self, *, disk_threshold: float = 0.10) -> CheckResult: usage = disk_usage_summary(Path.home()) percent_free = usage["percent_free"] + warn_limit = min(disk_threshold + WARN_THRESHOLD_MARGIN, 1.0) + if percent_free <= disk_threshold: status = "fail" message = f"Low disk space: {percent_free:.2%} free (<= {disk_threshold:.0%})" - elif percent_free <= disk_threshold + WARN_THRESHOLD_MARGIN: + elif percent_free <= warn_limit: status = "warn" message = f"Disk space is getting low: {percent_free:.2%} free" else: diff --git a/src/sysforge/cli.py b/src/sysforge/cli.py index 326ca90..c94ff93 100644 --- a/src/sysforge/cli.py +++ b/src/sysforge/cli.py @@ -27,11 +27,14 @@ def _exit_code_from_summary(summary: object) -> int: return 2 warn = summary.get("warn") fail = summary.get("fail") + passed = summary.get("pass") if ( not isinstance(warn, int) or isinstance(warn, bool) or not isinstance(fail, int) or isinstance(fail, bool) + or not isinstance(passed, int) + or isinstance(passed, bool) ): return 2 if fail > 0: diff --git a/tests/test_checks.py b/tests/test_checks.py index 7540f9a..50197aa 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -58,3 +58,15 @@ def fake_usage(percent_free: float) -> dict[str, float]: lambda _: fake_usage(0.5), ) assert DiskSpaceCheck().run(disk_threshold=0.1).status == "pass" + + monkeypatch.setattr( + "sysforge.checks.core.disk_usage_summary", + lambda _: fake_usage(0.15), + ) + assert DiskSpaceCheck().run(disk_threshold=0.1).status == "warn" + + monkeypatch.setattr( + "sysforge.checks.core.disk_usage_summary", + lambda _: fake_usage(0.151), + ) + assert DiskSpaceCheck().run(disk_threshold=0.1).status == "pass" diff --git a/tests/test_cli.py b/tests/test_cli.py index 46a15a6..88a939b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -93,6 +93,15 @@ def test_doctor_malformed_summary_wrong_types_exits_2(monkeypatch) -> None: assert result.exit_code == 2 +def test_doctor_malformed_summary_pass_wrong_type_exits_2(monkeypatch) -> None: + monkeypatch.setattr( + "sysforge.cli.run_checks", + lambda disk_threshold: {"results": [], "summary": {"pass": "1", "warn": 0, "fail": 0}}, + ) + result = runner.invoke(app, ["doctor"]) + assert result.exit_code == 2 + + def test_doctor_respects_pretty_option(monkeypatch, tmp_path: Path) -> None: monkeypatch.setattr( "sysforge.cli.run_checks",