-
Notifications
You must be signed in to change notification settings - Fork 76
/
classifier_test.go
279 lines (249 loc) · 5.92 KB
/
classifier_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
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package classifier
import (
"bytes"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"testing/iotest"
"github.com/davecgh/go-spew/spew"
)
type scenario struct {
expected []string
data []byte
}
var defaultThreshold = .8
var baseLicenses = "./licenses"
func classifier() (*Classifier, error) {
c := NewClassifier(defaultThreshold)
return c, c.LoadLicenses(baseLicenses)
}
func getScenarioFilenames() ([]string, error) {
scenarios := "./scenarios"
var files []string
err := filepath.Walk(scenarios, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(path, "md") || info.IsDir() {
return nil
}
files = append(files, path)
return nil
})
return files, err
}
func TestMatchScenarios(t *testing.T) {
c, err := classifier()
if err != nil {
t.Fatalf("couldn't instantiate standard test classifier: %v", err)
}
files, err := getScenarioFilenames()
if err != nil {
t.Fatalf("encountered error walking scenarios directory: %v", err)
}
for _, f := range files {
s := readScenario(f)
m := c.Match(s.data)
checkMatches(t, m, f, s.expected)
}
}
func readScenario(path string) *scenario {
var s scenario
b, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Couldn't read scenario %s: %v", path, err)
}
// A scenario consists of any number of comment lines, which are ignored, then a line of the form
// EXPECTED: A,B,C
//
// or EXPECTED:<EOL>
// where A,B,C is a comma-separated list of expected licenses.
lines := strings.SplitN(string(b), "EXPECTED:", 2)
// The first part of lines is description, which we ignore. We then split on a linefeed to get the
// list of licenses and the rest of the data content.
lines = strings.SplitN(lines[1], "\n", 2)
if lines[0] != "" {
s.expected = strings.Split(lines[0], ",")
} else {
s.expected = []string{}
}
s.data = []byte(lines[1])
return &s
}
func TestContainsAndOverlaps(t *testing.T) {
tests := []struct {
name string
a, b *Match
contains bool
overlaps bool
}{
{
name: "no intersection",
a: &Match{
StartLine: 1,
EndLine: 3,
},
b: &Match{
StartLine: 4,
EndLine: 5,
},
contains: false,
overlaps: false,
},
{
name: "overlap at end",
a: &Match{
StartLine: 4,
EndLine: 10,
},
b: &Match{
StartLine: 1,
EndLine: 5,
},
contains: false,
overlaps: true,
},
{
name: "overlap at end",
a: &Match{
StartLine: 1,
EndLine: 10,
},
b: &Match{
StartLine: 4,
EndLine: 12,
},
contains: false,
overlaps: true,
},
{
name: "contains",
a: &Match{
StartLine: 1,
EndLine: 10,
},
b: &Match{
StartLine: 4,
EndLine: 7,
},
contains: true,
overlaps: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := contains(test.a, test.b); got != test.contains {
t.Errorf("contains: got %v want %v", got, test.contains)
}
if got := overlaps(test.a, test.b); got != test.overlaps {
t.Errorf("overlaps: got %v want %v", got, test.overlaps)
}
})
}
}
func TestLicName(t *testing.T) {
tests := []struct {
name string
expected string
}{
{
// The filename for a license
name: "GPL-2.0.txt",
expected: "GPL-2.0",
},
{
// The filename for a header reference to a license
name: "GPL-2.0.header.txt",
expected: "GPL-2.0",
},
{
// The filename for a variant header reference to a license
name: "GPL-2.0.header_a.txt",
expected: "GPL-2.0",
},
{
// The filename for a variant license body
name: "Apache-2.0_no_toc.txt",
expected: "Apache-2.0",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
})
}
}
func TestTrimExtraneousText(t *testing.T) {
in := `Sample text
END OF TERMS AND CONDITIONS
Extra text is here`
expected := "Sample text\n"
if got := trimExtraneousTrailingText(in); got != expected {
t.Errorf("trimExtraneousTrailingText: got %q want %q", got, expected)
}
}
func TestMatchFrom(t *testing.T) {
tr := iotest.TimeoutReader(strings.NewReader("some data"))
c, err := classifier()
if err != nil {
t.Fatalf("couldn't instantiate standard Google classifier: %v", err)
}
_, err = c.MatchFrom(tr)
if !errors.Is(err, iotest.ErrTimeout) {
t.Errorf("got %v want %v", err, iotest.ErrTimeout)
}
files, err := getScenarioFilenames()
if err != nil {
t.Fatalf("encountered error walking scenarios directory: %v", err)
}
for _, f := range files {
s := readScenario(f)
r := bytes.NewReader(s.data)
m, err := c.MatchFrom(r)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
checkMatches(t, m, f, s.expected)
}
}
// checkMatches diffs the resulting matches against the expected content and
// sets test results.
func checkMatches(t *testing.T, m Matches, f string, e []string) {
found := make(map[string]bool)
// Uniquify the licenses found
for _, l := range m {
found[l.Name] = true
}
var names []string
for l := range found {
names = append(names, l)
}
sort.Strings(names)
if len(names) != len(e) {
t.Errorf("Match(%q) number matches: %v, want %v: %v", f, len(names), len(e), spew.Sdump(m))
return
}
for i := 0; i < len(names); i++ {
w := strings.TrimSpace(e[i])
if got, want := names[i], w; got != want {
t.Errorf("Match(%q) = %q, want %q", f, got, want)
}
}
}