-
Notifications
You must be signed in to change notification settings - Fork 16
/
stats_test.go
199 lines (156 loc) · 4.32 KB
/
stats_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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"testing"
)
func createSourceFixture(dir, name, fixture string) {
os.MkdirAll(dir, 0755)
err := ioutil.WriteFile(path.Join(dir, name), []byte(fixture), 0644)
check(err)
}
func TestAnalyzeSourceTree(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
if stats.ImportStatsByPath["github.com/pelletier/go-toml"] == nil {
t.Error("Expected to have go-toml in the dependencies tree")
}
}
func TestAnalyzeSourceTreeIgnoresGopack(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
createSourceFixture(path.Join(pwd, GopackDir, "src"), "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
i := stats.ImportStatsByPath["github.com/pelletier/go-toml"]
if len(i.ReferencePositions) == 2 {
t.Error("Expected to ignore the gopack directory")
}
}
func TestReferenceDifferentDependencies(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
createSourceFixture(pwd, "bar.go", `package main
import "github.com/gorilla/mux"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
istats := stats.ImportStatsByPath["github.com/pelletier/go-toml"]
if !istats.Remote {
t.Errorf("Expected reference to be remote\n")
}
if len(istats.ReferencePositions) != 1 {
t.Errorf("Expected to have 1 reference to github.com/pelletier/go-toml\n")
}
istats = stats.ImportStatsByPath["github.com/gorilla/mux"]
if !istats.Remote {
t.Errorf("Expected reference to be remote\n")
}
if len(istats.ReferencePositions) != 1 {
t.Errorf("Expected to have 1 reference to github.com/gorilla/mux\n")
}
}
func TestReferenceSameDependencies(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
createSourceFixture(pwd, "bar.go", `package main
import "fmt"
import "github.com/pelletier/go-toml"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
istats := stats.ImportStatsByPath["github.com/pelletier/go-toml"]
if !istats.Remote {
t.Errorf("Expected reference to be remote\n")
}
if len(istats.ReferencePositions) != 2 {
t.Errorf("Expected to have 2 references to github.com/pelletier/go-toml\n")
}
expected := fmt.Sprintf("* %s/bar.go:3\n* %s/foo.go:2", pwd, pwd)
list := istats.ReferenceList()
if list != expected {
t.Errorf("Expected reference list to be %s but it was %s\n", expected, list)
}
}
func TestReferenceLocalDependencies(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "bar.go", `package main
import "fmt"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
istats := stats.ImportStatsByPath["fmt"]
if istats.Remote {
t.Errorf("Expected reference to not be remote\n")
}
}
func TestUsedDependencies(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "bar.go", `package main
import "fmt"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
if !stats.IsImportUsed("fmt") {
t.Errorf("Expected fmt to be used\n")
}
if stats.IsImportUsed("os") {
t.Errorf("Expected os to not be used\n")
}
}
func TestGetStatsSummary(t *testing.T) {
setupTestPwd()
createSourceFixture(pwd, "foo.go", `package main
import "github.com/pelletier/go-toml"
`)
createSourceFixture(pwd, "bar.go", `package main
import "fmt"
import "./foo"
import "github.com/pelletier/go-foo"
import "github.com/pelletier/go-toml"
`)
stats, err := AnalyzeSourceTree(pwd)
if err != nil {
t.Fatal(err)
}
s := stats.GetSummary()
checkSumaryItem(t, s.Get(0), "github.com/pelletier/go-toml", "R github.com/pelletier/go-toml 2")
checkSumaryItem(t, s.Get(1), "github.com/pelletier/go-foo", "R github.com/pelletier/go-foo 1")
checkSumaryItem(t, s.Get(2), "./foo", "L ./foo 1")
checkSumaryItem(t, s.Get(3), "fmt", "S fmt 1")
}
func checkSumaryItem(t *testing.T, item SummaryItem, path, legend string) {
if item.Path != path {
t.Errorf("Expected item to be %s\n", path)
}
actual := item.Legend()
if actual != legend {
t.Errorf("Expected legend to be %s, but was %s\n", legend, actual)
}
}