diff --git a/fs/parseduration.go b/fs/parseduration.go index cd3f192ac..b5104cc6c 100644 --- a/fs/parseduration.go +++ b/fs/parseduration.go @@ -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 { diff --git a/fs/parseduration_test.go b/fs/parseduration_test.go index 04830ef06..69de520ca 100644 --- a/fs/parseduration_test.go +++ b/fs/parseduration_test.go @@ -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) }