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 "os" 9 "path/filepath" 10 "testing" 11 12 "golang.org/x/tools/go/packages/packagestest" 13) 14 15var 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 47type fileTest struct { 48 module, fragment, expect string 49 check func(t *testing.T, exported *packagestest.Exported, filename string) 50} 51 52func 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 67func 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 78func 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 88func 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 }, 119 }, 120 { 121 Name: "example.com/extra", 122 Files: map[string]interface{}{ 123 "yo.go": true, 124 "geez/help.go": true, 125 }, 126 }, 127 { 128 Name: "example.com/extra/v2", 129 Files: map[string]interface{}{ 130 "me.go": true, 131 "geez/help.go": true, 132 }, 133 }, 134 { 135 Name: "example.com/tempmod", 136 Files: map[string]interface{}{ 137 "main.go": true, 138 }, 139 }, 140 { 141 Name: "example.com/what@v1.0.0", 142 Files: map[string]interface{}{ 143 "main.go": true, 144 }, 145 }, 146 { 147 Name: "example.com/what@v1.1.0", 148 Files: map[string]interface{}{ 149 "main.go": true, 150 }, 151 }, 152 }, 153 }, 154 } { 155 t.Run(tt.testdir, func(t *testing.T) { 156 got, err := packagestest.GroupFilesByModules(tt.testdir) 157 if err != nil { 158 t.Fatalf("could not group files %v", err) 159 } 160 if len(got) != len(tt.want) { 161 t.Fatalf("%s: wanted %d modules but got %d", tt.testdir, len(tt.want), len(got)) 162 } 163 for i, w := range tt.want { 164 g := got[i] 165 if filepath.FromSlash(g.Name) != filepath.FromSlash(w.Name) { 166 t.Fatalf("%s: wanted module[%d].Name to be %s but got %s", tt.testdir, i, filepath.FromSlash(w.Name), filepath.FromSlash(g.Name)) 167 } 168 for fh := range w.Files { 169 if _, ok := g.Files[fh]; !ok { 170 t.Fatalf("%s, module[%d]: wanted %s but could not find", tt.testdir, i, fh) 171 } 172 } 173 for fh := range g.Files { 174 if _, ok := w.Files[fh]; !ok { 175 t.Fatalf("%s, module[%d]: found unexpected file %s", tt.testdir, i, fh) 176 } 177 } 178 } 179 }) 180 } 181} 182