e2e/symbols_test.go

e6cd75b5f28bacf51620bb531320c30fd4e66bfd
gitbay/e2e/symbols_test.go history · blame · raw

66 lines · 2332 bytes

1 symbol in this file
 1package e2e
 2
 3import (
 4	"encoding/json"
 5	"os"
 6	"path/filepath"
 7	"strings"
 8	"testing"
 9)
10
11// A push to the default branch is indexed in the background, and
12// repo symbols then finds a Go function where it is defined (#293).
13func TestSymbolsIndexAfterPush(t *testing.T) {
14	t.Setenv("GITBAY_SYMBOLS_TICK", "200ms")
15	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
16	aliceKey := inst.newKey(t, "alice")
17	bobKey := inst.newKey(t, "bob")
18	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
19	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
20	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app", "--private"); code != 0 {
21		t.Fatalf("repo create: %s", errOut)
22	}
23
24	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "symbols", "alice/app", "Serve"); code != 3 ||
25		!strings.Contains(errOut, "no symbol index") {
26		t.Fatalf("before any push: exit %d: %s", code, errOut)
27	}
28
29	work := t.TempDir()
30	env := inst.gitEnv(aliceKey)
31	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
32	dir := filepath.Join(work, "w")
33	os.WriteFile(filepath.Join(dir, "server.go"), []byte("package app\n\n// Serve runs.\nfunc Serve() {}\n"), 0o644)
34	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
35	mustGit(t, dir, env, "add", ".")
36	mustGit(t, dir, env, "commit", "-q", "-m", "base")
37	mustGit(t, dir, env, "push", "-q", "origin", "main")
38
39	var out string
40	waitFor(t, "symbol index", func() bool {
41		var code int
42		out, _, code = inst.ssh(t, aliceKey, "", "repo", "symbols", "alice/app", "Serve", "--json")
43		return code == 0
44	})
45	var res struct {
46		Data []struct {
47			Name string `json:"name"`
48			Kind string `json:"kind"`
49			Path string `json:"path"`
50			Line int    `json:"line"`
51		} `json:"data"`
52	}
53	if err := json.Unmarshal([]byte(out), &res); err != nil {
54		t.Fatalf("%v: %s", err, out)
55	}
56	if len(res.Data) != 1 || res.Data[0].Name != "Serve" || res.Data[0].Kind != "function" ||
57		res.Data[0].Path != "server.go" || res.Data[0].Line != 4 {
58		t.Fatalf("repo symbols: %s", out)
59	}
60
61	// The index follows read access: to bob the repository does not exist.
62	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "symbols", "alice/app", "Serve"); code != 3 ||
63		!strings.Contains(errOut, "not found") {
64		t.Fatalf("outsider: exit %d: %s", code, errOut)
65	}
66}