GoPLS Viewer

Home|gopls/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
5package packagestest_test
6
7import (
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
18var 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
50type fileTest struct {
51    modulefragmentexpect string
52    check                    func(t *testing.Texported *packagestest.Exportedfilename string)
53}
54
55func checkFiles(t *testing.Texported *packagestest.Exportedtests []fileTest) {
56    for _test := range tests {
57        expect := filepath.Join(exported.Temp(), filepath.FromSlash(test.expect))
58        got := exported.File(test.moduletest.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"gotexpect)
63        }
64        if test.check != nil {
65            test.check(texportedgot)
66        }
67    }
68}
69
70func checkLink(expect string) func(t *testing.Texported *packagestest.Exportedfilename string) {
71    expect = filepath.FromSlash(expect)
72    return func(t *testing.Texported *packagestest.Exportedfilename string) {
73        if targeterr := os.Readlink(filename); err != nil {
74            t.Errorf("Error checking link %v: %v"filenameerr)
75        } else if target != expect {
76            t.Errorf("Link %v does not match, got %v expected %v"filenametargetexpect)
77        }
78    }
79}
80
81func checkContent(expect string) func(t *testing.Texported *packagestest.Exportedfilename string) {
82    return func(t *testing.Texported *packagestest.Exportedfilename string) {
83        if contenterr := exported.FileContents(filename); err != nil {
84            t.Errorf("Error reading %v: %v"filenameerr)
85        } else if string(content) != expect {
86            t.Errorf("Content of %v does not match, got %v expected %v"filenamestring(content), expect)
87        }
88    }
89}
90
91func 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            goterr := 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.testdirlen(tt.want), len(got))
166            }
167            for iw := 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.testdirifilepath.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.testdirifh)
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.testdirifh)
180                    }
181                }
182            }
183        })
184    }
185}
186
187func 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    tmpDirerr := 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 fragmentcontents := range src {
207        fullpath := filepath.Join(tmpDirfilepath.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(gotfilepath.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(gotwant) {
226        t.Errorf("packagestest.MustCopyFileTree = %v, want %v"gotwant)
227    }
228
229    // packagestest.Export is happy.
230    exported := packagestest.Export(tpackagestest.Modules, []packagestest.Module{{
231        Name:  "example.com",
232        Filespackagestest.MustCopyFileTree(tmpDir),
233    }})
234    defer exported.Cleanup()
235}
236
MembersX
checkFiles.tests
checkContent.BlockStmt.err
fileTest.module
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.err
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.RangeStmt_4223.i
TestMustCopyFiles.err
TestMustCopyFiles.RangeStmt_5421.contents
TestMustCopyFiles.RangeStmt_5421.BlockStmt.fullpath
TestMustCopyFiles.RangeStmt_5421.BlockStmt.err
TestMustCopyFiles.RangeStmt_5782.fragment
TestGroupFilesByModules.RangeStmt_2615.tt
checkLink.BlockStmt.target
TestGroupFilesByModules.t
TestMustCopyFiles.RangeStmt_5421.fragment
fileTest.fragment
checkFiles.RangeStmt_1284.BlockStmt.expect
checkLink.expect
TestGroupFilesByModules
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.RangeStmt_4223.BlockStmt.RangeStmt_4639.fh
TestMustCopyFiles
TestMustCopyFiles.copied
TestMustCopyFiles.got
checkFiles.exported
fileTest
fileTest.expect
checkFiles.RangeStmt_1284.test
checkLink
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.RangeStmt_4223.BlockStmt.RangeStmt_4478.fh
sort
checkFiles.t
checkContent
checkContent.BlockStmt.content
fileTest.check
checkContent.expect
TestMustCopyFiles.tmpDir
TestMustCopyFiles.exported
checkFiles.RangeStmt_1284.BlockStmt.got
checkLink.BlockStmt.err
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.got
TestGroupFilesByModules.RangeStmt_2615.BlockStmt.BlockStmt.RangeStmt_4223.w
TestMustCopyFiles.t
TestMustCopyFiles.src
TestMustCopyFiles.want
checkFiles
Members
X