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