.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / lintcmd / lint_test.go
1 package lintcmd
2
3 import (
4         "go/token"
5         "log"
6         "os"
7         "path/filepath"
8         "runtime"
9         "strings"
10         "testing"
11
12         "honnef.co/go/tools/config"
13         "honnef.co/go/tools/lintcmd/runner"
14
15         "golang.org/x/tools/go/packages"
16 )
17
18 func testdata() string {
19         testdata, err := filepath.Abs("testdata")
20         if err != nil {
21                 log.Fatal(err)
22         }
23         return testdata
24 }
25
26 func lintPackage(t *testing.T, name string) []problem {
27         l, err := newLinter(config.Config{})
28         if err != nil {
29                 t.Fatal(err)
30         }
31         cfg := &packages.Config{
32                 Env: append(os.Environ(), "GOPATH="+testdata(), "GO111MODULE=off"),
33         }
34         ps, _, err := l.Lint(cfg, []string{name})
35         if err != nil {
36                 t.Fatal(err)
37         }
38         return ps
39 }
40
41 func trimPosition(pos *token.Position) {
42         idx := strings.Index(pos.Filename, "/testdata/src/")
43         if idx >= 0 {
44                 pos.Filename = pos.Filename[idx+len("/testdata/src/"):]
45         }
46 }
47
48 func TestErrors(t *testing.T) {
49         t.Run("invalid package declaration", func(t *testing.T) {
50                 ps := lintPackage(t, "broken_pkgerror")
51                 if len(ps) != 1 {
52                         t.Fatalf("got %d problems, want 1", len(ps))
53                 }
54                 if want := "expected 'package', found pckage"; ps[0].Message != want {
55                         t.Errorf("got message %q, want %q", ps[0].Message, want)
56                 }
57                 if ps[0].Position.Filename == "" {
58                         t.Errorf("didn't get useful position")
59                 }
60         })
61
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")
65                 }
66                 ps := lintPackage(t, "broken_typeerror")
67                 if len(ps) != 1 {
68                         t.Fatalf("got %d problems, want 1", len(ps))
69                 }
70                 trimPosition(&ps[0].Position)
71                 want := problem{
72                         Diagnostic: runner.Diagnostic{
73                                 Position: token.Position{
74                                         Filename: "broken_typeerror/pkg.go",
75                                         Offset:   0,
76                                         Line:     5,
77                                         Column:   10,
78                                 },
79                                 Message:  "cannot convert \"\" (untyped string constant) to int",
80                                 Category: "compile",
81                         },
82                         Severity: 0,
83                 }
84                 if !ps[0].equal(want) {
85                         t.Errorf("got %#v, want %#v", ps[0], want)
86                 }
87         })
88
89         t.Run("missing dep", func(t *testing.T) {
90                 t.Skip("Go 1.12 behaves incorrectly for missing packages")
91         })
92
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")
96                 }
97                 ps := lintPackage(t, "broken_parse")
98                 if len(ps) != 1 {
99                         t.Fatalf("got %d problems, want 1", len(ps))
100                 }
101
102                 trimPosition(&ps[0].Position)
103                 want := problem{
104                         Diagnostic: runner.Diagnostic{
105                                 Position: token.Position{
106                                         Filename: "broken_parse/pkg.go",
107                                         Offset:   0,
108                                         Line:     3,
109                                         Column:   1,
110                                 },
111                                 Message:  "expected declaration, found asd",
112                                 Category: "compile",
113                         },
114                         Severity: 0,
115                 }
116                 if !ps[0].equal(want) {
117                         t.Errorf("got %#v, want %#v", ps[0], want)
118                 }
119         })
120 }