-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyright_test.go
70 lines (64 loc) · 1.61 KB
/
copyright_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
// Copyright 2024 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 copyright
import (
"bytes"
"internal/testenv"
"io"
"io/fs"
"os"
"path/filepath"
"testing"
)
var copyright = []byte("Copyright")
var permitted = [][]byte{
[]byte("// Code generated by "),
[]byte("// Code generated from "),
[]byte("// Created by cgo -cdefs"),
[]byte("// DO NOT EDIT\n// generated by:"),
[]byte("// Empty assembly file"),
[]byte("// Generated using cgo"),
[]byte("// Original source:\n//\thttp://www.zorinaq.com/papers/md5-amd64.html"), // public domain crypto/md5
[]byte("// created by cgo -cdefs"),
[]byte("// go run mkasm.go"),
[]byte("// mkerrors"),
[]byte("// mksys"),
[]byte("// run\n// Code generated by"), // cmd/compile/internal/test/constFold_test.go
}
func TestCopyright(t *testing.T) {
buf := make([]byte, 2048)
filepath.WalkDir(filepath.Join(testenv.GOROOT(t), "src"), func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && (d.Name() == "testdata" || d.Name() == "vendor") {
return filepath.SkipDir
}
switch filepath.Ext(d.Name()) {
default:
return nil
case ".s", ".go":
// check
}
f, err := os.Open(path)
if err != nil {
t.Error(err)
return nil
}
defer f.Close()
n, err := f.Read(buf)
if err != nil && err != io.EOF {
t.Error(err)
return nil
}
b := buf[:n]
if bytes.Contains(b, copyright) {
return nil
}
for _, ok := range permitted {
if bytes.HasPrefix(b, ok) {
return nil
}
}
t.Errorf("%s: missing copyright notice", path)
return nil
})
}