syncthing/meta/copyright_test.go
Jakob Borg 200a7fc844 meta: Move metadata checks into meta directory, make them tests
This moves a few things from script/ to a new directory meta/, and makes
them real Go tests. These are the authors, copyright, metalint and gofmt
checks. That means that they can now be run by

go test -v ./meta

and optionally filtered by the usual -run thing to go test. Also -short
will cut down on the metalint stuff and exclude the authors check (which
is slow because it runs git lots of times).

Mainly this makes everything easier on things like build servers where
we can now just run tests instead of do a bunch of scripting.

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4252
2017-07-07 20:43:26 +00:00

73 lines
1.5 KiB
Go

// 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`,
}
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)
}