e2e/reporename_test.go

c72da83f1a127ce5d5310bddea7240344b0dc11f
gitbay/e2e/reporename_test.go history · blame · raw

75 lines · 3166 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// repo rename moves the directory and the row together; what hangs off
11// the repository by id (issues here) follows it (#190).
12func TestRepoRename(t *testing.T) {
13	inst := startInstance(t)
14	aliceKey := inst.newKey(t, "alice")
15	bobKey := inst.newKey(t, "bob")
16	inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
17	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
18	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/box"); code != 0 {
19		t.Fatalf("repo create: %s", errOut)
20	}
21	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "create", "alice/box", "--title", "'keep'"); code != 0 {
22		t.Fatalf("issue create: %s", errOut)
23	}
24	env := inst.gitEnv(aliceKey)
25	work := t.TempDir()
26	mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/box"), "box")
27	dir := filepath.Join(work, "box")
28	if err := os.WriteFile(filepath.Join(dir, "README"), []byte("hi\n"), 0o644); err != nil {
29		t.Fatal(err)
30	}
31	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
32	mustGit(t, dir, env, "add", "README")
33	mustGit(t, dir, env, "commit", "-q", "-m", "one")
34	mustGit(t, dir, env, "push", "-q", "origin", "main")
35
36	if _, errOut, code := inst.ssh(t, bobKey, "", "repo", "rename", "alice/box", "crate"); code != 4 {
37		t.Fatalf("non-admin rename: %d %s", code, errOut)
38	}
39	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "rename", "alice/box", "box"); code != 2 {
40		t.Fatalf("rename to the same name: %d %s", code, errOut)
41	}
42	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "rename", "alice/box", "'bad name'"); code == 0 {
43		t.Fatal("invalid name accepted")
44	}
45	out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "rename", "alice/box", "crate", "--json")
46	if code != 0 || !strings.Contains(out, `"repo":"alice/crate"`) {
47		t.Fatalf("rename: %d %s %s", code, out, errOut)
48	}
49
50	clone := filepath.Join(t.TempDir(), "crate")
51	mustGit(t, t.TempDir(), env, "clone", "-q", inst.sshURL("alice/crate"), clone)
52	if _, err := os.Stat(filepath.Join(clone, "README")); err != nil {
53		t.Fatalf("renamed repository lost its content: %v", err)
54	}
55	if out, code := gitRun(t, t.TempDir(), env, "clone", "-q", inst.sshURL("alice/box")); code == 0 {
56		t.Fatalf("old path still clones:\n%s", out)
57	}
58	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "alice/box"); code != 3 {
59		t.Fatalf("old path still shows: %d", code)
60	}
61	if _, errOut, code := inst.ssh(t, aliceKey, "", "issue", "show", "alice/crate", "1"); code != 0 {
62		t.Fatalf("issue did not follow the rename: %s", errOut)
63	}
64
65	// A collision with an existing repository is refused and nothing moves.
66	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/box"); code != 0 {
67		t.Fatalf("recreate box: %s", errOut)
68	}
69	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "rename", "alice/crate", "box"); code == 0 || !strings.Contains(errOut, "already") {
70		t.Fatalf("collision rename: %d %s", code, errOut)
71	}
72	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "show", "alice/crate"); code != 0 {
73		t.Fatal("refused rename moved the repository anyway")
74	}
75}