Thread (39 messages) flat view 39 messages, 3 authors, 2021-09-22
STALE1828d

Revision v3 of 4 in this series.

Revisions (4)
  1. v1 [diff vs current]
  2. v2 [diff vs current]
  3. v3 current
  4. v4 [diff vs current]

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

From: Ævar Arnfjörð Bjarmason <hidden>
Date: 2021-08-05 10:37:47
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
 t/lib-subtest.sh | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
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 () {
-- 
2.33.0.rc0.635.g0ab9d6d3b5a
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help