diff --git a/cmd/check_test.go b/cmd/check_test.go index 3a9996d..8195017 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -27,7 +27,7 @@ func Test_CheckOption(t *testing.T) { stderr []string }{ { - name: "paser --jobs argument error", + name: "parser --jobs argument error", args: args{ cmd: &cobra.Command{}, args: []string{}, diff --git a/cmd/export_test.go b/cmd/export_test.go index 20cec73..a071e4e 100644 --- a/cmd/export_test.go +++ b/cmd/export_test.go @@ -5,7 +5,6 @@ import ( "io" "os" "path/filepath" - "reflect" "runtime" "strings" "testing" @@ -31,7 +30,7 @@ func Test_ExportOption(t *testing.T) { stderr []string }{ { - name: "paser --output argument error", + name: "parser --output argument error", args: args{ cmd: &cobra.Command{}, args: []string{}, @@ -105,8 +104,9 @@ func Test_validPkgInfo(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := validPkgInfo(tt.args.pkgs); !reflect.DeepEqual(got, tt.want) { - t.Errorf("validPkgInfo() = %v, want %v", got, tt.want) + got := validPkgInfo(tt.args.pkgs) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("value is mismatch (-want +got):\n%s", diff) } }) } diff --git a/cmd/update.go b/cmd/update.go index 53c37e9..da7af42 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -7,7 +7,6 @@ import ( "os/signal" "runtime" "strconv" - "strings" "syscall" "time" @@ -33,7 +32,7 @@ under $GOPATH/bin and automatically updates commands to the latest version.`, } cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") - cmd.Flags().StringP("exclude", "e", "", "specify binaries which should not be updated separated using ',' without spaces as a delimiter") + cmd.Flags().StringSliceP("exclude", "e", []string{}, "specify binaries which should not be updated separated using ',' without spaces as a delimiter") cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use") return cmd @@ -71,33 +70,32 @@ func gup(cmd *cobra.Command, args []string) int { return 1 } - exclude, err := cmd.Flags().GetString("exclude") + excludePkgList, err := cmd.Flags().GetStringSlice("exclude") if err != nil { print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--exclude)", err)) return 1 } pkgs = extractUserSpecifyPkg(pkgs, args) - - pkgs = excludePkgs(exclude, pkgs) + pkgs = excludePkgs(excludePkgList, pkgs) if len(pkgs) == 0 { - print.Err("unable to update package: no package information") + print.Err("unable to update package: no package information or no package under $GOBIN") return 1 } return update(pkgs, dryRun, notify, cpus) } -func excludePkgs(exclude string, pkgs []goutil.Package) []goutil.Package { - excludelist := strings.Split(exclude, ",") - - for i := len(pkgs) - 1; i >= 0; i-- { - if slice.Contains(excludelist, pkgs[i].Name) { - pkgs = append(pkgs[:i], pkgs[i+1:]...) +func excludePkgs(excludePkgList []string, pkgs []goutil.Package) []goutil.Package { + packageList := []goutil.Package{} + for _, v := range pkgs { + if slice.Contains(excludePkgList, v.Name) { + print.Info(fmt.Sprintf("Exclude '%s' from the update target", v.Name)) + continue } + packageList = append(packageList, v) } - - return pkgs + return packageList } type updateResult struct { diff --git a/cmd/update_test.go b/cmd/update_test.go index 8fb0a94..a99457a 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "os" - "reflect" "runtime" "strings" "testing" @@ -27,7 +26,7 @@ func Test_gup(t *testing.T) { stderr []string }{ { - name: "paser --dry-run argument error", + name: "parser --dry-run argument error", args: args{ cmd: &cobra.Command{}, args: []string{}, @@ -39,7 +38,7 @@ func Test_gup(t *testing.T) { }, }, { - name: "paser --notify argument error", + name: "parser --notify argument error", args: args{ cmd: &cobra.Command{}, args: []string{}, @@ -51,7 +50,7 @@ func Test_gup(t *testing.T) { }, }, { - name: "paser --jobs argument error", + name: "parser --jobs argument error", args: args{ cmd: &cobra.Command{}, args: []string{}, @@ -65,13 +64,13 @@ func Test_gup(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.name == "paser --dry-run argument error" { + if tt.name == "parser --dry-run argument error" { tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") tt.args.cmd.Flags().BoolP("jobs", "j", false, "Specify the number of CPU cores to use") - } else if tt.name == "paser --notify argument error" { + } else if tt.name == "parser --notify argument error" { tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") tt.args.cmd.Flags().BoolP("jobs", "j", false, "Specify the number of CPU cores to use") - } else if tt.name == "paser --jobs argument error" { + } else if tt.name == "parser --jobs argument error" { tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes") tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications") } @@ -165,8 +164,9 @@ func Test_extractUserSpecifyPkg(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := extractUserSpecifyPkg(tt.args.pkgs, tt.args.targets); !reflect.DeepEqual(got, tt.want) { - t.Errorf("extractUserSpecifyPkg() = %v, want %v", got, tt.want) + got := extractUserSpecifyPkg(tt.args.pkgs, tt.args.targets) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("value is mismatch (-want +got):\n%s", diff) } }) } @@ -174,8 +174,8 @@ func Test_extractUserSpecifyPkg(t *testing.T) { func Test_excludeUserSpecifiedPkg(t *testing.T) { type args struct { - pkgs []goutil.Package - exclude string + pkgs []goutil.Package + excludePkgList []string } tests := []struct { name string @@ -196,7 +196,7 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) { Name: "pkg3", }, }, - exclude: "pkg1,pkg3", + excludePkgList: []string{"pkg1", "pkg3"}, }, want: []goutil.Package{ { @@ -205,7 +205,7 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) { }, }, { - name: "find user specify package", + name: "find user specify package (exclude all package)", args: args{ pkgs: []goutil.Package{ { @@ -218,16 +218,12 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) { Name: "pkg3", }, }, - exclude: "pkg1,pkg2", - }, - want: []goutil.Package{ - { - Name: "pkg3", - }, + excludePkgList: []string{"pkg1", "pkg2", "pkg3"}, }, + want: []goutil.Package{}, }, { - name: "find user specify package", + name: "If the excluded package does not exist", args: args{ pkgs: []goutil.Package{ { @@ -240,9 +236,12 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) { Name: "pkg3", }, }, - exclude: "pkg1", + excludePkgList: []string{"pkg4"}, }, want: []goutil.Package{ + { + Name: "pkg1", + }, { Name: "pkg2", }, @@ -254,8 +253,9 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := excludePkgs(tt.args.exclude, tt.args.pkgs); !reflect.DeepEqual(got, tt.want) { - t.Errorf("extractUserSpecifyPkg() = %v, want %v", got, tt.want) + got := excludePkgs(tt.args.excludePkgList, tt.args.pkgs) + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("value is mismatch (-want +got):\n%s", diff) } }) }