e2e/compare_test.go
52 lines · 2232 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 inst := startInstance(t)
14 aliceKey := inst.newKey(t, "alice")
15 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
16 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
17 t.Fatalf("repo create: %s", errOut)
18 }
19 work := t.TempDir()
20 env := inst.gitEnv(aliceKey)
21 mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
22 dir := filepath.Join(work, "w")
23 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("one\n"), 0o644)
24 mustGit(t, dir, env, "checkout", "-q", "-b", "main")
25 mustGit(t, dir, env, "add", ".")
26 mustGit(t, dir, env, "commit", "-q", "-m", "base")
27 mustGit(t, dir, env, "push", "-q", "origin", "main")
28 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
29 os.WriteFile(filepath.Join(dir, "f.txt"), []byte("one\ntwo\n"), 0o644)
30 mustGit(t, dir, env, "add", ".")
31 mustGit(t, dir, env, "commit", "-q", "-m", "add two")
32 mustGit(t, dir, env, "push", "-q", "origin", "feat")
33 // main moves on: a compare from the merge base must not show this
34 // as a removal on feat.
35 mustGit(t, dir, env, "checkout", "-q", "main")
36 os.WriteFile(filepath.Join(dir, "g.txt"), []byte("g\n"), 0o644)
37 mustGit(t, dir, env, "add", ".")
38 mustGit(t, dir, env, "commit", "-q", "-m", "main moves")
39 mustGit(t, dir, env, "push", "-q", "origin", "main")
40
41 status, body := inst.get(t, "/alice/app/compare/main...feat")
42 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") {
43 t.Fatalf("compare page: %d\n%s", status, body)
44 }
45 if status, _ := inst.get(t, "/alice/app/compare/main...nope"); status != 404 {
46 t.Fatalf("compare with a missing ref: %d", status)
47 }
48 out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "diff", "alice/app", "main", "feat")
49 if code != 0 || !strings.Contains(out, "+two") || strings.Contains(out, "g.txt") {
50 t.Fatalf("repo diff: exit %d\n%s%s", code, out, errOut)
51 }
52}