1+ //nolint:paralleltest,errcheck,gosec
12package cmd
23
34import (
@@ -70,6 +71,68 @@ func Test_check(t *testing.T) {
7071 }
7172}
7273
74+ func Test_CheckOption (t * testing.T ) {
75+ type args struct {
76+ cmd * cobra.Command
77+ args []string
78+ }
79+ tests := []struct {
80+ name string
81+ args args
82+ want int
83+ stderr []string
84+ }{
85+ {
86+ name : "parser --jobs argument error" ,
87+ args : args {
88+ cmd : & cobra.Command {},
89+ args : []string {},
90+ },
91+ want : 1 ,
92+ stderr : []string {
93+ "gup:ERROR: can not parse command line argument (--jobs): flag accessed but not defined: jobs" ,
94+ "" ,
95+ },
96+ },
97+ }
98+ for _ , tt := range tests {
99+ t .Run (tt .name , func (t * testing.T ) {
100+ OsExit = func (code int ) {}
101+ defer func () {
102+ OsExit = os .Exit
103+ }()
104+
105+ orgStdout := print .Stdout
106+ orgStderr := print .Stderr
107+ pr , pw , err := os .Pipe ()
108+ if err != nil {
109+ t .Fatal (err )
110+ }
111+ print .Stdout = pw
112+ print .Stderr = pw
113+
114+ if got := check (tt .args .cmd , tt .args .args ); got != tt .want {
115+ t .Errorf ("check() = %v, want %v" , got , tt .want )
116+ }
117+ pw .Close ()
118+ print .Stdout = orgStdout
119+ print .Stderr = orgStderr
120+
121+ buf := bytes.Buffer {}
122+ _ , err = io .Copy (& buf , pr )
123+ if err != nil {
124+ t .Error (err )
125+ }
126+ defer pr .Close ()
127+ got := strings .Split (buf .String (), "\n " )
128+
129+ if diff := cmp .Diff (tt .stderr , got ); diff != "" {
130+ t .Errorf ("value is mismatch (-want +got):\n %s" , diff )
131+ }
132+ })
133+ }
134+ }
135+
73136func Test_check_not_use_go_cmd (t * testing.T ) {
74137 t .Run ("Not found go command" , func (t * testing.T ) {
75138 t .Setenv ("PATH" , "" )
@@ -99,7 +162,7 @@ func Test_check_not_use_go_cmd(t *testing.T) {
99162 got := strings .Split (buf .String (), "\n " )
100163
101164 want := []string {}
102- if runtime .GOOS == "windows" {
165+ if runtime .GOOS == goosWindows {
103166 want = append (want , `gup:ERROR: you didn't install golang: exec: "go": executable file not found in %PATH%` )
104167 want = append (want , "" )
105168 } else {
@@ -114,7 +177,6 @@ func Test_check_not_use_go_cmd(t *testing.T) {
114177
115178func Test_check_gobin_is_empty (t * testing.T ) {
116179 type args struct {
117- cmd * cobra.Command
118180 args []string
119181 }
120182 tests := []struct {
@@ -136,7 +198,7 @@ func Test_check_gobin_is_empty(t *testing.T) {
136198 },
137199 }
138200
139- if runtime .GOOS == "windows" {
201+ if runtime .GOOS == goosWindows {
140202 tests = append (tests , struct {
141203 name string
142204 gobin string
0 commit comments