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