Skip to content

Commit edbd94c

Browse files
authored
Merge pull request #203 from iTrooz/tests
chore: clean up tests
2 parents 157b232 + b83000b commit edbd94c

File tree

11 files changed

+153
-894
lines changed

11 files changed

+153
-894
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ gup
1717
dist
1818
cover.*
1919
/completions
20+
21+
cmd/testdata/gobin_tmp

cmd/check_test.go

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,16 @@ import (
1515
"github.com/spf13/cobra"
1616
)
1717

18-
func Test_CheckOption(t *testing.T) {
19-
type args struct {
20-
cmd *cobra.Command
21-
args []string
22-
}
23-
tests := []struct {
24-
name string
25-
args args
26-
want int
27-
stderr []string
28-
}{
29-
{
30-
name: "parser --jobs argument error",
31-
args: args{
32-
cmd: &cobra.Command{},
33-
args: []string{},
34-
},
35-
want: 1,
36-
stderr: []string{
37-
"gup:ERROR: can not parse command line argument (--jobs): flag accessed but not defined: jobs",
38-
"",
39-
},
40-
},
41-
}
42-
for _, tt := range tests {
43-
t.Run(tt.name, func(t *testing.T) {
44-
OsExit = func(code int) {}
45-
defer func() {
46-
OsExit = os.Exit
47-
}()
48-
49-
orgStdout := print.Stdout
50-
orgStderr := print.Stderr
51-
pr, pw, err := os.Pipe()
52-
if err != nil {
53-
t.Fatal(err)
54-
}
55-
print.Stdout = pw
56-
print.Stderr = pw
57-
58-
if got := check(tt.args.cmd, tt.args.args); got != tt.want {
59-
t.Errorf("check() = %v, want %v", got, tt.want)
60-
}
61-
pw.Close()
62-
print.Stdout = orgStdout
63-
print.Stderr = orgStderr
64-
65-
buf := bytes.Buffer{}
66-
_, err = io.Copy(&buf, pr)
67-
if err != nil {
68-
t.Error(err)
69-
}
70-
defer pr.Close()
71-
got := strings.Split(buf.String(), "\n")
72-
73-
if diff := cmp.Diff(tt.stderr, got); diff != "" {
74-
t.Errorf("value is mismatch (-want +got):\n%s", diff)
75-
}
76-
})
77-
}
78-
}
79-
8018
func Test_check(t *testing.T) {
81-
type args struct {
82-
cmd *cobra.Command
83-
args []string
84-
}
8519
tests := []struct {
8620
name string
8721
gobin string
88-
args args
22+
args []string
8923
want int
9024
}{
9125
{
9226
name: "not go install command in $GOBIN",
9327
gobin: filepath.Join("testdata", "check_fail"),
94-
args: args{},
9528
want: 1,
9629
},
9730
}
@@ -108,10 +41,7 @@ func Test_check(t *testing.T) {
10841
print.Stdout = pw
10942
print.Stderr = pw
11043

111-
cmd := &cobra.Command{}
112-
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
113-
cmd.Flags().Bool("ignore-go-update", false, "Ignore updates to the Go toolchain")
114-
if got := check(cmd, tt.args.args); got != tt.want {
44+
if got := check(newCheckCmd(), tt.args); got != tt.want {
11545
t.Errorf("check() = %v, want %v", got, tt.want)
11646
}
11747
pw.Close()
@@ -259,10 +189,7 @@ func Test_check_gobin_is_empty(t *testing.T) {
259189
print.Stdout = pw
260190
print.Stderr = pw
261191

262-
cmd := &cobra.Command{}
263-
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")
264-
cmd.Flags().Bool("ignore-go-update", false, "Ignore updates to the Go toolchain")
265-
if got := check(cmd, tt.args.args); got != tt.want {
192+
if got := check(newCheckCmd(), tt.args.args); got != tt.want {
266193
t.Errorf("check() = %v, want %v", got, tt.want)
267194
}
268195
pw.Close()

cmd/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func writeConfigFile(pkgs []goutil.Package) error {
9595
}
9696

9797
func outputConfig(pkgs []goutil.Package) error {
98-
return config.WriteConfFile(os.Stdout, pkgs)
98+
return config.WriteConfFile(print.Stdout, pkgs)
9999
}
100100

101101
func validPkgInfo(pkgs []goutil.Package) []goutil.Package {

cmd/export_test.go

Lines changed: 8 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
8321
func 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

15896
func 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-
326207
func Test_writeConfigFile(t *testing.T) {
327208
type args struct {
328209
pkgs []goutil.Package

0 commit comments

Comments
 (0)