Compare commits

...
Author SHA1 Message Date
J. Nick Koston ea556f21b1 Reject cron range endpoints outside the field's bounds 2026-10-28 16:48:38 -10:00
J. Nick Koston 1472ba64ef Parametrize the wrapping cron range tests 2026-10-28 16:33:41 -10:00
J. Nick Koston 2ad1b7de66 [time] Fix cron ranges that wrap around, like hours 22-2
A range whose end is below its start built the wrong two halves and
matched every value of the field. Take begin..max and min..end instead.
2026-10-28 16:26:59 -10:00
2 changed files with 40 additions and 2 deletions

No files matched your search

+6 -1
View File
@@ -183,8 +183,13 @@ def _parse_cron_part(part, min_value, max_value, special_mapping):
end_n = _parse_cron_int(
end, special_mapping, "Number for time range must be integer, got {}"
)
for n in (begin_n, end_n):
if not min_value <= n <= max_value:
raise cv.Invalid(
f"Range '{part}': {n} is out of range (min={min_value} max={max_value})"
)
if end_n < begin_n:
return set(range(end_n, max_value + 1)) | set(range(min_value, begin_n + 1))
return set(range(begin_n, max_value + 1)) | set(range(min_value, end_n + 1))
return set(range(begin_n, end_n + 1))
return {
+34 -1
View File
@@ -1,5 +1,6 @@
"""Tests for time component cron expression parsing."""
from collections.abc import Callable
import errno
import subprocess
import sys
@@ -7,7 +8,14 @@ from unittest.mock import MagicMock, patch
import pytest
from esphome.components.time import _load_tzdata, _parse_cron_part, validate_tz
from esphome.components.time import (
_load_tzdata,
_parse_cron_part,
validate_cron_days_of_week,
validate_cron_hours,
validate_tz,
)
import esphome.config_validation as cv
def test_star_slash_seconds() -> None:
@@ -83,6 +91,31 @@ def test_range() -> None:
assert _parse_cron_part("1-5", 0, 59, {}) == {1, 2, 3, 4, 5}
@pytest.mark.parametrize(
("validator", "value", "expected"),
[
(validate_cron_hours, "22-2", [0, 1, 2, 22, 23]),
# Sunday is 1
(validate_cron_days_of_week, "SAT-MON", [1, 2, 7]),
],
)
def test_wrapping_range(
validator: Callable[[str], list[int]], value: str, expected: list[int]
) -> None:
assert validator(value) == expected
@pytest.mark.parametrize(
("validator", "value"),
[(validate_cron_hours, "24-2"), (validate_cron_days_of_week, "SAT-0")],
)
def test_range_endpoint_out_of_bounds(
validator: Callable[[str], list[int]], value: str
) -> None:
with pytest.raises(cv.Invalid, match="out of range"):
validator(value)
def test_single_value() -> None:
assert _parse_cron_part("30", 0, 59, {}) == {30}