Skip to content

Commit f40ae5c

Browse files
Pete Wyckoffgitster
authored andcommitted
git-p4: use test_when_finished in tests
Cleanup nicely when tests fail. This avoids many duplicated lines in the tests, and adds cleanup in a couple of tests that did not have it. When one fails, now all the rest will not fail too. Signed-off-by: Pete Wyckoff <[email protected]> Acked-by: Luke Diamand <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b35acb5 commit f40ae5c

File tree

1 file changed

+49
-54
lines changed

1 file changed

+49
-54
lines changed

t/t9800-git-p4.sh

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,33 @@ test_expect_success 'add p4 files' '
4545
cd "$TRASH_DIRECTORY"
4646
'
4747

48+
cleanup_git() {
49+
cd "$TRASH_DIRECTORY" &&
50+
rm -rf "$git" &&
51+
mkdir "$git"
52+
}
53+
4854
test_expect_success 'basic git-p4 clone' '
4955
"$GITP4" clone --dest="$git" //depot &&
56+
test_when_finished cleanup_git &&
5057
cd "$git" &&
5158
git log --oneline >lines &&
52-
test_line_count = 1 lines &&
53-
cd .. &&
54-
rm -rf "$git" && mkdir "$git"
59+
test_line_count = 1 lines
5560
'
5661

5762
test_expect_success 'git-p4 clone @all' '
5863
"$GITP4" clone --dest="$git" //depot@all &&
64+
test_when_finished cleanup_git &&
5965
cd "$git" &&
6066
git log --oneline >lines &&
61-
test_line_count = 2 lines &&
62-
cd .. &&
63-
rm -rf "$git" && mkdir "$git"
67+
test_line_count = 2 lines
6468
'
6569

6670
test_expect_success 'git-p4 sync uninitialized repo' '
6771
test_create_repo "$git" &&
72+
test_when_finished cleanup_git &&
6873
cd "$git" &&
69-
test_must_fail "$GITP4" sync &&
70-
rm -rf "$git" && mkdir "$git"
74+
test_must_fail "$GITP4" sync
7175
'
7276

7377
#
@@ -76,19 +80,18 @@ test_expect_success 'git-p4 sync uninitialized repo' '
7680
#
7781
test_expect_success 'git-p4 sync new branch' '
7882
test_create_repo "$git" &&
83+
test_when_finished cleanup_git &&
7984
cd "$git" &&
8085
test_commit head &&
8186
"$GITP4" sync --branch=refs/remotes/p4/depot //depot@all &&
8287
git log --oneline p4/depot >lines &&
83-
cat lines &&
84-
test_line_count = 2 lines &&
85-
cd .. &&
86-
rm -rf "$git" && mkdir "$git"
88+
test_line_count = 2 lines
8789
'
8890

8991
test_expect_success 'exit when p4 fails to produce marshaled output' '
9092
badp4dir="$TRASH_DIRECTORY/badp4dir" &&
9193
mkdir -p "$badp4dir" &&
94+
test_when_finished "rm -rf $badp4dir" &&
9295
cat >"$badp4dir"/p4 <<-EOF &&
9396
#!$SHELL_PATH
9497
exit 1
@@ -106,29 +109,26 @@ test_expect_success 'add p4 files with wildcards in the names' '
106109
echo file-wild-at >file-wild@at &&
107110
echo file-wild-percent >file-wild%percent &&
108111
p4 add -f file-wild* &&
109-
p4 submit -d "file wildcards" &&
110-
cd "$TRASH_DIRECTORY"
112+
p4 submit -d "file wildcards"
111113
'
112114

113115
test_expect_success 'wildcard files git-p4 clone' '
114116
"$GITP4" clone --dest="$git" //depot &&
117+
test_when_finished cleanup_git &&
115118
cd "$git" &&
116119
test -f file-wild#hash &&
117120
test -f file-wild\*star &&
118121
test -f file-wild@at &&
119-
test -f file-wild%percent &&
120-
cd "$TRASH_DIRECTORY" &&
121-
rm -rf "$git" && mkdir "$git"
122+
test -f file-wild%percent
122123
'
123124

124125
test_expect_success 'clone bare' '
125126
"$GITP4" clone --dest="$git" --bare //depot &&
127+
test_when_finished cleanup_git &&
126128
cd "$git" &&
127129
test ! -d .git &&
128130
bare=`git config --get core.bare` &&
129-
test "$bare" = true &&
130-
cd "$TRASH_DIRECTORY" &&
131-
rm -rf "$git" && mkdir "$git"
131+
test "$bare" = true
132132
'
133133

134134
p4_add_user() {
@@ -173,6 +173,7 @@ test_expect_success 'preserve users' '
173173
p4_add_user bob Bob &&
174174
p4_grant_admin alice &&
175175
"$GITP4" clone --dest="$git" //depot &&
176+
test_when_finished cleanup_git &&
176177
cd "$git" &&
177178
echo "username: a change by alice" >> file1 &&
178179
echo "username: a change by bob" >> file2 &&
@@ -181,27 +182,25 @@ test_expect_success 'preserve users' '
181182
git config git-p4.skipSubmitEditCheck true &&
182183
P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit --preserve-user &&
183184
p4_check_commit_author file1 alice &&
184-
p4_check_commit_author file2 bob &&
185-
cd "$TRASH_DIRECTORY" &&
186-
rm -rf "$git" && mkdir "$git"
185+
p4_check_commit_author file2 bob
187186
'
188187

189188
# Test username support, submitting as bob, who lacks admin rights. Should
190189
# not submit change to p4 (git diff should show deltas).
191190
test_expect_success 'refuse to preserve users without perms' '
192191
"$GITP4" clone --dest="$git" //depot &&
192+
test_when_finished cleanup_git &&
193193
cd "$git" &&
194194
echo "username-noperms: a change by alice" >> file1 &&
195195
git commit --author "Alice <alice@localhost>" -m "perms: a change by alice" file1 &&
196196
! P4EDITOR=touch P4USER=bob P4PASSWD=secret "$GITP4" commit --preserve-user &&
197-
! git diff --exit-code HEAD..p4/master > /dev/null &&
198-
cd "$TRASH_DIRECTORY" &&
199-
rm -rf "$git" && mkdir "$git"
197+
! git diff --exit-code HEAD..p4/master > /dev/null
200198
'
201199

202200
# What happens with unknown author? Without allowMissingP4Users it should fail.
203201
test_expect_success 'preserve user where author is unknown to p4' '
204202
"$GITP4" clone --dest="$git" //depot &&
203+
test_when_finished cleanup_git &&
205204
cd "$git" &&
206205
git config git-p4.skipSubmitEditCheck true
207206
echo "username-bob: a change by bob" >> file1 &&
@@ -215,9 +214,7 @@ test_expect_success 'preserve user where author is unknown to p4' '
215214
git config git-p4.preserveUser true &&
216215
P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit &&
217216
git diff --exit-code HEAD..p4/master > /dev/null &&
218-
p4_check_commit_author file1 alice &&
219-
cd "$TRASH_DIRECTORY" &&
220-
rm -rf "$git" && mkdir "$git"
217+
p4_check_commit_author file1 alice
221218
'
222219

223220
# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
@@ -226,31 +223,29 @@ test_expect_success 'preserve user where author is unknown to p4' '
226223
# Test: warning disabled and user is the same.
227224
test_expect_success 'not preserving user with mixed authorship' '
228225
"$GITP4" clone --dest="$git" //depot &&
229-
(
230-
cd "$git" &&
231-
git config git-p4.skipSubmitEditCheck true &&
232-
p4_add_user derek Derek &&
233-
234-
make_change_by_user usernamefile3 Derek derek@localhost &&
235-
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
236-
grep "git author derek@localhost does not match" actual &&
237-
238-
make_change_by_user usernamefile3 Charlie charlie@localhost &&
239-
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
240-
grep "git author charlie@localhost does not match" actual &&
241-
242-
make_change_by_user usernamefile3 alice alice@localhost &&
243-
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
244-
! grep "git author.*does not match" actual &&
245-
246-
git config git-p4.skipUserNameCheck true &&
247-
make_change_by_user usernamefile3 Charlie charlie@localhost &&
248-
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
249-
! grep "git author.*does not match" actual &&
250-
251-
p4_check_commit_author usernamefile3 alice
252-
) &&
253-
rm -rf "$git" && mkdir "$git"
226+
test_when_finished cleanup_git &&
227+
cd "$git" &&
228+
git config git-p4.skipSubmitEditCheck true &&
229+
p4_add_user derek Derek &&
230+
231+
make_change_by_user usernamefile3 Derek derek@localhost &&
232+
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
233+
grep "git author derek@localhost does not match" actual &&
234+
235+
make_change_by_user usernamefile3 Charlie charlie@localhost &&
236+
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
237+
grep "git author charlie@localhost does not match" actual &&
238+
239+
make_change_by_user usernamefile3 alice alice@localhost &&
240+
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
241+
! grep "git author.*does not match" actual &&
242+
243+
git config git-p4.skipUserNameCheck true &&
244+
make_change_by_user usernamefile3 Charlie charlie@localhost &&
245+
P4EDITOR=cat P4USER=alice P4PASSWD=secret "$GITP4" commit >actual &&
246+
! grep "git author.*does not match" actual &&
247+
248+
p4_check_commit_author usernamefile3 alice
254249
'
255250

256251

0 commit comments

Comments
 (0)