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 | "io/ioutil" |
9 | "os" |
10 | "path/filepath" |
11 | "reflect" |
12 | "sort" |
13 | "testing" |
14 | |
15 | "golang.org/x/tools/go/packages/packagestest" |
16 | ) |
17 | |
18 | var testdata = []packagestest.Module{{ |
19 | Name: "golang.org/fake1", |
20 | Files: map[string]interface{}{ |
21 | "a.go": packagestest.Symlink("testdata/a.go"), // broken symlink |
22 | "b.go": "invalid file contents", |
23 | }, |
24 | Overlay: map[string][]byte{ |
25 | "b.go": []byte("package fake1"), |
26 | "c.go": []byte("package fake1"), |
27 | }, |
28 | }, { |
29 | Name: "golang.org/fake2", |
30 | Files: map[string]interface{}{ |
31 | "other/a.go": "package fake2", |
32 | }, |
33 | }, { |
34 | Name: "golang.org/fake2/v2", |
35 | Files: map[string]interface{}{ |
36 | "other/a.go": "package fake2", |
37 | }, |
38 | }, { |
39 | Name: "golang.org/fake3@v1.0.0", |
40 | Files: map[string]interface{}{ |
41 | "other/a.go": "package fake3", |
42 | }, |
43 | }, { |
44 | Name: "golang.org/fake3@v1.1.0", |
45 | Files: map[string]interface{}{ |
46 | "other/a.go": "package fake3", |
47 | }, |
48 | }} |
49 | |
50 | type fileTest struct { |
51 | module, fragment, expect string |
52 | check func(t *testing.T, exported *packagestest.Exported, filename string) |
53 | } |
54 | |
55 | func checkFiles(t *testing.T, exported *packagestest.Exported, tests []fileTest) { |
56 | for _, test := range tests { |
57 | expect := filepath.Join(exported.Temp(), filepath.FromSlash(test.expect)) |
58 | got := exported.File(test.module, test.fragment) |
59 | if got == "" { |
60 | t.Errorf("File %v missing from the output", expect) |
61 | } else if got != expect { |
62 | t.Errorf("Got file %v, expected %v", got, expect) |
63 | } |
64 | if test.check != nil { |
65 | test.check(t, exported, got) |
66 | } |
67 | } |
68 | } |
69 | |
70 | func checkLink(expect string) func(t *testing.T, exported *packagestest.Exported, filename string) { |
71 | expect = filepath.FromSlash(expect) |
72 | return func(t *testing.T, exported *packagestest.Exported, filename string) { |
73 | if target, err := os.Readlink(filename); err != nil { |
74 | t.Errorf("Error checking link %v: %v", filename, err) |
75 | } else if target != expect { |
76 | t.Errorf("Link %v does not match, got %v expected %v", filename, target, expect) |
77 | } |
78 | } |
79 | } |
80 | |
81 | func checkContent(expect string) func(t *testing.T, exported *packagestest.Exported, filename string) { |
82 | return func(t *testing.T, exported *packagestest.Exported, filename string) { |
83 | if content, err := exported.FileContents(filename); err != nil { |
84 | t.Errorf("Error reading %v: %v", filename, err) |
85 | } else if string(content) != expect { |
86 | t.Errorf("Content of %v does not match, got %v expected %v", filename, string(content), expect) |
87 | } |
88 | } |
89 | } |
90 | |
91 | func TestGroupFilesByModules(t *testing.T) { |
92 | for _, tt := range []struct { |
93 | testdir string |
94 | want []packagestest.Module |
95 | }{ |
96 | { |
97 | testdir: "testdata/groups/one", |
98 | want: []packagestest.Module{ |
99 | { |
100 | Name: "testdata/groups/one", |
101 | Files: map[string]interface{}{ |
102 | "main.go": true, |
103 | }, |
104 | }, |
105 | { |
106 | Name: "example.com/extra", |
107 | Files: map[string]interface{}{ |
108 | "help.go": true, |
109 | }, |
110 | }, |
111 | }, |
112 | }, |
113 | { |
114 | testdir: "testdata/groups/two", |
115 | want: []packagestest.Module{ |
116 | { |
117 | Name: "testdata/groups/two", |
118 | Files: map[string]interface{}{ |
119 | "main.go": true, |
120 | "expect/yo.go": true, |
121 | "expect/yo_test.go": true, |
122 | }, |
123 | }, |
124 | { |
125 | Name: "example.com/extra", |
126 | Files: map[string]interface{}{ |
127 | "yo.go": true, |
128 | "geez/help.go": true, |
129 | }, |
130 | }, |
131 | { |
132 | Name: "example.com/extra/v2", |
133 | Files: map[string]interface{}{ |
134 | "me.go": true, |
135 | "geez/help.go": true, |
136 | }, |
137 | }, |
138 | { |
139 | Name: "example.com/tempmod", |
140 | Files: map[string]interface{}{ |
141 | "main.go": true, |
142 | }, |
143 | }, |
144 | { |
145 | Name: "example.com/what@v1.0.0", |
146 | Files: map[string]interface{}{ |
147 | "main.go": true, |
148 | }, |
149 | }, |
150 | { |
151 | Name: "example.com/what@v1.1.0", |
152 | Files: map[string]interface{}{ |
153 | "main.go": true, |
154 | }, |
155 | }, |
156 | }, |
157 | }, |
158 | } { |
159 | t.Run(tt.testdir, func(t *testing.T) { |
160 | got, err := packagestest.GroupFilesByModules(tt.testdir) |
161 | if err != nil { |
162 | t.Fatalf("could not group files %v", err) |
163 | } |
164 | if len(got) != len(tt.want) { |
165 | t.Fatalf("%s: wanted %d modules but got %d", tt.testdir, len(tt.want), len(got)) |
166 | } |
167 | for i, w := range tt.want { |
168 | g := got[i] |
169 | if filepath.FromSlash(g.Name) != filepath.FromSlash(w.Name) { |
170 | t.Fatalf("%s: wanted module[%d].Name to be %s but got %s", tt.testdir, i, filepath.FromSlash(w.Name), filepath.FromSlash(g.Name)) |
171 | } |
172 | for fh := range w.Files { |
173 | if _, ok := g.Files[fh]; !ok { |
174 | t.Fatalf("%s, module[%d]: wanted %s but could not find", tt.testdir, i, fh) |
175 | } |
176 | } |
177 | for fh := range g.Files { |
178 | if _, ok := w.Files[fh]; !ok { |
179 | t.Fatalf("%s, module[%d]: found unexpected file %s", tt.testdir, i, fh) |
180 | } |
181 | } |
182 | } |
183 | }) |
184 | } |
185 | } |
186 | |
187 | func TestMustCopyFiles(t *testing.T) { |
188 | // Create the following test directory structure in a temporary directory. |
189 | src := map[string]string{ |
190 | // copies all files under the specified directory. |
191 | "go.mod": "module example.com", |
192 | "m.go": "package m", |
193 | "a/a.go": "package a", |
194 | // contents from a nested module shouldn't be copied. |
195 | "nested/go.mod": "module example.com/nested", |
196 | "nested/m.go": "package nested", |
197 | "nested/b/b.go": "package b", |
198 | } |
199 | |
200 | tmpDir, err := ioutil.TempDir("", t.Name()) |
201 | if err != nil { |
202 | t.Fatalf("failed to create a temporary directory: %v", err) |
203 | } |
204 | defer os.RemoveAll(tmpDir) |
205 | |
206 | for fragment, contents := range src { |
207 | fullpath := filepath.Join(tmpDir, filepath.FromSlash(fragment)) |
208 | if err := os.MkdirAll(filepath.Dir(fullpath), 0755); err != nil { |
209 | t.Fatal(err) |
210 | } |
211 | if err := ioutil.WriteFile(fullpath, []byte(contents), 0644); err != nil { |
212 | t.Fatal(err) |
213 | } |
214 | } |
215 | |
216 | copied := packagestest.MustCopyFileTree(tmpDir) |
217 | var got []string |
218 | for fragment := range copied { |
219 | got = append(got, filepath.ToSlash(fragment)) |
220 | } |
221 | want := []string{"go.mod", "m.go", "a/a.go"} |
222 | |
223 | sort.Strings(got) |
224 | sort.Strings(want) |
225 | if !reflect.DeepEqual(got, want) { |
226 | t.Errorf("packagestest.MustCopyFileTree = %v, want %v", got, want) |
227 | } |
228 | |
229 | // packagestest.Export is happy. |
230 | exported := packagestest.Export(t, packagestest.Modules, []packagestest.Module{{ |
231 | Name: "example.com", |
232 | Files: packagestest.MustCopyFileTree(tmpDir), |
233 | }}) |
234 | defer exported.Cleanup() |
235 | } |
236 |
Members