mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-04-01 17:53:39 +00:00
* Update gometalinter config gometalinter now uses `maligned` instead of `aligncheck` (https://github.com/alecthomas/gometalinter/pull/367), so we need to update our config accordingly. * Update gometalinter * Disable gotype linter gotype does not seem to play nicely with the gb vendor directory. In particular, it wants each of our dependencies to be built and installed (see https://github.com/golang/go/issues/10969), but (empirically) it will not accept them being installed in `pkg` but insists on them being in `vendor/pkg`. This presents a problem because `gb build` builds the packages into `pkg` (which doesn't seem entirely unreasonable since `.` comes before `vendor` in `$GOPATH`). `go install github.com/x/y` does install in `vendor/pkg` but requires us to know the name of each package. The general conclusion of https://github.com/alecthomas/gometalinter/issues/91 seems to have been that the easiest thing to do is to disable `gotype` for now. * Fix `unparam` lint * Fix goshadow lint
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package regressiontests
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gotestyourself/gotestyourself/fs"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type Issue struct {
|
|
Linter string `json:"linter"`
|
|
Severity string `json:"severity"`
|
|
Path string `json:"path"`
|
|
Line int `json:"line"`
|
|
Col int `json:"col"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func (i *Issue) String() string {
|
|
col := ""
|
|
if i.Col != 0 {
|
|
col = fmt.Sprintf("%d", i.Col)
|
|
}
|
|
return fmt.Sprintf("%s:%d:%s:%s: %s (%s)", strings.TrimSpace(i.Path), i.Line, col, i.Severity, strings.TrimSpace(i.Message), i.Linter)
|
|
}
|
|
|
|
type Issues []Issue
|
|
|
|
// ExpectIssues runs gometalinter and expects it to generate exactly the
|
|
// issues provided.
|
|
func ExpectIssues(t *testing.T, linter string, source string, expected Issues, extraFlags ...string) {
|
|
// Write source to temporary directory.
|
|
dir, err := ioutil.TempDir(".", "gometalinter-")
|
|
require.NoError(t, err)
|
|
defer os.RemoveAll(dir)
|
|
|
|
testFile := filepath.Join(dir, "test.go")
|
|
err = ioutil.WriteFile(testFile, []byte(source), 0644)
|
|
require.NoError(t, err)
|
|
|
|
actual := RunLinter(t, linter, dir, extraFlags...)
|
|
assert.Equal(t, expected, actual)
|
|
}
|
|
|
|
// RunLinter runs the gometalinter as a binary against the files at path and
|
|
// returns the issues it encountered
|
|
func RunLinter(t *testing.T, linter string, path string, extraFlags ...string) Issues {
|
|
binary, cleanup := buildBinary(t)
|
|
defer cleanup()
|
|
|
|
args := []string{
|
|
"-d", "--disable-all", "--enable", linter, "--json",
|
|
"--sort=path", "--sort=line", "--sort=column", "--sort=message",
|
|
"./...",
|
|
}
|
|
args = append(args, extraFlags...)
|
|
cmd := exec.Command(binary, args...)
|
|
cmd.Dir = path
|
|
|
|
errBuffer := new(bytes.Buffer)
|
|
cmd.Stderr = errBuffer
|
|
|
|
output, _ := cmd.Output()
|
|
|
|
var actual Issues
|
|
err := json.Unmarshal(output, &actual)
|
|
if !assert.NoError(t, err) {
|
|
fmt.Printf("Stderr: %s\n", errBuffer)
|
|
fmt.Printf("Output: %s\n", output)
|
|
return nil
|
|
}
|
|
return filterIssues(actual, linter, path)
|
|
}
|
|
|
|
func buildBinary(t *testing.T) (string, func()) {
|
|
tmpdir := fs.NewDir(t, "regression-test-binary")
|
|
path := tmpdir.Join("gometalinter")
|
|
cmd := exec.Command("go", "build", "-o", path, "..")
|
|
require.NoError(t, cmd.Run())
|
|
return path, tmpdir.Remove
|
|
}
|
|
|
|
// filterIssues to just the issues relevant for the current linter and normalize
|
|
// the error message by removing the directory part of the path from both Path
|
|
// and Message
|
|
func filterIssues(issues Issues, linterName string, dir string) Issues {
|
|
filtered := Issues{}
|
|
for _, issue := range issues {
|
|
if issue.Linter == linterName || linterName == "" {
|
|
issue.Path = strings.Replace(issue.Path, dir+string(os.PathSeparator), "", -1)
|
|
issue.Message = strings.Replace(issue.Message, dir+string(os.PathSeparator), "", -1)
|
|
issue.Message = strings.Replace(issue.Message, dir, "", -1)
|
|
filtered = append(filtered, issue)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|