|
8 | 8 | "os"
|
9 | 9 | "path/filepath"
|
10 | 10 | "testing"
|
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
11 | 13 | )
|
12 | 14 |
|
13 | 15 | type id struct {
|
@@ -51,19 +53,123 @@ func testModCache(t *testing.T) string {
|
51 | 53 | return dir
|
52 | 54 | }
|
53 | 55 |
|
| 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 | + |
54 | 165 | // choose the semantically-latest version, with a single symbol
|
55 | 166 | func TestDirsSinglePath(t *testing.T) {
|
56 | 167 | for _, itest := range idtests {
|
57 | 168 | t.Run(itest.importPath, func(t *testing.T) {
|
58 | 169 | // create a new test GOMODCACHE
|
59 | 170 | dir := testModCache(t)
|
60 | 171 | 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 { |
67 | 173 | t.Fatal(err)
|
68 | 174 | }
|
69 | 175 | }
|
|
0 commit comments