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