forked from pmezard/licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
licenses_test.go
230 lines (208 loc) · 5.72 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/groove-x/go-licenses/modinfo"
)
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 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 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)
}
}
func TestFilterLinkedModule(t *testing.T) {
tests := []struct {
Path string
Linked bool
}{
// only module aware packages are supposed to be linked
{Path: "path/filepath", Linked: false},
{Path: "crypto/tls", Linked: false},
{Path: "golang.org/x/net", Linked: false},
{Path: "github.com/groove-x/go-licenses", Linked: true},
}
mods := make(map[string]*modinfo.ModulePublic)
for _, tt := range tests {
mods[tt.Path] = &modinfo.ModulePublic{Path: tt.Path}
}
linkedMods, err := filterLinkedModule(mods)
if err != nil {
t.Fatal(err)
}
for _, tt := range tests {
linked := false
for _, linkedMod := range linkedMods {
if linkedMod.Path == tt.Path {
linked = true
}
}
if linked != tt.Linked {
t.Fatalf("%s: want %t, got %t", tt.Path, tt.Linked, linked)
}
}
}