forked from elastic/go-licenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
310 lines (299 loc) · 9.65 KB
/
main_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 main
import (
"bytes"
"errors"
"flag"
"os"
"path/filepath"
"reflect"
"testing"
)
var update = flag.Bool("update", false, "updates the golden files with the latest iteration of the code")
func Test_run(t *testing.T) {
type args struct {
args []string
license string
licensor string
exclude []string
ext string
copyright bool
dry bool
}
tests := []struct {
name string
args args
want int
err error
wantOutput string
wantGolden bool
}{
{
name: "Run a diff prints a list of files that need the license header",
args: args{
args: []string{"testdata"},
license: defaultLicense,
licensor: defaultLicensor,
exclude: []string{"excludedpath", "x-pack", "x-pack-v2", "cloud"},
ext: defaultExt,
copyright: false,
dry: true,
},
want: 1,
err: &Error{code: 1},
wantOutput: `
testdata/multilevel/doc.go: is missing the license header
testdata/multilevel/main.go: is missing the license header
testdata/multilevel/sublevel/autogen.go: is missing the license header
testdata/multilevel/sublevel/doc.go: is missing the license header
testdata/multilevel/sublevel/partial.go: is missing the license header
testdata/singlelevel/doc.go: is missing the license header
testdata/singlelevel/main.go: is missing the license header
testdata/singlelevel/wrapper.go: is missing the license header
`[1:],
},
{
name: "Run a diff prints a list of files that need the Elastic license header",
args: args{
args: []string{"testdata"},
license: "Elastic",
licensor: defaultLicensor,
ext: defaultExt,
copyright: false,
dry: true,
},
want: 1,
err: &Error{code: 1},
wantOutput: `
testdata/cloud/doc.go: is missing the license header
testdata/cloud/wrong.go: is missing the license header
testdata/excludedpath/file.go: is missing the license header
testdata/multilevel/doc.go: is missing the license header
testdata/multilevel/main.go: is missing the license header
testdata/multilevel/sublevel/autogen.go: is missing the license header
testdata/multilevel/sublevel/doc.go: is missing the license header
testdata/multilevel/sublevel/partial.go: is missing the license header
testdata/singlelevel/doc.go: is missing the license header
testdata/singlelevel/main.go: is missing the license header
testdata/singlelevel/wrapper.go: is missing the license header
testdata/singlelevel/zrapper.go: is missing the license header
testdata/x-pack/wrong.go: is missing the license header
testdata/x-pack-v2/correct.go: is missing the license header
testdata/x-pack-v2/wrong.go: is missing the license header
`[1:],
},
{
name: "Run a diff prints a list of files that need the Elastic license 2.0 header",
args: args{
args: []string{"testdata"},
license: "Elasticv2",
licensor: defaultLicensor,
ext: defaultExt,
copyright: false,
dry: true,
},
want: 1,
err: &Error{code: 1},
wantOutput: `
testdata/cloud/doc.go: is missing the license header
testdata/cloud/wrong.go: is missing the license header
testdata/excludedpath/file.go: is missing the license header
testdata/multilevel/doc.go: is missing the license header
testdata/multilevel/main.go: is missing the license header
testdata/multilevel/sublevel/autogen.go: is missing the license header
testdata/multilevel/sublevel/doc.go: is missing the license header
testdata/multilevel/sublevel/partial.go: is missing the license header
testdata/singlelevel/doc.go: is missing the license header
testdata/singlelevel/main.go: is missing the license header
testdata/singlelevel/wrapper.go: is missing the license header
testdata/singlelevel/zrapper.go: is missing the license header
testdata/x-pack/correct.go: is missing the license header
testdata/x-pack/wrong.go: is missing the license header
testdata/x-pack-v2/wrong.go: is missing the license header
`[1:],
},
{
name: "Run a diff prints a list of files that need the Cloud license header",
args: args{
args: []string{"testdata"},
license: "Cloud",
licensor: defaultLicensor,
ext: defaultExt,
copyright: false,
dry: true,
},
want: 1,
err: &Error{code: 1},
wantOutput: `
testdata/cloud/wrong.go: is missing the license header
testdata/excludedpath/file.go: is missing the license header
testdata/multilevel/doc.go: is missing the license header
testdata/multilevel/main.go: is missing the license header
testdata/multilevel/sublevel/autogen.go: is missing the license header
testdata/multilevel/sublevel/doc.go: is missing the license header
testdata/multilevel/sublevel/partial.go: is missing the license header
testdata/singlelevel/doc.go: is missing the license header
testdata/singlelevel/main.go: is missing the license header
testdata/singlelevel/wrapper.go: is missing the license header
testdata/singlelevel/zrapper.go: is missing the license header
testdata/x-pack/correct.go: is missing the license header
testdata/x-pack/wrong.go: is missing the license header
testdata/x-pack-v2/correct.go: is missing the license header
testdata/x-pack-v2/wrong.go: is missing the license header
`[1:],
},
{
name: "Run against an unexisting dir fails",
args: args{
args: []string{"ignore"},
license: defaultLicense,
licensor: defaultLicensor,
ext: defaultExt,
copyright: false,
dry: false,
},
want: 2,
err: goosPathError(2, "ignore"),
},
{
name: "Unknown license fails",
args: args{
args: []string{"ignore"},
license: "foo",
licensor: defaultLicensor,
ext: defaultExt,
copyright: false,
dry: false,
},
want: 7,
err: &Error{err: errors.New("unknown license: foo"), code: 7},
},
{
name: "Check ASL2 license rewrite",
args: args{
args: []string{"testdata"},
license: defaultLicense,
licensor: defaultLicensor,
exclude: []string{"excludedpath"},
ext: defaultExt,
copyright: false,
dry: false,
},
want: 0,
wantGolden: true,
},
{
name: "Check ASL2-short license rewrite",
args: args{
args: []string{"testdata"},
license: "ASL2-Short",
licensor: defaultLicensor,
exclude: []string{"excludedpath"},
ext: defaultExt,
copyright: false,
dry: false,
},
want: 0,
wantGolden: true,
},
{
name: "Check Cloud license rewrite",
args: args{
args: []string{"testdata"},
license: "Cloud",
licensor: defaultLicensor,
exclude: []string{"excludedpath"},
ext: defaultExt,
copyright: false,
dry: false,
},
want: 0,
wantGolden: true,
},
{
name: "Check Elastic license rewrite",
args: args{
args: []string{"testdata"},
license: "Elastic",
licensor: defaultLicensor,
exclude: []string{"excludedpath"},
ext: defaultExt,
copyright: false,
dry: false,
},
want: 0,
wantGolden: true,
},
{
name: "Check Elastic 2.0 license rewrite",
args: args{
args: []string{"testdata"},
license: "Elasticv2",
licensor: defaultLicensor,
exclude: []string{"excludedpath"},
ext: defaultExt,
copyright: false,
dry: false,
},
want: 0,
wantGolden: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.args[0] != "ignore" {
defer copyFixtures(t, tt.args.args[0])()
}
var buf = new(bytes.Buffer)
var err = run(tt.args.args, tt.args.license, tt.args.licensor, tt.args.exclude, tt.args.ext, tt.args.copyright, tt.args.dry, buf)
if !reflect.DeepEqual(err, tt.err) {
t.Errorf("run() error = %v, wantErr %v", err, tt.err)
return
}
var got = Code(err)
if got != tt.want {
t.Errorf("run() = %v, want %v", got, tt.want)
}
var gotOutput = buf.String()
tt.wantOutput = filepath.FromSlash(tt.wantOutput)
if gotOutput != tt.wantOutput {
t.Errorf("Output = \n%v\n want \n%v", gotOutput, tt.wantOutput)
}
if tt.wantGolden {
goldenDirectory := filepath.Join("golden", tt.args.license)
if *update {
copyFixtures(t, goldenDirectory)
if err := run([]string{goldenDirectory}, tt.args.license, tt.args.licensor, tt.args.exclude, tt.args.ext, tt.args.copyright, tt.args.dry, buf); err != nil {
t.Fatal(err)
}
}
hashDirectories(t, "testdata", goldenDirectory)
}
})
}
}
func BenchmarkRun(b *testing.B) {
args := []string{"."}
excluded := append(defaultExludedDirs, "golden")
b.Run("dot", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
run(args, defaultLicense, defaultLicensor, excluded, defaultExt, false, false, os.Stdout)
}
})
}