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:
+7
-2
@@ -7,6 +7,7 @@
|
||||
// watchpr owner/repo#12 owner/repo:15
|
||||
// watchpr --once --json owner/repo#12
|
||||
// watchpr --interval 30s owner/repo#12
|
||||
// watchpr --interval 30 owner/repo#12
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -34,7 +35,7 @@ func main() {
|
||||
// tests can invoke Execute and assert the exit behaviour without spawning a
|
||||
// process.
|
||||
func newRootCmd() *cobra.Command {
|
||||
var interval time.Duration
|
||||
var intervalFlag string
|
||||
var once, jsonMode bool
|
||||
|
||||
root := &cobra.Command{
|
||||
@@ -50,6 +51,10 @@ func newRootCmd() *cobra.Command {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("no PR references given (e.g. owner/repo#12)")
|
||||
}
|
||||
interval, err := agent.ParseDurationFlag("interval", intervalFlag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
refs := make([]agent.PRRef, 0, len(args))
|
||||
for _, a := range args {
|
||||
ref, err := agent.ParsePRRef(a)
|
||||
@@ -68,7 +73,7 @@ func newRootCmd() *cobra.Command {
|
||||
root.SetVersionTemplate("{{.Version}}\n")
|
||||
|
||||
f := root.Flags()
|
||||
f.DurationVar(&interval, "interval", 60*time.Second, "Polling interval")
|
||||
f.StringVar(&intervalFlag, "interval", "60s", "Polling interval: a duration (30s, 2m, 1h30m) or a bare number of seconds")
|
||||
f.BoolVar(&once, "once", false, "Check once, print current state, and exit")
|
||||
f.BoolVar(&jsonMode, "json", false, "Emit JSON")
|
||||
|
||||
|
||||
@@ -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