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 <pw@padd.com> Acked-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pete Wyckoff committed Jul 31, 2011 at 09:44 UTC f40ae5cee61f35bdc46b17ccc11fbc6eb7977c89
1 file changed +49 -54
t/t9800-git-p4.sh
+49 -54
@@ -45,29 +45,33 @@ test_expect_success 'add p4 files' '
45 cd "$TRASH_DIRECTORY"
46 '
47
48 +cleanup_git() {
49 + cd "$TRASH_DIRECTORY" &&
50 + rm -rf "$git" &&
51 + mkdir "$git"
52 +}
53 +
54 test_expect_success 'basic git-p4 clone' '
55 "$GITP4" clone --dest="$git" //depot &&
56 + test_when_finished cleanup_git &&
57 cd "$git" &&
58 git log --oneline >lines &&
52 - test_line_count = 1 lines &&
53 - cd .. &&
54 - rm -rf "$git" && mkdir "$git"
59 + test_line_count = 1 lines
60 '
61
62 test_expect_success 'git-p4 clone @all' '
63 "$GITP4" clone --dest="$git" //depot@all &&
64 + test_when_finished cleanup_git &&
65 cd "$git" &&
66 git log --oneline >lines &&
61 - test_line_count = 2 lines &&
62 - cd .. &&
63 - rm -rf "$git" && mkdir "$git"
67 + test_line_count = 2 lines
68 '
69
70 test_expect_success 'git-p4 sync uninitialized repo' '
71 test_create_repo "$git" &&
72 + test_when_finished cleanup_git &&
73 cd "$git" &&
69 - test_must_fail "$GITP4" sync &&
70 - rm -rf "$git" && mkdir "$git"
74 + test_must_fail "$GITP4" sync
75 '
76
77 #
@@ -76,19 +80,18 @@ test_expect_success 'git-p4 sync uninitialized repo' '
80 #
81 test_expect_success 'git-p4 sync new branch' '
82 test_create_repo "$git" &&
83 + test_when_finished cleanup_git &&
84 cd "$git" &&
85 test_commit head &&
86 "$GITP4" sync --branch=refs/remotes/p4/depot //depot@all &&
87 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
89 '
90
91 test_expect_success 'exit when p4 fails to produce marshaled output' '
92 badp4dir="$TRASH_DIRECTORY/badp4dir" &&
93 mkdir -p "$badp4dir" &&
94 + test_when_finished "rm -rf $badp4dir" &&
95 cat >"$badp4dir"/p4 <<-EOF &&
96 #!$SHELL_PATH
97 exit 1
@@ -106,29 +109,26 @@ test_expect_success 'add p4 files with wildcards in the names' '
109 echo file-wild-at >file-wild@at &&
110 echo file-wild-percent >file-wild%percent &&
111 p4 add -f file-wild* &&
109 - p4 submit -d "file wildcards" &&
110 - cd "$TRASH_DIRECTORY"
112 + p4 submit -d "file wildcards"
113 '
114
115 test_expect_success 'wildcard files git-p4 clone' '
116 "$GITP4" clone --dest="$git" //depot &&
117 + test_when_finished cleanup_git &&
118 cd "$git" &&
119 test -f file-wild#hash &&
120 test -f file-wild\*star &&
121 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
123 '
124
125 test_expect_success 'clone bare' '
126 "$GITP4" clone --dest="$git" --bare //depot &&
127 + test_when_finished cleanup_git &&
128 cd "$git" &&
129 test ! -d .git &&
130 bare=`git config --get core.bare` &&
129 - test "$bare" = true &&
130 - cd "$TRASH_DIRECTORY" &&
131 - rm -rf "$git" && mkdir "$git"
131 + test "$bare" = true
132 '
133
134 p4_add_user() {
@@ -173,6 +173,7 @@ test_expect_success 'preserve users' '
173 p4_add_user bob Bob &&
174 p4_grant_admin alice &&
175 "$GITP4" clone --dest="$git" //depot &&
176 + test_when_finished cleanup_git &&
177 cd "$git" &&
178 echo "username: a change by alice" >> file1 &&
179 echo "username: a change by bob" >> file2 &&
@@ -181,27 +182,25 @@ test_expect_success 'preserve users' '
182 git config git-p4.skipSubmitEditCheck true &&
183 P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit --preserve-user &&
184 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
186 '
187
188 # Test username support, submitting as bob, who lacks admin rights. Should
189 # not submit change to p4 (git diff should show deltas).
190 test_expect_success 'refuse to preserve users without perms' '
191 "$GITP4" clone --dest="$git" //depot &&
192 + test_when_finished cleanup_git &&
193 cd "$git" &&
194 echo "username-noperms: a change by alice" >> file1 &&
195 git commit --author "Alice <alice@localhost>" -m "perms: a change by alice" file1 &&
196 ! 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
198 '
199
200 # What happens with unknown author? Without allowMissingP4Users it should fail.
201 test_expect_success 'preserve user where author is unknown to p4' '
202 "$GITP4" clone --dest="$git" //depot &&
203 + test_when_finished cleanup_git &&
204 cd "$git" &&
205 git config git-p4.skipSubmitEditCheck true
206 echo "username-bob: a change by bob" >> file1 &&
@@ -215,9 +214,7 @@ test_expect_success 'preserve user where author is unknown to p4' '
214 git config git-p4.preserveUser true &&
215 P4EDITOR=touch P4USER=alice P4PASSWD=secret "$GITP4" commit &&
216 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
218 '
219
220 # 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' '
223 # Test: warning disabled and user is the same.
224 test_expect_success 'not preserving user with mixed authorship' '
225 "$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
249 '
250
251