internal/hookd/symbols_test.go

v1.41.0
gitbay/internal/hookd/symbols_test.go history · blame · raw

63 lines · 2076 bytes

 1package hookd
 2
 3import (
 4	"context"
 5	"os"
 6	"path/filepath"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/control"
10	"gitbay.org/gitbay/internal/policy"
11	"gitbay.org/gitbay/internal/store"
12	"gitbay.org/gitbay/internal/symbols"
13)
14
15// A push to the default branch asks for a symbol index and builds none
16// itself; a push to another branch asks for nothing. The worker builds
17// the index afterwards.
18func TestPostReceiveRequestsSymbolIndex(t *testing.T) {
19	f := newShapeFixture(t)
20	requested := func() bool {
21		t.Helper()
22		reqs, err := f.st.SymbolRequests()
23		if err != nil {
24			t.Fatal(err)
25		}
26		return len(reqs) == 1 && reqs[0].RepoID == f.repo.ID && !reqs[0].Force
27	}
28
29	f.git(f.src, "checkout", "-q", "-b", "side")
30	side := f.appCommit("side")
31	f.sync()
32	f.srv.postReceive(Request{RepoID: f.repo.ID, UserID: f.uid, Scope: "full", Updates: []policy.RefUpdate{
33		{Ref: "refs/heads/side", Old: zeroSHA40, New: side}}})
34	if requested() {
35		t.Fatal("a push to a side branch asked for a symbol index")
36	}
37
38	f.git(f.src, "checkout", "-q", "main")
39	os.WriteFile(filepath.Join(f.src, "main.go"), []byte("package main\n\nfunc Serve() {}\n"), 0o644)
40	f.git(f.src, "add", ".")
41	f.git(f.src, "commit", "-q", "-m", "go")
42	head := f.sha("HEAD")
43	f.sync()
44	f.srv.postReceive(Request{RepoID: f.repo.ID, UserID: f.uid, Scope: "full", Updates: []policy.RefUpdate{
45		{Ref: "refs/heads/main", Old: f.base, New: head}}})
46	if !requested() {
47		t.Fatal("a push to the default branch did not ask for a symbol index")
48	}
49	if _, err := f.st.SymbolIndexFor(f.repo.ID); err != store.ErrNotFound {
50		t.Fatalf("the push built an index itself: %v", err)
51	}
52
53	w := symbols.New(f.st, func(owner, name string) string { return control.RepoDir(f.root, owner, name) })
54	w.Sweep(context.Background())
55	x, err := f.st.SymbolIndexFor(f.repo.ID)
56	if err != nil || x.Commit != head || x.State != "ok" {
57		t.Fatalf("index after the sweep = %+v, %v", x, err)
58	}
59	rows, _ := f.st.SearchSymbols(x.ID, "Serve", "", 0, 0)
60	if len(rows) != 1 || rows[0].Path != "main.go" || rows[0].Line != 3 {
61		t.Fatalf("Serve = %+v", rows)
62	}
63}