gunk-dev/the-valley

repodir_test.go

source6a7605f70901main's published tip history
6.8 KiB
1package cmd
2
3import (
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.
14func 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
36func mustExistingRepoDir(t *testing.T, ref string) string {
37 t.Helper()
38 dir, err := existingRepoDir(ref)
39 if err != nil {
40 t.Fatalf("existingRepoDir(%q): %v", ref, err)
41 }
42 return dir
43}
44
45func TestExistingRepoDirUsesCurrentCheckout(t *testing.T) {
46 dir := initRepo(t, filepath.Join(t.TempDir(), "widget"), "https://github.com/acme/widget.git")
47 writeRegistry(t, withTempHome(t), nil)
48 t.Chdir(dir)
49
50 // No reference at all: whatever repo we are standing in.
51 if got := mustExistingRepoDir(t, ""); got != dir {
52 t.Errorf("existingRepoDir(\"\") = %q, want %q", got, dir)
53 }
54 // A reference the current checkout demonstrably satisfies.
55 if got := mustExistingRepoDir(t, "acme/widget"); got != dir {
56 t.Errorf("existingRepoDir(\"acme/widget\") = %q, want %q", got, dir)
57 }
58 // A different repo must not silently resolve to the current checkout.
59 if got := mustExistingRepoDir(t, "acme/gadget"); got != "" {
60 t.Errorf("existingRepoDir(\"acme/gadget\") = %q, want \"\"", got)
61 }
62 // Nor may a bare name, which says nothing about this checkout's origin.
63 if got := mustExistingRepoDir(t, "widget"); got != "" {
64 t.Errorf("existingRepoDir(\"widget\") = %q, want \"\"", got)
65 }
66}
67
68func TestExistingRepoDirFindsRegisteredProject(t *testing.T) {
69 root := t.TempDir()
70 widget := initRepo(t, filepath.Join(root, "widget"), "git@github.com:acme/widget.git")
71 local := initRepo(t, filepath.Join(root, "gizmo"), filepath.Join(root, "gizmo-origin.git"))
72 writeRegistry(t, withTempHome(t), map[string]string{"widget": widget, "gizmo": local})
73 // Outside any checkout, so only the registry can answer.
74 t.Chdir(root)
75
76 // Matched by origin slug, in every reference form.
77 for _, ref := range []string{"acme/widget", "https://github.com/acme/widget", "git@github.com:acme/widget.git"} {
78 if got := mustExistingRepoDir(t, ref); got != widget {
79 t.Errorf("existingRepoDir(%q) = %q, want %q", ref, got, widget)
80 }
81 }
82 // A project whose origin is not a GitHub URL can only be matched by name.
83 if got := mustExistingRepoDir(t, "gizmo"); got != local {
84 t.Errorf("existingRepoDir(\"gizmo\") = %q, want %q", got, local)
85 }
86 // A GitHub repo nobody has checked out stays unresolved (no cloning here).
87 if got := mustExistingRepoDir(t, "acme/unknown"); got != "" {
88 t.Errorf("existingRepoDir(\"acme/unknown\") = %q, want \"\"", got)
89 }
90}
91
92// A project registered under a name that collides with a different GitHub repo
93// must not be handed back for that repo — the origin disagrees.
94func TestExistingRepoDirIgnoresNameCollisionWithOtherOrigin(t *testing.T) {
95 root := t.TempDir()
96 widget := initRepo(t, filepath.Join(root, "widget"), "https://github.com/acme/widget.git")
97 writeRegistry(t, withTempHome(t), map[string]string{"widget": widget})
98 t.Chdir(root)
99
100 if got := mustExistingRepoDir(t, "other/widget"); got != "" {
101 t.Errorf("existingRepoDir(\"other/widget\") = %q, want \"\"", got)
102 }
103}
104
105// A registry at both its names is refused, naming both, by every lookup of
106// a checkout through it, rather than taken for one with no projects.
107func TestExistingRepoDirRefusesRegistryUnderBothNames(t *testing.T) {
108 root := t.TempDir()
109 widget := initRepo(t, filepath.Join(root, "widget"), "https://github.com/acme/widget.git")
110 home := withTempHome(t)
111 writeRegistry(t, home, map[string]string{"widget": widget})
112 flow := filepath.Join(home, ".valley", "flow", "projects.json")
113 if err := os.MkdirAll(filepath.Dir(flow), 0o755); err != nil {
114 t.Fatal(err)
115 }
116 if err := os.WriteFile(flow, []byte(`{"projects": {}}`), 0o644); err != nil {
117 t.Fatal(err)
118 }
119 t.Chdir(root)
120
121 both := func(what string, err error) {
122 t.Helper()
123 if err == nil || !strings.Contains(err.Error(), filepath.Join(home, ".klaus", "projects.json")) || !strings.Contains(err.Error(), flow) {
124 t.Errorf("%s with a registry under both names: %v, want an error naming both", what, err)
125 }
126 }
127 _, err := existingRepoDir("acme/widget")
128 both("existingRepoDir", err)
129 _, err = resolveRepoDirUncached(context.Background(), "widget")
130 both("resolveRepoDir", err)
131 _, err = reviewRepoSlug(context.Background(), "widget")
132 both("reviewRepoSlug", err)
133 _, err = trackChange(context.Background(), "widget", "nxvp", nil, nil, valleyListings{})
134 both("trackChange", err)
135}
136
137func TestResolveRepoDirWithoutTargetOutsideRepo(t *testing.T) {
138 writeRegistry(t, withTempHome(t), nil)
139 t.Chdir(t.TempDir())
140
141 _, err := resolveRepoDirUncached(context.Background(), "")
142 if !errors.Is(err, errNoTargetRepo) {
143 t.Errorf("error = %v, want errNoTargetRepo", err)
144 }
145}
146
147func TestRepoSlug(t *testing.T) {
148 tests := []struct {
149 ref string
150 want string
151 }{
152 {"acme/widget", "acme/widget"},
153 {"https://github.com/acme/widget.git", "acme/widget"},
154 {"git@github.com:acme/widget", "acme/widget"},
155 {"widget", ""}, // bare project name, no owner
156 {"", ""}, // nothing to resolve
157 {"/srv/git/widget.git", ""}, // a local path, not a GitHub repo
158 }
159 for _, tt := range tests {
160 if got := repoSlug(tt.ref); got != tt.want {
161 t.Errorf("repoSlug(%q) = %q, want %q", tt.ref, got, tt.want)
162 }
163 }
164}
165
166// A command that goes on without a registry it can't read stops at one
167// under both names, naming both; one that doesn't parse it still goes on
168// without, and none at all is the default registry.
169func TestRegistryUnlessConflict(t *testing.T) {
170 home := t.TempDir()
171 t.Setenv("HOME", home)
172 legacy, flow := filepath.Join(home, ".klaus", "projects.json"), filepath.Join(home, ".valley", "flow", "projects.json")
173
174 if reg, err := registryUnlessConflict(); err != nil || reg == nil {
175 t.Fatalf("no registry: %v, %v; want the default one", reg, err)
176 }
177 writeJSON(t, legacy, map[string]any{"projects": map[string]string{}})
178 if err := os.WriteFile(legacy, []byte("{"), 0o644); err != nil {
179 t.Fatal(err)
180 }
181 if reg, err := registryUnlessConflict(); err != nil || reg != nil {
182 t.Fatalf("unparsed registry: %v, %v; want none and no error", reg, err)
183 }
184 writeJSON(t, flow, map[string]any{"projects": map[string]string{}})
185 _, err := registryUnlessConflict()
186 if err == nil || !strings.Contains(err.Error(), legacy) || !strings.Contains(err.Error(), flow) {
187 t.Fatalf("registry under both names: %v; want an error naming both", err)
188 }
189}