forked from michaeldfallen/git-radar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-commits.sh
executable file
·428 lines (339 loc) · 8.9 KB
/
test-commits.sh
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
scriptDir="$(cd "$(dirname "$0")"; pwd)"
source "$scriptDir/radar-base.sh"
tmpfile=""
cd_to_tmp() {
tmpfile="/tmp/git-prompt-tests-$(time_now)$1"
mkdir -p "$tmpfile"
cd "$tmpfile"
}
rm_tmp() {
cd $scriptDir
rm -rf /tmp/git-prompt-tests*
}
test_commits_with_no_commits() {
cd_to_tmp
git init --quiet
assertEquals "0" "$(commits_ahead_of_remote)"
assertEquals "0" "$(commits_behind_of_remote)"
rm_tmp
}
test_commits_behind_no_remote() {
cd_to_tmp
git init --quiet
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
assertEquals "0" "$(commits_behind_of_remote)"
rm_tmp
}
test_commits_ahead_no_remote() {
cd_to_tmp
git init --quiet
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
assertEquals "0" "$(commits_ahead_of_remote)"
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
assertEquals "0" "$(commits_ahead_of_remote)"
rm_tmp
}
test_commits_ahead_with_remote() {
cd_to_tmp "remote"
git init --quiet
touch README
git add .
git commit -m "initial commit" --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout master --quiet
repoLocation="$(pwd)"
cd "$remoteLocation"
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
cd "$repoLocation"
git fetch origin --quiet
assertEquals "1" "$(commits_ahead_of_remote)"
cd "$remoteLocation"
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
cd "$repoLocation"
git fetch origin --quiet
assertEquals "2" "$(commits_ahead_of_remote)"
rm_tmp
}
test_commits_ahead_with_remote() {
cd_to_tmp "remote"
git init --quiet
touch README
git add .
git commit -m "initial commit" --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout master --quiet
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
assertEquals "1" "$(commits_ahead_of_remote)"
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
assertEquals "2" "$(commits_ahead_of_remote)"
rm_tmp
}
test_remote_ahead_master() {
cd_to_tmp "remote"
git init --quiet
touch README
git add .
git commit -m "initial commit" --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout master --quiet
git checkout -b foo --quiet
git push --quiet -u origin foo >/dev/null
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
assertEquals "0" "$(remote_ahead_of_master)"
git push --quiet
assertEquals "1" "$(remote_ahead_of_master)"
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
assertEquals "1" "$(remote_ahead_of_master)"
git push --quiet
assertEquals "2" "$(remote_ahead_of_master)"
rm_tmp
}
test_remote_behind_master() {
cd_to_tmp "remote"
git init --bare --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout -b master --quiet
touch README
git add README
git commit -m "initial commit" --quiet
git push --quiet -u origin master >/dev/null
git reset --quiet --hard HEAD
git checkout -b foo --quiet
git push --quiet -u origin foo >/dev/null
assertEquals "0" "$(remote_behind_of_master)"
git checkout master --quiet
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
git push --quiet >/dev/null
git checkout foo --quiet
assertEquals "1" "$(remote_behind_of_master)"
git checkout master --quiet
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
git push --quiet >/dev/null
git checkout foo --quiet
assertEquals "2" "$(remote_behind_of_master)"
rm_tmp
}
test_remote_branch_starts_with_local_branch_name() {
cd_to_tmp "remote"
git init --bare --quiet
remoteLocation="$(pwd)"
cd_to_tmp "local"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout -b master --quiet
touch README
git add README
git commit -m "initial commit" --quiet
git push --quiet -u origin master >/dev/null
git reset --quiet --hard HEAD
git checkout -b foobar --quiet
touch foobarfile
git add foobarfile
git commit -m "added foobar" --quiet
git push --quiet -u origin foobar >/dev/null
git checkout -b foo --quiet
assertEquals "0" "$(remote_ahead_of_master)"
assertEquals "0" "$(remote_behind_of_master)"
assertEquals "0" "$(commits_behind_of_remote)"
assertEquals "0" "$(commits_ahead_of_remote)"
rm_tmp
}
test_remote_branch_ends_with_local_branch_name() {
cd_to_tmp "remote"
git init --bare --quiet
remoteLocation="$(pwd)"
cd_to_tmp "local"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout -b master --quiet
touch README
git add README
git commit -m "initial commit" --quiet
git push --quiet -u origin master >/dev/null
git reset --quiet --hard HEAD
git checkout -b foobar --quiet
touch foobarfile
git add foobarfile
git commit -m "added foobar" --quiet
git push --quiet -u origin foobar >/dev/null
git checkout -b bar --quiet
assertEquals "0" "$(remote_ahead_of_master)"
assertEquals "0" "$(remote_behind_of_master)"
assertEquals "0" "$(commits_behind_of_remote)"
assertEquals "0" "$(commits_ahead_of_remote)"
rm_tmp
}
test_dont_call_remote_branch_name() {
cd_to_tmp "remote"
git init --bare --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout -b master --quiet
touch README
git add README
git commit -m "initial commit" --quiet
git push --quiet -u origin master >/dev/null
git reset --quiet --hard HEAD
git checkout -b foo --quiet
git push --quiet -u origin foo >/dev/null
remote_branch="$(remote_branch_name)"
debug_output="$(
{
set -x
output="$(
remote_behind_of_master "$remote_branch";
remote_ahead_of_master "$remote_branch";
commits_ahead_of_remote "$remote_branch";
commits_behind_of_remote "$remote_branch";
)"
set +x
} 2>&1
echo "$output"
)"
#Grep through the output and look for remote_branch_name being called
usages="$(echo "$debug_output" | grep 'remote_branch_name' | wc -l )"
#wc -l has a weird output
assertEquals " 0" "$usages"
rm_tmp
}
test_dont_remote_if_remote_is_master() {
cd_to_tmp
git init --quiet
remote_branch="origin/master"
debug_output="$(
{
set -x
output="$(
remote_behind_of_master "$remote_branch";
remote_ahead_of_master "$remote_branch";
)"
set +x
} 2>&1
echo "$output"
)"
usages="$(echo "$debug_output" | grep 'git rev-list' | wc -l )"
assertEquals " 0" "$usages"
rm_tmp
}
test_quiet_if_no_remote_master() {
cd_to_tmp "remote"
git init --quiet
touch README
git add .
git checkout -b foo --quiet
git commit -m "initial commit" --quiet
remoteLocation="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remoteLocation
git fetch origin --quiet
git checkout foo --quiet
repoLocation="$(pwd)"
remote_branch="$(remote_branch_name)"
debug_output="$(
{
output="$(
remote_behind_of_master "$remote_branch";
)"
} 2>&1
echo "$output"
)"
assertEquals "0" "$debug_output"
debug_output="$(
{
output="$(
remote_ahead_of_master "$remote_branch";
)"
} 2>&1
echo "$output"
)"
assertEquals "0" "$debug_output"
rm_tmp
}
test_local_commits() {
local up="↑"
local both="⇵"
local down="↓"
cd_to_tmp "remote"
assertEquals "" "$(zsh_color_local_commits)"
assertEquals "" "$(bash_color_local_commits)"
assertEquals "" "$(color_local_commits)"
git init --quiet
touch README
git add .
git commit -m "initial commit" --quiet
remote="$(pwd)"
cd_to_tmp "new"
git init --quiet
git remote add origin $remote
git fetch origin --quiet
git checkout master --quiet
repo="$(pwd)"
assertEquals "" "$(zsh_color_local_commits)"
assertEquals "" "$(bash_color_local_commits)"
assertEquals "" "$(color_local_commits)"
cd "$repo"
echo "bar" > bar
git add .
git commit -m "test commit" --quiet
assertEquals "1$up" "$(zsh_color_local_commits)"
assertEquals "1$up" "$(bash_color_local_commits)"
assertEquals "1$up" "$(color_local_commits)"
cd "$remote"
echo "foo" > foo
git add .
git commit -m "test commit" --quiet
cd "$repo"
git fetch origin --quiet
assertEquals "1${both}1" "$(zsh_color_local_commits)"
assertEquals "1${both}1" "$(bash_color_local_commits)"
assertEquals "1${both}1" "$(color_local_commits)"
git reset --hard HEAD^ --quiet
assertEquals "1$down" "$(zsh_color_local_commits)"
assertEquals "1$down" "$(bash_color_local_commits)"
assertEquals "1$down" "$(color_local_commits)"
}
. ./shunit/shunit2