12 "golang.org/x/tools/go/packages"
15 func testdata() string {
16 testdata, err := filepath.Abs("testdata")
23 func lintPackage(t *testing.T, name string) []Problem {
25 cfg := &packages.Config{
26 Env: append(os.Environ(), "GOPATH="+testdata(), "GO111MODULE=off"),
28 ps, err := l.Lint(cfg, []string{name})
35 func trimPosition(pos *token.Position) {
36 idx := strings.Index(pos.Filename, "/testdata/src/")
38 pos.Filename = pos.Filename[idx+len("/testdata/src/"):]
42 func TestErrors(t *testing.T) {
43 t.Run("invalid package declaration", func(t *testing.T) {
44 ps := lintPackage(t, "broken_pkgerror")
46 t.Fatalf("got %d problems, want 1", len(ps))
48 if want := "expected 'package', found pckage"; ps[0].Message != want {
49 t.Errorf("got message %q, want %q", ps[0].Message, want)
51 if ps[0].Pos.Filename == "" {
52 t.Errorf("didn't get useful position")
56 t.Run("type error", func(t *testing.T) {
57 if runtime.GOOS == "windows" {
58 t.Skip("don't deal with Windows line endings or absolute file names")
60 ps := lintPackage(t, "broken_typeerror")
62 t.Fatalf("got %d problems, want 1", len(ps))
64 trimPosition(&ps[0].Pos)
67 Filename: "broken_typeerror/pkg.go",
72 Message: "cannot convert \"\" (untyped string constant) to int",
76 if !ps[0].Equal(want) {
77 t.Errorf("got %#v, want %#v", ps[0], want)
81 t.Run("missing dep", func(t *testing.T) {
82 t.Skip("Go 1.12 behaves incorrectly for missing packages")
85 t.Run("parse error", func(t *testing.T) {
86 if runtime.GOOS == "windows" {
87 t.Skip("don't deal with Windows line endings or absolute file names")
89 ps := lintPackage(t, "broken_parse")
91 t.Fatalf("got %d problems, want 1", len(ps))
94 trimPosition(&ps[0].Pos)
97 Filename: "broken_parse/pkg.go",
102 Message: "expected declaration, found asd",
106 if !ps[0].Equal(want) {
107 t.Errorf("got %#v, want %#v", ps[0], want)