mirror of
https://github.com/rclone/rclone.git
synced 2025-08-13 23:38:51 +02:00
Fix test failure in different local time zones
TestParseDuration relied on an elapsed time calculation which would vary based on the system local time. Fix the test by not relying on the system time location. Also make the test more deterministic by injecting time in tests rather than using system time. Fixes #4529.
This commit is contained in:
committed by
Nick Craig-Wood
parent
cdaea62932
commit
6156f90601
@ -15,6 +15,11 @@ import (
|
||||
var _ pflag.Value = (*Duration)(nil)
|
||||
|
||||
func TestParseDuration(t *testing.T) {
|
||||
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
|
||||
getNow := func() time.Time {
|
||||
return now
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
in string
|
||||
want time.Duration
|
||||
@ -37,12 +42,12 @@ func TestParseDuration(t *testing.T) {
|
||||
{"1x", 0, true},
|
||||
{"off", time.Duration(DurationOff), false},
|
||||
{"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false},
|
||||
{"2001-02-03", time.Since(time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)), false},
|
||||
{"2001-02-03 10:11:12", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
|
||||
{"2001-02-03T10:11:12", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
|
||||
{"2001-02-03T10:11:12.123Z", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 123, time.UTC)), false},
|
||||
{"2001-02-03", now.Sub(time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)), false},
|
||||
{"2001-02-03 10:11:12", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
|
||||
{"2001-02-03T10:11:12", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
|
||||
{"2001-02-03T10:11:12.123Z", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 123, time.UTC)), false},
|
||||
} {
|
||||
duration, err := ParseDuration(test.in)
|
||||
duration, err := parseDurationFromNow(test.in, getNow)
|
||||
if test.err {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
@ -58,6 +63,11 @@ func TestParseDuration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDurationString(t *testing.T) {
|
||||
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
|
||||
getNow := func() time.Time {
|
||||
return now
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
in time.Duration
|
||||
want string
|
||||
@ -88,7 +98,7 @@ func TestDurationString(t *testing.T) {
|
||||
got := Duration(test.in).String()
|
||||
assert.Equal(t, test.want, got)
|
||||
// Test the reverse
|
||||
reverse, err := ParseDuration(test.want)
|
||||
reverse, err := parseDurationFromNow(test.want, getNow)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.in, reverse)
|
||||
}
|
||||
|
Reference in New Issue
Block a user