e2e/defaultbranch_test.go

v1.43.1
gitbay/e2e/defaultbranch_test.go history · blame · raw

95 lines · 4119 bytes

 1package e2e
 2
 3import (
 4	"net/url"
 5	"os"
 6	"path/filepath"
 7	"strings"
 8	"testing"
 9)
10
11// A first push of a branch other than main moves the unborn HEAD to it,
12// and repo settings default-branch moves it later (#189).
13func TestDefaultBranch(t *testing.T) {
14	t.Parallel()
15	inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
16	aliceKey := inst.newKey(t, "alice")
17	bobKey := inst.newKey(t, "bob")
18	inst.admin(t, "admin", "user", "create", "alice",
19		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
20	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
21	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/proj"); code != 0 {
22		t.Fatalf("repo create: %s", errOut)
23	}
24
25	env := inst.gitEnv(aliceKey)
26	dir := filepath.Join(t.TempDir(), "proj")
27	mustGit(t, t.TempDir(), env, "init", "-q", "-b", "master", dir)
28	if err := os.WriteFile(filepath.Join(dir, "README"), []byte("hello\n"), 0o644); err != nil {
29		t.Fatal(err)
30	}
31	mustGit(t, dir, env, "add", "README")
32	mustGit(t, dir, env, "commit", "-q", "-m", "one")
33	mustGit(t, dir, env, "remote", "add", "origin", inst.sshURL("alice/proj"))
34	mustGit(t, dir, env, "push", "-q", "origin", "master")
35
36	// HEAD followed the push: a clone checks master out, and the record
37	// agrees.
38	clone := filepath.Join(t.TempDir(), "c1")
39	mustGit(t, t.TempDir(), env, "clone", "-q", inst.sshURL("alice/proj"), clone)
40	if head := strings.TrimSpace(mustGit(t, clone, env, "symbolic-ref", "HEAD")); head != "refs/heads/master" {
41		t.Fatalf("clone HEAD %q", head)
42	}
43	if _, err := os.Stat(filepath.Join(clone, "README")); err != nil {
44		t.Fatalf("clone checked out nothing: %v", err)
45	}
46	out, _, _ := inst.ssh(t, aliceKey, "", "repo", "show", "alice/proj", "--json")
47	if !strings.Contains(out, `"default_branch":"master"`) {
48		t.Fatalf("repo show after first push: %s", out)
49	}
50
51	// A second branch does not move HEAD; the setting does.
52	mustGit(t, dir, env, "checkout", "-q", "-b", "dev")
53	if err := os.WriteFile(filepath.Join(dir, "DEV"), []byte("dev\n"), 0o644); err != nil {
54		t.Fatal(err)
55	}
56	mustGit(t, dir, env, "add", "DEV")
57	mustGit(t, dir, env, "commit", "-q", "-m", "two")
58	mustGit(t, dir, env, "push", "-q", "origin", "dev")
59	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "show", "alice/proj", "--json")
60	if !strings.Contains(out, `"default_branch":"master"`) {
61		t.Fatalf("second branch moved the default: %s", out)
62	}
63	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "settings", "default-branch", "alice/proj", "dev"); code != 4 {
64		t.Fatalf("non-admin set the default branch: %d %s", code, errOut)
65	}
66	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "settings", "default-branch", "alice/proj", "nope"); code != 1 || !strings.Contains(errOut, "no branch") {
67		t.Fatalf("missing branch accepted: %d %s", code, errOut)
68	}
69	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "settings", "default-branch", "alice/proj", "dev"); code != 0 {
70		t.Fatalf("default-branch: %s", errOut)
71	}
72	clone = filepath.Join(t.TempDir(), "c2")
73	mustGit(t, t.TempDir(), env, "clone", "-q", inst.sshURL("alice/proj"), clone)
74	if head := strings.TrimSpace(mustGit(t, clone, env, "symbolic-ref", "HEAD")); head != "refs/heads/dev" {
75		t.Fatalf("clone HEAD after setting %q", head)
76	}
77	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "show", "alice/proj", "--json")
78	if !strings.Contains(out, `"default_branch":"dev"`) {
79		t.Fatalf("repo show after setting: %s", out)
80	}
81
82	// The settings page carries the same control.
83	alice := inst.login(t, aliceKey)
84	set := inst.base() + "/alice/proj/settings"
85	if _, body := browserGet(t, alice, set); !strings.Contains(body, `value="dev" selected`) {
86		t.Fatalf("settings page does not show the default branch:\n%s", body)
87	}
88	if status, _ := browserPost(t, alice, set, url.Values{"field": {"default-branch"}, "default-branch": {"master"}}); status != 200 {
89		t.Fatalf("settings post: %d", status)
90	}
91	out, _, _ = inst.ssh(t, aliceKey, "", "repo", "show", "alice/proj", "--json")
92	if !strings.Contains(out, `"default_branch":"master"`) {
93		t.Fatalf("repo show after web: %s", out)
94	}
95}