mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
feat(cli): extend duration to longer units (#15040)
This PR is a proposal to improve the situation described in #14750 For some precise commands - we would like to be able to use durations bigger than hours, minutes.. This PR extends the Duration proposed by Go with : - `d` - a day or 24hours. - `y` - a year or 365 days. I also removed the default value for lifetime and instead fetch the maxLifetime value from codersdk - so by default if no value set we use the value defined in the config.
This commit is contained in:
@ -41,6 +41,50 @@ func TestDurationDisplay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtendedParseDuration(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, testCase := range []struct {
|
||||
Duration string
|
||||
Expected time.Duration
|
||||
ExpectedOk bool
|
||||
}{
|
||||
{"1d", 24 * time.Hour, true},
|
||||
{"1y", 365 * 24 * time.Hour, true},
|
||||
{"10s", 10 * time.Second, true},
|
||||
{"1m", 1 * time.Minute, true},
|
||||
{"20h", 20 * time.Hour, true},
|
||||
{"10y10d10s", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second, true},
|
||||
{"10ms", 10 * time.Millisecond, true},
|
||||
{"5y10d10s5y2ms8ms", 10*365*24*time.Hour + 10*24*time.Hour + 10*time.Second + 10*time.Millisecond, true},
|
||||
{"10yz10d10s", 0, false},
|
||||
{"1µs2h1d", 1*time.Microsecond + 2*time.Hour + 1*24*time.Hour, true},
|
||||
{"1y365d", 2 * 365 * 24 * time.Hour, true},
|
||||
{"1µs10us", 1*time.Microsecond + 10*time.Microsecond, true},
|
||||
// negative related tests
|
||||
{"-", 0, false},
|
||||
{"-2h10m", -2*time.Hour - 10*time.Minute, true},
|
||||
{"--10s", 0, false},
|
||||
{"10s-10m", 0, false},
|
||||
// overflow related tests
|
||||
{"-20000000000000h", 0, false},
|
||||
{"92233754775807y", 0, false},
|
||||
{"200y200y200y200y200y", 0, false},
|
||||
{"9223372036854775807s", 0, false},
|
||||
} {
|
||||
testCase := testCase
|
||||
t.Run(testCase.Duration, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
actual, err := extendedParseDuration(testCase.Duration)
|
||||
if testCase.ExpectedOk {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, testCase.Expected, actual)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRelative(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.Equal(t, relative(time.Minute), "in 1m")
|
||||
|
Reference in New Issue
Block a user