fork of https://github.com/sourcegraph/zoekt
1package main
2
3import (
4 "os"
5 "strings"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9 "github.com/sourcegraph/zoekt"
10 "github.com/sourcegraph/zoekt/index"
11)
12
13func TestMergeMeta(t *testing.T) {
14 dir := t.TempDir()
15
16 repoNames := []string{"repo0", "repo1", "repo2", "repo3"}
17 var repoFns []string
18
19 for _, name := range repoNames {
20 opts := index.Options{
21 IndexDir: dir,
22 RepositoryDescription: zoekt.Repository{
23 Name: name,
24 RawConfig: map[string]string{
25 "public": "1",
26 },
27 },
28 }
29 opts.SetDefaults()
30 b, err := index.NewBuilder(opts)
31 if err != nil {
32 t.Fatalf("NewBuilder: %v", err)
33 }
34 if err := b.AddFile("F", []byte(strings.Repeat("abc", 100))); err != nil {
35 t.Fatalf("AddFile: %v", err)
36 }
37 if err := b.Finish(); err != nil {
38 t.Errorf("Finish: %v", err)
39 }
40 repoFns = append(repoFns, opts.FindAllShards()...)
41 }
42
43 // update meta on repo3 then test it changed
44 opts := &index.Options{
45 IndexDir: dir,
46 RepositoryDescription: zoekt.Repository{
47 Name: "repo3",
48 RawConfig: map[string]string{
49 "public": "0",
50 },
51 },
52 }
53 opts.SetDefaults()
54 if err := mergeMeta(opts); err != nil {
55 t.Fatal(err)
56 }
57 repos, _, _ := index.ReadMetadataPath(repoFns[3])
58 if got, want := repos[0].RawConfig["public"], "0"; got != want {
59 t.Fatalf("failed to update metadata of repo3. Got public %q want %q", got, want)
60 }
61
62 // create a compound shard. Use a new indexdir to avoid the need to cleanup
63 // old shards.
64 dir = t.TempDir()
65 tmpFn, dstFn, err := merge(t, dir, repoFns)
66 if err != nil {
67 t.Fatal(err)
68 }
69 if err := os.Rename(tmpFn, dstFn); err != nil {
70 t.Fatal(err)
71 }
72
73 readPublic := func() []string {
74 var public []string
75 repos, _, _ := index.ReadMetadataPath(dstFn)
76 for _, r := range repos {
77 public = append(public, r.RawConfig["public"])
78 }
79 return public
80 }
81
82 if d := cmp.Diff([]string{"1", "1", "1", "0"}, readPublic()); d != "" {
83 t.Fatalf("wanted only repo3 to be marked private:\n%s", d)
84 }
85
86 // Update a repo1 in compound shard to be private
87 opts = &index.Options{
88 IndexDir: dir,
89 RepositoryDescription: zoekt.Repository{
90 Name: "repo1",
91 RawConfig: map[string]string{
92 "public": "0",
93 },
94 },
95 }
96 opts.SetDefaults()
97 if err := mergeMeta(opts); err != nil {
98 t.Fatal(err)
99 }
100 if d := cmp.Diff([]string{"1", "0", "1", "0"}, readPublic()); d != "" {
101 t.Fatalf("wanted only repo1 to be marked private:\n%s", d)
102 }
103}
104
105func merge(t *testing.T, dstDir string, names []string) (string, string, error) {
106 t.Helper()
107
108 var files []index.IndexFile
109 for _, fn := range names {
110 f, err := os.Open(fn)
111 if err != nil {
112 return "", "", err
113 }
114 defer f.Close()
115
116 indexFile, err := index.NewIndexFile(f)
117 if err != nil {
118 return "", "", err
119 }
120 defer indexFile.Close()
121
122 files = append(files, indexFile)
123 }
124
125 return index.Merge(dstDir, files...)
126}