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:
@@ -4,6 +4,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -70,3 +71,37 @@ func TestOnceRunsAnonymouslyWhenNoTokenIsAvailable(t *testing.T) {
|
||||
t.Errorf("sent %d Authorization headers, want none", authHeaders)
|
||||
}
|
||||
}
|
||||
|
||||
// A bare integer interval means seconds and must survive flag parsing: the
|
||||
// command should fail on the missing PR reference, not on the flag value.
|
||||
func TestExecuteBareIntervalIsSeconds(t *testing.T) {
|
||||
cmd := newRootCmd()
|
||||
cmd.SetArgs([]string{"--interval", "15"})
|
||||
cmd.SetOut(io.Discard)
|
||||
cmd.SetErr(io.Discard)
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatal("Execute() = nil, want the no-references error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no PR references given") {
|
||||
t.Fatalf("Execute() error = %v, want the no-references error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// An unparseable interval is rejected before any Vault/Gitea call, with an
|
||||
// error naming the flag and showing valid forms.
|
||||
func TestExecuteBadIntervalErrors(t *testing.T) {
|
||||
cmd := newRootCmd()
|
||||
cmd.SetArgs([]string{"--interval", "soon", "unkin/repo#1"})
|
||||
cmd.SetOut(io.Discard)
|
||||
cmd.SetErr(io.Discard)
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatal("Execute() = nil, want error for an unparseable --interval")
|
||||
}
|
||||
for _, want := range []string{"--interval", "30s"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Execute() error %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user