From d820b75db19bd5aa84541d7cf7f9dcc3ddb8ed7a Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Sun, 24 Jul 2022 19:47:51 +0200 Subject: [PATCH] Add (and fix from) tests Signed-off-by: Sam Therapy --- cli/cli.go | 20 ++++++++++---------- cli/cli_test.go | 32 ++++++++++++++++++++++++++++++++ doc/awl.1.md | 2 +- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 80dc2a7..f14e0e2 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -46,7 +46,7 @@ func ParseCLI(version string) (Options, error) { reverse = flag.Bool("reverse", false, "do a reverse lookup", flag.OptShorthand('x')) timeout = flag.Float32("timeout", 1, "Timeout, in `seconds`") - retry = flag.Int("retry", 2, "number of `times` to retry") + retry = flag.Int("retries", 2, "number of `times` to retry") dnssec = flag.Bool("dnssec", false, "enable DNSSEC", flag.OptShorthand('D')) truncate = flag.Bool("no-truncate", false, "ignore truncation if a UDP request truncates (default= retry with TCP)") @@ -130,15 +130,6 @@ func ParseCLI(version string) (Options, error) { }, } - // Set timeout to 0.5 seconds if set below 0.5 - if float32(opts.Request.Timeout) < (0.5 * float32(time.Second)) { - opts.Request.Timeout = (time.Second / 2) - } - - if opts.Request.Retries < 0 { - opts.Request.Timeout = 0 - } - opts.Logger.Info("POSIX flags parsed") opts.Logger.Debug(fmt.Sprintf("%+v", opts)) @@ -166,5 +157,14 @@ func ParseCLI(version string) (Options, error) { } opts.Logger.Info("Port set to", opts.Port) + // Set timeout to 0.5 seconds if set below 0.5 + if opts.Request.Timeout < (time.Second / 2) { + opts.Request.Timeout = (time.Second / 2) + } + + if opts.Request.Retries < 0 { + opts.Request.Retries = 0 + } + return opts, nil } diff --git a/cli/cli_test.go b/cli/cli_test.go index e371036..21b9e63 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -5,6 +5,7 @@ package cli_test import ( "os" "testing" + "time" "git.froth.zone/sam/awl/cli" "gotest.tools/v3/assert" @@ -53,6 +54,37 @@ func TestVersion(t *testing.T) { os.Args = old } +func TestTimeout(t *testing.T) { + args := [][]string{ + {"awl", "+timeout=0"}, + {"awl", "--timeout", "0"}, + } + for _, test := range args { + old := os.Args + os.Args = test + opt, err := cli.ParseCLI("TEST") + assert.NilError(t, err) + assert.Equal(t, opt.Request.Timeout, time.Second/2) + os.Args = old + } +} + +func TestRetries(t *testing.T) { + args := [][]string{ + {"awl", "+retry=-2"}, + {"awl", "+tries=-2"}, + {"awl", "--retries", "-2"}, + } + for _, test := range args { + old := os.Args + os.Args = test + opt, err := cli.ParseCLI("TEST") + assert.NilError(t, err) + assert.Equal(t, opt.Request.Retries, 0) + os.Args = old + } +} + func FuzzFlags(f *testing.F) { testcases := []string{"git.froth.zone", "", "!12345", "google.com.edu.org.fr"} for _, tc := range testcases { diff --git a/doc/awl.1.md b/doc/awl.1.md index 623fd95..3f22798 100644 --- a/doc/awl.1.md +++ b/doc/awl.1.md @@ -86,7 +86,7 @@ _Default Ports_: Set the timeout period. Floating point numbers are accepted.++ 0.5 seconds is the minimum. - *--retry* _int_, *+tries*=_int_, *+ retry*=_int_++ + *--retries* _int_, *+tries*=_int_, *+ retry*=_int_++ Set the number of retries.++ Retry is one more than tries, dig style