cronspec_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package cronspec
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. )
  7. // #1648 upstream: the panel's scheduler runs WithSeconds(), whose parser refused
  8. // a standard five-field crontab, so an admin's "30 8 * * *" never fired.
  9. func TestParseNextFire(t *testing.T) {
  10. from := time.Date(2026, 10, 11, 7, 0, 0, 0, time.UTC)
  11. cases := []struct {
  12. spec string
  13. want time.Time
  14. }{
  15. {"30 8 * * *", time.Date(2026, 10, 11, 8, 30, 0, 0, time.UTC)},
  16. {" 15 30 8 * * * ", time.Date(2026, 10, 11, 8, 30, 15, 0, time.UTC)},
  17. {"@daily", time.Date(2026, 10, 12, 0, 0, 0, 0, time.UTC)},
  18. {"@every 90m", from.Add(90 * time.Minute)},
  19. }
  20. for _, c := range cases {
  21. t.Run(c.spec, func(t *testing.T) {
  22. schedule, err := Parse(c.spec)
  23. if err != nil {
  24. t.Fatalf("Parse(%q): %v", c.spec, err)
  25. }
  26. if got := schedule.Next(from); !got.Equal(c.want) {
  27. t.Errorf("Parse(%q).Next = %v, want %v", c.spec, got, c.want)
  28. }
  29. })
  30. }
  31. }
  32. func TestParseRejects(t *testing.T) {
  33. for _, spec := range []string{"", " ", "every day", "61 * * * *", "* * * *"} {
  34. t.Run(spec, func(t *testing.T) {
  35. _, err := Parse(spec)
  36. if err == nil {
  37. t.Fatalf("Parse(%q): want error, got nil", spec)
  38. }
  39. if !strings.Contains(err.Error(), "cron schedule") {
  40. t.Errorf("Parse(%q) error = %q, want it to name the cron schedule", spec, err)
  41. }
  42. })
  43. }
  44. }