diff mbox series

[v3,6/9] test-lib tests: assert no copy/pasted mock test code

Message ID patch-v3-6.9-bc79b29f3c-20210805T103237Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series test-lib tests: split off subtest code in t0000 into lib-subtest.sh | expand

Commit Message

Ævar Arnfjörð Bjarmason Aug. 5, 2021, 10:37 a.m. UTC
In the preceding commit we got rid of a bunch of copy/pasted test code
from t0000-basic.sh in favor of re-using earlier already set up tests.

Since copy/pasting the tests is likely to be something done by mistake
in the future, let's add an assertion that detects whether this has
happened.

Now that we don't provide a unique test_description="" to these tests
we can rely on the content written out being the same in this
case. Let's use the object store to hash it, and attempt to create a
"blob-<CONTENT_OID>" tag. If another test has already written out the
same content we'll fail, and the tag envelope will provide an error
message pointing us in the right direction..

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/lib-subtest.sh | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/t/lib-subtest.sh b/t/lib-subtest.sh
index cdbc2d9332..6857afdaa5 100644
--- a/t/lib-subtest.sh
+++ b/t/lib-subtest.sh
@@ -1,3 +1,34 @@ 
+_assert_unique_sub_test () {
+	name=$1 &&
+
+	# Alert about the copy/paste programming
+	hash=$(git hash-object -w "$name") &&
+	cat >tag.sig <<-EOF &&
+	object $hash
+	type blob
+	tag $hash
+	tagger . <> 0 +0000
+
+	duplicate script detected!
+
+	This test script was already written as:
+
+	$name
+
+	You can just re-use its test code with your own
+	run_sub_test_lib_test*()
+	EOF
+
+	tag=$(git mktag <tag.sig) &&
+	if ! git update-ref refs/tags/blob-$hash $tag $(test_oid zero) 2>/dev/null
+	then
+		msg=$(git for-each-ref refs/tags/blob-$hash \
+			--format='%(contents)' refs/tags/blob-$hash)
+		error "on write of $name: $msg"
+		return 1
+	fi
+}
+
 write_sub_test_lib_test () {
 	name="$1" # stdin is the body of the test code
 	mkdir "$name" &&
@@ -7,7 +38,8 @@  write_sub_test_lib_test () {
 	# Point to the t/test-lib.sh, which isn't in ../ as usual
 	. "\$TEST_DIRECTORY"/test-lib.sh
 	EOF
-	cat >>"$name/$name.sh"
+	cat >>"$name/$name.sh" &&
+	_assert_unique_sub_test "$name/$name.sh"
 }
 
 _run_sub_test_lib_test_common () {