-
Notifications
You must be signed in to change notification settings - Fork 85
/
merge_test.go
126 lines (108 loc) · 2.57 KB
/
merge_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package zoekt
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
// We compare 2 simple shards before and after the transformation
// explode(merge(shard1, shard2)). We expect the input and output shards to be
// identical.
func TestExplode(t *testing.T) {
simpleShards := []string{
"./testdata/shards/repo_v16.00000.zoekt",
"./testdata/shards/repo2_v16.00000.zoekt",
}
// repo name -> IndexMetadata
m := make(map[string]*IndexMetadata, 2)
// merge
var files []IndexFile
for _, fn := range simpleShards {
f, err := os.Open(fn)
if err != nil {
t.Fatal(err)
}
defer f.Close()
indexFile, err := NewIndexFile(f)
if err != nil {
t.Fatal(err)
}
defer indexFile.Close()
// We save indexMeta because the fields ID and IndexTime are the 2 sources of
// non-determinism when building a new shard.
repoMeta, indexMeta, err := ReadMetadata(indexFile)
if err != nil {
t.Fatal(err)
}
if len(repoMeta) != 1 {
t.Fatal("this test assumes that indexFile contains only 1 repo")
}
m[repoMeta[0].Name] = indexMeta
files = append(files, indexFile)
}
tmpDir := t.TempDir()
tmpName, dstName, err := Merge(tmpDir, files...)
if err != nil {
t.Fatal(err)
}
err = os.Rename(tmpName, dstName)
if err != nil {
t.Fatal(err)
}
// explode
f, err := os.Open(dstName)
if err != nil {
t.Fatal(err)
}
defer f.Close()
indexFile, err := NewIndexFile(f)
if err != nil {
t.Fatal(err)
}
defer indexFile.Close()
overwriteIndexTimeAndID := func(ib *IndexBuilder) {
ib.ID = m[ib.repoList[0].Name].ID
ib.IndexTime = m[ib.repoList[0].Name].IndexTime
}
exploded, err := explode(tmpDir, indexFile, overwriteIndexTimeAndID)
if err != nil {
t.Fatal(err)
}
for tmp, final := range exploded {
err = os.Rename(tmp, final)
if err != nil {
t.Fatal(err)
}
}
for _, s := range simpleShards {
checkSameShards(t, s, filepath.Join(tmpDir, filepath.Base(s)))
}
}
// checkSameShards compares 2 shards byte by byte. The shards are expected to be
// small enough to be read in all at once.
func checkSameShards(t *testing.T, shard1, shard2 string) {
t.Helper()
b1, err := os.ReadFile(shard1)
if err != nil {
t.Fatal(err)
}
b2, err := os.ReadFile(shard2)
if err != nil {
t.Fatal(err)
}
// We could also use bytes.Equal, but the output of cmd.Diff is very helpful for
// differences in metadata.
d := cmp.Diff(b1, b2)
if d == "" {
return
}
if *update {
t.Logf("updating %s", shard1)
err := os.WriteFile(shard1, b2, 0o600)
if err != nil {
t.Fatal(err)
}
return
}
t.Fatalf("-%s\n+%s:\n%s", shard1, shard2, d)
}