Skip to content

Commit 5dff8c5

Browse files
committed
.tools: Run 'golint ./...' on each commit
Instead of just checking the tip, run 'golint ./...' on each commit in the series. This makes sure we catch errors with earlier commits even if they were fixed by subsequent commits. I've added ValidateResult.Detail to hold stdout from ExecTree calls, because golint just prints warnings to its stdout and always returns an exit code of 0. Then in the golint rule, I check vr.Detail and fail any result with content there. In the main block, I print YAML blocks [1] with vr.Detail, so folks consuming the TAP can figure out what golint was complaining about. The YAML block gets printed when the verbose flag is set or when there's an error (otherwise it's hard to figure out what golint was complaining about in non-verbose mode). The YAML library ensures we don't get bitten by YAML-sensitive characters in the Detail string (e.g. colons). The gopkg.in location is a nifty site allowing us to link to a specific API of the project (which is hosted on GitHub) [2]. [1]: http://testanything.org/tap-version-13-specification.html [2]: http://godoc.org/gopkg.in/yaml.v2 Signed-off-by: W. Trevor King <[email protected]>
1 parent d51f2ff commit 5dff8c5

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

.tools/validate.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"os/exec"
1313
"regexp"
1414
"strings"
15+
16+
yaml "gopkg.in/yaml.v2"
1517
)
1618

1719
// DefaultRules are the standard validation to perform on git commits
@@ -44,6 +46,13 @@ var DefaultRules = []ValidateRule{
4446
func(c CommitEntry) (vr ValidateResult) {
4547
return ExecTree(c, "go", "vet", "./...")
4648
},
49+
func(c CommitEntry) (vr ValidateResult) {
50+
vr = ExecTree(c, os.ExpandEnv("$HOME/gopath/bin/golint"), "./...")
51+
if len(vr.Detail) > 0 {
52+
vr.Pass = false
53+
}
54+
return vr
55+
},
4756
}
4857

4958
var (
@@ -98,6 +107,19 @@ func main() {
98107
} else if !r.Pass {
99108
fmt.Printf("not ok - %s\n", r.Msg)
100109
}
110+
if (*flVerbose || !r.Pass) && len(r.Detail) > 0 {
111+
m := map[string]string{"message": r.Detail}
112+
buf, err := yaml.Marshal(m)
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
lines := strings.Split(strings.TrimSpace(string(buf)), "\n")
117+
fmt.Println(" ---")
118+
for _, line := range lines {
119+
fmt.Printf(" %s\n", line)
120+
}
121+
fmt.Println(" ...")
122+
}
101123
}
102124
}
103125
_, fail := results.PassFail()
@@ -124,6 +146,7 @@ type ValidateResult struct {
124146
CommitEntry CommitEntry
125147
Pass bool
126148
Msg string
149+
Detail string
127150
}
128151

129152
// ValidateResults is a set of results. This is type makes it easy for the following function.
@@ -275,7 +298,8 @@ func GitCheckoutTree(commit string, directory string) error {
275298
// wrapping any errors in a ValidateResult object.
276299
func ExecTree(c CommitEntry, args ...string) (vr ValidateResult) {
277300
vr.CommitEntry = c
278-
err := execTree(c, args...)
301+
stdout, err := execTree(c, args...)
302+
vr.Detail = strings.TrimSpace(stdout)
279303
if err == nil {
280304
vr.Pass = true
281305
vr.Msg = strings.Join(args, " ")
@@ -287,22 +311,25 @@ func ExecTree(c CommitEntry, args ...string) (vr ValidateResult) {
287311
}
288312

289313
// execTree executes a command in a checkout of the commit's tree
290-
func execTree(c CommitEntry, args ...string) error {
314+
func execTree(c CommitEntry, args ...string) (string, error) {
291315
dir, err := ioutil.TempDir("", "go-validate-")
292316
if err != nil {
293-
return err
317+
return "", err
294318
}
295319
defer os.RemoveAll(dir)
296320
err = GitCheckoutTree(c["commit"], dir)
297321
if err != nil {
298-
return err
322+
return "", err
299323
}
324+
buf := bytes.NewBuffer([]byte{})
300325
cmd := exec.Command(args[0], args[1:]...)
301326
cmd.Dir = dir
327+
cmd.Stdout = buf
302328
cmd.Stderr = os.Stderr
303329
err = cmd.Run()
330+
stdout := string(buf.Bytes())
304331
if err != nil {
305-
return err
332+
return stdout, err
306333
}
307-
return nil
334+
return stdout, nil
308335
}

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ go:
66
sudo: false
77

88
before_install:
9-
- go get golang.org/x/tools/cmd/vet
109
- go get github.com/golang/lint/golint
10+
- go get golang.org/x/tools/cmd/vet
11+
- go get gopkg.in/yaml.v2
1112

1213
install: true
1314

1415
script:
15-
- $HOME/gopath/bin/golint ./...
1616
- go run .tools/validate.go -range ${TRAVIS_COMMIT_RANGE}
1717

0 commit comments

Comments
 (0)