From 43ca9661b0c13377185240af7ff16169cd849a6c Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 2 Apr 2026 01:21:52 +0100 Subject: [PATCH] fix: invalid times take priority over dates in the past for error messaging --- nag | 18 +++++++++--------- test/nag.bats | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/nag b/nag index 7c76eb8..7f96f93 100755 --- a/nag +++ b/nag @@ -618,7 +618,7 @@ _validate_and_normalise_rules() { do local _canon _canon="$(_normalise_rule "${_rule}")" || - _exit_1 printf "Invalid rule: %s\\n" "${_rule}" + _exit_1 printf "Invalid rule: %s.\\n" "${_rule}" _normalised+=("${_canon}") done IFS=$'\n\t' @@ -854,16 +854,16 @@ _parse_time() { local _time_str="${1:-}" [[ -n "${_time_str}" ]] || _exit_1 printf "No time specified.\\n" - local _lower - _lower="$(printf "%s" "${_time_str}" | tr '[:upper:]' '[:lower:]')" - if [[ "${_lower}" == *"yesterday"* ]] || [[ "${_lower}" == *" ago"* ]] - then - _exit_1 printf "Time is in the past: %s\\n" "${_time_str}" - fi - local _timestamp _timestamp="$(date -d "${_time_str}" +%s 2>/dev/null)" || - _exit_1 printf "Invalid time: %s\\n" "${_time_str}" + _exit_1 printf "Invalid time: %s. See 'date -d' for accepted formats.\\n" "${_time_str}" + + local _lower + _lower="$(printf "%s" "${_time_str}" | tr '[:upper:]' '[:lower:]')" + if [[ "${_lower}" == *"yesterday"* ]] || [[ "${_lower}" == *" ago"* ]] || [[ "${_lower}" == "last "* ]] + then + _exit_1 printf "Time is in the past: %s.\\n" "${_time_str}" + fi local _now _now="$(date +%s)" diff --git a/test/nag.bats b/test/nag.bats index 07b91d2..ebe92ed 100644 --- a/test/nag.bats +++ b/test/nag.bats @@ -74,11 +74,37 @@ load test_helper @test "at rejects yesterday" { run_nag at "yesterday 3pm" "too late" [ "${status}" -eq 1 ] + [[ "${output}" =~ "past" ]] } @test "at rejects ago" { run_nag at "2 hours ago" "too late" [ "${status}" -eq 1 ] + [[ "${output}" =~ "past" ]] +} + +@test "at rejects last" { + run_nag at "last friday" "too late" + [ "${status}" -eq 1 ] + [[ "${output}" =~ "past" ]] +} + +@test "invalid time with ago says invalid, not past" { + run_nag at "sdjkfhskdjfh ago" "nope" + [ "${status}" -eq 1 ] + [[ "${output}" =~ "Invalid time" ]] +} + +@test "invalid time with yesterday says invalid, not past" { + run_nag at "sdjkfh yesterday blah" "nope" + [ "${status}" -eq 1 ] + [[ "${output}" =~ "Invalid time" ]] +} + +@test "invalid time with last says invalid, not past" { + run_nag at "last sdjkfh" "nope" + [ "${status}" -eq 1 ] + [[ "${output}" =~ "Invalid time" ]] } @test "at rolls past date forward to next year" {