diff mbox series

[v6,01/22] fsck tests: refactor one test to use a sub-repo

Message ID patch-v6-01.22-ebe89f65354-20210907T104559Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series fsck: lib-ify object-file.c & better fsck "invalid object" error reporting | expand

Commit Message

Ævar Arnfjörð Bjarmason Sept. 7, 2021, 10:57 a.m. UTC
Refactor one of the fsck tests to use a throwaway repository. It's a
pervasive pattern in t1450-fsck.sh to spend a lot of effort on the
teardown of a tests so we're not leaving corrupt content for the next
test.

We should instead simply use something like this test_create_repo
pattern. It's both less verbose, and makes things easier to debug as a
failing test can have their state left behind under -d without
damaging the state for other tests.

But let's punt on that general refactoring and just change this one
test, I'm going to change it further in subsequent commits.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 t/t1450-fsck.sh | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

Comments

Taylor Blau Sept. 16, 2021, 7:40 p.m. UTC | #1
On Tue, Sep 07, 2021 at 12:57:56PM +0200, Ævar Arnfjörð Bjarmason wrote:
> Refactor one of the fsck tests to use a throwaway repository. It's a
> pervasive pattern in t1450-fsck.sh to spend a lot of effort on the
> teardown of a tests so we're not leaving corrupt content for the next
> test.

OK. I seem to recall you advocating against this pattern elsewhere[1], but
this is a good example of why it can sometimes make writing tests much
easier when not having to reason about what leaks out of running a test.

[1]: https://lore.kernel.org/git/87zgsnj0q0.fsf@evledraar.gmail.com/,
although after re-reading it it looks like you were more focused on the
unnecessary "rm -fr repo" there and not the "git init +
test_when_finished rm -fr" pattern.

> -test_expect_success 'object with bad sha1' '
> -	sha=$(echo blob | git hash-object -w --stdin) &&
> -	old=$(test_oid_to_path "$sha") &&
> -	new=$(dirname $old)/$(test_oid ff_2) &&
> -	sha="$(dirname $new)$(basename $new)" &&
> -	mv .git/objects/$old .git/objects/$new &&
> -	test_when_finished "remove_object $sha" &&
> -	git update-index --add --cacheinfo 100644 $sha foo &&
> -	test_when_finished "git read-tree -u --reset HEAD" &&
> -	tree=$(git write-tree) &&
> -	test_when_finished "remove_object $tree" &&
> -	cmt=$(echo bogus | git commit-tree $tree) &&
> -	test_when_finished "remove_object $cmt" &&
> -	git update-ref refs/heads/bogus $cmt &&
> -	test_when_finished "git update-ref -d refs/heads/bogus" &&
> -
> -	test_must_fail git fsck 2>out &&
> -	test_i18ngrep "$sha.*corrupt" out
> +test_expect_success 'object with hash mismatch' '
> +	git init --bare hash-mismatch &&
> +	(
> +		cd hash-mismatch &&
> +		oid=$(echo blob | git hash-object -w --stdin) &&
> +		old=$(test_oid_to_path "$oid") &&
> +		new=$(dirname $old)/$(test_oid ff_2) &&
> +		oid="$(dirname $new)$(basename $new)" &&
> +		mv objects/$old objects/$new &&
> +		git update-index --add --cacheinfo 100644 $oid foo &&
> +		tree=$(git write-tree) &&
> +		cmt=$(echo bogus | git commit-tree $tree) &&
> +		git update-ref refs/heads/bogus $cmt &&
> +		test_must_fail git fsck 2>out &&
> +		test_i18ngrep "$oid.*corrupt" out
> +	)
>  '

This all looks fine to me. The translation is s/sha/oid and removing all
of the now-unnecessary test_when_finished calls.

But the test_i18ngrep (which isn't new) could probably also stand to get
cleaned up and converted to a normal grep.

Thanks,
Taylor
Ævar Arnfjörð Bjarmason Sept. 17, 2021, 9:27 a.m. UTC | #2
On Thu, Sep 16 2021, Taylor Blau wrote:

> On Tue, Sep 07, 2021 at 12:57:56PM +0200, Ævar Arnfjörð Bjarmason wrote:
>> Refactor one of the fsck tests to use a throwaway repository. It's a
>> pervasive pattern in t1450-fsck.sh to spend a lot of effort on the
>> teardown of a tests so we're not leaving corrupt content for the next
>> test.
>
> OK. I seem to recall you advocating against this pattern elsewhere[1], but
> this is a good example of why it can sometimes make writing tests much
> easier when not having to reason about what leaks out of running a test.
>
> [1]: https://lore.kernel.org/git/87zgsnj0q0.fsf@evledraar.gmail.com/,
> although after re-reading it it looks like you were more focused on the
> unnecessary "rm -fr repo" there and not the "git init +
> test_when_finished rm -fr" pattern.

I was referring to a different pattern there, replied in some detail at
https://lore.kernel.org/git/87y27veeyj.fsf@evledraar.gmail.com/

>> -test_expect_success 'object with bad sha1' '
>> -	sha=$(echo blob | git hash-object -w --stdin) &&
>> -	old=$(test_oid_to_path "$sha") &&
>> -	new=$(dirname $old)/$(test_oid ff_2) &&
>> -	sha="$(dirname $new)$(basename $new)" &&
>> -	mv .git/objects/$old .git/objects/$new &&
>> -	test_when_finished "remove_object $sha" &&
>> -	git update-index --add --cacheinfo 100644 $sha foo &&
>> -	test_when_finished "git read-tree -u --reset HEAD" &&
>> -	tree=$(git write-tree) &&
>> -	test_when_finished "remove_object $tree" &&
>> -	cmt=$(echo bogus | git commit-tree $tree) &&
>> -	test_when_finished "remove_object $cmt" &&
>> -	git update-ref refs/heads/bogus $cmt &&
>> -	test_when_finished "git update-ref -d refs/heads/bogus" &&
>> -
>> -	test_must_fail git fsck 2>out &&
>> -	test_i18ngrep "$sha.*corrupt" out
>> +test_expect_success 'object with hash mismatch' '
>> +	git init --bare hash-mismatch &&
>> +	(
>> +		cd hash-mismatch &&
>> +		oid=$(echo blob | git hash-object -w --stdin) &&
>> +		old=$(test_oid_to_path "$oid") &&
>> +		new=$(dirname $old)/$(test_oid ff_2) &&
>> +		oid="$(dirname $new)$(basename $new)" &&
>> +		mv objects/$old objects/$new &&
>> +		git update-index --add --cacheinfo 100644 $oid foo &&
>> +		tree=$(git write-tree) &&
>> +		cmt=$(echo bogus | git commit-tree $tree) &&
>> +		git update-ref refs/heads/bogus $cmt &&
>> +		test_must_fail git fsck 2>out &&
>> +		test_i18ngrep "$oid.*corrupt" out
>> +	)
>>  '
>
> This all looks fine to me. The translation is s/sha/oid and removing all
> of the now-unnecessary test_when_finished calls.
>
> But the test_i18ngrep (which isn't new) could probably also stand to get
> cleaned up and converted to a normal grep.

Thanks, I missed that one!
diff mbox series

Patch

diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
index 5071ac63a5b..7becab5ba1e 100755
--- a/t/t1450-fsck.sh
+++ b/t/t1450-fsck.sh
@@ -48,24 +48,22 @@  remove_object () {
 	rm "$(sha1_file "$1")"
 }
 
-test_expect_success 'object with bad sha1' '
-	sha=$(echo blob | git hash-object -w --stdin) &&
-	old=$(test_oid_to_path "$sha") &&
-	new=$(dirname $old)/$(test_oid ff_2) &&
-	sha="$(dirname $new)$(basename $new)" &&
-	mv .git/objects/$old .git/objects/$new &&
-	test_when_finished "remove_object $sha" &&
-	git update-index --add --cacheinfo 100644 $sha foo &&
-	test_when_finished "git read-tree -u --reset HEAD" &&
-	tree=$(git write-tree) &&
-	test_when_finished "remove_object $tree" &&
-	cmt=$(echo bogus | git commit-tree $tree) &&
-	test_when_finished "remove_object $cmt" &&
-	git update-ref refs/heads/bogus $cmt &&
-	test_when_finished "git update-ref -d refs/heads/bogus" &&
-
-	test_must_fail git fsck 2>out &&
-	test_i18ngrep "$sha.*corrupt" out
+test_expect_success 'object with hash mismatch' '
+	git init --bare hash-mismatch &&
+	(
+		cd hash-mismatch &&
+		oid=$(echo blob | git hash-object -w --stdin) &&
+		old=$(test_oid_to_path "$oid") &&
+		new=$(dirname $old)/$(test_oid ff_2) &&
+		oid="$(dirname $new)$(basename $new)" &&
+		mv objects/$old objects/$new &&
+		git update-index --add --cacheinfo 100644 $oid foo &&
+		tree=$(git write-tree) &&
+		cmt=$(echo bogus | git commit-tree $tree) &&
+		git update-ref refs/heads/bogus $cmt &&
+		test_must_fail git fsck 2>out &&
+		test_i18ngrep "$oid.*corrupt" out
+	)
 '
 
 test_expect_success 'branch pointing to non-commit' '