Skip to content

Commit d7f2462

Browse files
danmeltonkaralabe
authored andcommitted
build: add Travis job to lint Go code #15372 (#15416)
* build: [finishes #15372] implements generalized linter and travis job * .travis, build: minor polishes, disable deadcode
1 parent 4fe30bf commit d7f2462

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

‎.travis.yml‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ matrix:
3838
- sudo chmod 666 /dev/fuse
3939
- sudo chown root:$USER /etc/fuse.conf
4040
- go run build/ci.go install
41-
- go run build/ci.go test -coverage -misspell
41+
- go run build/ci.go test -coverage
4242

4343
- os: osx
4444
go: 1.9.x
@@ -48,7 +48,21 @@ matrix:
4848
- brew install caskroom/cask/brew-cask
4949
- brew cask install osxfuse
5050
- go run build/ci.go install
51-
- go run build/ci.go test -coverage -misspell
51+
- go run build/ci.go test -coverage
52+
53+
# This builder only tests code linters on latest version of Go
54+
- os: linux
55+
dist: trusty
56+
sudo: required
57+
go: 1.9.x
58+
env:
59+
- lint
60+
script:
61+
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends --force-yes install fuse
62+
- sudo modprobe fuse
63+
- sudo chmod 666 /dev/fuse
64+
- sudo chown root:$USER /etc/fuse.conf
65+
- go run build/ci.go lint
5266

5367
# This builder does the Ubuntu PPA and Linux Azure uploads
5468
- os: linux

‎build/ci.go‎

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Usage: go run ci.go <command> <command flags/arguments>
2424
Available commands are:
2525
2626
install [ -arch architecture ] [ packages... ] -- builds packages and executables
27-
test [ -coverage ] [ -misspell ] [ packages... ] -- runs the tests
27+
test [ -coverage ] [ packages... ] -- runs the tests
28+
lint -- runs certain pre-selected linters
2829
archive [ -arch architecture ] [ -type zip|tar ] [ -signer key-envvar ] [ -upload dest ] -- archives build artefacts
2930
importkeys -- imports signing keys from env
3031
debsrc [ -signer key-id ] [ -upload dest ] -- creates a debian source package
@@ -146,6 +147,8 @@ func main() {
146147
doInstall(os.Args[2:])
147148
case "test":
148149
doTest(os.Args[2:])
150+
case "lint":
151+
doLint(os.Args[2:])
149152
case "archive":
150153
doArchive(os.Args[2:])
151154
case "debsrc":
@@ -280,7 +283,6 @@ func goToolArch(arch string, subcmd string, args ...string) *exec.Cmd {
280283

281284
func doTest(cmdline []string) {
282285
var (
283-
misspell = flag.Bool("misspell", false, "Whether to run the spell checker")
284286
coverage = flag.Bool("coverage", false, "Whether to record code coverage")
285287
)
286288
flag.CommandLine.Parse(cmdline)
@@ -294,10 +296,7 @@ func doTest(cmdline []string) {
294296

295297
// Run analysis tools before the tests.
296298
build.MustRun(goTool("vet", packages...))
297-
if *misspell {
298-
// TODO(karalabe): Reenable after false detection is fixed: https://github.com/client9/misspell/issues/105
299-
// spellcheck(packages)
300-
}
299+
301300
// Run the actual tests.
302301
gotest := goTool("test", buildFlags(env)...)
303302
// Test a single package at a time. CI builders are slow
@@ -306,36 +305,26 @@ func doTest(cmdline []string) {
306305
if *coverage {
307306
gotest.Args = append(gotest.Args, "-covermode=atomic", "-cover")
308307
}
308+
309309
gotest.Args = append(gotest.Args, packages...)
310310
build.MustRun(gotest)
311311
}
312312

313-
// spellcheck runs the client9/misspell spellchecker package on all Go, Cgo and
314-
// test files in the requested packages.
315-
func spellcheck(packages []string) {
316-
// Ensure the spellchecker is available
317-
build.MustRun(goTool("get", "github.com/client9/misspell/cmd/misspell"))
313+
// runs gometalinter on requested packages
314+
func doLint(cmdline []string) {
315+
flag.CommandLine.Parse(cmdline)
318316

319-
// Windows chokes on long argument lists, check packages individually
320-
for _, pkg := range packages {
321-
// The spell checker doesn't work on packages, gather all .go files for it
322-
out, err := goTool("list", "-f", "{{.Dir}}{{range .GoFiles}}\n{{.}}{{end}}{{range .CgoFiles}}\n{{.}}{{end}}{{range .TestGoFiles}}\n{{.}}{{end}}", pkg).CombinedOutput()
323-
if err != nil {
324-
log.Fatalf("source file listing failed: %v\n%s", err, string(out))
325-
}
326-
// Retrieve the folder and assemble the source list
327-
lines := strings.Split(string(out), "\n")
328-
root := lines[0]
329-
330-
sources := make([]string, 0, len(lines)-1)
331-
for _, line := range lines[1:] {
332-
if line = strings.TrimSpace(line); line != "" {
333-
sources = append(sources, filepath.Join(root, line))
334-
}
335-
}
336-
// Run the spell checker for this particular package
337-
build.MustRunCommand(filepath.Join(GOBIN, "misspell"), append([]string{"-error"}, sources...)...)
317+
packages := []string{"./..."}
318+
if len(flag.CommandLine.Args()) > 0 {
319+
packages = flag.CommandLine.Args()
338320
}
321+
// Get metalinter and install all supported linters
322+
build.MustRun(goTool("get", "gopkg.in/alecthomas/gometalinter.v1"))
323+
build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v1"), "--install")
324+
325+
configs := []string{"--vendor", "--disable-all", "--enable=vet"} // Add additional linters to the slice with "--enable=linter-name"
326+
327+
build.MustRunCommand(filepath.Join(GOBIN, "gometalinter.v1"), append(configs, packages...)...)
339328
}
340329

341330
// Release Packaging

0 commit comments

Comments
 (0)