Skip to content

Commit dec6bf1

Browse files
committed
internal/modindex: update module cache index
This CL contains tests that the module cache index can be updated. The tests cover all the cases in modindex.go. Change-Id: Ia5988fb0d2be1cbe5e26d9d5d0f0d43ce1e0e5d2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/619695 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 6c6def2 commit dec6bf1

File tree

3 files changed

+117
-24
lines changed

3 files changed

+117
-24
lines changed

internal/modindex/dir_test.go

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"path/filepath"
1010
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
1113
)
1214

1315
type id struct {
@@ -51,19 +53,123 @@ func testModCache(t *testing.T) string {
5153
return dir
5254
}
5355

56+
// add a trivial package to the test module cache
57+
func addPkg(cachedir, dir string) error {
58+
if err := os.MkdirAll(filepath.Join(cachedir, dir), 0755); err != nil {
59+
return err
60+
}
61+
return os.WriteFile(filepath.Join(cachedir, 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+
if err := IndexModCache(dir, true); err != nil {
80+
t.Fatal(err)
81+
}
82+
// add new stuff to the module cache
83+
for _, it := range idtests {
84+
for i, d := range it.dirs {
85+
if it.best != i {
86+
continue // only add the new stuff
87+
}
88+
if err := addPkg(dir, d); err != nil {
89+
t.Fatal(err)
90+
}
91+
}
92+
}
93+
if err := IndexModCache(dir, false); err != nil {
94+
t.Fatal(err)
95+
}
96+
index2, err := ReadIndex(dir)
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
// build a fresh index
101+
if err := IndexModCache(dir, true); err != nil {
102+
t.Fatal(err)
103+
}
104+
index1, err := ReadIndex(dir)
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
// they should be the same except maybe for the time
109+
index1.Changed = index2.Changed
110+
if diff := cmp.Diff(index1, index2); diff != "" {
111+
t.Errorf("mismatching indexes (-updated +cleared):\n%s", diff)
112+
}
113+
}
114+
115+
// update, where new stuff is semantically worse than some old stuff
116+
func TestIncrementalNope(t *testing.T) {
117+
dir := testModCache(t)
118+
// build old index
119+
for _, it := range idtests {
120+
for i, d := range it.dirs {
121+
if i == 0 {
122+
continue // wait for second pass
123+
}
124+
if err := addPkg(dir, d); err != nil {
125+
t.Fatal(err)
126+
}
127+
}
128+
}
129+
if err := IndexModCache(dir, true); err != nil {
130+
t.Fatal(err)
131+
}
132+
// add new stuff to the module cache
133+
for _, it := range idtests {
134+
for i, d := range it.dirs {
135+
if i > 0 {
136+
break // only add the new one
137+
}
138+
if err := addPkg(dir, d); err != nil {
139+
t.Fatal(err)
140+
}
141+
}
142+
}
143+
if err := IndexModCache(dir, false); err != nil {
144+
t.Fatal(err)
145+
}
146+
index2, err := ReadIndex(dir)
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
// build a fresh index
151+
if err := IndexModCache(dir, true); err != nil {
152+
t.Fatal(err)
153+
}
154+
index1, err := ReadIndex(dir)
155+
if err != nil {
156+
t.Fatal(err)
157+
}
158+
// they should be the same except maybe for the time
159+
index1.Changed = index2.Changed
160+
if diff := cmp.Diff(index1, index2); diff != "" {
161+
t.Errorf("mismatching indexes (-updated +cleared):\n%s", diff)
162+
}
163+
}
164+
54165
// choose the semantically-latest version, with a single symbol
55166
func TestDirsSinglePath(t *testing.T) {
56167
for _, itest := range idtests {
57168
t.Run(itest.importPath, func(t *testing.T) {
58169
// create a new test GOMODCACHE
59170
dir := testModCache(t)
60171
for _, d := range itest.dirs {
61-
if err := os.MkdirAll(filepath.Join(dir, d), 0755); err != nil {
62-
t.Fatal(err)
63-
}
64-
err := os.WriteFile(filepath.Join(dir, d, "foo.go"),
65-
[]byte("package foo\nfunc Foo() {}"), 0600)
66-
if err != nil {
172+
if err := addPkg(dir, d); err != nil {
67173
t.Fatal(err)
68174
}
69175
}

internal/modindex/directories.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func byImportPath(dirs []Relpath) (map[string][]*directory, error) {
4949
return ans, nil
5050
}
5151

52-
// sort the directories by semantic version, lates first
52+
// sort the directories by semantic version, latest first
5353
func semanticSort(v []*directory) {
5454
slices.SortFunc(v, func(l, r *directory) int {
5555
if n := semver.Compare(l.version, r.version); n != 0 {

internal/modindex/modindex.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package modindex
1414

1515
import (
16-
"log"
1716
"path/filepath"
1817
"slices"
1918
"strings"
@@ -91,34 +90,22 @@ func (w *work) buildIndex() error {
9190
if err != nil {
9291
return err
9392
}
94-
log.Printf("%d dirs, %d new", len(dirs), len(newdirs))
9593
// for each import path it might occur only in newdirs,
9694
// only in w.oldIndex, or in both.
9795
// If it occurs in both, use the semantically later one
9896
if w.oldIndex != nil {
99-
killed := 0
10097
for _, e := range w.oldIndex.Entries {
10198
found, ok := newdirs[e.ImportPath]
10299
if !ok {
103-
continue
100+
w.newIndex.Entries = append(w.newIndex.Entries, e)
101+
continue // use this one, there is no new one
104102
}
105103
if semver.Compare(found[0].version, e.Version) > 0 {
106-
// the new one is better, disable the old one
107-
e.ImportPath = ""
108-
killed++
104+
// use the new one
109105
} else {
110106
// use the old one, forget the new one
111-
delete(newdirs, e.ImportPath)
112-
}
113-
}
114-
log.Printf("%d killed, %d new", killed, len(newdirs))
115-
}
116-
// Build the skeleton of the new index using newdirs,
117-
// and include the surviving parts of the old index
118-
if w.oldIndex != nil {
119-
for _, e := range w.oldIndex.Entries {
120-
if e.ImportPath != "" {
121107
w.newIndex.Entries = append(w.newIndex.Entries, e)
108+
delete(newdirs, e.ImportPath)
122109
}
123110
}
124111
}

0 commit comments

Comments
 (0)