git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/7] modernize t9300: use test_when_finished for clean-up

From
Johannes Sixt <j6t@kdbg.org>
Date
Nov 19, 2015, 19:09 UTC
Message-ID
<3c526bd9b7edfeb117263d785f11c18cd28e5fd7.1447959452.git.j6t@kdbg.org>
In-Reply-To
<cover.1447959452.git.j6t@kdbg.org>

A number of clean-ups of test cases are performed outside of test_expect_success. Replace these cases by using test_when_finished.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 t/t9300-fast-import.sh | 36 ++++++++++++++++--------------------
 1 file changed, 16 insertions(+), 20 deletions(-)
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index c36afdb..adabd68 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -344,9 +344,9 @@ M 755 0000000000000000000000000000000000000001 zero1
 
 INPUT_END
 test_expect_success 'B: fail on invalid blob sha1' '
+	test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
 	test_must_fail git fast-import <input
 '
-rm -f .git/objects/pack_* .git/objects/index_*
 
 cat >input <<INPUT_END
 commit TEMP_TAG
@@ -359,14 +359,13 @@ from refs/heads/master
 
 INPUT_END
 test_expect_success 'B: accept branch name "TEMP_TAG"' '
+	test_when_finished "rm -f .git/TEMP_TAG
+		git gc
+		git prune" &&
 	git fast-import <input &&
 	test -f .git/TEMP_TAG &&
 	test `git rev-parse master` = `git rev-parse TEMP_TAG^`
 '
-rm -f .git/TEMP_TAG
-
-git gc 2>/dev/null >/dev/null
-git prune 2>/dev/null >/dev/null
 
 cat >input <<INPUT_END
 commit refs/heads/empty-committer-1
@@ -376,15 +375,14 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: accept empty committer' '
+	test_when_finished "git update-ref -d refs/heads/empty-committer-1
+		git gc
+		git prune" &&
 	git fast-import <input &&
 	out=$(git fsck) &&
 	echo "$out" &&
 	test -z "$out"
 '
-git update-ref -d refs/heads/empty-committer-1 || true
-
-git gc 2>/dev/null >/dev/null
-git prune 2>/dev/null >/dev/null
 
 cat >input <<INPUT_END
 commit refs/heads/empty-committer-2
@@ -394,15 +392,14 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: accept and fixup committer with no name' '
+	test_when_finished "git update-ref -d refs/heads/empty-committer-2
+		git gc
+		git prune" &&
 	git fast-import <input &&
 	out=$(git fsck) &&
 	echo "$out" &&
 	test -z "$out"
 '
-git update-ref -d refs/heads/empty-committer-2 || true
-
-git gc 2>/dev/null >/dev/null
-git prune 2>/dev/null >/dev/null
 
 cat >input <<INPUT_END
 commit refs/heads/invalid-committer
@@ -412,9 +409,9 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: fail on invalid committer (1)' '
+	test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
 	test_must_fail git fast-import <input
 '
-git update-ref -d refs/heads/invalid-committer || true
 
 cat >input <<INPUT_END
 commit refs/heads/invalid-committer
@@ -424,9 +421,9 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: fail on invalid committer (2)' '
+	test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
 	test_must_fail git fast-import <input
 '
-git update-ref -d refs/heads/invalid-committer || true
 
 cat >input <<INPUT_END
 commit refs/heads/invalid-committer
@@ -436,9 +433,9 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: fail on invalid committer (3)' '
+	test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
 	test_must_fail git fast-import <input
 '
-git update-ref -d refs/heads/invalid-committer || true
 
 cat >input <<INPUT_END
 commit refs/heads/invalid-committer
@@ -448,9 +445,9 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: fail on invalid committer (4)' '
+	test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
 	test_must_fail git fast-import <input
 '
-git update-ref -d refs/heads/invalid-committer || true
 
 cat >input <<INPUT_END
 commit refs/heads/invalid-committer
@@ -460,9 +457,9 @@ empty commit
 COMMIT
 INPUT_END
 test_expect_success 'B: fail on invalid committer (5)' '
+	test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
 	test_must_fail git fast-import <input
 '
-git update-ref -d refs/heads/invalid-committer || true
 
 ###
 ### series C
@@ -914,6 +911,7 @@ g/b/h
 EOF
 
 test_expect_success 'L: nested tree copy does not corrupt deltas' '
+	test_when_finished "git update-ref -d refs/heads/L2" &&
 	git fast-import <input &&
 	git ls-tree L2 g/b/ >tmp &&
 	cat tmp | cut -f 2 >actual &&
@@ -921,8 +919,6 @@ test_expect_success 'L: nested tree copy does not corrupt deltas' '
 	git fsck `git rev-parse L2`
 '
 
-git update-ref -d refs/heads/L2
-
 ###
 ### series M
 ###
-- 
2.6.2.337.ga235d84
Previous: Johannes SixtNext: Johannes Sixt
Message 6 of 10 in “Modernize t9300-fast-import”
  1. 0/7 Modernize t9300-fast-importJohannes Sixt, Nov 19, 2015
  2. 1/7 modernize t9300: single-quote placement and indentationJohannes Sixt, Nov 19, 2015
  3. 2/7 modernize t9300: use test_must_failJohannes Sixt, Nov 19, 2015
  4. 3/7 modernize t9300: use test_must_be_emptyJohannes Sixt, Nov 19, 2015
  5. 4/7 modernize t9300: wrap lines after &&Johannes Sixt, Nov 19, 2015
  6. 5/7 modernize t9300: use test_when_finished for clean-upJohannes Sixt, Nov 19, 2015
  7. 6/7 modernize t9300: mark here-doc words to ignore tab indentationJohannes Sixt, Nov 19, 2015
  8. 7/7 modernize t9300: move test preparations into test_expect_successJohannes Sixt, Nov 19, 2015
  9. Eric SunshineNov 20, 2015
  10. Jeff KingNov 20, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.