1 Star 0 Fork 0

lonely/gometalinter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
support.go 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
package regressiontests
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
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
func (e Issues) Len() int { return len(e) }
func (e Issues) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
func (e Issues) Less(i, j int) bool { return e[i].String() < e[j].String() }
// 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-")
if !assert.NoError(t, err) {
return
}
defer os.RemoveAll(dir)
w, err := os.Create(filepath.Join(dir, "test.go"))
if !assert.NoError(t, err) {
return
}
defer os.Remove(w.Name())
_, err = w.WriteString(source)
_ = w.Close()
if !assert.NoError(t, err) {
return
}
// Run gometalinter.
args := []string{"go", "run", "../main.go", "--disable-all", "--enable", linter, "--json", dir}
args = append(args, extraFlags...)
cmd := exec.Command(args[0], args[1:]...)
if !assert.NoError(t, err) {
return
}
output, _ := cmd.Output()
var actual Issues
err = json.Unmarshal(output, &actual)
if !assert.NoError(t, err) {
return
}
// Remove output from other linters.
actualForLinter := Issues{}
for _, issue := range actual {
if issue.Linter == linter || linter == "" {
// Normalise path.
issue.Path = "test.go"
issue.Message = strings.Replace(issue.Message, w.Name(), "test.go", -1)
issue.Message = strings.Replace(issue.Message, dir, "", -1)
actualForLinter = append(actualForLinter, issue)
}
}
sort.Sort(expected)
sort.Sort(actualForLinter)
assert.Equal(t, expected, actualForLinter)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lonely0422/gometalinter.git
git@gitee.com:lonely0422/gometalinter.git
lonely0422
gometalinter
gometalinter
v1.0.0

搜索帮助