-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_test.go
More file actions
444 lines (355 loc) · 10.3 KB
/
project_test.go
File metadata and controls
444 lines (355 loc) · 10.3 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package reuse
import (
"os"
"path/filepath"
"testing"
)
func TestOpenProject_WithReuseTOML(t *testing.T) {
root := setupFakeProject(t, "toml")
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
if p.ReuseTOML == nil {
t.Fatal("expected ReuseTOML to be set")
}
if p.Dep5 != nil {
t.Error("expected Dep5 to be nil when REUSE.toml exists")
}
if len(p.LicenseFiles) == 0 {
t.Error("expected license files in LICENSES/")
}
}
func TestOpenProject_WithDep5(t *testing.T) {
root := setupFakeProject(t, "dep5")
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
if p.ReuseTOML != nil {
t.Error("expected ReuseTOML to be nil")
}
if p.Dep5 == nil {
t.Fatal("expected Dep5 to be set")
}
}
func TestOpenProject_HeaderOnly(t *testing.T) {
root := setupFakeProject(t, "header")
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
if p.ReuseTOML != nil {
t.Error("expected ReuseTOML to be nil")
}
if p.Dep5 != nil {
t.Error("expected Dep5 to be nil")
}
}
func TestProject_ReuseInfoOf_FileHeader(t *testing.T) {
root := setupFakeProject(t, "header")
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/main.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Test Author"})
}
func TestProject_ReuseInfoOf_DotLicenseSidecar(t *testing.T) {
root := setupFakeProject(t, "header")
// Add a .license sidecar for an image.
mkfile(t, root, "assets/logo.png", "\x89PNG binary")
mkfile(t, root, "assets/logo.png.license", `SPDX-License-Identifier: CC0-1.0
SPDX-FileCopyrightText: 2024 Designer`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("assets/logo.png")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"CC0-1.0"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Designer"})
if info.SourceType != DotLicense {
t.Errorf("SourceType = %v, want DotLicense", info.SourceType)
}
}
func TestProject_ReuseInfoOf_Override(t *testing.T) {
root := setupFakeProject(t, "toml")
// The file has MIT in its header, but REUSE.toml overrides it.
mkfile(t, root, "docs/guide.md", `<!-- SPDX-License-Identifier: MIT -->
<!-- SPDX-FileCopyrightText: 2024 File Author -->`)
// Set up an override annotation for docs/.
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
SPDX-FileCopyrightText = "2024 TOML Author"
SPDX-License-Identifier = "MIT"
[[annotations]]
path = "docs/**"
precedence = "override"
SPDX-FileCopyrightText = "2024 Override Author"
SPDX-License-Identifier = "CC-BY-SA-4.0"
`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("docs/guide.md")
if err != nil {
t.Fatal(err)
}
// Override should use TOML values, not file header.
assertSlice(t, info.LicenseExpressions, []string{"CC-BY-SA-4.0"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Override Author"})
}
func TestProject_ReuseInfoOf_Closest(t *testing.T) {
root := setupFakeProject(t, "toml")
// File with no REUSE info.
mkfile(t, root, "src/empty.go", `package src
func Empty() {}`)
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
SPDX-FileCopyrightText = "2024 TOML Fallback"
SPDX-License-Identifier = "MIT"
`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/empty.go")
if err != nil {
t.Fatal(err)
}
// Closest should fill in from TOML since file has no info.
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 TOML Fallback"})
}
func TestProject_ReuseInfoOf_ClosestPartial(t *testing.T) {
root := setupFakeProject(t, "toml")
// File with copyright but no license.
mkfile(t, root, "src/partial.go", `// SPDX-FileCopyrightText: 2024 Partial Author
package src`)
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
SPDX-FileCopyrightText = "2024 TOML Author"
SPDX-License-Identifier = "MIT"
`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/partial.go")
if err != nil {
t.Fatal(err)
}
// Copyright from file, license from TOML.
assertSlice(t, info.CopyrightNotices, []string{"2024 Partial Author"})
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
}
func TestProject_ReuseInfoOf_Aggregate(t *testing.T) {
root := setupFakeProject(t, "toml")
mkfile(t, root, "src/agg.go", `// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 File Author
package src`)
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 TOML Author"
SPDX-License-Identifier = "Apache-2.0"
`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/agg.go")
if err != nil {
t.Fatal(err)
}
// Both file and TOML values should be present.
assertSlice(t, info.LicenseExpressions, []string{"MIT", "Apache-2.0"})
assertSlice(t, info.CopyrightNotices, []string{"2024 File Author", "2024 TOML Author"})
}
func TestProject_ReuseInfoOf_Dep5(t *testing.T) {
root := setupFakeProject(t, "dep5")
// File with no headers, should fall through to dep5.
mkfile(t, root, "src/bare.go", `package src
func Bare() {}`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/bare.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Dep5 Author"})
}
func TestProject_ReuseInfoOf_SidecarOverridesAll(t *testing.T) {
root := setupFakeProject(t, "toml")
mkfile(t, root, "src/special.go", `// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 File Author
package src`)
mkfile(t, root, "src/special.go.license", `SPDX-License-Identifier: GPL-3.0-only
SPDX-FileCopyrightText: 2024 Sidecar Author`)
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
precedence = "override"
SPDX-FileCopyrightText = "2024 TOML Author"
SPDX-License-Identifier = "Apache-2.0"
`)
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
info, err := p.ReuseInfoOf("src/special.go")
if err != nil {
t.Fatal(err)
}
// Sidecar takes absolute precedence over everything.
assertSlice(t, info.LicenseExpressions, []string{"GPL-3.0-only"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Sidecar Author"})
}
func TestProject_AllReuseInfo(t *testing.T) {
root := setupFakeProject(t, "header")
p, err := OpenProject(root)
if err != nil {
t.Fatal(err)
}
all, err := p.AllReuseInfo()
if err != nil {
t.Fatal(err)
}
if len(all) == 0 {
t.Error("expected at least one file in AllReuseInfo")
}
info, ok := all["src/main.go"]
if !ok {
t.Fatal("expected src/main.go in results")
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
}
func TestProject_ReuseExample(t *testing.T) {
// Uses the fsfe/reuse-example submodule for real-world conformance.
exampleDir := filepath.Join("testdata", "reuse-example")
if _, err := os.Stat(exampleDir); os.IsNotExist(err) {
t.Skip("testdata/reuse-example submodule not checked out")
}
p, err := OpenProject(exampleDir)
if err != nil {
t.Fatal(err)
}
all, err := p.AllReuseInfo()
if err != nil {
t.Fatal(err)
}
if len(all) == 0 {
t.Fatal("expected covered files in reuse-example")
}
// Every covered file should have at least a license.
for path, info := range all {
if !info.HasLicense() {
t.Errorf("%s: missing license", path)
}
if !info.HasCopyright() {
t.Errorf("%s: missing copyright", path)
}
}
}
func TestProject_FakeRepository(t *testing.T) {
fakeDir := filepath.Join("testdata", "fake_repository")
if _, err := os.Stat(fakeDir); os.IsNotExist(err) {
t.Skip("testdata/fake_repository not found")
}
p, err := OpenProject(fakeDir)
if err != nil {
t.Fatal(err)
}
if p.ReuseTOML == nil {
t.Fatal("expected REUSE.toml")
}
// src/main.go has file headers and matches TOML closest.
info, err := p.ReuseInfoOf("src/main.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Alice"})
assertSlice(t, info.Contributors, []string{"Bob"})
// src/dual.go has dual licensing.
info, err = p.ReuseInfoOf("src/dual.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT OR Apache-2.0"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Alice", "2023 Bob"})
// src/noheader.go has no headers, so closest TOML fills in.
info, err = p.ReuseInfoOf("src/noheader.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 TOML Org"})
// src/ignore.go has REUSE-IgnoreStart/End, so only visible tags show.
info, err = p.ReuseInfoOf("src/ignore.go")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"MIT"})
assertSlice(t, info.CopyrightNotices, []string{"2024 Visible"})
// assets/logo.png has a .license sidecar, which takes priority.
info, err = p.ReuseInfoOf("assets/logo.png")
if err != nil {
t.Fatal(err)
}
assertSlice(t, info.LicenseExpressions, []string{"CC0-1.0"})
if info.SourceType != DotLicense {
t.Errorf("assets/logo.png SourceType = %v, want DotLicense", info.SourceType)
}
// License files in LICENSES/ should be discovered.
if len(p.LicenseFiles) < 3 {
t.Errorf("expected at least 3 license files, got %d", len(p.LicenseFiles))
}
}
// Test helpers.
func setupFakeProject(t *testing.T, mode string) string {
t.Helper()
root := t.TempDir()
// Common files.
mkfile(t, root, "LICENSES/MIT.txt", "MIT License text")
mkfile(t, root, "src/main.go", `// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2024 Test Author
package main`)
switch mode {
case "toml":
writeReuseTOML(t, root, `version = 1
[[annotations]]
path = "src/**"
SPDX-FileCopyrightText = "2024 TOML Author"
SPDX-License-Identifier = "MIT"
`)
case "dep5":
mkfile(t, root, ".reuse/dep5", `Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Files: *
Copyright: 2024 Dep5 Author
License: MIT
`)
case "header":
// No global licensing config; header-only.
}
return root
}
func writeReuseTOML(t *testing.T, root, content string) {
t.Helper()
mkfile(t, root, "REUSE.toml", content)
}