Skip to content

Commit 0fa8601

Browse files
authored
Revert "chore: clean up tests"
1 parent c9f34e6 commit 0fa8601

File tree

11 files changed

+894
-153
lines changed

11 files changed

+894
-153
lines changed

.gitignore

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

cmd/check_test.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,83 @@ 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+
1880
func Test_check(t *testing.T) {
81+
type args struct {
82+
cmd *cobra.Command
83+
args []string
84+
}
1985
tests := []struct {
2086
name string
2187
gobin string
22-
args []string
88+
args args
2389
want int
2490
}{
2591
{
2692
name: "not go install command in $GOBIN",
2793
gobin: filepath.Join("testdata", "check_fail"),
94+
args: args{},
2895
want: 1,
2996
},
3097
}
@@ -41,7 +108,10 @@ func Test_check(t *testing.T) {
41108
print.Stdout = pw
42109
print.Stderr = pw
43110

44-
if got := check(newCheckCmd(), tt.args); got != tt.want {
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 {
45115
t.Errorf("check() = %v, want %v", got, tt.want)
46116
}
47117
pw.Close()
@@ -189,7 +259,10 @@ func Test_check_gobin_is_empty(t *testing.T) {
189259
print.Stdout = pw
190260
print.Stderr = pw
191261

192-
if got := check(newCheckCmd(), tt.args.args); got != tt.want {
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 {
193266
t.Errorf("check() = %v, want %v", got, tt.want)
194267
}
195268
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(print.Stdout, pkgs)
98+
return config.WriteConfFile(os.Stdout, pkgs)
9999
}
100100

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

cmd/export_test.go

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

96158
func 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+
207326
func Test_writeConfigFile(t *testing.T) {
208327
type args struct {
209328
pkgs []goutil.Package

0 commit comments

Comments
 (0)