2015-08-12 21:04:19 +00:00
|
|
|
// 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-08-12 21:04:19 +00:00
|
|
|
|
|
|
|
// Checks for files missing copyright notice
|
2017-07-07 20:43:26 +00:00
|
|
|
package meta
|
2015-08-12 21:04:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-07-07 20:43:26 +00:00
|
|
|
"testing"
|
2015-08-12 21:04:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// File extensions to check
|
2017-07-07 20:43:26 +00:00
|
|
|
var copyrightCheckExts = map[string]bool{
|
2015-08-12 21:04:19 +00:00
|
|
|
".go": true,
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:43:26 +00:00
|
|
|
// Directories to search
|
|
|
|
var copyrightCheckDirs = []string{".", "../cmd", "../lib", "../test", "../script"}
|
|
|
|
|
2015-08-12 21:04:19 +00:00
|
|
|
// Valid copyright headers, searched for in the top five lines in each file.
|
|
|
|
var copyrightRegexps = []string{
|
|
|
|
`Copyright`,
|
|
|
|
`package auto`,
|
|
|
|
`automatically generated by genxdr`,
|
2016-07-04 12:53:11 +00:00
|
|
|
`generated by protoc`,
|
2015-08-12 21:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var copyrightRe = regexp.MustCompile(strings.Join(copyrightRegexps, "|"))
|
|
|
|
|
2017-07-07 20:43:26 +00:00
|
|
|
func TestCheckCopyright(t *testing.T) {
|
|
|
|
for _, dir := range copyrightCheckDirs {
|
2015-08-12 21:04:19 +00:00
|
|
|
err := filepath.Walk(dir, checkCopyright)
|
|
|
|
if err != nil {
|
2017-07-07 20:43:26 +00:00
|
|
|
t.Error(err)
|
2015-08-12 21:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCopyright(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-07 20:43:26 +00:00
|
|
|
if !copyrightCheckExts[filepath.Ext(path)] {
|
2015-08-12 21:04:19 +00:00
|
|
|
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)
|
|
|
|
}
|