-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiteratePackages_test.go
142 lines (140 loc) · 3.74 KB
/
iteratePackages_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
package parser
import (
"fmt"
"github.com/mathbalduino/go-log/loggerCLI"
"golang.org/x/tools/go/packages"
"testing"
)
func TestIteratePackages(t *testing.T) {
t.Run("Should use the optionalLogger, if provided, giving it to the callback", func(t *testing.T) {
mock := &mockLoggerCLI{}
mock.mockTrace = func(string, ...interface{}) LoggerCLI { return mock }
p := &GoParser{pkgs: []*packages.Package{{}}, logger: loggerCLI.New(false, 0)}
e := p.iteratePackages(func(_ *packages.Package, logger LoggerCLI) error {
m, isMock := logger.(*mockLoggerCLI)
if !isMock || m != mock {
t.Fatalf("The LoggerCLI given to the callback is not the expected one")
}
return nil
}, mock)
if e != nil {
t.Fatalf("Expected to be nil")
}
})
t.Run("Should return nil when there are no packages", func(t *testing.T) {
p := &GoParser{pkgs: nil, logger: loggerCLI.New(false, 0)}
e := p.iteratePackages(nil)
if e != nil {
t.Fatalf("Expected to be nil")
}
})
t.Run("Should skip packages with error", func(t *testing.T) {
m := loggerCLI.New(false, 0)
okPkg := "c"
p := &GoParser{
pkgs: []*packages.Package{
{PkgPath: "a", Errors: []packages.Error{{}, {}}},
{PkgPath: "b", Errors: []packages.Error{{}}},
{PkgPath: okPkg, Errors: nil},
{PkgPath: "d", Errors: []packages.Error{{}}},
},
logger: m,
}
calls := 0
e := p.iteratePackages(func(pkg *packages.Package, parentLog LoggerCLI) error {
calls += 1
if pkg.PkgPath != okPkg {
t.Fatalf("Packages with error should be skipped")
}
return nil
})
if e != nil {
t.Fatalf("Expected to be nil")
}
if calls != 1 {
t.Fatalf("Expected to iterate over just one pkg")
}
})
t.Run("Should stop iteration when the callback returns any error, forwarding the returned error", func(t *testing.T) {
m := loggerCLI.New(false, 0)
p := &GoParser{
pkgs: []*packages.Package{
{Errors: nil},
{Errors: nil},
},
logger: m,
}
calls := 0
e := p.iteratePackages(func(pkg *packages.Package, parentLog LoggerCLI) error {
calls += 1
return fmt.Errorf("any")
})
if e == nil {
t.Fatalf("Expected to be not nil")
}
if calls != 1 {
t.Fatalf("Expected to stop at the first callback returned error")
}
})
t.Run("Should skip any package that is not the focus", func(t *testing.T) {
m := loggerCLI.New(false, 0)
focusPkg := "focusedPkg"
p := &GoParser{
pkgs: []*packages.Package{
{PkgPath: "a", Errors: nil},
{PkgPath: focusPkg, Errors: nil},
{PkgPath: "b", Errors: nil},
},
logger: m,
focus: FocusPackagePath(focusPkg),
}
calls := 0
e := p.iteratePackages(func(pkg *packages.Package, parentLog LoggerCLI) error {
calls += 1
if pkg.PkgPath != focusPkg {
t.Fatalf("Expected to iterate only over the focused package")
}
return nil
})
if e != nil {
t.Fatalf("Expected to be nil")
}
if calls != 1 {
t.Fatalf("Expected to iterate only over the focused package")
}
})
t.Run("Should call the callback for every package that needs to be iterated and return nil error", func(t *testing.T) {
m := loggerCLI.New(false, 0)
p := &GoParser{
pkgs: []*packages.Package{
{PkgPath: "a", Errors: nil},
{PkgPath: "b", Errors: nil},
{PkgPath: "c", Errors: nil},
},
logger: m,
}
aCalls, bCalls, cCalls := 0, 0, 0
e := p.iteratePackages(func(pkg *packages.Package, parentLog LoggerCLI) error {
switch pkg.PkgPath {
case "a":
aCalls += 1
return nil
case "b":
bCalls += 1
return nil
case "c":
cCalls += 1
return nil
default:
t.Fatalf("Unexpected package iteration")
return nil
}
})
if e != nil {
t.Fatalf("Expected to be nil")
}
if aCalls != 1 || bCalls != 1 || cCalls != 1 {
t.Fatalf("Each package must be iterated one time")
}
})
}