| 1 | // Copyright 2024 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 modindex_test |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/google/go-cmp/cmp" |
| 13 | |
| 14 | . "golang.org/x/tools/internal/modindex" |
| 15 | ) |
| 16 | |
| 17 | type id struct { |
| 18 | importPath string |
| 19 | best int // which of the dirs is the one that should have been chosen |
| 20 | dirs []string |
| 21 | } |
| 22 | |
| 23 | var idtests = []id{ |
| 24 | { // get one right |
| 25 | importPath: "cloud.google.com/go/longrunning", |
| 26 | best: 2, |
| 27 | dirs: []string{ |
| 28 | "cloud.google.com/go/[email protected]", |
| 29 | "cloud.google.com/go/[email protected]", |
| 30 | "cloud.google.com/[email protected]/longrunning", |
| 31 | "cloud.google.com/[email protected]/longrunning", |
| 32 | }, |
| 33 | }, |
| 34 | { // make sure we can run more than one test |
| 35 | importPath: "cloud.google.com/go/compute/metadata", |
| 36 | best: 2, |
| 37 | dirs: []string{ |
| 38 | "cloud.google.com/go/compute/[email protected]", |
| 39 | "cloud.google.com/go/compute/[email protected]", |
| 40 | "cloud.google.com/go/[email protected]/metadata", |
| 41 | "cloud.google.com/[email protected]/compute/metadata", |
| 42 | }, |
| 43 | }, |
| 44 | { // test bizarre characters in directory name |
| 45 | importPath: "bad,guy.com/go", |
| 46 | best: 0, |
| 47 | dirs: []string{"bad,guy.com/[email protected]"}, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | func testModCache(t *testing.T) string { |
| 52 | IndexDir = t.TempDir() |
| 53 | return IndexDir |
| 54 | } |
| 55 | |
| 56 | // add a trivial package to the test module cache |
| 57 | func addPkg(gomodcache, dir string) error { |
| 58 | if err := os.MkdirAll(filepath.Join(gomodcache, dir), 0755); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | return os.WriteFile(filepath.Join(gomodcache, dir, "foo.go"), |
| 62 | []byte("package foo\nfunc Foo() {}"), 0644) |
| 63 | } |
| 64 | |
| 65 | // update, where new stuff is semantically better than old stuff |
| 66 | func TestIncremental(t *testing.T) { |
| 67 | dir := testModCache(t) |
| 68 | // build old index |
| 69 | for _, it := range idtests { |
| 70 | for i, d := range it.dirs { |
| 71 | if it.best == i { |
| 72 | continue // wait for second pass |
| 73 | } |
| 74 | if err := addPkg(dir, d); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | index0, err := Create(dir) |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | // add new stuff to the module cache |
| 84 | for _, it := range idtests { |
| 85 | for i, d := range it.dirs { |
| 86 | if it.best != i { |
| 87 | continue // only add the new stuff |
| 88 | } |
| 89 | if err := addPkg(dir, d); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if index1, err := Update(dir); err != nil { |
| 95 | t.Fatalf("failed to update index: %v", err) |
| 96 | } else if before, after := len(index0.Entries), len(index1.Entries); after <= before { |
| 97 | t.Fatalf("updated index is not larger (before %d, after %d)", before, after) |
| 98 | } |
| 99 | index2, err := Read(dir) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | // build a fresh index |
| 104 | if _, err := Create(dir); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | index1, err := Read(dir) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | // they should be the same except maybe for the time |
| 112 | index1.ValidAt = index2.ValidAt |
| 113 | if diff := cmp.Diff(index1, index2); diff != "" { |
| 114 | t.Errorf("mismatching indexes (-updated +cleared):\n%s", diff) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // update, where new stuff is semantically worse than some old stuff |
| 119 | func TestIncrementalNope(t *testing.T) { |
| 120 | dir := testModCache(t) |
| 121 | // build old index |
| 122 | for _, it := range idtests { |
| 123 | for i, d := range it.dirs { |
| 124 | if i == 0 { |
| 125 | continue // wait for second pass |
| 126 | } |
| 127 | if err := addPkg(dir, d); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | if _, err := Create(dir); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | // add new stuff to the module cache |
| 136 | for _, it := range idtests { |
| 137 | for i, d := range it.dirs { |
| 138 | if i > 0 { |
| 139 | break // only add the new one |
| 140 | } |
| 141 | if err := addPkg(dir, d); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | index2, err := Update(dir) |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | // build a fresh index |
| 151 | if _, err := Create(dir); err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | index1, err := Read(dir) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | // they should be the same except maybe for the time |
| 159 | index1.ValidAt = index2.ValidAt |
| 160 | if diff := cmp.Diff(index1, index2); diff != "" { |
| 161 | t.Errorf("mismatching indexes (-updated +cleared):\n%s", diff) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // choose the semantically-latest version, with a single symbol |
| 166 | func TestDirsSinglePath(t *testing.T) { |
| 167 | for _, itest := range idtests { |
| 168 | t.Run(itest.importPath, func(t *testing.T) { |
| 169 | // create a new test GOMODCACHE |
| 170 | dir := testModCache(t) |
| 171 | for _, d := range itest.dirs { |
| 172 | if err := addPkg(dir, d); err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | } |
| 176 | // build and check the index |
| 177 | if _, err := Create(dir); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | ix, err := Read(dir) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if len(ix.Entries) != 1 { |
| 185 | t.Fatalf("got %d entries, wanted 1", len(ix.Entries)) |
| 186 | } |
| 187 | if ix.Entries[0].ImportPath != itest.importPath { |
| 188 | t.Fatalf("got %s import path, wanted %s", ix.Entries[0].ImportPath, itest.importPath) |
| 189 | } |
| 190 | gotDir := filepath.ToSlash(ix.Entries[0].Dir) |
| 191 | if gotDir != itest.dirs[itest.best] { |
| 192 | t.Fatalf("got dir %s, wanted %s", gotDir, itest.dirs[itest.best]) |
| 193 | } |
| 194 | nms := ix.Entries[0].Names |
| 195 | if len(nms) != 1 { |
| 196 | t.Fatalf("got %d names, expected 1", len(nms)) |
| 197 | } |
| 198 | if nms[0] != "Foo F 0" { |
| 199 | t.Fatalf("got %q, expected Foo F 0", nms[0]) |
| 200 | } |
| 201 | }) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | func TestMissingGOMODCACHE(t *testing.T) { |
| 206 | // behave properly if the cached dir is empty |
| 207 | dir := testModCache(t) |
| 208 | if _, err := Create(dir); err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | des, err := os.ReadDir(IndexDir) |
| 212 | if err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | if len(des) != 2 { |
| 216 | t.Errorf("got %d, but expected two entries in index dir", len(des)) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestMissingIndex(t *testing.T) { |
| 221 | // behave properly if there is no existing index |
| 222 | dir := testModCache(t) |
| 223 | if _, err := Update(dir); err != nil { |
| 224 | t.Fatal(err) |
| 225 | } |
| 226 | des, err := os.ReadDir(IndexDir) |
| 227 | if err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | if len(des) != 2 { |
| 231 | t.Errorf("got %d, but expected two entries in index dir", len(des)) |
| 232 | } |
| 233 | } |