e2e/commitrefs_test.go
88 lines · 3797 bytes
1package e2e
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8)
9
10func TestCommitMessageIssueActions(t *testing.T) {
11 inst := startInstance(t)
12 aliceKey := inst.newKey(t, "alice")
13 inst.admin(t, "admin", "user", "create", "alice",
14 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
15
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, "a.txt"), []byte("a\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 for _, title := range []string{"'one'", "'two'", "'three'"} {
29 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", title); code != 0 {
30 t.Fatal("issue create failed")
31 }
32 }
33
34 // A closing keyword on the default branch closes the issue with a
35 // comment; a bare reference (and a nonexistent #99) only comments.
36 os.WriteFile(filepath.Join(dir, "b.txt"), []byte("b\n"), 0o644)
37 mustGit(t, dir, env, "add", ".")
38 mustGit(t, dir, env, "commit", "-q", "-m", "repair the widget\n\nFixes #1. Related to #2 but not #99.")
39 mustGit(t, dir, env, "push", "-q", "origin", "main")
40
41 out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json")
42 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
43 t.Fatalf("issue 1 not closed by commit: %s", out)
44 }
45 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
46 if !strings.Contains(out, `"state":"open"`) || !strings.Contains(out, "referenced in commit") ||
47 !strings.Contains(out, "repair the widget") {
48 t.Fatalf("issue 2 not referenced: %s", out)
49 }
50
51 // Commits on a feature branch do nothing until they land on the
52 // default branch via a merge — then the merge path acts exactly once.
53 mustGit(t, dir, env, "checkout", "-q", "-b", "feat")
54 os.WriteFile(filepath.Join(dir, "c.txt"), []byte("c\n"), 0o644)
55 mustGit(t, dir, env, "add", ".")
56 mustGit(t, dir, env, "commit", "-q", "-m", "finish the gadget\n\nCloses #3")
57 mustGit(t, dir, env, "push", "-q", "origin", "feat")
58 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
59 if !strings.Contains(out, `"state":"open"`) {
60 t.Fatalf("branch push acted early: %s", out)
61 }
62 if _, _, code := inst.ssh(t, aliceKey, "", "mr", "create", "alice/app",
63 "--source", "feat", "--target", "main", "--title", "'gadget'"); code != 0 {
64 t.Fatal("mr create failed")
65 }
66 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1"); code != 0 {
67 t.Fatalf("merge: %s", errOut)
68 }
69 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "3", "--json")
70 if !strings.Contains(out, `"state":"closed"`) || !strings.Contains(out, "closed by commit") {
71 t.Fatalf("merge did not close issue 3: %s", out)
72 }
73 if strings.Count(out, "closed by commit") != 1 {
74 t.Fatalf("duplicate close comments: %s", out)
75 }
76
77 // Pushing more commits does not re-act on already-processed shas.
78 os.WriteFile(filepath.Join(dir, "d.txt"), []byte("d\n"), 0o644)
79 mustGit(t, dir, env, "checkout", "-q", "main")
80 mustGit(t, dir, env, "pull", "-q", "origin", "main")
81 mustGit(t, dir, env, "add", ".")
82 mustGit(t, dir, env, "commit", "-q", "-m", "unrelated")
83 mustGit(t, dir, env, "push", "-q", "origin", "main")
84 out, _, _ = inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "2", "--json")
85 if strings.Count(out, "referenced in commit") != 1 {
86 t.Fatalf("reference duplicated: %s", out)
87 }
88}