summaryrefslogtreecommitdiffstats
path: root/models/actions/schedule_spec_test.go
blob: 0c26fce4b2c5ea7e92987735e04bc14d2164eb35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestActionScheduleSpec_Parse(t *testing.T) {
	// Mock the local timezone is not UTC
	local := time.Local
	tz, err := time.LoadLocation("Asia/Shanghai")
	require.NoError(t, err)
	defer func() {
		time.Local = local
	}()
	time.Local = tz

	now, err := time.Parse(time.RFC3339, "2024-07-31T15:47:55+08:00")
	require.NoError(t, err)

	tests := []struct {
		name    string
		spec    string
		want    string
		wantErr assert.ErrorAssertionFunc
	}{
		{
			name:    "regular",
			spec:    "0 10 * * *",
			want:    "2024-07-31T10:00:00Z",
			wantErr: assert.NoError,
		},
		{
			name:    "invalid",
			spec:    "0 10 * *",
			want:    "",
			wantErr: assert.Error,
		},
		{
			name:    "with timezone",
			spec:    "TZ=America/New_York 0 10 * * *",
			want:    "2024-07-31T14:00:00Z",
			wantErr: assert.NoError,
		},
		{
			name:    "timezone irrelevant",
			spec:    "@every 5m",
			want:    "2024-07-31T07:52:55Z",
			wantErr: assert.NoError,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			s := &ActionScheduleSpec{
				Spec: tt.spec,
			}
			got, err := s.Parse()
			tt.wantErr(t, err)

			if err == nil {
				assert.Equal(t, tt.want, got.Next(now).UTC().Format(time.RFC3339))
			}
		})
	}
}