internal/control/definitions_test.go
112 lines · 4162 bytes
5 symbols in this file
1package control
2
3import (
4 "bytes"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "gitbay.org/gitbay/internal/protocol"
12 "gitbay.org/gitbay/internal/store"
13)
14
15// definitionsFixture is a repository whose default branch holds use.go,
16// which calls names defined in lib.go, and a symbol index of that tree.
17func definitionsFixture(t *testing.T) (*store.Store, store.Repo, store.User, string) {
18 t.Helper()
19 st, repo, uid := newQueueTestRepo(t)
20 root := t.TempDir()
21 git := gitRunner(t)
22 src := filepath.Join(root, "src")
23 git(root, "init", "-q", "-b", "main", "src")
24 os.WriteFile(filepath.Join(src, "lib.go"), []byte("package app\n\nfunc Parse() {}\nfunc Run() {}\nfunc Run2() {}\n"), 0o644)
25 os.WriteFile(filepath.Join(src, "use.go"), []byte("package app\n\nfunc main() {\n\tParse()\n\tRun()\n\tunknown()\n}\n"), 0o644)
26 git(src, "add", ".")
27 git(src, "commit", "-q", "-m", "base")
28 dir := RepoDir(root, repo.OwnerName, repo.Name)
29 os.MkdirAll(filepath.Dir(dir), 0o755)
30 git(root, "clone", "-q", "--bare", src, dir)
31 head := strings.TrimSpace(git(dir, "rev-parse", "main"))
32 tree := strings.TrimSpace(git(dir, "rev-parse", "main^{tree}"))
33 rows := []store.SymbolRow{
34 {Name: "Parse", Key: "Parse", Kind: "function", Path: "lib.go", Line: 3},
35 {Name: "Run", Key: "Run", Kind: "function", Path: "lib.go", Line: 4},
36 {Name: "Server.Run", Key: "Run", Kind: "method", Path: "srv.go", Line: 9},
37 {Name: "main", Key: "main", Kind: "function", Path: "use.go", Line: 3},
38 }
39 if _, err := st.ReplaceSymbolIndex(store.SymbolIndex{RepoID: repo.ID, Commit: head, Tree: tree, State: "ok"}, rows); err != nil {
40 t.Fatal(err)
41 }
42 return st, repo, store.User{ID: uid, Username: "alice"}, root
43}
44
45func runDefinitions(t *testing.T, st *store.Store, u store.User, root string, argv ...string) (int, string) {
46 t.Helper()
47 var out, errOut bytes.Buffer
48 c := &Ctx{User: u, Scope: "full", Store: st, Stdout: &out, Stderr: &errOut, JSON: true}
49 c.Cfg.Server.Root = root
50 c.Cfg.Limits.MaxBlobBytes = 1 << 20
51 code := Dispatch(c, argv)
52 return code, out.String() + errOut.String()
53}
54
55// repo definitions says where each name a file uses is defined: the
56// first definition and how many there are, so a client links a name
57// defined once to its line and one defined more often to a search, the
58// way the blob page does. Names the index does not hold are left out.
59func TestRepoDefinitionsListsTheNamesAFileUses(t *testing.T) {
60 st, repo, alice, root := definitionsFixture(t)
61
62 code, out := runDefinitions(t, st, alice, root, "repo", "definitions", repo.Path(), "use.go")
63 if code != protocol.ExitOK {
64 t.Fatalf("exit %d: %s", code, out)
65 }
66 var env struct {
67 Data []definitionOut `json:"data"`
68 }
69 if err := json.Unmarshal([]byte(out), &env); err != nil {
70 t.Fatal(err)
71 }
72 want := []definitionOut{
73 {Name: "Parse", Count: 1, Path: "lib.go", Line: 3},
74 {Name: "Run", Count: 2, Path: "lib.go", Line: 4},
75 {Name: "main", Count: 1, Path: "use.go", Line: 3},
76 }
77 if len(env.Data) != len(want) {
78 t.Fatalf("got %+v", env.Data)
79 }
80 for i := range want {
81 if env.Data[i] != want[i] {
82 t.Errorf("row %d: got %+v, want %+v", i, env.Data[i], want[i])
83 }
84 }
85}
86
87func TestRepoDefinitionsRefusals(t *testing.T) {
88 st, repo, alice, root := definitionsFixture(t)
89 for _, tc := range []struct {
90 argv []string
91 code int
92 want string
93 }{
94 {[]string{"repo", "definitions", repo.Path(), "nope.go"}, protocol.ExitNotFound, "no such file"},
95 {[]string{"repo", "definitions", repo.Path(), "../x"}, protocol.ExitUsage, "inside the repository"},
96 {[]string{"repo", "definitions", repo.Path(), "use.go", "--ref", "nope"}, protocol.ExitNotFound, "not at the indexed tree"},
97 } {
98 code, out := runDefinitions(t, st, alice, root, tc.argv...)
99 if code != tc.code || !strings.Contains(out, tc.want) {
100 t.Errorf("%v: exit %d, %s", tc.argv, code, out)
101 }
102 }
103}
104
105func TestRepoDefinitionsScreen(t *testing.T) {
106 st, repo, alice, root := definitionsFixture(t)
107 out := atTerminalIn(t, st, alice, root, "repo", "definitions", repo.Path(), "use.go")
108 if !strings.Contains(out, "Parse") || !strings.Contains(out, "lib.go:4") {
109 t.Fatalf("screen:\n%s", out)
110 }
111 checkLegend(t, out)
112}