...
1
2
3
4
5 package api
6
7 import (
8 "bytes"
9 "os"
10 "path/filepath"
11 "regexp"
12 "testing"
13 )
14
15
16 var sentinel = regexp.MustCompile(`(//|#) Copyright \d\d\d\d (Google LLC|The Go Authors)(\.)*( All rights reserved\.)*
17 (//|#) Use of this source code is governed by a BSD-style
18 (//|#) license that can be found in the LICENSE file.
19 `)
20
21 const prefix = "// Copyright"
22
23
24 var skip = map[string]bool{
25 "tools.go": true,
26 "internal/third_party/uritemplates/uritemplates.go": true,
27 "internal/third_party/uritemplates/uritemplates_test.go": true,
28 }
29
30
31 func TestLicense(t *testing.T) {
32 err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
33 if skip[path] {
34 return nil
35 }
36
37 if err != nil {
38 return err
39 }
40
41 if filepath.Ext(path) != ".go" && filepath.Ext(path) != ".sh" {
42 return nil
43 }
44
45 src, err := os.ReadFile(path)
46 if err != nil {
47 return nil
48 }
49
50
51 if !sentinel.Match(src) {
52 t.Errorf("%v: license header not present", path)
53 return nil
54 }
55
56
57 if filepath.Ext(path) == ".go" && !bytes.HasPrefix(src, []byte(prefix)) {
58 t.Errorf("%v: license header not at the top", path)
59 }
60 return nil
61 })
62 if err != nil {
63 t.Fatal(err)
64 }
65 }
66
View as plain text