diff --git a/src/schedls/operations/doctor.py b/src/schedls/operations/doctor.py index 741eb0c..ad2a612 100644 --- a/src/schedls/operations/doctor.py +++ b/src/schedls/operations/doctor.py @@ -35,31 +35,32 @@ def run_doctor( ) return + schedls_details = [("version", __version__), ("Python", platform.python_version())] + systemd_details = [ + ("available", _yes_no(systemd["available"])), + ("system manager", _reachable(systemd["system_manager"])), + ("user manager", _reachable(systemd["user_manager"])), + ("systemd-analyze", _yes_no(systemd["analyze"])), + ("calendar validation", _yes_no(systemd["calendar_validation"])), + ("user lingering", _lingering(systemd["user_lingering"])), + ] + cron_details = [ + ("crontab", _yes_no(cron["crontab_available"])), + ("system cron files", _yes_no(cron["file_sources"])), + ("implementation", cron["implementation"] or "unknown"), + ("current user allowed", _yes_no(cron["user_allowed"])), + ("syntax validation", _yes_no(cron["validation"])), + ] + width = max(len(key) for key, _ in [*schedls_details, *systemd_details, *cron_details]) + output.heading("schedls") - output.key_values([("version", __version__), ("Python", platform.python_version())]) + output.key_values(schedls_details, width=width) output.line() output.heading("systemd") - output.key_values( - [ - ("available", _yes_no(systemd["available"])), - ("system manager", _reachable(systemd["system_manager"])), - ("user manager", _reachable(systemd["user_manager"])), - ("systemd-analyze", _yes_no(systemd["analyze"])), - ("calendar validation", _yes_no(systemd["calendar_validation"])), - ("user lingering", _lingering(systemd["user_lingering"])), - ] - ) + output.key_values(systemd_details, width=width) output.line() output.heading("cron") - output.key_values( - [ - ("crontab", _yes_no(cron["crontab_available"])), - ("system cron files", _yes_no(cron["file_sources"])), - ("implementation", cron["implementation"] or "unknown"), - ("current user allowed", _yes_no(cron["user_allowed"])), - ("syntax validation", _yes_no(cron["validation"])), - ] - ) + output.key_values(cron_details, width=width) output.line() output.heading("Result") output.line(f" {'usable' if usable else 'unusable'}") diff --git a/src/schedls/operations/inspect.py b/src/schedls/operations/inspect.py index b369cdb..e7940d5 100644 --- a/src/schedls/operations/inspect.py +++ b/src/schedls/operations/inspect.py @@ -116,7 +116,6 @@ def render_show(output: Output, job: ScheduledJob) -> None: core.extend([("Previous", previous), ("Command", job.command.display() or "unknown")]) if job.last_result: core.append(("Result", job.last_result)) - output.key_values(core) details: list[tuple[str, str]] = [] if job.systemd is not None: @@ -143,9 +142,11 @@ def render_show(output: Output, job: ScheduledJob) -> None: details.append(("SHELL", job.cron.shell)) if job.cron.mailto: details.append(("MAILTO", job.cron.mailto)) + width = max(len(key) for key, _ in [*core, *details]) + output.key_values(core, width=width) if details: output.line() - output.key_values(details) + output.key_values(details, width=width) for warning in job.warnings: output.line() output.diagnostic(f"Warning: {warning}") diff --git a/src/schedls/output.py b/src/schedls/output.py index 05313a1..e3ec8b2 100644 --- a/src/schedls/output.py +++ b/src/schedls/output.py @@ -99,10 +99,10 @@ class Output: for row in materialized: self.line(" ".join(cell.ljust(widths[i]) for i, cell in enumerate(row)).rstrip()) - def key_values(self, pairs: Sequence[tuple[str, str]]) -> None: + def key_values(self, pairs: Sequence[tuple[str, str]], *, width: int | None = None) -> None: if not pairs: return - width = max(len(key) for key, _ in pairs) + width = max(width if width is not None else 0, *(len(key) for key, _ in pairs)) for key, value in pairs: label = self.style(key.ljust(width), "bold", "cyan") self.line(f"{label} {value}".rstrip()) diff --git a/tests/unit/test_operations.py b/tests/unit/test_operations.py index 8ca040a..e601b07 100644 --- a/tests/unit/test_operations.py +++ b/tests/unit/test_operations.py @@ -7,6 +7,7 @@ from dataclasses import replace import pytest +from schedls import __version__ from schedls.backends.base import CommandPlan, FileChange, MutationResult, Plan from schedls.backends.cron import CronBackend from schedls.backends.systemd import SystemdBackend @@ -73,6 +74,24 @@ def test_render_show() -> None: assert "systemd user timer" in text +def test_render_show_aligns_systemd_details() -> None: + job = replace( + _job(), + systemd=SystemdDetails( + timer_path="/var/lib/schedls/backup.timer", + service_path="/var/lib/schedls/backup.service", + persistent=True, + ), + ) + output, stream = _output() + + inspect_ops.render_show(output, job) + + backend = next(line for line in stream.getvalue().splitlines() if line.startswith("Backend")) + timer = next(line for line in stream.getvalue().splitlines() if line.startswith("Timer")) + assert backend.index("systemd") == timer.index("/var/lib/schedls/backup.timer") + + def test_render_show_explains_unavailable_cron_history() -> None: job = ScheduledJob( name="cron-1", @@ -94,6 +113,9 @@ def test_render_show_explains_unavailable_cron_history() -> None: assert "unavailable (cron does not provide per-job history)" in previous assert "\n\nBackend" not in text assert "\n\nSource" in text + status = next(line for line in text.splitlines() if line.startswith("Status")) + source = next(line for line in text.splitlines() if line.startswith("Source")) + assert status.index("active") == source.index("current user's crontab") def test_render_show_keeps_cron_next_run_null_in_json() -> None: @@ -203,6 +225,10 @@ def test_doctor_human() -> None: text = stream.getvalue() assert "Cronie" in text assert "usable" in text + version = next(line for line in text.splitlines() if line.startswith("version")) + available = next(line for line in text.splitlines() if line.startswith("available")) + crontab = next(line for line in text.splitlines() if line.startswith("crontab")) + assert version.index(__version__) == available.index("yes") == crontab.index("yes") def test_doctor_json() -> None: diff --git a/tests/unit/test_output.py b/tests/unit/test_output.py index e59430b..1661750 100644 --- a/tests/unit/test_output.py +++ b/tests/unit/test_output.py @@ -96,9 +96,18 @@ def test_key_values_colour_labels_but_not_values() -> None: stream = io.StringIO() output = Output(color="always", stdout=stream) - output.key_values([("S", "active"), ("Status", "waiting")]) + output.key_values([("S", "active"), ("Status", "waiting")], width=8) - assert stream.getvalue() == ("\033[1m\033[36mS \033[0m active\n\033[1m\033[36mStatus\033[0m waiting\n") + assert stream.getvalue() == ("\033[1m\033[36mS \033[0m active\n\033[1m\033[36mStatus \033[0m waiting\n") + + +def test_key_values_honours_requested_width() -> None: + stream = io.StringIO() + output = Output(color="never", stdout=stream) + + output.key_values([("Key", "value")], width=6) + + assert stream.getvalue() == "Key value\n" def test_key_values_honours_no_color(monkeypatch) -> None: