-
Notifications
You must be signed in to change notification settings - Fork 76
/
license_test.go
267 lines (239 loc) · 6.22 KB
/
license_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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package licensecheck
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/google/licensecheck/internal/match"
)
func init() {
flag.IntVar(&match.TraceDFA, "tracedfa", match.TraceDFA, "trace DFA execution that bails out after `n` non-matching steps")
}
func TestTestdata(t *testing.T) {
files, err := filepath.Glob("testdata/*")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatalf("no testdata files found")
}
for _, file := range files {
name := filepath.Base(file)
if name == "README" {
continue
}
if info, err := os.Stat(file); err == nil && info.IsDir() {
continue
}
if !strings.Contains(file, ".t") {
t.Errorf("unexpected file: %v", file)
}
file := file
t.Run(name, func(t *testing.T) {
t.Parallel() // faster and tests for races in parallel usage
data, err := ioutil.ReadFile(file)
if err != nil {
t.Fatal(err)
}
// See testdata/README for definition of test data file.
// Header ends at blank line.
i := bytes.Index(data, []byte("\n\n"))
if i < 0 {
t.Fatalf("%s: invalid test data file: no blank line terminating header", file)
}
hdr, data := strings.Split(string(data[:i]), "\n"), data[i+2:]
lineno := 1
// Skip leading comment lines.
for len(hdr) > 0 && strings.HasPrefix(hdr[0], "#") {
hdr = hdr[1:]
lineno++
}
if len(hdr) > 0 && strings.HasPrefix(hdr[0], "set ") {
t.Fatalf("%s:%d: set not implemented", file, lineno)
hdr = hdr[1:]
lineno++
}
if len(hdr) < 1 {
t.Fatalf("%s: header too short", file)
}
linenoStart := lineno
parseCoverage := func() Coverage {
var want Coverage
want.Percent, err = parsePercent(hdr[0])
if err != nil {
t.Fatalf("%s:%d: parsing want.Percent: %v", file, lineno, err)
}
hdr = hdr[1:]
lineno++
for i, line := range hdr {
f := strings.Fields(line)
if len(f) != 2 && len(f) != 3 {
t.Fatalf("%s:%d: bad match field count", file, lineno)
}
var m Match
m.ID = f[0]
m.Start, m.End, err = parseRange(f[1], len(data))
if err != nil {
t.Fatalf("%s:%d: parsing want.Match[%d].Start,End: %v", file, lineno, i, err)
}
if len(f) == 3 {
if f[2] != "URL" {
t.Fatalf("%s:%d: field 2 should be omitted or should be 'URL'", file, lineno)
}
m.IsURL = true
}
want.Match = append(want.Match, m)
lineno++
}
return want
}
want := parseCoverage()
linenoEnd := lineno
cov := Scan(data)
for _, m := range cov.Match {
typ := licenseType(m.ID)
if m.Type != typ {
t.Errorf("%s: match %s has Type=%s, want %s", file, m.ID, m.Type, typ)
}
}
mismatch := false
var buf bytes.Buffer
if !matchPercent(cov.Percent, want.Percent) {
fmt.Fprintf(&buf, "- %.1f%%\n+ %.1f%%\n", want.Percent, cov.Percent)
mismatch = true
} else {
fmt.Fprintf(&buf, " %.1f%%\n", cov.Percent)
}
covm, wantm := cov.Match, want.Match
for len(covm) > 0 || len(wantm) > 0 {
switch {
case len(covm) > 0 && (len(wantm) == 0 || covm[0].End < wantm[0].Start):
fmt.Fprintf(&buf, "+ %v\n", fmtMatch(covm[0], len(data)))
covm = covm[1:]
mismatch = true
case len(covm) > 0 && len(wantm) > 0 && matchMatch(covm[0], wantm[0]):
fmt.Fprintf(&buf, " %v\n", fmtMatch(covm[0], len(data)))
covm = covm[1:]
wantm = wantm[1:]
default:
fmt.Fprintf(&buf, "- %v\n", fmtMatch(wantm[0], len(data)))
wantm = wantm[1:]
mismatch = true
}
}
if mismatch {
t.Errorf("%s:%d,%d: diff -want +have:\n%s", file, linenoStart, linenoEnd, buf.Bytes())
}
})
}
}
// fmtMatch formats the match m for printing.
func fmtMatch(m Match, end int) string {
// Special case for EOF end position.
var hi string
if m.End == end {
hi = "$"
} else {
hi = fmt.Sprintf("%d", m.End)
}
s := fmt.Sprintf("%s %d,%s", m.ID, m.Start, hi)
if m.IsURL {
s += " URL"
}
return s
}
// parsePercent parses a percentage (float ending in %).
func parsePercent(s string) (float64, error) {
if !strings.HasSuffix(s, "%") {
return 0, fmt.Errorf("missing %% suffix")
}
return strconv.ParseFloat(s[:len(s)-len("%")], 64)
}
// parseRange parses a start,end range (two decimals separated by a comma).
// As a special case, the second decimal can be $ meaning end-of-file.
func parseRange(s string, end int) (int, int, error) {
i := strings.Index(s, ",")
if i < 0 {
return 0, 0, fmt.Errorf("malformed range")
}
lo, err := strconv.Atoi(s[:i])
if err != nil {
return 0, 0, err
}
var hi int
if s[i+1:] == "$" {
hi = end
} else {
hi, err = strconv.Atoi(s[i+1:])
if err != nil {
return 0, 0, err
}
}
return lo, hi, nil
}
// matchPercent reports whether have matches want.
// We require that they match to within 0.1.
func matchPercent(have, want float64) bool {
return math.Abs(have-want) < 0.1
}
// matchMatch reports whether have matches want.
func matchMatch(have, want Match) bool {
return have.ID == want.ID &&
have.Start == want.Start &&
have.End == want.End &&
have.IsURL == want.IsURL
}
var benchdata []byte
func BenchmarkScanTestdata(b *testing.B) {
if benchdata == nil {
files, err := filepath.Glob("testdata/*")
if err != nil {
b.Fatal(err)
}
if len(files) == 0 {
b.Fatalf("no testdata files found")
}
for _, file := range files {
if info, err := os.Stat(file); err == nil && info.IsDir() {
continue
}
data, err := ioutil.ReadFile(file)
if err != nil {
b.Fatal(err)
}
benchdata = append(benchdata, data...)
}
}
b.SetBytes(int64(len(benchdata)))
for i := 0; i < b.N; i++ {
Scan(benchdata)
}
}
var trace = flag.String("tr", "", "trace DFA execution on `file` in TestTrace")
func TestTrace(t *testing.T) {
if *trace == "" {
t.Skip("-tr not given")
}
data, err := ioutil.ReadFile(*trace)
if err != nil {
t.Fatal(err)
}
match.TraceDFA = 10
cov := Scan(data)
match.TraceDFA = 0
var buf bytes.Buffer
fmt.Fprintf(&buf, "%.1f%%\n", cov.Percent)
for _, m := range cov.Match {
fmt.Fprintf(&buf, "%v\n", fmtMatch(m, len(data)))
}
t.Logf("coverage:\n%v", buf.String())
}