-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcrdd_test.sh
executable file
·356 lines (287 loc) · 12.5 KB
/
tcrdd_test.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
#! /usr/bin/env bash
. ./test_utils.sh
should_print_usage_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
startStatus=$(runAsAlice git status -s)
runAsAlice ./tcrdd.sh "${arguments[@]}" > $stdout
status=$(runAsAlice git status -s)
currentHash=$(runAsAlice getHeadHash)
stdoutContent=$(cat $stdout)
assertEquals 'Alice s code should not be commited' "$headHash" "$currentHash"
assertEquals "Nothing should have changed for git" "$startStatus" "$status"
assertContains "Usage should be displayed" "$stdoutContent" "Usage :"
}
test_print_usage_when_given_short_option() {
and_any_command=true
should_print_usage_when_given -h $and_any_command
}
test_print_usage_when_given_long_option() {
and_any_command=true
should_print_usage_when_given --help $and_any_command
}
test_print_usage_when_given_no_test_command() {
no_test_command=
should_print_usage_when_given $no_test_command
}
should_commit_and_push_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
message=$(runAsAlice getHeadMessage)
currentHash=$(runAsAlice getHeadHash)
originHash=$(runAsAlice getOriginHeadHash)
assertNotEquals 'Alice s code is not commited' "$headHash" "$currentHash"
assertNull 'Not everything is committed by alice' "$status"
assertNull 'Alice commit message should be empty' "$message"
assertTrue 'Created file should still be there' '[ -f ${aliceClone}/aFile ]'
assertEquals 'Alice s should be pushed' "$originHash" "$currentHash"
}
test_commit_and_push_when_assumed_green_and_tests_pass__short_option() {
should_commit_and_push_when_given -g $and_tests_pass
}
test_commit_and_push_when_assumed_green_and_tests_pass__long_option() {
should_commit_and_push_when_given --green $and_tests_pass
}
should_revert_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
currentHash=$(runAsAlice getHeadHash)
assertNull 'Alice s code is not reverted' "$status"
assertEquals 'Alice head should be the same as before' "$headHash" "$currentHash"
assertFalse 'Created file should be removed' '[ -f ${aliceClone}/aFile ]'
}
test_revert_when_assumed_red_and_tests_pass__short_option() {
should_revert_when_given -r $and_tests_pass
}
test_revert_when_assumed_red_and_tests_pass__long_option() {
should_revert_when_given --red $and_tests_pass
}
test_revert_when_assumed_green_and_tests_fail__short_option() {
should_revert_when_given -g $and_tests_fail
}
test_revert_when_assumed_green_and_tests_fail__long_option() {
should_revert_when_given --green $and_tests_fail
}
should_remove_untracked_files_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
currentHash=$(runAsAlice getHeadHash)
assertNull 'Alice s code is not reverted' "$status"
assertEquals 'Alice head should be the same as before' "$headHash" "$currentHash"
assertFalse 'Created file should be removed' '[ -f ${aliceClone}/aFile ]'
}
test_remove_untracked_files_when_assumed_green_and_tests_fail__short_option() {
should_remove_untracked_files_when_given -g $and_tests_fail
}
test_remove_untracked_files_when_assumed_green_and_tests_fail__long_option() {
should_remove_untracked_files_when_given --green $and_tests_fail
}
should_restore_files_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh --green true > /dev/null 2>&1
echo otherContent >> ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
content=$(runAsAlice cat aFile)
currentHash=$(runAsAlice getHeadHash)
assertNull 'Alice s code should be reverted' "$status"
assertEquals 'File content should be reverted' "content" "$content"
}
test_restore_files_when_assumed_green_and_tests_fail__short_option() {
should_restore_files_when_given -g $and_tests_fail
}
test_restore_files_when_assumed_green_and_tests_fail__long_option() {
should_restore_files_when_given --green $and_tests_fail
}
should_not_push_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
message=$(runAsAlice getHeadMessage)
currentHash=$(runAsAlice getHeadHash)
originHash=$(runAsAlice getOriginHeadHash)
assertNotEquals 'Alice s code is not commited' "$headHash" "$currentHash"
assertNull 'Not everything is committed by alice' "$status"
assertNull 'Alice commit message should be empty' "$message"
assertTrue 'Created file should still be there' '[ -f ${aliceClone}/aFile ]'
assertNotEquals 'Alice s should not be pushed' "$originHash" "$currentHash"
}
test_does_not_push_when_assumed_red_and_tests_fail__short_option() {
should_not_push_when_given -r $and_tests_fail
}
test_does_not_push_when_assumed_red_and_tests_fail__long_option() {
should_not_push_when_given --red $and_tests_fail
}
test_does_not_push_when_assumed_green_and_tests_pass_in_local_mode__short_option() {
should_not_push_when_given -l -g $and_tests_pass
}
test_does_not_push_when_assumed_green_and_tests_pass_in_local_mode__long_option() {
should_not_push_when_given --local --green $and_tests_pass
}
should_amend_commit_and_not_push_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh --red false > /dev/null 2>&1
echo otherContent >> ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
nbCommits=$(runAsAlice git rev-list --count ${headHash}..HEAD)
currentHash=$(runAsAlice getHeadHash)
originHash=$(runAsAlice getOriginHeadHash)
assertNull 'Everything should be committed by alice' "$status"
assertNotEquals 'Alice s should not be pushed' "$originHash" "$currentHash"
assertEquals 'Only one commit should exist' 1 "${nbCommits}"
}
test_amend_last_red_commit_when_assumed_red_and_tests_fail__short_option() {
should_amend_commit_and_not_push_when_given -r $and_tests_fail
}
test_amend_last_red_commit_when_assumed_red_and_tests_fail__long_option() {
should_amend_commit_and_not_push_when_given --red $and_tests_fail
}
test_amend_last_red_commit_when_assumed_green_and_tests_pass_in_local_mode__short_option() {
should_amend_commit_and_not_push_when_given -l -g $and_tests_pass
}
test_amend_last_red_commit_when_assumed_green_and_tests_pass_in_local_mode__long_option() {
should_amend_commit_and_not_push_when_given --local --green $and_tests_pass
}
should_amend_commit_and_push_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh --red false > /dev/null 2>&1
echo otherContent >> ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
status=$(runAsAlice git status -s)
nbCommits=$(runAsAlice git rev-list --count ${headHash}..HEAD)
currentHash=$(runAsAlice getHeadHash)
originHash=$(runAsAlice getOriginHeadHash)
assertNull 'Everything should be committed by alice' "$status"
assertEquals 'Alice s should be pushed' "$originHash" "$currentHash"
assertEquals 'Only one commit should exist' 1 "${nbCommits}"
}
test_amend_last_red_commit_and_push_when_assumed_green_and_tests_pass__short_option() {
should_amend_commit_and_push_when_given -g $and_tests_pass
}
test_amend_last_red_commit_and_push_when_assumed_green_and_tests_pass__long_option() {
should_amend_commit_and_push_when_given --green $and_tests_pass
}
should_amend_commit_with_message_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh --red false > /dev/null 2>&1
echo otherContent >> ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
nbCommits=$(runAsAlice git rev-list --count ${headHash}..HEAD)
message=$(runAsAlice getHeadMessage)
assertEquals 'Alice commit message should not be empty' "Commit message" "$message"
assertEquals 'Only one commit should exist' 1 "${nbCommits}"
}
test_amend_commit_with_message_when_assumed_green_and_tests_pass__short_option() {
should_amend_commit_with_message_when_given -g -m "Commit message" $and_tests_pass
}
test_amend_commit_with_message_when_assumed_green_and_tests_pass__long_option() {
should_amend_commit_with_message_when_given --green --message "Commit message" $and_tests_pass
}
should_commit_with_message_when_given() {
arguments=("$@")
echo content > ${aliceClone}/aFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
message=$(runAsAlice getHeadMessage)
assertEquals 'Alice commit message should not be empty' "Commit message" "$message"
}
test_commit_with_message_when_assumed_green_and_tests_pass__short_option() {
should_commit_with_message_when_given -g -m "Commit message" $and_tests_pass
}
test_commit_with_message_when_assumed_green_and_tests_pass__long_option() {
should_commit_with_message_when_given --green --message "Commit message" $and_tests_pass
}
test_commit_with_message_when_assumed_red_and_tests_fail__short_option() {
should_commit_with_message_when_given -r -m "Commit message" $and_tests_fail
}
test_commit_with_message_when_assumed_red_and_tests_fail__long_option() {
should_commit_with_message_when_given --red --message "Commit message" $and_tests_fail
}
should_pull_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${bobClone}/aFile
runAsBob git add . > /dev/null 2>&1
runAsBob git commit -m "bob commit" > /dev/null 2>&1
runAsBob git push > /dev/null 2>&1
bobHash=$(runAsBob getHeadHash)
echo otherContent >> ${aliceClone}/otherFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
searchBobCommit=$(runAsAlice git log --pretty=%H | grep ${bobHash})
assertNotNull 'Bob s code should be found' "${searchBobCommit}"
commitsSinceBob=$(runAsAlice git rev-list --count ${bobHash}..HEAD)
assertEquals 'Bob s code should be pulled' 1 "${commitsSinceBob}"
}
test_pull_code_when_assumed_green_and_tests_pass__short_option() {
should_pull_when_given -g $and_tests_pass
}
test_pull_code_when_assumed_green_and_tests_pass__long_option() {
should_pull_when_given --green $and_tests_pass
}
test_pull_code_when_assumed_red_and_tests_fail__short_option() {
should_pull_when_given -r $and_tests_fail
}
test_pull_code_when_assumed_red_and_tests_fail__long_option() {
should_pull_when_given --red $and_tests_fail
}
should_not_pull_when_given() {
arguments=("$@")
headHash=$(runAsAlice getHeadHash)
echo content > ${bobClone}/aFile
runAsBob git add . > /dev/null 2>&1
runAsBob git commit -m "bob commit" > /dev/null 2>&1
runAsBob git push > /dev/null 2>&1
bobHash=$(runAsBob getHeadHash)
echo otherContent >> ${aliceClone}/otherFile
runAsAlice ./tcrdd.sh "${arguments[@]}" > /dev/null 2>&1
searchBobCommit=$(runAsAlice git log --pretty=%H | grep ${bobHash})
assertNull 'Bob s code should not be found' "${searchBobCommit}"
}
test_not_pull_code_when_assumed_green_and_tests_pass_in_local_mode__short_option() {
should_not_pull_when_given -l -g $and_tests_pass
}
test_not_pull_code_when_assumed_green_and_tests_pass_in_local_mode__long_option() {
should_not_pull_when_given --local --green $and_tests_pass
}
test_not_pull_code_when_assumed_red_and_tests_fail_in_local_mode__short_option() {
should_not_pull_when_given -l -r $and_tests_fail
}
test_not_pull_code_when_assumed_red_and_tests_fail_in_local_mode__long_option() {
should_not_pull_when_given --local --red $and_tests_fail
}
oneTimeSetUp() {
export HOME="${SHUNIT_TMPDIR}"
workingDirectory=`pwd`
bareRepository="${SHUNIT_TMPDIR}/repository"
aliceClone="${SHUNIT_TMPDIR}/alice"
bobClone="${SHUNIT_TMPDIR}/bob"
stdout="${SHUNIT_TMPDIR}/stdout"
}
setUp() {
createRepositories > /dev/null 2>&1
}
tearDown() {
deleteRepositories > /dev/null 2>&1
rm -f $stdout
}
. ./shunit2/shunit2