-
Notifications
You must be signed in to change notification settings - Fork 31
/
licenses_test.go
214 lines (194 loc) · 5.36 KB
/
licenses_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
package main
import (
"fmt"
"path/filepath"
"strings"
"testing"
)
type testResult struct {
Package string
License string
Score int
Extra int
Missing int
Err string
}
func listTestLicenses(pkgs []string) ([]testResult, error) {
gopath, err := filepath.Abs("testdata")
if err != nil {
return nil, err
}
licenses, err := listLicenses(gopath, pkgs)
if err != nil {
return nil, err
}
res := []testResult{}
for _, l := range licenses {
r := testResult{
Package: l.Package,
}
if l.Template != nil {
r.License = l.Template.Title
r.Score = int(100 * l.Score)
}
if l.Err != "" {
r.Err = "some error"
}
r.Extra = len(l.ExtraWords)
r.Missing = len(l.MissingWords)
res = append(res, r)
}
return res, nil
}
func compareTestLicenses(pkgs []string, wanted []testResult) error {
stringify := func(res []testResult) string {
parts := []string{}
for _, r := range res {
s := fmt.Sprintf("%s \"%s\" %d%%", r.Package, r.License, r.Score)
if r.Err != "" {
s += " " + r.Err
}
if r.Extra > 0 {
s += fmt.Sprintf(" +%d", r.Extra)
}
if r.Missing > 0 {
s += fmt.Sprintf(" -%d", r.Missing)
}
parts = append(parts, s)
}
return strings.Join(parts, "\n")
}
licenses, err := listTestLicenses(pkgs)
if err != nil {
return err
}
got := stringify(licenses)
expected := stringify(wanted)
if got != expected {
return fmt.Errorf("licenses do not match:\n%s\n!=\n%s", got, expected)
}
return nil
}
func TestNoDependencies(t *testing.T) {
err := compareTestLicenses([]string{"colors/red"}, []testResult{
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
})
if err != nil {
t.Fatal(err)
}
}
func TestMultipleLicenses(t *testing.T) {
err := compareTestLicenses([]string{"colors/blue"}, []testResult{
{Package: "colors/blue", License: "Apache License 2.0", Score: 100},
})
if err != nil {
t.Fatal(err)
}
}
func TestNoLicense(t *testing.T) {
err := compareTestLicenses([]string{"colors/green"}, []testResult{
{Package: "colors/green", License: "", Score: 0},
})
if err != nil {
t.Fatal(err)
}
}
func TestMainWithDependencies(t *testing.T) {
// It also tests license retrieval in parent directory.
err := compareTestLicenses([]string{"colors/cmd/paint"}, []testResult{
{Package: "colors/cmd/paint", License: "Academic Free License v3.0", Score: 100},
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
})
if err != nil {
t.Fatal(err)
}
}
func TestMainWithAliasedDependencies(t *testing.T) {
err := compareTestLicenses([]string{"colors/cmd/mix"}, []testResult{
{Package: "colors/cmd/mix", License: "Academic Free License v3.0", Score: 100},
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
{Package: "couleurs/red", License: "GNU Lesser General Public License v2.1",
Score: 100},
})
if err != nil {
t.Fatal(err)
}
}
func TestMissingPackage(t *testing.T) {
_, err := listTestLicenses([]string{"colors/missing"})
if err == nil {
t.Fatal("no error on missing package")
}
if _, ok := err.(*MissingError); !ok {
t.Fatalf("MissingError expected")
}
}
func TestMismatch(t *testing.T) {
err := compareTestLicenses([]string{"colors/yellow"}, []testResult{
{Package: "colors/yellow", License: "Microsoft Reciprocal License", Score: 25,
Extra: 106, Missing: 131},
})
if err != nil {
t.Fatal(err)
}
}
func TestNoBuildableGoSourceFiles(t *testing.T) {
_, err := listTestLicenses([]string{"colors/cmd"})
if err == nil {
t.Fatal("no error on missing package")
}
if _, ok := err.(*MissingError); !ok {
t.Fatalf("MissingError expected")
}
}
func TestBroken(t *testing.T) {
err := compareTestLicenses([]string{"colors/broken"}, []testResult{
{Package: "colors/broken", License: "GNU General Public License v3.0", Score: 100},
{Package: "colors/missing", License: "", Score: 0, Err: "some error"},
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
})
if err != nil {
t.Fatal(err)
}
}
func TestBrokenDependency(t *testing.T) {
err := compareTestLicenses([]string{"colors/purple"}, []testResult{
{Package: "colors/broken", License: "GNU General Public License v3.0", Score: 100},
{Package: "colors/missing", License: "", Score: 0, Err: "some error"},
{Package: "colors/purple", License: "", Score: 0},
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
})
if err != nil {
t.Fatal(err)
}
}
func TestPackageExpression(t *testing.T) {
err := compareTestLicenses([]string{"colors/cmd/..."}, []testResult{
{Package: "colors/cmd/mix", License: "Academic Free License v3.0", Score: 100},
{Package: "colors/cmd/paint", License: "Academic Free License v3.0", Score: 100},
{Package: "colors/red", License: "MIT License", Score: 98, Missing: 2},
{Package: "couleurs/red", License: "GNU Lesser General Public License v2.1",
Score: 100},
})
if err != nil {
t.Fatal(err)
}
}
func TestCleanLicenseData(t *testing.T) {
data := `The MIT License (MIT)
Copyright (c) 2013 Ben Johnson
Some other lines.
And more.
`
cleaned := string(cleanLicenseData([]byte(data)))
wanted := "the mit license (mit)\n\t\n\tsome other lines.\n\tand more.\n\t"
if wanted != cleaned {
t.Fatalf("license data mismatch: %q\n!=\n%q", cleaned, wanted)
}
}
func TestStandardPackages(t *testing.T) {
err := compareTestLicenses([]string{"encoding/json", "cmd/addr2line"}, []testResult{})
if err != nil {
t.Fatal(err)
}
}