e2e/cli_test.go
228 lines · 8240 bytes
1package e2e
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13func buildGitbayCLI(t *testing.T) string {
14 t.Helper()
15 bin := filepath.Join(t.TempDir(), "gitbay")
16 cmd := exec.Command("go", "build", "-o", bin, "gitbay.org/gitbay/cmd/gitbay")
17 cmd.Dir = ".."
18 if out, err := cmd.CombinedOutput(); err != nil {
19 t.Fatalf("build gitbay: %v\n%s", err, out)
20 }
21 return bin
22}
23
24// cli runs the forge binary with an isolated config home.
25type cli struct {
26 bin string
27 configDir string
28 inst *instance
29 key string
30}
31
32func (c *cli) run(t *testing.T, dir, stdin string, args ...string) (string, string, int) {
33 t.Helper()
34 cmd := exec.Command(c.bin, args...)
35 cmd.Dir = dir
36 cmd.Env = append(os.Environ(),
37 "XDG_CONFIG_HOME="+c.configDir,
38 "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null",
39 "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.test",
40 "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.test",
41 "EDITOR=", // no editor in tests: bodies come from flags
42 )
43 if stdin != "" {
44 cmd.Stdin = strings.NewReader(stdin)
45 }
46 var out, errOut strings.Builder
47 cmd.Stdout = &out
48 cmd.Stderr = &errOut
49 err := cmd.Run()
50 code := 0
51 if ee, ok := err.(*exec.ExitError); ok {
52 code = ee.ExitCode()
53 } else if err != nil {
54 t.Fatalf("gitbay %v: %v", args, err)
55 }
56 return out.String(), errOut.String(), code
57}
58
59func (c *cli) must(t *testing.T, dir, stdin string, args ...string) string {
60 t.Helper()
61 out, errOut, code := c.run(t, dir, stdin, args...)
62 if code != 0 {
63 t.Fatalf("gitbay %v: exit %d\nstdout: %s\nstderr: %s", args, code, out, errOut)
64 }
65 return out
66}
67
68func TestCLI(t *testing.T) {
69 inst := startInstance(t)
70 aliceKey := inst.newKey(t, "alice")
71 inst.admin(t, "admin", "user", "create", "alice",
72 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
73
74 c := &cli{
75 bin: buildGitbayCLI(t),
76 configDir: t.TempDir(),
77 inst: inst,
78 key: aliceKey,
79 }
80
81 // Configure the instance, with ssh options so the test's throwaway key
82 // and known_hosts are used.
83 c.must(t, "", "", "remote", "add", "test", "127.0.0.1",
84 "--port", fmt.Sprint(inst.port),
85 "--ssh-option", "-i", "--ssh-option", aliceKey,
86 "--ssh-option", "-oIdentitiesOnly=yes",
87 "--ssh-option", "-oStrictHostKeyChecking=no",
88 "--ssh-option", "-oUserKnownHostsFile="+filepath.Join(inst.sshDir, "kh"),
89 "--ssh-option", "-oBatchMode=yes",
90 "--default")
91 if out := c.must(t, "", "", "remote", "list"); !strings.Contains(out, "test\tgit@127.0.0.1") || !strings.Contains(out, "(default)") {
92 t.Fatalf("remote list: %s", out)
93 }
94
95 // whoami through the CLI, JSON passthrough intact.
96 out := c.must(t, "", "", "auth", "whoami", "--json")
97 var env struct {
98 ProtocolVersion int `json:"protocol_version"`
99 Data struct {
100 Username string `json:"username"`
101 } `json:"data"`
102 }
103 if err := json.Unmarshal([]byte(out), &env); err != nil || env.Data.Username != "alice" || env.ProtocolVersion != 1 {
104 t.Fatalf("whoami via CLI: %v %s", err, out)
105 }
106
107 // Repo create + clone through the CLI.
108 c.must(t, "", "", "repo", "create", "alice/proj")
109
110 // Secret values travel on stdin through the CLI (piped, no flag).
111 c.must(t, "", "hunter2\n", "repo", "secret", "set", "alice/proj", "TOKEN")
112 if out := c.must(t, "", "", "repo", "secret", "list", "alice/proj"); !strings.Contains(out, "TOKEN") || strings.Contains(out, "hunter2") {
113 t.Fatalf("secret list via CLI: %s", out)
114 }
115
116 work := t.TempDir()
117 c.must(t, work, "", "repo", "clone", "alice/proj")
118 dir := filepath.Join(work, "proj")
119 if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil {
120 t.Fatal("clone did not produce a repo")
121 }
122
123 // Push some content (plain git, using the clone's remote).
124 cliGitEnv := inst.gitEnv(aliceKey)
125 os.WriteFile(filepath.Join(dir, "README"), []byte("hi\n"), 0o644)
126 mustGit(t, dir, cliGitEnv, "checkout", "-q", "-b", "main")
127 mustGit(t, dir, cliGitEnv, "add", ".")
128 mustGit(t, dir, cliGitEnv, "commit", "-q", "-m", "init")
129 mustGit(t, dir, cliGitEnv, "push", "-q", "origin", "main")
130
131 // Inside the clone, the repo argument is inferred from origin.
132 c.must(t, dir, "", "issue", "create", "--title", "inferred repo works", "--body", "body")
133 out = c.must(t, dir, "", "issue", "list")
134 if !strings.Contains(out, "inferred repo works") {
135 t.Fatalf("issue list in clone: %s", out)
136 }
137 // Explicit owner/name still works from anywhere.
138 out = c.must(t, "", "", "issue", "show", "alice/proj", "1")
139 if !strings.Contains(out, "inferred repo works") {
140 t.Fatalf("issue show explicit: %s", out)
141 }
142 // Outside a clone with no explicit repo: usage error, not a hang.
143 _, errOut, code := c.run(t, "", "", "issue", "list")
144 if code != 2 || !strings.Contains(errOut, "none inferable") {
145 t.Fatalf("bare issue list outside clone: exit %d, %s", code, errOut)
146 }
147
148 // Exit codes pass through: missing issue is 3.
149 if _, _, code := c.run(t, dir, "", "issue", "show", "99"); code != 3 {
150 t.Fatalf("missing issue via CLI: exit %d, want 3", code)
151 }
152
153 // MR flow: branch, push, create (inferred), checkout, merge.
154 mustGit(t, dir, cliGitEnv, "checkout", "-q", "-b", "feature")
155 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("feature\n"), 0o644)
156 mustGit(t, dir, cliGitEnv, "add", ".")
157 mustGit(t, dir, cliGitEnv, "commit", "-q", "-m", "feature work")
158 mustGit(t, dir, cliGitEnv, "push", "-q", "origin", "feature")
159 c.must(t, dir, "", "mr", "create", "--source", "feature", "--target", "main", "--title", "via cli")
160
161 // mr checkout uses the clone's own git; the MR ref comes from origin.
162 mustGit(t, dir, cliGitEnv, "checkout", "-q", "main")
163 cmd := exec.Command(c.bin, "mr", "checkout", "1")
164 cmd.Dir = dir
165 cmd.Env = append(cliGitEnv, "XDG_CONFIG_HOME="+c.configDir)
166 if out, err := cmd.CombinedOutput(); err != nil {
167 t.Fatalf("mr checkout: %v\n%s", err, out)
168 }
169 branch := strings.TrimSpace(mustGit(t, dir, cliGitEnv, "rev-parse", "--abbrev-ref", "HEAD"))
170 if branch != "mr/1" {
171 t.Fatalf("mr checkout branch = %s", branch)
172 }
173 if _, err := os.Stat(filepath.Join(dir, "f.txt")); err != nil {
174 t.Fatal("mr checkout content missing")
175 }
176
177 c.must(t, dir, "", "mr", "merge", "1")
178 out = c.must(t, dir, "", "mr", "show", "1")
179 if !strings.Contains(out, "merged") {
180 t.Fatalf("mr not merged: %s", out)
181 }
182
183 // keys add reads the public key from CLI stdin.
184 secondKey := inst.newKey(t, "alice2")
185 pub, _ := os.ReadFile(secondKey + ".pub")
186 c.must(t, "", string(pub), "auth", "keys", "add", "--scope", "git")
187 if out = c.must(t, "", "", "auth", "keys", "list"); len(strings.Split(strings.TrimSpace(out), "\n")) != 2 {
188 t.Fatalf("keys list: %s", out)
189 }
190
191 // forge init: new local project, repo created server-side, origin set.
192 proj := filepath.Join(t.TempDir(), "newthing")
193 os.MkdirAll(proj, 0o755)
194 c.must(t, proj, "", "init", "--private")
195 originOut := mustGit(t, proj, cliGitEnv, "remote", "get-url", "origin")
196 if !strings.Contains(originOut, "/alice/newthing.git") {
197 t.Fatalf("init origin: %s", originOut)
198 }
199 if out = c.must(t, "", "", "repo", "show", "alice/newthing"); !strings.Contains(out, "private") {
200 t.Fatalf("init-created repo: %s", out)
201 }
202
203 // A clone whose origin points at a FOREIGN host must not hijack the
204 // command: the configured default instance is used, and repo inference
205 // is dropped rather than guessed across hosts.
206 foreign := filepath.Join(t.TempDir(), "f")
207 os.MkdirAll(foreign, 0o755)
208 mustGit(t, foreign, cliGitEnv, "init", "-q")
209 mustGit(t, foreign, cliGitEnv, "remote", "add", "origin", "git@github.example:someone/thing.git")
210 out = c.must(t, foreign, "", "auth", "whoami")
211 if strings.TrimSpace(out) != "alice" {
212 t.Fatalf("foreign-origin whoami hit the wrong host: %q", out)
213 }
214 _, errOut2, code2 := c.run(t, foreign, "", "issue", "list")
215 if code2 != 2 || !strings.Contains(errOut2, "none inferable") {
216 t.Fatalf("foreign-origin repo inference: exit %d, %s", code2, errOut2)
217 }
218
219 // Man pages and completions generate.
220 manDir := t.TempDir()
221 c.must(t, "", "", "man", "--dir", manDir)
222 if entries, _ := os.ReadDir(manDir); len(entries) < 10 {
223 t.Fatalf("man pages: only %d generated", len(entries))
224 }
225 if out = c.must(t, "", "", "completion", "zsh"); !strings.Contains(out, "compdef") {
226 t.Fatal("zsh completion missing")
227 }
228}