diff mbox series

[v2,1/2] gc: add tests for --cruft and friends

Message ID 35d2c97715008db3957a69a8260e2046c0e5e738.1666815209.git.me@ttaylorr.com (mailing list archive)
State Superseded
Headers show
Series gc: let feature.experimental imply gc.cruftPacks | expand

Commit Message

Taylor Blau Oct. 26, 2022, 8:13 p.m. UTC
From: Emily Shaffer <emilyshaffer@google.com>

In 5b92477f89 (builtin/gc.c: conditionally avoid pruning objects via
loose, 2022-05-20) gc learned to respect '--cruft' and 'gc.cruftPacks'.
'--cruft' is exercised in t5329-pack-objects-cruft.sh, but in a way that
doesn't check whether a lone gc run generates these cruft packs.
'gc.cruftPacks' is never exercised.

Add some tests to exercise these options to gc in the gc test suite.

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
 t/t6500-gc.sh | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

Comments

Junio C Hamano Oct. 26, 2022, 9:03 p.m. UTC | #1
Taylor Blau <me@ttaylorr.com> writes:

> +prepare_cruft_history () {
> +	test_commit base &&
> +
> +	test_commit --no-tag foo &&
> +	test_commit --no-tag bar &&
> +	git reset HEAD^^
> +}

OK.  Three pearls on a single strand, then the tip-two gets rewound
away.

> +assert_cruft_pack_exists () {
> +	find .git/objects/pack -name "*.mtimes" >mtimes &&
> +	sed -e 's/\.mtimes$/\.pack/g' mtimes >packs &&

Somebody recently made comment that we already depend on the fact
that the default action for "find" is "-print".  I do not see the
need for the 'mtimes' intermediary file.  It does not hurt, but it
is not needed.

> +	test_file_not_empty packs &&
> +	while read pack
> +	do
> +		test_path_is_file "$pack" || return 1
> +	done <packs
> +}

OK.  We enumerate .mtimes files and ensure corresponding .pack
exists for each of them.  That might miss a case where we thought we
created a cruft pack (i.e. .pack exists) but somehow failed to
create the matching .mtimes file by mistake, but it is hard to tell
such a "broken cruft pack" file from a normal pack file, so I think
the above is the best we can do.

> +test_expect_success 'gc --cruft generates a cruft pack' '
> +	test_when_finished "rm -fr crufts" &&
> +	git init crufts &&
> +	(
> +		cd crufts &&
> +
> +		prepare_cruft_history &&
> +		git gc --cruft &&
> +		assert_cruft_pack_exists
> +	)
> +'
> +
> +test_expect_success 'gc.cruftPacks=true generates a cruft pack' '
> +	test_when_finished "rm -fr crufts" &&
> +	git init crufts &&
> +	(
> +		cd crufts &&
> +
> +		prepare_cruft_history &&
> +		git -c gc.cruftPacks=true gc &&
> +		assert_cruft_pack_exists
> +	)
> +'

We could instead do away without when-finished clean-up and use
separate test subdirectory.  Consistently doing so may make CI
forensic easier.  I do not think it is necessary in the test history
used here that is too simple to be realisic.  If something breaks at
CI, it is simple enough to reproduce it manually, so I do not think
it is worth a reroll for that.

>  run_and_wait_for_auto_gc () {
>  	# We read stdout from gc for the side effect of waiting until the
>  	# background gc process exits, closing its fd 9.  Furthermore, the
diff mbox series

Patch

diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index cd6c53360d..9110a39088 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -202,6 +202,49 @@  test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "e
 	grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
 '
 
+prepare_cruft_history () {
+	test_commit base &&
+
+	test_commit --no-tag foo &&
+	test_commit --no-tag bar &&
+	git reset HEAD^^
+}
+
+assert_cruft_pack_exists () {
+	find .git/objects/pack -name "*.mtimes" >mtimes &&
+	sed -e 's/\.mtimes$/\.pack/g' mtimes >packs &&
+
+	test_file_not_empty packs &&
+	while read pack
+	do
+		test_path_is_file "$pack" || return 1
+	done <packs
+}
+
+test_expect_success 'gc --cruft generates a cruft pack' '
+	test_when_finished "rm -fr crufts" &&
+	git init crufts &&
+	(
+		cd crufts &&
+
+		prepare_cruft_history &&
+		git gc --cruft &&
+		assert_cruft_pack_exists
+	)
+'
+
+test_expect_success 'gc.cruftPacks=true generates a cruft pack' '
+	test_when_finished "rm -fr crufts" &&
+	git init crufts &&
+	(
+		cd crufts &&
+
+		prepare_cruft_history &&
+		git -c gc.cruftPacks=true gc &&
+		assert_cruft_pack_exists
+	)
+'
+
 run_and_wait_for_auto_gc () {
 	# We read stdout from gc for the side effect of waiting until the
 	# background gc process exits, closing its fd 9.  Furthermore, the