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
|
// Copyright (C) 2015 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
// Checks for files missing copyright notice
package meta
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
// File extensions to check
var copyrightCheckExts = map[string]bool{
".go": true,
}
// Directories to search
var copyrightCheckDirs = []string{".", "../cmd", "../lib", "../test", "../script"}
// Valid copyright headers, searched for in the top five lines in each file.
var copyrightRegexps = []string{
`Copyright`,
`package auto`,
`automatically generated by genxdr`,
`generated by protoc`,
`^// Code generated .* DO NOT EDIT\.$`,
}
var copyrightRe = regexp.MustCompile(strings.Join(copyrightRegexps, "|"))
func TestCheckCopyright(t *testing.T) {
for _, dir := range copyrightCheckDirs {
err := filepath.Walk(dir, checkCopyright)
if err != nil {
t.Error(err)
}
}
}
func checkCopyright(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
if !copyrightCheckExts[filepath.Ext(path)] {
return nil
}
fd, err := os.Open(path)
if err != nil {
return err
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
for i := 0; scanner.Scan() && i < 5; i++ {
if copyrightRe.MatchString(scanner.Text()) {
return nil
}
}
return fmt.Errorf("missing copyright in %s?", path)
}
|