Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sysforge/checks/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/sysforge/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down