From 332b1896d17395d04f1b41f633647f7577cbbfcf Mon Sep 17 00:00:00 2001 From: DRON-666 <64691982+DRON-666@users.noreply.github.com> Date: Wed, 23 Dec 2020 23:26:04 +0300 Subject: [PATCH] Some `options` fixes Add tests for bool type. Fix subtle bug in TestOptionsApplyInvalid. Fix options list formatting. --- cmd/restic/cmd_options.go | 8 +++++++- internal/options/options_test.go | 30 +++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/cmd/restic/cmd_options.go b/cmd/restic/cmd_options.go index d18a86570..471319dfb 100644 --- a/cmd/restic/cmd_options.go +++ b/cmd/restic/cmd_options.go @@ -23,8 +23,14 @@ Exit status is 0 if the command was successful, and non-zero if there was any er DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { fmt.Printf("All Extended Options:\n") + var maxLen int for _, opt := range options.List() { - fmt.Printf(" %-15s %s\n", opt.Namespace+"."+opt.Name, opt.Text) + if l := len(opt.Namespace + "." + opt.Name); l > maxLen { + maxLen = l + } + } + for _, opt := range options.List() { + fmt.Printf(" %*s %s\n", -maxLen, opt.Namespace+"."+opt.Name, opt.Text) } }, } diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 8d268992a..ce5153f5b 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -125,6 +125,7 @@ type Target struct { Name string `option:"name"` ID int `option:"id"` Timeout time.Duration `option:"timeout"` + Switch bool `option:"switch"` Other string } @@ -155,7 +156,15 @@ var setTests = []struct { "timeout": "10m3s", }, Target{ - Timeout: time.Duration(10*time.Minute + 3*time.Second), + Timeout: 10*time.Minute + 3*time.Second, + }, + }, + { + Options{ + "switch": "true", + }, + Target{ + Switch: true, }, }, } @@ -202,6 +211,13 @@ var invalidSetTests = []struct { "ns", `time: missing unit in duration "?2134"?`, }, + { + Options{ + "switch": "yes", + }, + "ns", + `strconv.ParseBool: parsing "yes": invalid syntax`, + }, } func TestOptionsApplyInvalid(t *testing.T) { @@ -213,9 +229,9 @@ func TestOptionsApplyInvalid(t *testing.T) { t.Fatalf("expected error %v not found", test.err) } - matched, err := regexp.MatchString(test.err, err.Error()) - if err != nil { - t.Fatal(err) + matched, e := regexp.MatchString(test.err, err.Error()) + if e != nil { + t.Fatal(e) } if !matched { @@ -226,11 +242,11 @@ func TestOptionsApplyInvalid(t *testing.T) { } func TestListOptions(t *testing.T) { - var teststruct = struct { + teststruct := struct { Foo string `option:"foo" help:"bar text help"` }{} - var tests = []struct { + tests := []struct { cfg interface{} opts []Help }{ @@ -281,7 +297,7 @@ func TestListOptions(t *testing.T) { } func TestAppendAllOptions(t *testing.T) { - var tests = []struct { + tests := []struct { cfgs map[string]interface{} opts []Help }{