-
-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathlint_test.go
More file actions
103 lines (93 loc) · 2.17 KB
/
lint_test.go
File metadata and controls
103 lines (93 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package lint
import (
"go/token"
"log"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/tools/go/packages"
)
func testdata() string {
testdata, err := filepath.Abs("testdata")
if err != nil {
log.Fatal(err)
}
return testdata
}
func lintPackage(t *testing.T, name string) []Problem {
l := Linter{}
cfg := &packages.Config{
Env: append(os.Environ(), "GOPATH="+testdata(), "GO111MODULE=off"),
}
ps, err := l.Lint(cfg, []string{name})
if err != nil {
t.Fatal(err)
}
return ps
}
func trimPosition(pos *token.Position) {
idx := strings.Index(pos.Filename, "/testdata/src/")
if idx >= 0 {
pos.Filename = pos.Filename[idx+len("/testdata/src/"):]
}
}
func TestErrors(t *testing.T) {
t.Run("invalid package declaration", func(t *testing.T) {
ps := lintPackage(t, "broken_pkgerror")
if len(ps) != 1 {
t.Fatalf("got %d problems, want 1", len(ps))
}
if want := "expected 'package', found pckage"; ps[0].Message != want {
t.Errorf("got message %q, want %q", ps[0].Message, want)
}
if ps[0].Pos.Filename == "" {
t.Errorf("didn't get useful position")
}
})
t.Run("type error", func(t *testing.T) {
ps := lintPackage(t, "broken_typeerror")
if len(ps) != 1 {
t.Fatalf("got %d problems, want 1", len(ps))
}
trimPosition(&ps[0].Pos)
want := Problem{
Pos: token.Position{
Filename: "broken_typeerror/pkg.go",
Offset: 42,
Line: 5,
Column: 10,
},
Message: "cannot convert \"\" (untyped string constant) to int",
Check: "compile",
Severity: 0,
}
if ps[0] != want {
t.Errorf("got %#v, want %#v", ps[0], want)
}
})
t.Run("missing dep", func(t *testing.T) {
t.Skip("Go 1.12 behaves incorrectly for missing packages")
})
t.Run("parse error", func(t *testing.T) {
ps := lintPackage(t, "broken_parse")
if len(ps) != 1 {
t.Fatalf("got %d problems, want 1", len(ps))
}
trimPosition(&ps[0].Pos)
want := Problem{
Pos: token.Position{
Filename: "broken_parse/pkg.go",
Offset: 13,
Line: 3,
Column: 1,
},
Message: "expected declaration, found asd",
Check: "compile",
Severity: 0,
}
if ps[0] != want {
t.Errorf("got %#v, want %#v", ps[0], want)
}
})
}