Accept a bare integer as seconds for watchpr --interval
- add agent.ParseDurationFlag: bare integer means seconds, duration strings still parse - take --interval as a string and parse it in the command - reject unparseable and non-positive values with an error naming the flag
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package agent
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParsePRRef(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -77,3 +81,54 @@ func TestParseRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDurationFlag(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
want time.Duration
|
||||
wantErr bool
|
||||
}{
|
||||
{"15", 15 * time.Second, false},
|
||||
{"15s", 15 * time.Second, false},
|
||||
{"2m", 2 * time.Minute, false},
|
||||
{"1h30m", 90 * time.Minute, false},
|
||||
{"500ms", 500 * time.Millisecond, false},
|
||||
{" 45 ", 45 * time.Second, false},
|
||||
{"0", 0, true},
|
||||
{"0s", 0, true},
|
||||
{"-5", 0, true},
|
||||
{"-5s", 0, true},
|
||||
{"15x", 0, true},
|
||||
{"", 0, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got, err := ParseDurationFlag("interval", tt.in)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("ParseDurationFlag(%q): expected error, got %v", tt.in, got)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("ParseDurationFlag(%q): unexpected error: %v", tt.in, err)
|
||||
continue
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("ParseDurationFlag(%q) = %v, want %v", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The error must name the flag and show valid forms instead of surfacing
|
||||
// time.ParseDuration's "missing unit" wording.
|
||||
func TestParseDurationFlagErrorMessage(t *testing.T) {
|
||||
_, err := ParseDurationFlag("interval", "soon")
|
||||
if err == nil {
|
||||
t.Fatal("ParseDurationFlag(\"soon\"): expected error")
|
||||
}
|
||||
for _, want := range []string{"--interval", `"soon"`, "30s", "seconds"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user