Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / go / packages / packagestest / export_test.go
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package packagestest_test
6
7 import (
8         "os"
9         "path/filepath"
10         "testing"
11
12         "golang.org/x/tools/go/packages/packagestest"
13 )
14
15 var testdata = []packagestest.Module{{
16         Name: "golang.org/fake1",
17         Files: map[string]interface{}{
18                 "a.go": packagestest.Symlink("testdata/a.go"),
19                 "b.go": "invalid file contents",
20         },
21         Overlay: map[string][]byte{
22                 "b.go": []byte("package fake1"),
23                 "c.go": []byte("package fake1"),
24         },
25 }, {
26         Name: "golang.org/fake2",
27         Files: map[string]interface{}{
28                 "other/a.go": "package fake2",
29         },
30 }, {
31         Name: "golang.org/fake2/v2",
32         Files: map[string]interface{}{
33                 "other/a.go": "package fake2",
34         },
35 }, {
36         Name: "golang.org/fake3@v1.0.0",
37         Files: map[string]interface{}{
38                 "other/a.go": "package fake3",
39         },
40 }, {
41         Name: "golang.org/fake3@v1.1.0",
42         Files: map[string]interface{}{
43                 "other/a.go": "package fake3",
44         },
45 }}
46
47 type fileTest struct {
48         module, fragment, expect string
49         check                    func(t *testing.T, exported *packagestest.Exported, filename string)
50 }
51
52 func checkFiles(t *testing.T, exported *packagestest.Exported, tests []fileTest) {
53         for _, test := range tests {
54                 expect := filepath.Join(exported.Temp(), filepath.FromSlash(test.expect))
55                 got := exported.File(test.module, test.fragment)
56                 if got == "" {
57                         t.Errorf("File %v missing from the output", expect)
58                 } else if got != expect {
59                         t.Errorf("Got file %v, expected %v", got, expect)
60                 }
61                 if test.check != nil {
62                         test.check(t, exported, got)
63                 }
64         }
65 }
66
67 func checkLink(expect string) func(t *testing.T, exported *packagestest.Exported, filename string) {
68         expect = filepath.FromSlash(expect)
69         return func(t *testing.T, exported *packagestest.Exported, filename string) {
70                 if target, err := os.Readlink(filename); err != nil {
71                         t.Errorf("Error checking link %v: %v", filename, err)
72                 } else if target != expect {
73                         t.Errorf("Link %v does not match, got %v expected %v", filename, target, expect)
74                 }
75         }
76 }
77
78 func checkContent(expect string) func(t *testing.T, exported *packagestest.Exported, filename string) {
79         return func(t *testing.T, exported *packagestest.Exported, filename string) {
80                 if content, err := exported.FileContents(filename); err != nil {
81                         t.Errorf("Error reading %v: %v", filename, err)
82                 } else if string(content) != expect {
83                         t.Errorf("Content of %v does not match, got %v expected %v", filename, string(content), expect)
84                 }
85         }
86 }
87
88 func TestGroupFilesByModules(t *testing.T) {
89         for _, tt := range []struct {
90                 testdir string
91                 want    []packagestest.Module
92         }{
93                 {
94                         testdir: "testdata/groups/one",
95                         want: []packagestest.Module{
96                                 {
97                                         Name: "testdata/groups/one",
98                                         Files: map[string]interface{}{
99                                                 "main.go": true,
100                                         },
101                                 },
102                                 {
103                                         Name: "example.com/extra",
104                                         Files: map[string]interface{}{
105                                                 "help.go": true,
106                                         },
107                                 },
108                         },
109                 },
110                 {
111                         testdir: "testdata/groups/two",
112                         want: []packagestest.Module{
113                                 {
114                                         Name: "testdata/groups/two",
115                                         Files: map[string]interface{}{
116                                                 "main.go":           true,
117                                                 "expect/yo.go":      true,
118                                                 "expect/yo_test.go": true,
119                                         },
120                                 },
121                                 {
122                                         Name: "example.com/extra",
123                                         Files: map[string]interface{}{
124                                                 "yo.go":        true,
125                                                 "geez/help.go": true,
126                                         },
127                                 },
128                                 {
129                                         Name: "example.com/extra/v2",
130                                         Files: map[string]interface{}{
131                                                 "me.go":        true,
132                                                 "geez/help.go": true,
133                                         },
134                                 },
135                                 {
136                                         Name: "example.com/tempmod",
137                                         Files: map[string]interface{}{
138                                                 "main.go": true,
139                                         },
140                                 },
141                                 {
142                                         Name: "example.com/what@v1.0.0",
143                                         Files: map[string]interface{}{
144                                                 "main.go": true,
145                                         },
146                                 },
147                                 {
148                                         Name: "example.com/what@v1.1.0",
149                                         Files: map[string]interface{}{
150                                                 "main.go": true,
151                                         },
152                                 },
153                         },
154                 },
155         } {
156                 t.Run(tt.testdir, func(t *testing.T) {
157                         got, err := packagestest.GroupFilesByModules(tt.testdir)
158                         if err != nil {
159                                 t.Fatalf("could not group files %v", err)
160                         }
161                         if len(got) != len(tt.want) {
162                                 t.Fatalf("%s: wanted %d modules but got %d", tt.testdir, len(tt.want), len(got))
163                         }
164                         for i, w := range tt.want {
165                                 g := got[i]
166                                 if filepath.FromSlash(g.Name) != filepath.FromSlash(w.Name) {
167                                         t.Fatalf("%s: wanted module[%d].Name to be %s but got %s", tt.testdir, i, filepath.FromSlash(w.Name), filepath.FromSlash(g.Name))
168                                 }
169                                 for fh := range w.Files {
170                                         if _, ok := g.Files[fh]; !ok {
171                                                 t.Fatalf("%s, module[%d]: wanted %s but could not find", tt.testdir, i, fh)
172                                         }
173                                 }
174                                 for fh := range g.Files {
175                                         if _, ok := w.Files[fh]; !ok {
176                                                 t.Fatalf("%s, module[%d]: found unexpected file %s", tt.testdir, i, fh)
177                                         }
178                                 }
179                         }
180                 })
181         }
182 }