e2e/cli_test.go

3c0c20da787d91bcad19aec3b5e1b5c4ac5a4745
gitbay/e2e/cli_test.go history · blame · raw

239 lines · 8860 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	// A noun's --help is the server's reference for that noun, flags
160	// included, not cobra's flagless subcommand list (#130).
161	if out := c.must(t, dir, "", "issue", "--help"); !strings.Contains(out, "issue create <owner/name>") {
162		t.Fatalf("issue --help is not the server's reference:\n%s", out)
163	}
164
165	// Inside the clone on the branch, --source is the checked-out branch
166	// and --target the default branch; neither needs typing (#101).
167	c.must(t, dir, "", "mr", "create", "--title", "via cli")
168	if out := c.must(t, dir, "", "mr", "show", "1", "--json"); !strings.Contains(out, `"source":"feature"`) || !strings.Contains(out, `"target_ref":"main"`) {
169		t.Fatalf("mr create did not infer source and target:\n%s", out)
170	}
171
172	// mr checkout uses the clone's own git; the MR ref comes from origin.
173	mustGit(t, dir, cliGitEnv, "checkout", "-q", "main")
174	cmd := exec.Command(c.bin, "mr", "checkout", "1")
175	cmd.Dir = dir
176	cmd.Env = append(cliGitEnv, "XDG_CONFIG_HOME="+c.configDir)
177	if out, err := cmd.CombinedOutput(); err != nil {
178		t.Fatalf("mr checkout: %v\n%s", err, out)
179	}
180	branch := strings.TrimSpace(mustGit(t, dir, cliGitEnv, "rev-parse", "--abbrev-ref", "HEAD"))
181	if branch != "mr/1" {
182		t.Fatalf("mr checkout branch = %s", branch)
183	}
184	if _, err := os.Stat(filepath.Join(dir, "f.txt")); err != nil {
185		t.Fatal("mr checkout content missing")
186	}
187
188	c.must(t, dir, "", "mr", "merge", "1")
189	out = c.must(t, dir, "", "mr", "show", "1")
190	if !strings.Contains(out, "merged") {
191		t.Fatalf("mr not merged: %s", out)
192	}
193
194	// keys add reads the public key from CLI stdin.
195	secondKey := inst.newKey(t, "alice2")
196	pub, _ := os.ReadFile(secondKey + ".pub")
197	c.must(t, "", string(pub), "auth", "keys", "add", "--scope", "git")
198	if out = c.must(t, "", "", "auth", "keys", "list"); len(strings.Split(strings.TrimSpace(out), "\n")) != 2 {
199		t.Fatalf("keys list: %s", out)
200	}
201
202	// forge init: new local project, repo created server-side, origin set.
203	proj := filepath.Join(t.TempDir(), "newthing")
204	os.MkdirAll(proj, 0o755)
205	c.must(t, proj, "", "init", "--private")
206	originOut := mustGit(t, proj, cliGitEnv, "remote", "get-url", "origin")
207	if !strings.Contains(originOut, "/alice/newthing.git") {
208		t.Fatalf("init origin: %s", originOut)
209	}
210	if out = c.must(t, "", "", "repo", "show", "alice/newthing"); !strings.Contains(out, "private") {
211		t.Fatalf("init-created repo: %s", out)
212	}
213
214	// A clone whose origin points at a FOREIGN host must not hijack the
215	// command: the configured default instance is used, and repo inference
216	// is dropped rather than guessed across hosts.
217	foreign := filepath.Join(t.TempDir(), "f")
218	os.MkdirAll(foreign, 0o755)
219	mustGit(t, foreign, cliGitEnv, "init", "-q")
220	mustGit(t, foreign, cliGitEnv, "remote", "add", "origin", "git@github.example:someone/thing.git")
221	out = c.must(t, foreign, "", "auth", "whoami")
222	if strings.TrimSpace(out) != "alice" {
223		t.Fatalf("foreign-origin whoami hit the wrong host: %q", out)
224	}
225	_, errOut2, code2 := c.run(t, foreign, "", "issue", "list")
226	if code2 != 2 || !strings.Contains(errOut2, "none inferable") {
227		t.Fatalf("foreign-origin repo inference: exit %d, %s", code2, errOut2)
228	}
229
230	// Man pages and completions generate.
231	manDir := t.TempDir()
232	c.must(t, "", "", "man", "--dir", manDir)
233	if entries, _ := os.ReadDir(manDir); len(entries) < 10 {
234		t.Fatalf("man pages: only %d generated", len(entries))
235	}
236	if out = c.must(t, "", "", "completion", "zsh"); !strings.Contains(out, "compdef") {
237		t.Fatal("zsh completion missing")
238	}
239}