fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

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