@@ -18,68 +18,6 @@ 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-
8321func Test_validPkgInfo (t * testing.T ) {
8422 type args struct {
8523 pkgs []goutil.Package
@@ -156,33 +94,21 @@ func Test_export_not_use_go_cmd(t *testing.T) {
15694}
15795
15896func Test_export (t * testing.T ) {
159- type args struct {
160- cmd * cobra.Command
161- args []string
162- }
16397 tests := []struct {
16498 name string
165- args args
99+ args [] string
166100 gobin string
167101 want int
168102 stderr []string
169103 }{
170104 {
171- name : "can not make .config directory" ,
172- args : args {
173- cmd : & cobra.Command {},
174- args : []string {},
175- },
105+ name : "can not make .config directory" ,
176106 gobin : "" ,
177107 want : 1 ,
178108 stderr : []string {},
179109 },
180110 {
181- name : "no package information" ,
182- args : args {
183- cmd : & cobra.Command {},
184- args : []string {},
185- },
111+ name : "no package information" ,
186112 gobin : filepath .Join ("testdata" , "text" ),
187113 want : 1 ,
188114 stderr : []string {
@@ -196,17 +122,13 @@ func Test_export(t *testing.T) {
196122 if runtime .GOOS == "windows" {
197123 tests = append (tests , struct {
198124 name string
199- args args
125+ args [] string
200126 gobin string
201127 want int
202128 stderr []string
203129 }{
204130
205- name : "not exist gobin directory" ,
206- args : args {
207- cmd : & cobra.Command {},
208- args : []string {},
209- },
131+ name : "not exist gobin directory" ,
210132 gobin : filepath .Join ("testdata" , "dummy" ),
211133 want : 1 ,
212134 stderr : []string {
@@ -217,17 +139,13 @@ func Test_export(t *testing.T) {
217139 } else {
218140 tests = append (tests , struct {
219141 name string
220- args args
142+ args [] string
221143 gobin string
222144 want int
223145 stderr []string
224146 }{
225147
226- name : "not exist gobin directory" ,
227- args : args {
228- cmd : & cobra.Command {},
229- args : []string {},
230- },
148+ name : "not exist gobin directory" ,
231149 gobin : filepath .Join ("testdata" , "dummy" ),
232150 want : 1 ,
233151 stderr : []string {
@@ -258,8 +176,7 @@ func Test_export(t *testing.T) {
258176 print .Stdout = pw
259177 print .Stderr = pw
260178
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 {
179+ if got := export (newExportCmd (), tt .args ); got != tt .want {
263180 t .Errorf ("export() = %v, want %v" , got , tt .want )
264181 }
265182 pw .Close ()
@@ -287,42 +204,6 @@ func Test_export(t *testing.T) {
287204 }
288205}
289206
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-
326207func Test_writeConfigFile (t * testing.T ) {
327208 type args struct {
328209 pkgs []goutil.Package
0 commit comments