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.
5 package packagestest_test
12 "golang.org/x/tools/go/packages/packagestest"
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",
21 Overlay: map[string][]byte{
22 "b.go": []byte("package fake1"),
23 "c.go": []byte("package fake1"),
26 Name: "golang.org/fake2",
27 Files: map[string]interface{}{
28 "other/a.go": "package fake2",
31 Name: "golang.org/fake2/v2",
32 Files: map[string]interface{}{
33 "other/a.go": "package fake2",
36 Name: "golang.org/fake3@v1.0.0",
37 Files: map[string]interface{}{
38 "other/a.go": "package fake3",
41 Name: "golang.org/fake3@v1.1.0",
42 Files: map[string]interface{}{
43 "other/a.go": "package fake3",
47 type fileTest struct {
48 module, fragment, expect string
49 check func(t *testing.T, exported *packagestest.Exported, filename string)
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)
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)
61 if test.check != nil {
62 test.check(t, exported, got)
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)
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)
88 func TestGroupFilesByModules(t *testing.T) {
89 for _, tt := range []struct {
91 want []packagestest.Module
94 testdir: "testdata/groups/one",
95 want: []packagestest.Module{
97 Name: "testdata/groups/one",
98 Files: map[string]interface{}{
103 Name: "example.com/extra",
104 Files: map[string]interface{}{
111 testdir: "testdata/groups/two",
112 want: []packagestest.Module{
114 Name: "testdata/groups/two",
115 Files: map[string]interface{}{
117 "expect/yo.go": true,
118 "expect/yo_test.go": true,
122 Name: "example.com/extra",
123 Files: map[string]interface{}{
125 "geez/help.go": true,
129 Name: "example.com/extra/v2",
130 Files: map[string]interface{}{
132 "geez/help.go": true,
136 Name: "example.com/tempmod",
137 Files: map[string]interface{}{
142 Name: "example.com/what@v1.0.0",
143 Files: map[string]interface{}{
148 Name: "example.com/what@v1.1.0",
149 Files: map[string]interface{}{
156 t.Run(tt.testdir, func(t *testing.T) {
157 got, err := packagestest.GroupFilesByModules(tt.testdir)
159 t.Fatalf("could not group files %v", err)
161 if len(got) != len(tt.want) {
162 t.Fatalf("%s: wanted %d modules but got %d", tt.testdir, len(tt.want), len(got))
164 for i, w := range tt.want {
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))
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)
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)