Skip to content

test: Using local time in unit test fails in certain time zones #1540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions coderd/autobuild/schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@ import (
func Test_Weekly(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
spec string
at time.Time
expectedNext time.Time
expectedError string
expectedCron string
expectedTz string
name string
spec string
at time.Time
expectedNext time.Time
expectedError string
expectedCron string
expectedTz string
expectedString string
}{
{
name: "with timezone",
spec: "CRON_TZ=US/Central 30 9 * * 1-5",
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC),
expectedError: "",
expectedCron: "30 9 * * 1-5",
expectedTz: "US/Central",
name: "with timezone",
spec: "CRON_TZ=US/Central 30 9 * * 1-5",
at: time.Date(2022, 4, 1, 14, 29, 0, 0, time.UTC),
expectedNext: time.Date(2022, 4, 1, 14, 30, 0, 0, time.UTC),
expectedError: "",
expectedCron: "30 9 * * 1-5",
expectedTz: "US/Central",
expectedString: "CRON_TZ=US/Central 30 9 * * 1-5",
},
{
name: "without timezone",
spec: "CRON_TZ=UTC 30 9 * * 1-5",
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.Local),
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.Local),
expectedError: "",
expectedCron: "30 9 * * 1-5",
expectedTz: "UTC",
name: "without timezone",
spec: "30 9 * * 1-5",
at: time.Date(2022, 4, 1, 9, 29, 0, 0, time.UTC),
expectedNext: time.Date(2022, 4, 1, 9, 30, 0, 0, time.UTC),
expectedError: "",
expectedCron: "30 9 * * 1-5",
expectedTz: "UTC",
expectedString: "CRON_TZ=UTC 30 9 * * 1-5",
},
{
name: "invalid schedule",
Expand Down Expand Up @@ -91,9 +94,9 @@ func Test_Weekly(t *testing.T) {
nextTime := actual.Next(testCase.at)
require.NoError(t, err)
require.Equal(t, testCase.expectedNext, nextTime)
require.Equal(t, testCase.spec, actual.String())
require.Equal(t, testCase.expectedCron, actual.Cron())
require.Equal(t, testCase.expectedTz, actual.Timezone())
require.Equal(t, testCase.expectedString, actual.String())
} else {
require.EqualError(t, err, testCase.expectedError)
require.Nil(t, actual)
Expand Down