forked from akupila/gitprompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
313 lines (292 loc) · 6.93 KB
/
parser_test.go
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package gitprompt
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"testing"
)
func TestParseValues(t *testing.T) {
tests := []struct {
name string
setup string
expected *GitStatus
}{
{
name: "not git repo",
expected: nil,
},
{
name: "dirty",
setup: `
git init --initial-branch=master || git init
touch test
`,
expected: &GitStatus{
Untracked: 1,
Clean: true,
Outdated: true,
},
},
{
name: "staged",
setup: `
git init --initial-branch=master || git init
touch test
git add test
`,
expected: &GitStatus{
Staged: 1,
Clean: false,
Outdated: true,
},
},
{
name: "modified",
setup: `
git init --initial-branch=master || git init
echo "hello" >> test
git add test
git commit -m 'initial'
echo "world" >> test
`,
expected: &GitStatus{
Modified: 1,
Clean: false,
Outdated: true,
},
},
{
name: "deleted",
setup: `
git init --initial-branch=master || git init
echo "hello" >> test
git add test
git commit -m 'initial'
rm test
`,
expected: &GitStatus{
Modified: 1,
Clean: false,
Outdated: true,
},
},
{
name: "conflicts",
setup: `
git init --initial-branch=master || git init
git commit --allow-empty -m 'initial'
git checkout -b other
git checkout master
echo foo >> test
git add test
git commit -m 'first'
git checkout other
echo bar >> test
git add test
git commit -m 'first'
git rebase master || true
`,
expected: &GitStatus{
Conflicts: 1,
Clean: false,
Outdated: true,
},
},
{
name: "ahead",
setup: `
git init --initial-branch=master || git init
git remote add origin $REMOTE
git commit --allow-empty -m 'first'
git push -u origin HEAD
git commit --allow-empty -m 'second'
`,
expected: &GitStatus{
Ahead: 1,
Upstream: "origin/master",
Clean: true,
Outdated: true,
},
},
{
name: "behind",
setup: `
git init --initial-branch=master || git init
git remote add origin $REMOTE
git commit --allow-empty -m 'first'
git commit --allow-empty -m 'second'
git push -u origin HEAD
git reset --hard HEAD^
`,
expected: &GitStatus{
Behind: 1,
Upstream: "origin/master",
Clean: true,
Outdated: true,
},
},
{
name: "stashed",
setup: `
git init --initial-branch=master || git init
echo "hello" >> test
git add test
git commit -m 'initial'
echo "world" >> test
git stash
`,
expected: &GitStatus{
Stashed: 1,
Clean: true,
Outdated: false,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dir, cleanupDir := setupTestDir(t)
defer cleanupDir()
if test.setup != "" {
remote, cleanupRemote := setupRemote(t, dir)
defer cleanupRemote()
commands := "export REMOTE=" + remote + "\n" + test.setup
setupCommands(t, dir, commands)
}
actual, err := Parse()
if err != nil {
t.Errorf("Received unexpected error: %v", err)
return
}
if test.expected == nil {
if actual != nil {
t.Errorf("Expected nil return, got %v", actual)
}
return
}
assertInt(t, "Untracked", test.expected.Untracked, actual.Untracked)
assertInt(t, "Modified", test.expected.Modified, actual.Modified)
assertInt(t, "Staged", test.expected.Staged, actual.Staged)
assertInt(t, "Conflicts", test.expected.Conflicts, actual.Conflicts)
assertInt(t, "Ahead", test.expected.Ahead, actual.Ahead)
assertInt(t, "Behind", test.expected.Behind, actual.Behind)
assertInt(t, "Stashed", test.expected.Stashed, actual.Stashed)
assertString(t, "Upstream", test.expected.Upstream, actual.Upstream)
assertBool(t, "Clean", test.expected.Clean, actual.Clean)
assertBool(t, "Outdated", test.expected.Outdated, actual.Outdated)
})
}
}
func TestParseHead(t *testing.T) {
dir, done := setupTestDir(t)
defer done()
setupCommands(t, dir, `
git init --initial-branch=master || git init
`)
s, _ := Parse()
assertString(t, "branch", "master", s.Branch)
setupCommands(t, dir, `
git commit --allow-empty -m 'initial'
`)
s, _ = Parse()
assertString(t, "branch", "master", s.Branch)
setupCommands(t, dir, `
git checkout -b other
`)
s, _ = Parse()
assertString(t, "branch", "other", s.Branch)
setupCommands(t, dir, `
git commit --allow-empty -m 'second'
git checkout HEAD^
`)
s, _ = Parse()
assertString(t, "branch", "", s.Branch)
if len(s.Sha) != 40 {
t.Errorf("Expected 40 char hash, got %v (%s)", len(s.Sha), s.Sha)
}
}
func TestExecGitErr(t *testing.T) {
path := os.Getenv("PATH")
os.Setenv("PATH", "")
defer os.Setenv("PATH", path)
_, err := Parse()
if err == nil {
t.Errorf("Expected error when git not found on $PATH")
}
}
func setupTestDir(t *testing.T) (string, func()) {
dir, err := ioutil.TempDir("", "gitprompt-test")
if err != nil {
t.Fatalf("Create temp dir: %v", err)
}
if err = os.Chdir(dir); err != nil {
t.Fatalf("Could not change dir: %v", err)
}
return dir, func() {
if err = os.RemoveAll(dir); err != nil {
fmt.Fprintf(os.Stderr, "Failed to clean up test dir: %v\n", err)
}
}
}
func setupRemote(t *testing.T, dir string) (string, func()) {
// Set up remote dir
remote, err := ioutil.TempDir("", "gitprompt-test-remote.git")
if err != nil {
t.Fatalf("create temp dir for remote: %v", err)
}
remoteCmd := exec.Command("git", "init", "--bare")
remoteCmd.Dir = remote
if err = remoteCmd.Run(); err != nil {
t.Fatalf("Set up remote: %v", err)
}
return remote, func() {
if err = os.RemoveAll(remote); err != nil {
fmt.Fprintf(os.Stderr, "Failed to clean up remote dir: %v\n", err)
}
}
}
func setupCommands(t *testing.T, dir, commands string) {
commands = "set -eo pipefail\n" + commands
script := path.Join(dir, "setup.sh")
if err := ioutil.WriteFile(script, []byte(commands), 0644); err != nil {
t.Fatal(err)
}
cmd := exec.Command("bash", "setup.sh")
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = bufio.NewWriter(&stderr)
cmd.Stdout = bufio.NewWriter(&stdout)
cmd.Dir = dir
if err := cmd.Run(); err != nil {
t.Log("STDOUT:", stdout.String())
t.Log("STDERR:", stderr.String())
t.Fatalf("Setup command failed: %v", err)
}
if err := os.Remove(script); err != nil {
t.Fatal(err)
}
}
func assertString(t *testing.T, name, expected, actual string) {
t.Helper()
if expected == actual {
return
}
t.Errorf("%s does not match\n\tExpected: %q\n\tActual: %q", name, expected, actual)
}
func assertInt(t *testing.T, name string, expected, actual int) {
t.Helper()
if expected == actual {
return
}
t.Errorf("%s does not match\n\tExpected: %v\n\tActual: %v", name, expected, actual)
}
func assertBool(t *testing.T, name string, expected, actual bool) {
t.Helper()
if expected == actual {
return
}
t.Errorf("%s does not match\n\tExpected: %v\n\tActual: %v", name, expected, actual)
}