Repository navigation
Expand file tree
/
Copy pathgit_test.go
More file actions
276 lines (238 loc) · 6.9 KB
/
Copy pathgit_test.go
File metadata and controls
276 lines (238 loc) · 6.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
)
func hasArg(args []string, target string) bool {
for _, arg := range args {
if arg == target {
return true
}
}
return false
}
// HelperProcess is used to mock exec.Command
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
// Check the commant add being run
args := os.Args
for len(args) > 0 {
if args[0] == "--" {
args = args[1:]
break
}
args = args[1:]
}
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "No command\n")
os.Exit(2)
}
cmd, subargs := args[0], args[1:]
switch cmd {
case "git":
if len(subargs) > 0 {
switch subargs[0] {
case "rev-parse":
// isGitRepo
os.Exit(0)
case "add":
// stageAll
os.Exit(0)
case "diff":
if hasArg(subargs, "--binary") {
fmt.Print(os.Getenv("MOCK_GIT_DIFF_BINARY"))
os.Exit(0)
}
// getStagedDiff
// Return diff content if checking for changes, or empty if testing no changes
if os.Getenv("MOCK_GIT_DIFF_EMPTY") == "1" {
fmt.Print("")
} else {
fmt.Print("diff content")
}
os.Exit(0)
case "commit":
if os.Getenv("MOCK_GIT_EXPECT_EDIT") == "1" && !hasArg(subargs, "--edit") {
fmt.Fprint(os.Stderr, "missing --edit")
os.Exit(1)
}
if os.Getenv("MOCK_GIT_EXPECT_MESSAGE_FLAG") == "1" && !hasArg(subargs, "-m") {
fmt.Fprint(os.Stderr, "missing -m")
os.Exit(1)
}
if os.Getenv("MOCK_GIT_COMMIT_FAIL") == "1" {
fmt.Fprint(os.Stderr, "commit failed")
os.Exit(1)
}
os.Exit(0)
case "push":
// push
os.Exit(0)
case "reset":
if os.Getenv("MOCK_GIT_RESET_FAIL") == "1" {
fmt.Fprint(os.Stderr, "reset failed")
os.Exit(1)
}
os.Exit(0)
case "apply":
if os.Getenv("MOCK_GIT_APPLY_FAIL") == "1" {
fmt.Fprint(os.Stderr, "apply failed")
os.Exit(1)
}
os.Exit(0)
}
}
}
fmt.Fprintf(os.Stderr, "Unknown command %q\n", cmd)
os.Exit(2)
}
func mockExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
// Pass through specific mock env vars
if val := os.Getenv("MOCK_GIT_DIFF_EMPTY"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_DIFF_EMPTY="+val)
}
if val := os.Getenv("MOCK_GIT_DIFF_BINARY"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_DIFF_BINARY="+val)
}
if val := os.Getenv("MOCK_GIT_RESET_FAIL"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_RESET_FAIL="+val)
}
if val := os.Getenv("MOCK_GIT_APPLY_FAIL"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_APPLY_FAIL="+val)
}
if val := os.Getenv("MOCK_GIT_EXPECT_EDIT"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_EXPECT_EDIT="+val)
}
if val := os.Getenv("MOCK_GIT_EXPECT_MESSAGE_FLAG"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_EXPECT_MESSAGE_FLAG="+val)
}
if val := os.Getenv("MOCK_GIT_COMMIT_FAIL"); val != "" {
cmd.Env = append(cmd.Env, "MOCK_GIT_COMMIT_FAIL="+val)
}
return cmd
}
func TestStageAll(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
if err := stageAll(); err != nil {
t.Errorf("stageAll() failed: %v", err)
}
}
func TestGetStagedDiff(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
// Test with changes
diff, err := getStagedDiff()
if err != nil {
t.Errorf("getStagedDiff() failed: %v", err)
}
if diff != "diff content" {
t.Errorf("Expected 'diff content', got %q", diff)
}
// Test with no changes
os.Setenv("MOCK_GIT_DIFF_EMPTY", "1")
defer os.Unsetenv("MOCK_GIT_DIFF_EMPTY")
_, err = getStagedDiff()
if err == nil {
t.Error("Expected error when no changes are staged, got nil")
} else if !strings.Contains(err.Error(), "no changes to commit") {
t.Errorf("Expected 'no changes to commit' error, got %v", err)
}
}
func TestCommit(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_EXPECT_MESSAGE_FLAG", "1")
defer os.Unsetenv("MOCK_GIT_EXPECT_MESSAGE_FLAG")
if err := commit("test message"); err != nil {
t.Errorf("commit() failed: %v", err)
}
}
func TestCommitWithEditor(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_EXPECT_EDIT", "1")
defer os.Unsetenv("MOCK_GIT_EXPECT_EDIT")
os.Setenv("MOCK_GIT_EXPECT_MESSAGE_FLAG", "1")
defer os.Unsetenv("MOCK_GIT_EXPECT_MESSAGE_FLAG")
if err := commitWithEditor("test message"); err != nil {
t.Errorf("commitWithEditor() failed: %v", err)
}
}
func TestCommitWithEditorFailure(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_COMMIT_FAIL", "1")
defer os.Unsetenv("MOCK_GIT_COMMIT_FAIL")
err := commitWithEditor("test message")
if err == nil {
t.Fatal("Expected commitWithEditor() to fail")
}
if !strings.Contains(err.Error(), "failed to commit") {
t.Errorf("Expected commit failure message, got %v", err)
}
}
func TestSnapshotIndex(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_DIFF_BINARY", "binary patch")
defer os.Unsetenv("MOCK_GIT_DIFF_BINARY")
snapshot, err := snapshotIndex()
if err != nil {
t.Errorf("snapshotIndex() failed: %v", err)
}
if snapshot != "binary patch" {
t.Errorf("Expected 'binary patch', got %q", snapshot)
}
}
func TestRestoreIndex(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
if err := restoreIndex("some patch"); err != nil {
t.Errorf("restoreIndex() failed: %v", err)
}
}
func TestRestoreIndexSkipsApplyForEmptySnapshot(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_APPLY_FAIL", "1")
defer os.Unsetenv("MOCK_GIT_APPLY_FAIL")
if err := restoreIndex(""); err != nil {
t.Errorf("restoreIndex() with empty snapshot failed: %v", err)
}
}
func TestRestoreIndexResetFailure(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_RESET_FAIL", "1")
defer os.Unsetenv("MOCK_GIT_RESET_FAIL")
err := restoreIndex("some patch")
if err == nil {
t.Fatal("Expected restoreIndex() to fail when reset fails")
}
if !strings.Contains(err.Error(), "failed to reset index") {
t.Errorf("Expected reset failure message, got %v", err)
}
}
func TestRestoreIndexApplyFailure(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
os.Setenv("MOCK_GIT_APPLY_FAIL", "1")
defer os.Unsetenv("MOCK_GIT_APPLY_FAIL")
err := restoreIndex("some patch")
if err == nil {
t.Fatal("Expected restoreIndex() to fail when apply fails")
}
if !strings.Contains(err.Error(), "failed to restore staged changes") {
t.Errorf("Expected apply failure message, got %v", err)
}
}