aboutsummaryrefslogtreecommitdiff
path: root/meta/metalint_test.go
blob: 0c0407373db022543229e8268789a36fc84929f7 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (C) 2017 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/.

package meta

import (
	"bytes"
	"log"
	"os/exec"
	"strings"
	"testing"
)

var (
	// fast linters complete in a fraction of a second and might as well be
	// run always as part of the build
	fastLinters = []string{
		"deadcode",
		"golint",
		"ineffassign",
		"vet",
	}

	// slow linters take several seconds and are run only as part of the
	// "metalint" command.
	slowLinters = []string{
		"gosimple",
		"staticcheck",
		"structcheck",
		"unused",
		"varcheck",
	}

	// Which parts of the tree to lint
	lintDirs = []string{
		".",
		"../cmd/...",
		"../lib/...",
		"../script/...",
	}

	// Messages to ignore
	lintExcludes = []string{
		".pb.go",
		"should have comment",
		"protocol.Vector composite literal uses unkeyed fields",
		"cli.Requires composite literal uses unkeyed fields",
		"Use DialContext instead",   // Go 1.7
		"os.SEEK_SET is deprecated", // Go 1.7
		"SA4017",                    // staticcheck "is a pure function but its return value is ignored"
	}
)

func TestCheckMetalint(t *testing.T) {
	if !isGometalinterInstalled() {
		return
	}

	gometalinter(t, lintDirs, lintExcludes...)
}

func isGometalinterInstalled() bool {
	if _, err := runError("gometalinter", "--disable-all"); err != nil {
		log.Println("gometalinter is not installed")
		return false
	}
	return true
}

func gometalinter(_ *testing.T, dirs []string, excludes ...string) bool {
	params := []string{"--disable-all", "--concurrency=2", "--deadline=300s"}

	for _, linter := range fastLinters {
		params = append(params, "--enable="+linter)
	}

	if !testing.Short() {
		for _, linter := range slowLinters {
			params = append(params, "--enable="+linter)
		}
	}

	for _, exclude := range excludes {
		params = append(params, "--exclude="+exclude)
	}

	params = append(params, dirs...)

	bs, _ := runError("gometalinter", params...)

	nerr := 0
	lines := make(map[string]struct{})
	for _, line := range strings.Split(string(bs), "\n") {
		if line == "" {
			continue
		}
		if _, ok := lines[line]; ok {
			continue
		}
		log.Println(line)
		if strings.Contains(line, "executable file not found") {
			log.Println(` - Try "go run build.go setup" to install missing tools`)
		}
		lines[line] = struct{}{}
		nerr++
	}

	return nerr == 0
}

func runError(cmd string, args ...string) ([]byte, error) {
	ecmd := exec.Command(cmd, args...)
	bs, err := ecmd.CombinedOutput()
	return bytes.TrimSpace(bs), err
}