fix: invalid times take priority over dates in the past for error messaging

This commit is contained in:
Lewis Wynne 2026-04-02 01:21:52 +01:00
parent 464362bcbf
commit 43ca9661b0
2 changed files with 35 additions and 9 deletions

18
nag
View file

@ -618,7 +618,7 @@ _validate_and_normalise_rules() {
do do
local _canon local _canon
_canon="$(_normalise_rule "${_rule}")" || _canon="$(_normalise_rule "${_rule}")" ||
_exit_1 printf "Invalid rule: %s\\n" "${_rule}" _exit_1 printf "Invalid rule: %s.\\n" "${_rule}"
_normalised+=("${_canon}") _normalised+=("${_canon}")
done done
IFS=$'\n\t' IFS=$'\n\t'
@ -854,16 +854,16 @@ _parse_time() {
local _time_str="${1:-}" local _time_str="${1:-}"
[[ -n "${_time_str}" ]] || _exit_1 printf "No time specified.\\n" [[ -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 local _timestamp
_timestamp="$(date -d "${_time_str}" +%s 2>/dev/null)" || _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 local _now
_now="$(date +%s)" _now="$(date +%s)"

View file

@ -74,11 +74,37 @@ load test_helper
@test "at rejects yesterday" { @test "at rejects yesterday" {
run_nag at "yesterday 3pm" "too late" run_nag at "yesterday 3pm" "too late"
[ "${status}" -eq 1 ] [ "${status}" -eq 1 ]
[[ "${output}" =~ "past" ]]
} }
@test "at rejects ago" { @test "at rejects ago" {
run_nag at "2 hours ago" "too late" run_nag at "2 hours ago" "too late"
[ "${status}" -eq 1 ] [ "${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" { @test "at rolls past date forward to next year" {