e2e/compare_test.go

v1.39.0
gitbay/e2e/compare_test.go history · blame · raw

53 lines · 2246 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// The compare view shows what a branch adds on top of another from their
11// merge base, and repo diff is the same range over ssh (#118).
12func TestCompareView(t *testing.T) {
13	t.Parallel()
14	inst := startInstance(t)
15	aliceKey := inst.newKey(t, "alice")
16	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
17	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
18		t.Fatalf("repo create: %s", errOut)
19	}
20	work := t.TempDir()
21	env := inst.gitEnv(aliceKey)
22	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
23	dir := filepath.Join(work, "w")
24	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("one\n"), 0o644)
25	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
26	mustGit(t, dir, env, "add", ".")
27	mustGit(t, dir, env, "commit", "-q", "-m", "base")
28	mustGit(t, dir, env, "push", "-q", "origin", "main")
29	mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
30	os.WriteFile(filepath.Join(dir, "f.txt"), []byte("one\ntwo\n"), 0o644)
31	mustGit(t, dir, env, "add", ".")
32	mustGit(t, dir, env, "commit", "-q", "-m", "add two")
33	mustGit(t, dir, env, "push", "-q", "origin", "feat")
34	// main moves on: a compare from the merge base must not show this
35	// as a removal on feat.
36	mustGit(t, dir, env, "checkout", "-q", "main")
37	os.WriteFile(filepath.Join(dir, "g.txt"), []byte("g\n"), 0o644)
38	mustGit(t, dir, env, "add", ".")
39	mustGit(t, dir, env, "commit", "-q", "-m", "main moves")
40	mustGit(t, dir, env, "push", "-q", "origin", "main")
41
42	status, body := inst.get(t, "/alice/app/compare/main...feat")
43	if status != 200 || !strings.Contains(body, "add two") || !strings.Contains(body, `<tr class="add" id="f0-n2">`) || !strings.Contains(body, ">two</td>") || strings.Contains(body, "g.txt") {
44		t.Fatalf("compare page: %d\n%s", status, body)
45	}
46	if status, _ := inst.get(t, "/alice/app/compare/main...nope"); status != 404 {
47		t.Fatalf("compare with a missing ref: %d", status)
48	}
49	out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "diff", "alice/app", "main", "feat")
50	if code != 0 || !strings.Contains(out, "+two") || strings.Contains(out, "g.txt") {
51		t.Fatalf("repo diff: exit %d\n%s%s", code, out, errOut)
52	}
53}