2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-03 01:20:49 +00:00

CI: Check for packages importing "errors" from stdlib

This commit is contained in:
Alexander Neumann 2016-08-21 18:31:09 +02:00
parent d3f4c816c7
commit 038b63f7f7

View File

@ -3,7 +3,9 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -17,6 +19,12 @@ import (
"strings" "strings"
) )
// ForbiddenImports are the packages from the stdlib that should not be used in
// our code.
var ForbiddenImports = map[string]bool{
"errors": true,
}
var runCrossCompile = flag.Bool("cross-compile", true, "run cross compilation tests") var runCrossCompile = flag.Bool("cross-compile", true, "run cross compilation tests")
var minioServer = flag.String("minio", "", "path to the minio server binary") var minioServer = flag.String("minio", "", "path to the minio server binary")
var debug = flag.Bool("debug", false, "output debug messages") var debug = flag.Bool("debug", false, "output debug messages")
@ -345,7 +353,30 @@ func (env *TravisEnvironment) RunTests() error {
return err return err
} }
return runGofmt() if err = runGofmt(); err != nil {
return err
}
deps, err := findImports()
if err != nil {
return err
}
foundForbiddenImports := false
for name, imports := range deps {
for _, pkg := range imports {
if _, ok := ForbiddenImports[pkg]; ok {
fmt.Fprintf(os.Stderr, "========== package %v imports forbidden package %v\n", name, pkg)
foundForbiddenImports = true
}
}
}
if foundForbiddenImports {
return errors.New("CI: forbidden imports found")
}
return nil
} }
// AppveyorEnvironment is the environment on Windows. // AppveyorEnvironment is the environment on Windows.
@ -413,6 +444,46 @@ func updateEnv(env []string, override map[string]string) []string {
return newEnv return newEnv
} }
func findImports() (map[string][]string, error) {
res := make(map[string][]string)
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("Getwd() returned error: %v", err)
}
gopath := cwd + ":" + filepath.Join(cwd, "vendor")
cmd := exec.Command("go", "list", "-f", `{{.ImportPath}} {{join .Imports " "}}`, "./src/...")
cmd.Env = updateEnv(os.Environ(), map[string]string{"GOPATH": gopath})
cmd.Stderr = os.Stderr
output, err := cmd.Output()
if err != nil {
return nil, err
}
sc := bufio.NewScanner(bytes.NewReader(output))
for sc.Scan() {
wordScanner := bufio.NewScanner(strings.NewReader(sc.Text()))
wordScanner.Split(bufio.ScanWords)
if !wordScanner.Scan() {
return nil, fmt.Errorf("package name not found in line: %s", output)
}
name := wordScanner.Text()
var deps []string
for wordScanner.Scan() {
deps = append(deps, wordScanner.Text())
}
res[name] = deps
}
return res, nil
}
func runGofmt() error { func runGofmt() error {
dir, err := os.Getwd() dir, err := os.Getwd()
if err != nil { if err != nil {