-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
334 lines (308 loc) · 8.1 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bytes"
"flag"
"fmt"
"math/rand"
"os"
"reflect"
"strings"
"testing"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
const fixture = "fixtures/go.mod"
const nonexistentFixture = "fixtures/nonexistent_go.mod"
const invalidFixture = "fixtures/invalid_go.mod"
func TestMainFunction(t *testing.T) {
t.Setenv("TEST_MODE", "true")
// Save original os.Args and restore it after the test
origArgs := os.Args
defer func() { os.Args = origArgs }()
// Save original stdout and stderr and restore them after the test
origStdout := os.Stdout
origStderr := os.Stderr
defer func() {
os.Stdout = origStdout
os.Stderr = origStderr
}()
// Create pipes to capture stdout and stderr output
rOut, wOut, _ := os.Pipe()
rErr, wErr, _ := os.Pipe()
os.Stdout = wOut
os.Stderr = wErr
tests := []struct {
name string
args []string
modfile string
wantOut string
wantErr string
wantPanic bool
}{
{
name: "Help flag",
args: []string{"-help"},
wantOut: usage,
},
{
name: "No modfile provided",
args: []string{"-modfile", ""},
wantErr: "go.mod file not provided\n\n" + usage + "\n" + usageExample + "\n",
},
{
name: "Invalid modfile path",
args: []string{"-modfile", nonexistentFixture},
wantErr: "failed to read go.mod file: open invalid/path/go.mod: no such file or directory\n\n",
},
{
name: "Invalid modfile content",
args: []string{"-modfile", invalidFixture},
wantErr: "failed to parse go.mod file: testdata/invalid_go.mod:3: invalid module path: invalid content\n\n",
},
}
for _, tt := range tests {
// random string to avoid flag conflicts
s := fmt.Sprintf("TestMainFunction-%d", rand.Int())
flag.CommandLine = flag.NewFlagSet(s, flag.ExitOnError)
t.Run(tt.name, func(t *testing.T) {
// Set os.Args for the test
os.Args = append([]string{"cmd"}, tt.args...)
// Write modfile content if provided
if tt.modfile != "" {
err := os.WriteFile("testdata/go.mod", []byte(tt.modfile), 0644)
if err != nil {
t.Fatal(err)
}
defer os.Remove("testdata/go.mod")
}
// Run main function and capture panic if any
defer func() {
if r := recover(); r != nil {
if tt.wantErr == "" {
t.Errorf("main() panicked: %v", r)
}
}
}()
main()
// Close writers and read captured output
wOut.Close()
wErr.Close()
var bufOut, bufErr bytes.Buffer
if _, err := bufOut.ReadFrom(rOut); err != nil {
t.Fatal(err)
}
if _, err := bufErr.ReadFrom(rErr); err != nil {
t.Fatal(err)
}
// Check stdout
if gotOut := bufOut.String(); !strings.Contains(gotOut, tt.wantOut) {
if tt.wantOut == "" && gotOut == "" {
return
}
t.Errorf("stdout = %v, want %v", gotOut, tt.wantOut)
}
// Check stderr
if gotErr := bufErr.String(); !strings.Contains(gotErr, tt.wantErr) {
if tt.wantErr == "" && gotErr == "" {
return
}
t.Errorf("stderr = %v, want %v", gotErr, tt.wantErr)
}
})
}
}
func TestPrintError(t *testing.T) {
t.Setenv("TEST_MODE", "true")
// Save original stderr and restore it after the test
origStderr := os.Stderr
defer func() { os.Stderr = origStderr }()
// Create a pipe to capture stderr output
r, w, _ := os.Pipe()
os.Stderr = w
// Test cases
tests := []struct {
name string
message string
err error
with []bool
want string
}{
{
name: "Error with message only",
message: "test error",
err: nil,
with: nil,
want: "test error\n\n",
},
{
name: "Error with message and error",
message: "test error",
err: fmt.Errorf("an error occurred"),
with: nil,
want: "test error: an error occurred\n\n",
},
{
name: "Error with message, error, and usage",
message: "test error",
err: fmt.Errorf("an error occurred"),
with: []bool{true},
want: "test error: an error occurred\n\n" + usage + "\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// recover from panic to simulate os.Exit
defer func() {
if r := recover(); r != nil {
fmt.Print("Recovered from panic: ", r)
}
}()
// Reset flag.Usage to avoid printing usage multiple times
flag.Usage = func() {}
// Call printError
printError(tt.message, tt.err, tt.with...)
// Close the writer and read the captured output
w.Close()
var buf bytes.Buffer
if _, err := buf.ReadFrom(r); err != nil {
t.Fatal(err)
}
// Check if the output matches the expected output
if got := buf.String(); got != tt.want {
t.Errorf("printError() = %v, want %v", got, tt.want)
}
})
}
}
func TestCompareRequire(t *testing.T) {
tests := []struct {
name string
a string
b string
want bool
}{
{
name: "Exact match",
a: "github.com/gorilla/mux",
b: "github.com/gorilla/mux",
want: true,
},
{
name: "Wildcard match",
a: "github.com/gorilla/*",
b: "github.com/gorilla/mux",
want: true,
},
{
name: "No match",
a: "github.com/gorilla/schema",
b: "github.com/gorilla/mux",
want: false,
},
{
name: "Wildcard no match",
a: "github.com/gorilla/*",
b: "github.com/another/package",
want: false,
},
{
name: "Partial match",
a: "github.com/gorilla",
b: "github.com/gorilla/mux",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := compareRequire(tt.a, tt.b); got != tt.want {
t.Errorf("compareRequire(%v, %v) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestFindPackages(t *testing.T) {
tests := []struct {
name string
modfile *modfile.File
pkgs []string
wantPkg []Package
wantFound []bool
}{
{
name: "Single package found",
modfile: &modfile.File{
Require: []*modfile.Require{
{Mod: module.Version{Path: "github.com/gorilla/mux", Version: "v1.8.0"}},
},
},
pkgs: []string{"github.com/gorilla/mux"},
wantPkg: []Package{
{Path: "github.com/gorilla/mux", Version: "v1.8.0"},
},
wantFound: []bool{true},
},
{
name: "Multiple packages found",
modfile: &modfile.File{
Require: []*modfile.Require{
{Mod: module.Version{Path: "github.com/gorilla/mux", Version: "v1.8.0"}},
{Mod: module.Version{Path: "github.com/gorilla/schema", Version: "v1.2.0"}},
},
},
pkgs: []string{"github.com/gorilla/mux", "github.com/gorilla/schema"},
wantPkg: []Package{
{Path: "github.com/gorilla/mux", Version: "v1.8.0"},
{Path: "github.com/gorilla/schema", Version: "v1.2.0"},
},
wantFound: []bool{true, true},
},
{
name: "Package not found",
modfile: &modfile.File{
Require: []*modfile.Require{
{Mod: module.Version{Path: "github.com/gorilla/mux", Version: "v1.8.0"}},
},
},
pkgs: []string{"github.com/nonexistent/package"},
wantPkg: []Package{},
wantFound: []bool{false},
},
{
name: "Wildcard package found",
modfile: &modfile.File{
Require: []*modfile.Require{
{Mod: module.Version{Path: "github.com/gorilla/mux", Version: "v1.8.0"}},
{Mod: module.Version{Path: "github.com/gorilla/schema", Version: "v1.2.0"}},
},
},
pkgs: []string{"github.com/gorilla/*"},
wantPkg: []Package{
{Path: "github.com/gorilla/mux", Version: "v1.8.0"},
{Path: "github.com/gorilla/schema", Version: "v1.2.0"},
},
wantFound: []bool{true},
},
{
name: "Partial package found",
modfile: &modfile.File{
Require: []*modfile.Require{
{Mod: module.Version{Path: "github.com/gorilla/mux", Version: "v1.8.0"}},
},
},
pkgs: []string{"github.com/gorilla"},
wantPkg: []Package{},
wantFound: []bool{false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPkg, gotFound := findPackages(tt.modfile, tt.pkgs)
if (len(gotPkg) != 0 && len(tt.wantPkg) != 0) && !reflect.DeepEqual(gotPkg, tt.wantPkg) {
t.Errorf("findPackages() gotPkg = %v, want %v", gotPkg, tt.wantPkg)
}
if (len(gotFound) != 0 && len(tt.wantFound) != 0) && !reflect.DeepEqual(gotFound, tt.wantFound) {
t.Errorf("findPackages() gotFound = %v, want %v", gotFound, tt.wantFound)
}
})
}
}