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