From ffbb194300b8d94817cdb084dac82dfdfa2700d5 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Tue, 3 Nov 2026 05:50:44 +0100 Subject: [PATCH] fix(scheduled-jobs): a file without a last newline no longer swallows the next header GNU sed keeps a missing final newline, so the next section's header ran on into the file's last line and its jobs were lost. The report ends every such file with a newline of its own. Refs #86 --- napalm_device_types/_scheduled_reading.py | 4 +++- tests/test_scheduled_jobs_posix.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/napalm_device_types/_scheduled_reading.py b/napalm_device_types/_scheduled_reading.py index 7e09a4a..adcbb59 100644 --- a/napalm_device_types/_scheduled_reading.py +++ b/napalm_device_types/_scheduled_reading.py @@ -86,7 +86,9 @@ def _q(path: str) -> str: def _files(section: str, pattern: str) -> str: return ( f'for f in {pattern}; do [ -f "$f" ] || continue; printf \'[%s %s]\\n\' {section} "$f"; ' - 'sed \'s/^/|/\' "$f"; P="$P\n$f"; done; ' + # GNU sed keeps a missing last newline; without one of its own the next + # header would run on into the file's last line. + 'sed \'s/^/|/\' "$f"; [ -n "$(tail -c 1 "$f")" ] && echo; P="$P\n$f"; done; ' ) diff --git a/tests/test_scheduled_jobs_posix.py b/tests/test_scheduled_jobs_posix.py index 4b746e7..258bf47 100644 --- a/tests/test_scheduled_jobs_posix.py +++ b/tests/test_scheduled_jobs_posix.py @@ -266,6 +266,12 @@ class TestReading: assert backup["next_run"] == "2026-11-04T03:00:00+00:00" assert "last_result" not in backup + def test_a_file_without_a_last_newline_does_not_swallow_the_next(self, root: Path): + (root / "etc/cron.d/certbot").write_text("0 */12 * * * root certbot -q renew") + jobs = _jobs(Host(root)) + assert any(j["source"].endswith("certbot") for j in jobs.values()) + assert any(j["source"].endswith("spool/www-data") for j in jobs.values()) + def test_without_systemd_no_timers(self, root: Path): jobs = _jobs(Host(root, systemd=False)) assert not [j for j in jobs.values() if j["kind"] == "systemd_timer"]