| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // initRepo creates a real git repo at dir with the given origin URL. |
| 13 | func initRepo(t *testing.T, dir, origin string) string { |
| 14 | t.Helper() |
| 15 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 16 | t.Fatalf("mkdir %s: %v", dir, err) |
| 17 | } |
| 18 | for _, args := range [][]string{ |
| 19 | {"init", "-q", "-b", "main"}, |
| 20 | {"remote", "add", "origin", origin}, |
| 21 | } { |
| 22 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 23 | if out, err := cmd.CombinedOutput(); err != nil { |
| 24 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 25 | } |
| 26 | } |
| 27 | // macOS resolves TMPDIR through a symlink; git reports the real path. |
| 28 | real, err := filepath.EvalSymlinks(dir) |
| 29 | if err != nil { |
| 30 | t.Fatalf("resolving %s: %v", dir, err) |
| 31 | } |
| 32 | return real |
| 33 | } |
| 34 | |
| 35 | func TestExistingRepoDirUsesCurrentCheckout(t *testing.T) { |
| 36 | dir := initRepo(t, filepath.Join(t.TempDir(), "widget"), "https://github.com/acme/widget.git") |
| 37 | writeRegistry(t, withTempHome(t), nil) |
| 38 | t.Chdir(dir) |
| 39 | |
| 40 | // No reference at all: whatever repo we are standing in. |
| 41 | if got := existingRepoDir(""); got != dir { |
| 42 | t.Errorf("existingRepoDir(\"\") = %q, want %q", got, dir) |
| 43 | } |
| 44 | // A reference the current checkout demonstrably satisfies. |
| 45 | if got := existingRepoDir("acme/widget"); got != dir { |
| 46 | t.Errorf("existingRepoDir(\"acme/widget\") = %q, want %q", got, dir) |
| 47 | } |
| 48 | // A different repo must not silently resolve to the current checkout. |
| 49 | if got := existingRepoDir("acme/gadget"); got != "" { |
| 50 | t.Errorf("existingRepoDir(\"acme/gadget\") = %q, want \"\"", got) |
| 51 | } |
| 52 | // Nor may a bare name, which says nothing about this checkout's origin. |
| 53 | if got := existingRepoDir("widget"); got != "" { |
| 54 | t.Errorf("existingRepoDir(\"widget\") = %q, want \"\"", got) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestExistingRepoDirFindsRegisteredProject(t *testing.T) { |
| 59 | root := t.TempDir() |
| 60 | widget := initRepo(t, filepath.Join(root, "widget"), "git@github.com:acme/widget.git") |
| 61 | local := initRepo(t, filepath.Join(root, "gizmo"), filepath.Join(root, "gizmo-origin.git")) |
| 62 | writeRegistry(t, withTempHome(t), map[string]string{"widget": widget, "gizmo": local}) |
| 63 | // Outside any checkout, so only the registry can answer. |
| 64 | t.Chdir(root) |
| 65 | |
| 66 | // Matched by origin slug, in every reference form. |
| 67 | for _, ref := range []string{"acme/widget", "https://github.com/acme/widget", "git@github.com:acme/widget.git"} { |
| 68 | if got := existingRepoDir(ref); got != widget { |
| 69 | t.Errorf("existingRepoDir(%q) = %q, want %q", ref, got, widget) |
| 70 | } |
| 71 | } |
| 72 | // A project whose origin is not a GitHub URL can only be matched by name. |
| 73 | if got := existingRepoDir("gizmo"); got != local { |
| 74 | t.Errorf("existingRepoDir(\"gizmo\") = %q, want %q", got, local) |
| 75 | } |
| 76 | // A GitHub repo nobody has checked out stays unresolved (no cloning here). |
| 77 | if got := existingRepoDir("acme/unknown"); got != "" { |
| 78 | t.Errorf("existingRepoDir(\"acme/unknown\") = %q, want \"\"", got) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // A project registered under a name that collides with a different GitHub repo |
| 83 | // must not be handed back for that repo — the origin disagrees. |
| 84 | func TestExistingRepoDirIgnoresNameCollisionWithOtherOrigin(t *testing.T) { |
| 85 | root := t.TempDir() |
| 86 | widget := initRepo(t, filepath.Join(root, "widget"), "https://github.com/acme/widget.git") |
| 87 | writeRegistry(t, withTempHome(t), map[string]string{"widget": widget}) |
| 88 | t.Chdir(root) |
| 89 | |
| 90 | if got := existingRepoDir("other/widget"); got != "" { |
| 91 | t.Errorf("existingRepoDir(\"other/widget\") = %q, want \"\"", got) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestResolveRepoDirWithoutTargetOutsideRepo(t *testing.T) { |
| 96 | writeRegistry(t, withTempHome(t), nil) |
| 97 | t.Chdir(t.TempDir()) |
| 98 | |
| 99 | _, err := resolveRepoDirUncached(context.Background(), "") |
| 100 | if !errors.Is(err, errNoTargetRepo) { |
| 101 | t.Errorf("error = %v, want errNoTargetRepo", err) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestRepoSlug(t *testing.T) { |
| 106 | tests := []struct { |
| 107 | ref string |
| 108 | want string |
| 109 | }{ |
| 110 | {"acme/widget", "acme/widget"}, |
| 111 | {"https://github.com/acme/widget.git", "acme/widget"}, |
| 112 | {"git@github.com:acme/widget", "acme/widget"}, |
| 113 | {"widget", ""}, // bare project name, no owner |
| 114 | {"", ""}, // nothing to resolve |
| 115 | {"/srv/git/widget.git", ""}, // a local path, not a GitHub repo |
| 116 | } |
| 117 | for _, tt := range tests { |
| 118 | if got := repoSlug(tt.ref); got != tt.want { |
| 119 | t.Errorf("repoSlug(%q) = %q, want %q", tt.ref, got, tt.want) |
| 120 | } |
| 121 | } |
| 122 | } |