mirror of
https://github.com/rclone/rclone.git
synced 2025-01-22 06:09:21 +01: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:
parent
cdaea62932
commit
6156f90601
@ -88,8 +88,9 @@ func parseDurationDates(age string, epoch time.Time) (t time.Duration, err error
|
||||
return t, err
|
||||
}
|
||||
|
||||
// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not provided
|
||||
func ParseDuration(age string) (d time.Duration, err error) {
|
||||
// parseDurationFromNow parses a duration string. Allows ParseDuration to match the time
|
||||
// package and easier testing within the fs package.
|
||||
func parseDurationFromNow(age string, getNow func() time.Time) (d time.Duration, err error) {
|
||||
if age == "off" {
|
||||
return time.Duration(DurationOff), nil
|
||||
}
|
||||
@ -105,7 +106,7 @@ func ParseDuration(age string) (d time.Duration, err error) {
|
||||
return d, nil
|
||||
}
|
||||
|
||||
d, err = parseDurationDates(age, time.Now())
|
||||
d, err = parseDurationDates(age, getNow())
|
||||
if err == nil {
|
||||
return d, nil
|
||||
}
|
||||
@ -113,6 +114,11 @@ func ParseDuration(age string) (d time.Duration, err error) {
|
||||
return d, err
|
||||
}
|
||||
|
||||
// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not provided
|
||||
func ParseDuration(age string) (time.Duration, error) {
|
||||
return parseDurationFromNow(age, time.Now)
|
||||
}
|
||||
|
||||
// ReadableString parses d into a human readable duration.
|
||||
// Based on https://github.com/hako/durafmt
|
||||
func (d Duration) ReadableString() string {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user