gunk-dev/the-valley

project_test.go

sourceb8e46aa6aaccmain's published tip history
4.6 KiB
1package cmd
2
3import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "the-valley/flow/internal/project"
11)
12
13// withStubProjectDeps swaps getProjectDeps for a stub that returns the given
14// description (and no-op clone/resolve). Restores on test cleanup.
15func withStubProjectDeps(t *testing.T, description string) {
16 t.Helper()
17 prev := getProjectDeps
18 getProjectDeps = func() ProjectDeps {
19 return ProjectDeps{
20 ResolveGitHubRepo: func(name string) (string, string, error) {
21 return "stub-owner", "https://example.invalid/stub.git", nil
22 },
23 GitClone: func(url, targetDir string) error {
24 return initGitRepoErr(targetDir)
25 },
26 FetchDescription: func(repoSlug string) string {
27 return description
28 },
29 }
30 }
31 t.Cleanup(func() { getProjectDeps = prev })
32}
33
34// initGitRepoErr is the error-returning sibling of initGitRepo for use inside
35// stub GitClone implementations (where we don't have a *testing.T).
36func initGitRepoErr(dir string) error {
37 if err := os.MkdirAll(dir, 0o755); err != nil {
38 return err
39 }
40 if err := os.MkdirAll(filepath.Join(dir, ".git"), 0o755); err != nil {
41 return err
42 }
43 return nil
44}
45
46// invokeProjectAdd resets the projectAddCmd flags and calls runProjectAdd
47// directly. ref is the positional argument; path/description are flag values
48// ("" leaves the flag unset). Returns the captured stdout/stderr.
49func invokeProjectAdd(t *testing.T, ref, path, description string) string {
50 t.Helper()
51 // Reset flags so values don't leak between tests.
52 if err := projectAddCmd.Flags().Set("path", path); err != nil {
53 t.Fatal(err)
54 }
55 if err := projectAddCmd.Flags().Set("description", description); err != nil {
56 t.Fatal(err)
57 }
58 t.Cleanup(func() {
59 _ = projectAddCmd.Flags().Set("path", "")
60 _ = projectAddCmd.Flags().Set("description", "")
61 })
62
63 buf := &bytes.Buffer{}
64 projectAddCmd.SetOut(buf)
65 projectAddCmd.SetErr(buf)
66 if err := runProjectAdd(projectAddCmd, []string{ref}); err != nil {
67 t.Fatalf("runProjectAdd(%q): %v\n%s", ref, err, buf.String())
68 }
69 return buf.String()
70}
71
72func TestProjectAddExplicitDescription(t *testing.T) {
73 home := withTempHome(t)
74 withStubProjectDeps(t, "should-not-be-used")
75
76 repoDir := filepath.Join(t.TempDir(), "mytool")
77 initGitRepo(t, repoDir)
78
79 out := invokeProjectAdd(t, "owner/mytool", repoDir, "my cool tool")
80 if !strings.Contains(out, "my cool tool") {
81 t.Errorf("expected output to mention description, got: %q", out)
82 }
83
84 reg, err := project.LoadFrom(filepath.Join(home, ".klaus", "projects.json"))
85 if err != nil {
86 t.Fatalf("LoadFrom: %v", err)
87 }
88 if got := reg.Description("mytool"); got != "my cool tool" {
89 t.Errorf("Description = %q, want %q", got, "my cool tool")
90 }
91 if _, ok := reg.Get("mytool"); !ok {
92 t.Error("project mytool not registered")
93 }
94}
95
96func TestProjectAddGhDescriptionFallback(t *testing.T) {
97 home := withTempHome(t)
98 withStubProjectDeps(t, "from-github")
99
100 repoDir := filepath.Join(t.TempDir(), "mytool")
101 initGitRepo(t, repoDir)
102
103 out := invokeProjectAdd(t, "owner/mytool", repoDir, "")
104 if !strings.Contains(out, "from-github") {
105 t.Errorf("expected output to include fallback description, got: %q", out)
106 }
107
108 reg, err := project.LoadFrom(filepath.Join(home, ".klaus", "projects.json"))
109 if err != nil {
110 t.Fatalf("LoadFrom: %v", err)
111 }
112 if got := reg.Description("mytool"); got != "from-github" {
113 t.Errorf("Description = %q, want %q", got, "from-github")
114 }
115}
116
117func TestProjectAddNoDescriptionWhenGhEmpty(t *testing.T) {
118 home := withTempHome(t)
119 withStubProjectDeps(t, "") // gh returns empty -> no description
120
121 repoDir := filepath.Join(t.TempDir(), "mytool")
122 initGitRepo(t, repoDir)
123
124 out := invokeProjectAdd(t, "owner/mytool", repoDir, "")
125 // The output should not include an em dash since no description.
126 if strings.Contains(out, "—") {
127 t.Errorf("unexpected em-dash in output: %q", out)
128 }
129
130 reg, err := project.LoadFrom(filepath.Join(home, ".klaus", "projects.json"))
131 if err != nil {
132 t.Fatalf("LoadFrom: %v", err)
133 }
134 if got := reg.Description("mytool"); got != "" {
135 t.Errorf("Description = %q, want empty", got)
136 }
137 if _, ok := reg.Get("mytool"); !ok {
138 t.Error("project mytool not registered")
139 }
140}
141
142func TestProjectAddExplicitOverridesGh(t *testing.T) {
143 // --description should win over the gh fetch.
144 home := withTempHome(t)
145 withStubProjectDeps(t, "from-github")
146
147 repoDir := filepath.Join(t.TempDir(), "mytool")
148 initGitRepo(t, repoDir)
149
150 invokeProjectAdd(t, "owner/mytool", repoDir, "explicit wins")
151
152 reg, err := project.LoadFrom(filepath.Join(home, ".klaus", "projects.json"))
153 if err != nil {
154 t.Fatalf("LoadFrom: %v", err)
155 }
156 if got := reg.Description("mytool"); got != "explicit wins" {
157 t.Errorf("Description = %q, want %q", got, "explicit wins")
158 }
159}