gunk-dev/the-valley

sync_test.go

source9ee95920e399main's published tip history
5.0 KiB
1package cmd
2
3import (
4 "bytes"
5 "context"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "the-valley/flow/internal/project"
13)
14
15func runGitT(t *testing.T, dir string, args ...string) {
16 t.Helper()
17 cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
18 if out, err := cmd.CombinedOutput(); err != nil {
19 t.Fatalf("git %v: %v\n%s", args, err, out)
20 }
21}
22
23// setupProjectWithUpstream creates a bare origin and a clone, and returns the
24// clone path. Advances origin by one commit if advance=true.
25func setupProjectWithUpstream(t *testing.T, advance bool) string {
26 t.Helper()
27 seed := t.TempDir()
28 runGitT(t, ".", "init", "--initial-branch=main", seed)
29 runGitT(t, seed, "config", "user.email", "t@t.com")
30 runGitT(t, seed, "config", "user.name", "T")
31 os.WriteFile(filepath.Join(seed, "README.md"), []byte("# seed\n"), 0o644)
32 runGitT(t, seed, "add", "README.md")
33 runGitT(t, seed, "commit", "-m", "init")
34
35 bare := filepath.Join(t.TempDir(), "origin.git")
36 if out, err := exec.Command("git", "clone", "--bare", seed, bare).CombinedOutput(); err != nil {
37 t.Fatalf("bare clone: %v\n%s", err, out)
38 }
39
40 clone := filepath.Join(t.TempDir(), "clone")
41 if out, err := exec.Command("git", "clone", bare, clone).CombinedOutput(); err != nil {
42 t.Fatalf("clone: %v\n%s", err, out)
43 }
44 runGitT(t, clone, "config", "user.email", "t@t.com")
45 runGitT(t, clone, "config", "user.name", "T")
46
47 if advance {
48 staging := filepath.Join(t.TempDir(), "staging")
49 if out, err := exec.Command("git", "clone", bare, staging).CombinedOutput(); err != nil {
50 t.Fatalf("staging clone: %v\n%s", err, out)
51 }
52 runGitT(t, staging, "config", "user.email", "t@t.com")
53 runGitT(t, staging, "config", "user.name", "T")
54 os.WriteFile(filepath.Join(staging, "extra.txt"), []byte("x"), 0o644)
55 runGitT(t, staging, "add", "extra.txt")
56 runGitT(t, staging, "commit", "-m", "advance")
57 runGitT(t, staging, "push", "origin", "main")
58 }
59
60 return clone
61}
62
63// withTempHome sets HOME to a temp dir for the duration of the test and
64// registers cleanup.
65func withTempHome(t *testing.T) string {
66 t.Helper()
67 home := t.TempDir()
68 t.Setenv("HOME", home)
69 return home
70}
71
72// writeRegistry writes a projects.json inside HOME/.klaus pointing at the
73// given projects.
74func writeRegistry(t *testing.T, home string, projects map[string]string) {
75 t.Helper()
76 reg := &project.Registry{
77 ProjectsDir: filepath.Join(home, "src"),
78 Projects: projects,
79 }
80 regDir := filepath.Join(home, ".klaus")
81 if err := os.MkdirAll(regDir, 0o755); err != nil {
82 t.Fatal(err)
83 }
84 if err := reg.SaveTo(filepath.Join(regDir, "projects.json")); err != nil {
85 t.Fatal(err)
86 }
87}
88
89func TestSyncCmd_TableOutputAndExit(t *testing.T) {
90 home := withTempHome(t)
91 cleanClone := setupProjectWithUpstream(t, true) // behind upstream, clean
92
93 writeRegistry(t, home, map[string]string{
94 "demo": cleanClone,
95 })
96
97 buf := &bytes.Buffer{}
98 cmd := syncCmd
99 cmd.SetOut(buf)
100 cmd.SetErr(buf)
101 cmd.SetContext(context.Background())
102 if err := cmd.RunE(cmd, []string{}); err != nil {
103 t.Fatalf("syncCmd: %v", err)
104 }
105 out := buf.String()
106 if !strings.Contains(out, "demo") {
107 t.Errorf("output should include project name, got:\n%s", out)
108 }
109 if !strings.Contains(out, "pulled") {
110 t.Errorf("output should include 'pulled' status, got:\n%s", out)
111 }
112 if !strings.Contains(out, "main") {
113 t.Errorf("output should include branch name, got:\n%s", out)
114 }
115}
116
117func TestSyncCmd_DirtyIsSkippedNoError(t *testing.T) {
118 home := withTempHome(t)
119 clone := setupProjectWithUpstream(t, true)
120
121 // Dirty the clone
122 os.WriteFile(filepath.Join(clone, "wip.txt"), []byte("wip"), 0o644)
123
124 writeRegistry(t, home, map[string]string{"demo": clone})
125
126 buf := &bytes.Buffer{}
127 cmd := syncCmd
128 cmd.SetOut(buf)
129 cmd.SetErr(buf)
130 cmd.SetContext(context.Background())
131 err := cmd.RunE(cmd, []string{})
132 if err != nil {
133 t.Errorf("dirty clone should NOT cause a non-zero exit, got err: %v", err)
134 }
135 if !strings.Contains(buf.String(), "skipped") {
136 t.Errorf("output should include 'skipped', got:\n%s", buf.String())
137 }
138}
139
140func TestSyncCmd_FetchErrorReturnsError(t *testing.T) {
141 home := withTempHome(t)
142 clone := setupProjectWithUpstream(t, false)
143
144 // Break the remote so fetch fails
145 runGitT(t, clone, "remote", "set-url", "origin", filepath.Join(t.TempDir(), "nope.git"))
146
147 writeRegistry(t, home, map[string]string{"demo": clone})
148
149 buf := &bytes.Buffer{}
150 cmd := syncCmd
151 cmd.SetOut(buf)
152 cmd.SetErr(buf)
153 cmd.SetContext(context.Background())
154 err := cmd.RunE(cmd, []string{})
155 if err == nil {
156 t.Error("expected error exit when a project fetch fails")
157 }
158 if !strings.Contains(buf.String(), "error") {
159 t.Errorf("output should include 'error', got:\n%s", buf.String())
160 }
161}
162
163func TestSyncCmd_EmptyRegistry(t *testing.T) {
164 home := withTempHome(t)
165 writeRegistry(t, home, map[string]string{})
166
167 buf := &bytes.Buffer{}
168 cmd := syncCmd
169 cmd.SetOut(buf)
170 cmd.SetErr(buf)
171 cmd.SetContext(context.Background())
172 if err := cmd.RunE(cmd, []string{}); err != nil {
173 t.Fatalf("empty registry: %v", err)
174 }
175 if !strings.Contains(buf.String(), "No projects") {
176 t.Errorf("expected 'No projects' message, got:\n%s", buf.String())
177 }
178}