Skip to content

Commit 2405ed0

Browse files
kyleamgitster
authored andcommitted
t1400: use test_when_finished for cleanup
Move cleanup lines that occur after test blocks into test_when_finished calls within the test bodies. Don't move cleanup lines that seem to be related to mutiple tests rather than a single test. Signed-off-by: Kyle Meyer <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e063e5d commit 2405ed0

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

t/t1400-update-ref.sh

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,24 @@ test_expect_success "fail to delete $m with stale ref" '
4848
test $B = "$(cat .git/$m)"
4949
'
5050
test_expect_success "delete $m" '
51+
test_when_finished "rm -f .git/$m" &&
5152
git update-ref -d $m $B &&
5253
test_path_is_missing .git/$m
5354
'
54-
rm -f .git/$m
5555

56-
test_expect_success "delete $m without oldvalue verification" "
56+
test_expect_success "delete $m without oldvalue verification" '
57+
test_when_finished "rm -f .git/$m" &&
5758
git update-ref $m $A &&
58-
test $A = \$(cat .git/$m) &&
59+
test $A = $(cat .git/$m) &&
5960
git update-ref -d $m &&
6061
test_path_is_missing .git/$m
61-
"
62-
rm -f .git/$m
62+
'
6363

64-
test_expect_success \
65-
"fail to create $n" \
66-
"touch .git/$n_dir &&
67-
test_must_fail git update-ref $n $A"
68-
rm -f .git/$n_dir
64+
test_expect_success "fail to create $n" '
65+
test_when_finished "rm -f .git/$n_dir" &&
66+
touch .git/$n_dir &&
67+
test_must_fail git update-ref $n $A
68+
'
6969

7070
test_expect_success \
7171
"create $m (by HEAD)" \
@@ -80,28 +80,28 @@ test_expect_success "fail to delete $m (by HEAD) with stale ref" '
8080
test $B = $(cat .git/$m)
8181
'
8282
test_expect_success "delete $m (by HEAD)" '
83+
test_when_finished "rm -f .git/$m" &&
8384
git update-ref -d HEAD $B &&
8485
test_path_is_missing .git/$m
8586
'
86-
rm -f .git/$m
8787

8888
test_expect_success "deleting current branch adds message to HEAD's log" '
89+
test_when_finished "rm -f .git/$m" &&
8990
git update-ref $m $A &&
9091
git symbolic-ref HEAD $m &&
9192
git update-ref -m delete-$m -d $m &&
9293
test_path_is_missing .git/$m &&
9394
grep "delete-$m$" .git/logs/HEAD
9495
'
95-
rm -f .git/$m
9696

9797
test_expect_success "deleting by HEAD adds message to HEAD's log" '
98+
test_when_finished "rm -f .git/$m" &&
9899
git update-ref $m $A &&
99100
git symbolic-ref HEAD $m &&
100101
git update-ref -m delete-by-head -d HEAD &&
101102
test_path_is_missing .git/$m &&
102103
grep "delete-by-head$" .git/logs/HEAD
103104
'
104-
rm -f .git/$m
105105

106106
test_expect_success 'update-ref does not create reflogs by default' '
107107
test_when_finished "git update-ref -d $outside" &&
@@ -188,28 +188,29 @@ test_expect_success \
188188
"git update-ref HEAD $B $A &&
189189
test $B"' = $(cat .git/'"$m"')'
190190
test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
191+
test_when_finished "rm -f .git/$m" &&
191192
git update-ref -d HEAD $B &&
192193
! grep "$m" .git/packed-refs &&
193194
test_path_is_missing .git/$m
194195
'
195-
rm -f .git/$m
196196

197197
cp -f .git/HEAD .git/HEAD.orig
198198
test_expect_success "delete symref without dereference" '
199+
test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
199200
git update-ref --no-deref -d HEAD &&
200201
test_path_is_missing .git/HEAD
201202
'
202-
cp -f .git/HEAD.orig .git/HEAD
203203

204204
test_expect_success "delete symref without dereference when the referred ref is packed" '
205+
test_when_finished "cp -f .git/HEAD.orig .git/HEAD" &&
205206
echo foo >foo.c &&
206207
git add foo.c &&
207208
git commit -m foo &&
208209
git pack-refs --all &&
209210
git update-ref --no-deref -d HEAD &&
210211
test_path_is_missing .git/HEAD
211212
'
212-
cp -f .git/HEAD.orig .git/HEAD
213+
213214
git update-ref -d $m
214215

215216
test_expect_success 'update-ref -d is not confused by self-reference' '
@@ -241,21 +242,21 @@ test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
241242
test_expect_success '(not) create HEAD with old sha1' "
242243
test_must_fail git update-ref HEAD $A $B
243244
"
244-
test_expect_success "(not) prior created .git/$m" "
245+
test_expect_success "(not) prior created .git/$m" '
246+
test_when_finished "rm -f .git/$m" &&
245247
test_path_is_missing .git/$m
246-
"
247-
rm -f .git/$m
248+
'
248249

249250
test_expect_success \
250251
"create HEAD" \
251252
"git update-ref HEAD $A"
252253
test_expect_success '(not) change HEAD with wrong SHA1' "
253254
test_must_fail git update-ref HEAD $B $Z
254255
"
255-
test_expect_success "(not) changed .git/$m" "
256-
! test $B"' = $(cat .git/'"$m"')
256+
test_expect_success "(not) changed .git/$m" '
257+
test_when_finished "rm -f .git/$m" &&
258+
! test $B = $(cat .git/$m)
257259
'
258-
rm -f .git/$m
259260

260261
rm -f .git/logs/refs/heads/master
261262
test_expect_success \
@@ -309,10 +310,10 @@ $Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creati
309310
$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
310311
$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
311312
EOF
312-
test_expect_success \
313-
"verifying $m's log (logged by touch)" \
314-
"test_cmp expect .git/logs/$m"
315-
rm -rf .git/$m .git/logs expect
313+
test_expect_success "verifying $m's log (logged by touch)" '
314+
test_when_finished "rm -rf .git/$m .git/logs expect" &&
315+
test_cmp expect .git/logs/$m
316+
'
316317

317318
test_expect_success \
318319
"create $m (logged by config)" \
@@ -340,8 +341,8 @@ $B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
340341
EOF
341342
test_expect_success \
342343
"verifying $m's log (logged by config)" \
343-
'test_cmp expect .git/logs/$m'
344-
rm -f .git/$m .git/logs/$m expect
344+
'test_when_finished "rm -f .git/$m .git/logs/$m expect" &&
345+
test_cmp expect .git/logs/$m'
345346

346347
git update-ref $m $D
347348
cat >.git/logs/$m <<EOF
@@ -357,55 +358,55 @@ gd="Thu, 26 May 2005 18:33:00 -0500"
357358
ld="Thu, 26 May 2005 18:43:00 -0500"
358359
test_expect_success \
359360
'Query "master@{May 25 2005}" (before history)' \
360-
'rm -f o e &&
361+
'test_when_finished "rm -f o e" &&
361362
git rev-parse --verify "master@{May 25 2005}" >o 2>e &&
362363
test '"$C"' = $(cat o) &&
363364
test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
364365
test_expect_success \
365366
"Query master@{2005-05-25} (before history)" \
366-
'rm -f o e &&
367+
'test_when_finished "rm -f o e" &&
367368
git rev-parse --verify master@{2005-05-25} >o 2>e &&
368369
test '"$C"' = $(cat o) &&
369370
echo test "warning: Log for '\'master\'' only goes back to $ed." = "$(cat e)"'
370371
test_expect_success \
371372
'Query "master@{May 26 2005 23:31:59}" (1 second before history)' \
372-
'rm -f o e &&
373+
'test_when_finished "rm -f o e" &&
373374
git rev-parse --verify "master@{May 26 2005 23:31:59}" >o 2>e &&
374375
test '"$C"' = $(cat o) &&
375376
test "warning: Log for '\''master'\'' only goes back to $ed." = "$(cat e)"'
376377
test_expect_success \
377378
'Query "master@{May 26 2005 23:32:00}" (exactly history start)' \
378-
'rm -f o e &&
379+
'test_when_finished "rm -f o e" &&
379380
git rev-parse --verify "master@{May 26 2005 23:32:00}" >o 2>e &&
380381
test '"$C"' = $(cat o) &&
381382
test "" = "$(cat e)"'
382383
test_expect_success \
383384
'Query "master@{May 26 2005 23:32:30}" (first non-creation change)' \
384-
'rm -f o e &&
385+
'test_when_finished "rm -f o e" &&
385386
git rev-parse --verify "master@{May 26 2005 23:32:30}" >o 2>e &&
386387
test '"$A"' = $(cat o) &&
387388
test "" = "$(cat e)"'
388389
test_expect_success \
389390
'Query "master@{2005-05-26 23:33:01}" (middle of history with gap)' \
390-
'rm -f o e &&
391+
'test_when_finished "rm -f o e" &&
391392
git rev-parse --verify "master@{2005-05-26 23:33:01}" >o 2>e &&
392393
test '"$B"' = $(cat o) &&
393394
test "warning: Log for ref '"$m has gap after $gd"'." = "$(cat e)"'
394395
test_expect_success \
395396
'Query "master@{2005-05-26 23:38:00}" (middle of history)' \
396-
'rm -f o e &&
397+
'test_when_finished "rm -f o e" &&
397398
git rev-parse --verify "master@{2005-05-26 23:38:00}" >o 2>e &&
398399
test '"$Z"' = $(cat o) &&
399400
test "" = "$(cat e)"'
400401
test_expect_success \
401402
'Query "master@{2005-05-26 23:43:00}" (exact end of history)' \
402-
'rm -f o e &&
403+
'test_when_finished "rm -f o e" &&
403404
git rev-parse --verify "master@{2005-05-26 23:43:00}" >o 2>e &&
404405
test '"$E"' = $(cat o) &&
405406
test "" = "$(cat e)"'
406407
test_expect_success \
407408
'Query "master@{2005-05-28}" (past end of history)' \
408-
'rm -f o e &&
409+
'test_when_finished "rm -f o e" &&
409410
git rev-parse --verify "master@{2005-05-28}" >o 2>e &&
410411
test '"$D"' = $(cat o) &&
411412
test "warning: Log for ref '"$m unexpectedly ended on $ld"'." = "$(cat e)"'
@@ -415,7 +416,8 @@ rm -f .git/$m .git/logs/$m expect
415416

416417
test_expect_success \
417418
'creating initial files' \
418-
'echo TEST >F &&
419+
'test_when_finished rm -f M &&
420+
echo TEST >F &&
419421
git add F &&
420422
GIT_AUTHOR_DATE="2005-05-26 23:30" \
421423
GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
@@ -433,8 +435,7 @@ test_expect_success \
433435
echo $h_TEST >.git/MERGE_HEAD &&
434436
GIT_AUTHOR_DATE="2005-05-26 23:45" \
435437
GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
436-
h_MERGED=$(git rev-parse --verify HEAD) &&
437-
rm -f M'
438+
h_MERGED=$(git rev-parse --verify HEAD)'
438439

439440
cat >expect <<EOF
440441
$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add

0 commit comments

Comments
 (0)