@@ -18,6 +18,68 @@ import (
1818 "github.com/spf13/cobra"
1919)
2020
21+ func Test_ExportOption (t * testing.T ) {
22+ type args struct {
23+ cmd * cobra.Command
24+ args []string
25+ }
26+ tests := []struct {
27+ name string
28+ args args
29+ want int
30+ stderr []string
31+ }{
32+ {
33+ name : "parser --output argument error" ,
34+ args : args {
35+ cmd : & cobra.Command {},
36+ args : []string {},
37+ },
38+ want : 1 ,
39+ stderr : []string {
40+ "gup:ERROR: can not parse command line argument (--output): flag accessed but not defined: output" ,
41+ "" ,
42+ },
43+ },
44+ }
45+ for _ , tt := range tests {
46+ t .Run (tt .name , func (t * testing.T ) {
47+ OsExit = func (code int ) {}
48+ defer func () {
49+ OsExit = os .Exit
50+ }()
51+
52+ orgStdout := print .Stdout
53+ orgStderr := print .Stderr
54+ pr , pw , err := os .Pipe ()
55+ if err != nil {
56+ t .Fatal (err )
57+ }
58+ print .Stdout = pw
59+ print .Stderr = pw
60+
61+ if got := export (tt .args .cmd , tt .args .args ); got != tt .want {
62+ t .Errorf ("export() = %v, want %v" , got , tt .want )
63+ }
64+ pw .Close ()
65+ print .Stdout = orgStdout
66+ print .Stderr = orgStderr
67+
68+ buf := bytes.Buffer {}
69+ _ , err = io .Copy (& buf , pr )
70+ if err != nil {
71+ t .Error (err )
72+ }
73+ defer pr .Close ()
74+ got := strings .Split (buf .String (), "\n " )
75+
76+ if diff := cmp .Diff (tt .stderr , got ); diff != "" {
77+ t .Errorf ("value is mismatch (-want +got):\n %s" , diff )
78+ }
79+ })
80+ }
81+ }
82+
2183func Test_validPkgInfo (t * testing.T ) {
2284 type args struct {
2385 pkgs []goutil.Package
@@ -94,21 +156,33 @@ func Test_export_not_use_go_cmd(t *testing.T) {
94156}
95157
96158func Test_export (t * testing.T ) {
159+ type args struct {
160+ cmd * cobra.Command
161+ args []string
162+ }
97163 tests := []struct {
98164 name string
99- args [] string
165+ args args
100166 gobin string
101167 want int
102168 stderr []string
103169 }{
104170 {
105- name : "can not make .config directory" ,
171+ name : "can not make .config directory" ,
172+ args : args {
173+ cmd : & cobra.Command {},
174+ args : []string {},
175+ },
106176 gobin : "" ,
107177 want : 1 ,
108178 stderr : []string {},
109179 },
110180 {
111- name : "no package information" ,
181+ name : "no package information" ,
182+ args : args {
183+ cmd : & cobra.Command {},
184+ args : []string {},
185+ },
112186 gobin : filepath .Join ("testdata" , "text" ),
113187 want : 1 ,
114188 stderr : []string {
@@ -122,13 +196,17 @@ func Test_export(t *testing.T) {
122196 if runtime .GOOS == "windows" {
123197 tests = append (tests , struct {
124198 name string
125- args [] string
199+ args args
126200 gobin string
127201 want int
128202 stderr []string
129203 }{
130204
131- name : "not exist gobin directory" ,
205+ name : "not exist gobin directory" ,
206+ args : args {
207+ cmd : & cobra.Command {},
208+ args : []string {},
209+ },
132210 gobin : filepath .Join ("testdata" , "dummy" ),
133211 want : 1 ,
134212 stderr : []string {
@@ -139,13 +217,17 @@ func Test_export(t *testing.T) {
139217 } else {
140218 tests = append (tests , struct {
141219 name string
142- args [] string
220+ args args
143221 gobin string
144222 want int
145223 stderr []string
146224 }{
147225
148- name : "not exist gobin directory" ,
226+ name : "not exist gobin directory" ,
227+ args : args {
228+ cmd : & cobra.Command {},
229+ args : []string {},
230+ },
149231 gobin : filepath .Join ("testdata" , "dummy" ),
150232 want : 1 ,
151233 stderr : []string {
@@ -176,7 +258,8 @@ func Test_export(t *testing.T) {
176258 print .Stdout = pw
177259 print .Stderr = pw
178260
179- if got := export (newExportCmd (), tt .args ); got != tt .want {
261+ tt .args .cmd .Flags ().BoolP ("output" , "o" , false , "print command path information at STDOUT" )
262+ if got := export (tt .args .cmd , tt .args .args ); got != tt .want {
180263 t .Errorf ("export() = %v, want %v" , got , tt .want )
181264 }
182265 pw .Close ()
@@ -204,6 +287,42 @@ func Test_export(t *testing.T) {
204287 }
205288}
206289
290+ func Test_export_parse_error (t * testing.T ) {
291+ t .Run ("parse argument error" , func (t * testing.T ) {
292+ orgStdout := print .Stdout
293+ orgStderr := print .Stderr
294+ pr , pw , err := os .Pipe ()
295+ if err != nil {
296+ t .Fatal (err )
297+ }
298+ print .Stdout = pw
299+ print .Stderr = pw
300+
301+ if got := export (& cobra.Command {}, []string {}); got != 1 {
302+ t .Errorf ("export() = %v, want %v" , got , 1 )
303+ }
304+ pw .Close ()
305+ print .Stdout = orgStdout
306+ print .Stderr = orgStderr
307+
308+ buf := bytes.Buffer {}
309+ _ , err = io .Copy (& buf , pr )
310+ if err != nil {
311+ t .Error (err )
312+ }
313+ defer pr .Close ()
314+ got := strings .Split (buf .String (), "\n " )
315+
316+ want := []string {
317+ "gup:ERROR: can not parse command line argument (--output): flag accessed but not defined: output" ,
318+ "" ,
319+ }
320+ if diff := cmp .Diff (want , got ); diff != "" {
321+ t .Errorf ("value is mismatch (-want +got):\n %s" , diff )
322+ }
323+ })
324+ }
325+
207326func Test_writeConfigFile (t * testing.T ) {
208327 type args struct {
209328 pkgs []goutil.Package
0 commit comments