12 "honnef.co/go/tools/config"
13 "honnef.co/go/tools/lintcmd/runner"
15 "golang.org/x/tools/go/packages"
18 func testdata() string {
19 testdata, err := filepath.Abs("testdata")
26 func lintPackage(t *testing.T, name string) []problem {
27 l, err := newLinter(config.Config{})
31 cfg := &packages.Config{
32 Env: append(os.Environ(), "GOPATH="+testdata(), "GO111MODULE=off"),
34 ps, _, err := l.Lint(cfg, []string{name})
41 func trimPosition(pos *token.Position) {
42 idx := strings.Index(pos.Filename, "/testdata/src/")
44 pos.Filename = pos.Filename[idx+len("/testdata/src/"):]
48 func TestErrors(t *testing.T) {
49 t.Run("invalid package declaration", func(t *testing.T) {
50 ps := lintPackage(t, "broken_pkgerror")
52 t.Fatalf("got %d problems, want 1", len(ps))
54 if want := "expected 'package', found pckage"; ps[0].Message != want {
55 t.Errorf("got message %q, want %q", ps[0].Message, want)
57 if ps[0].Position.Filename == "" {
58 t.Errorf("didn't get useful position")
62 t.Run("type error", func(t *testing.T) {
63 if runtime.GOOS == "windows" {
64 t.Skip("don't deal with Windows line endings or absolute file names")
66 ps := lintPackage(t, "broken_typeerror")
68 t.Fatalf("got %d problems, want 1", len(ps))
70 trimPosition(&ps[0].Position)
72 Diagnostic: runner.Diagnostic{
73 Position: token.Position{
74 Filename: "broken_typeerror/pkg.go",
79 Message: "cannot convert \"\" (untyped string constant) to int",
84 if !ps[0].equal(want) {
85 t.Errorf("got %#v, want %#v", ps[0], want)
89 t.Run("missing dep", func(t *testing.T) {
90 t.Skip("Go 1.12 behaves incorrectly for missing packages")
93 t.Run("parse error", func(t *testing.T) {
94 if runtime.GOOS == "windows" {
95 t.Skip("don't deal with Windows line endings or absolute file names")
97 ps := lintPackage(t, "broken_parse")
99 t.Fatalf("got %d problems, want 1", len(ps))
102 trimPosition(&ps[0].Position)
104 Diagnostic: runner.Diagnostic{
105 Position: token.Position{
106 Filename: "broken_parse/pkg.go",
111 Message: "expected declaration, found asd",
116 if !ps[0].equal(want) {
117 t.Errorf("got %#v, want %#v", ps[0], want)