1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
package sync
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestGitCommand_SetsDir(t *testing.T) {
cmd := gitCommand("/tmp/example-repo", "status")
if cmd.Dir != "/tmp/example-repo" {
t.Fatalf("expected command dir to be set, got %q", cmd.Dir)
}
}
func TestGitCommand_LeavesDirEmptyForGlobalCommands(t *testing.T) {
cmd := gitCommand("", "ls-remote", "--tags", "origin", "v1.0.0")
if cmd.Dir != "" {
t.Fatalf("expected empty dir for global command, got %q", cmd.Dir)
}
}
func TestParseTagHashOutput_EmptyOutput(t *testing.T) {
for _, in := range [][]byte{nil, []byte(""), []byte(" \n\t \n")} {
hash, err := parseTagHashOutput(in, "v1.0.0", "origin")
if err == nil {
t.Fatalf("expected error for empty output %q, got hash %q", in, hash)
}
if hash != "" {
t.Fatalf("expected empty hash for empty output, got %q", hash)
}
if !strings.Contains(err.Error(), "v1.0.0") || !strings.Contains(err.Error(), "origin") {
t.Fatalf("expected error to mention tag and source, got %v", err)
}
}
}
func TestParseTagHashOutput_ReturnsFirstField(t *testing.T) {
hash, err := parseTagHashOutput([]byte("abc123\trefs/tags/v1.0.0\n"), "v1.0.0", "origin")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if hash != "abc123" {
t.Fatalf("expected hash %q, got %q", "abc123", hash)
}
}
func TestGetTagCommitHash_LocalTagPeelsToCommitHash(t *testing.T) {
repoPath := t.TempDir()
runGit(t, repoPath, "init")
runGit(t, repoPath, "-c", "user.name=Test", "-c", "user.email=test@example.com", "commit", "--allow-empty", "-m", "initial commit")
runGit(t, repoPath, "tag", "-a", "v1.0.0", "-m", "release v1.0.0")
headHash := runGit(t, repoPath, "rev-parse", "HEAD")
tagHash, err := getTagCommitHash(repoPath, "v1.0.0", "local")
if err != nil {
t.Fatalf("expected local tag hash lookup to succeed, got error: %v", err)
}
if tagHash != headHash {
t.Fatalf("expected peeled local tag hash %q, got %q", headHash, tagHash)
}
}
func TestGetTagCommitHash_LocalTagMissingReturnsError(t *testing.T) {
repoPath := t.TempDir()
runGit(t, repoPath, "init")
tagHash, err := getTagCommitHash(repoPath, "v9.9.9", "local")
if err == nil {
t.Fatalf("expected error for missing local tag, got hash %q", tagHash)
}
if tagHash != "" {
t.Fatalf("expected empty hash for missing local tag, got %q", tagHash)
}
}
func TestPopStash_NoStashReturnsError(t *testing.T) {
repoPath := t.TempDir()
runGit(t, repoPath, "init")
if err := popStash(repoPath); err == nil {
t.Fatal("expected error when popping stash with no entries")
}
}
func TestPopStash_RestoresStashedChanges(t *testing.T) {
repoPath := t.TempDir()
runGit(t, repoPath, "init")
runGit(t, repoPath, "config", "user.name", "Test User")
runGit(t, repoPath, "config", "user.email", "test@example.com")
trackedFile := filepath.Join(repoPath, "tracked.txt")
if err := os.WriteFile(trackedFile, []byte("first\n"), 0o644); err != nil {
t.Fatalf("write tracked file: %v", err)
}
runGit(t, repoPath, "add", "tracked.txt")
runGit(t, repoPath, "commit", "-m", "initial")
if err := os.WriteFile(trackedFile, []byte("second\n"), 0o644); err != nil {
t.Fatalf("update tracked file: %v", err)
}
runGit(t, repoPath, "stash", "push", "-m", "test-stash")
if err := popStash(repoPath); err != nil {
t.Fatalf("expected popStash to succeed, got error: %v", err)
}
content, err := os.ReadFile(trackedFile)
if err != nil {
t.Fatalf("read tracked file: %v", err)
}
if string(content) != "second\n" {
t.Fatalf("expected stashed content to be restored, got %q", string(content))
}
}
func TestStashChanges_NoStashEntryReturnsFalse(t *testing.T) {
repoPath := t.TempDir()
runGit(t, repoPath, "init")
runGit(t, repoPath, "config", "user.name", "Test User")
runGit(t, repoPath, "config", "user.email", "test@example.com")
trackedFile := filepath.Join(repoPath, "tracked.txt")
if err := os.WriteFile(trackedFile, []byte("committed\n"), 0o644); err != nil {
t.Fatalf("write tracked file: %v", err)
}
runGit(t, repoPath, "add", "tracked.txt")
runGit(t, repoPath, "commit", "-m", "initial")
untrackedFile := filepath.Join(repoPath, "untracked.txt")
if err := os.WriteFile(untrackedFile, []byte("new\n"), 0o644); err != nil {
t.Fatalf("write untracked file: %v", err)
}
stashed, err := stashChanges(repoPath)
if err != nil {
t.Fatalf("stashChanges() unexpected error: %v", err)
}
if stashed {
t.Fatal("stashChanges() = true, want false when no stash entry is created")
}
if stashList := runGit(t, repoPath, "stash", "list"); stashList != "" {
t.Fatalf("expected no stash entries, got %q", stashList)
}
}
func TestHandleWorkingDirectoryState_NoStashEntryReturnsNotStashed(t *testing.T) {
workDir := t.TempDir()
repoName := "repo"
repoPath := filepath.Join(workDir, repoName)
if err := os.MkdirAll(repoPath, 0o755); err != nil {
t.Fatalf("mkdir repo path: %v", err)
}
runGit(t, repoPath, "init")
runGit(t, repoPath, "config", "user.name", "Test User")
runGit(t, repoPath, "config", "user.email", "test@example.com")
trackedFile := filepath.Join(repoPath, "tracked.txt")
if err := os.WriteFile(trackedFile, []byte("committed\n"), 0o644); err != nil {
t.Fatalf("write tracked file: %v", err)
}
runGit(t, repoPath, "add", "tracked.txt")
runGit(t, repoPath, "commit", "-m", "initial")
untrackedFile := filepath.Join(repoPath, "untracked.txt")
if err := os.WriteFile(untrackedFile, []byte("new\n"), 0o644); err != nil {
t.Fatalf("write untracked file: %v", err)
}
s := &Syncer{workDir: workDir, repoName: repoName}
stashed, err := s.handleWorkingDirectoryState()
if err != nil {
t.Fatalf("handleWorkingDirectoryState() unexpected error: %v", err)
}
if stashed {
t.Fatal("handleWorkingDirectoryState() = true, want false when stash push creates no entry")
}
}
func runGit(t *testing.T, repoPath string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repoPath
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, string(output))
}
return strings.TrimSpace(string(output))
}
|