diff mbox series

[v2,3/4] test-lib-functions: add helper for trailing hash

Message ID 813e81a058227bd373cec802e443fcd677042fb4.1670862677.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Optionally skip hashing index on write | expand

Commit Message

Derrick Stolee Dec. 12, 2022, 4:31 p.m. UTC
From: Derrick Stolee <derrickstolee@github.com>

It can be helpful to check that a file format with a trailing hash has a
specific hash in the final bytes of a written file. This is made more
apparent by recent changes that allow skipping the hash algorithm and
writing a null hash at the end of the file instead.

Add a new test_trailing_hash helper and use it in t1600 to verify that
index.skipHash=true really does skip the hash computation, since
'git fsck' does not actually verify the hash.

Keep the 'git fsck' call to ensure that any potential future change to
check the index hash does not cause an error in this case.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 t/t1600-index.sh        | 3 +++
 t/test-lib-functions.sh | 8 ++++++++
 2 files changed, 11 insertions(+)

Comments

SZEDER Gábor Dec. 12, 2022, 6:14 p.m. UTC | #1
On Mon, Dec 12, 2022 at 04:31:16PM +0000, Derrick Stolee via GitGitGadget wrote:
> diff --git a/t/t1600-index.sh b/t/t1600-index.sh
> index 45feb0fc5d8..55914bc3506 100755
> --- a/t/t1600-index.sh
> +++ b/t/t1600-index.sh
> @@ -68,6 +68,9 @@ test_expect_success 'out of bounds index.version issues warning' '
>  test_expect_success 'index.skipHash config option' '
>  	rm -f .git/index &&
>  	git -c index.skipHash=true add a &&
> +	test_trailing_hash .git/index >hash &&
> +	echo $(test_oid zero) >expect &&

Nit: test_oid zero >expect

> +	test_cmp expect hash &&
>  	git fsck
>  '
Junio C Hamano Dec. 13, 2022, 12:55 a.m. UTC | #2
SZEDER Gábor <szeder.dev@gmail.com> writes:

>> +	test_trailing_hash .git/index >hash &&
>> +	echo $(test_oid zero) >expect &&
>
> Nit: test_oid zero >expect
>
>> +	test_cmp expect hash &&

Unfortunately they are not equivalent.

Usually we write these helpers to terminate their output with LF,
relying on the fact that terminating LF will be dropped when used in
a command substition, e.g. VAR=$(HELPER), but test_oid deviates from
the pattern and does not give the terminating LF to its output.
SZEDER Gábor Dec. 17, 2022, 5:37 p.m. UTC | #3
On Tue, Dec 13, 2022 at 09:55:47AM +0900, Junio C Hamano wrote:
> SZEDER Gábor <szeder.dev@gmail.com> writes:
> 
> >> +	test_trailing_hash .git/index >hash &&
> >> +	echo $(test_oid zero) >expect &&
> >
> > Nit: test_oid zero >expect
> >
> >> +	test_cmp expect hash &&
> 
> Unfortunately they are not equivalent.
> 
> Usually we write these helpers to terminate their output with LF,
> relying on the fact that terminating LF will be dropped when used in
> a command substition, e.g. VAR=$(HELPER), but test_oid deviates from
> the pattern and does not give the terminating LF to its output.

Oh, indeed.  But why does it omit the trailing LF?!  Alas, 2c02b110da
(t: add test functions to translate hash-related values, 2018-09-13)
doesn't seem to mention anything about it.  However, skimming through
the output of

  git grep 'test_oid ' -- ':/t/*.sh'

it appears that all but three uses of 'test_oid' are in command
substitutions, and those three exceptions are in t0000 checking that
'test_oid' actually works.  So I don't see any benefit of omitting
that trailing LF, but this and similar cases show its drawbacks.

  $ git grep 'echo $(test_oid ' -- ':/t/*.sh'
  t/t1302-repo-version.sh:        echo $(test_oid version) >expect &&
  t/t5313-pack-bounds-checks.sh:  echo $(test_oid oidfff) >file &&
diff mbox series

Patch

diff --git a/t/t1600-index.sh b/t/t1600-index.sh
index 45feb0fc5d8..55914bc3506 100755
--- a/t/t1600-index.sh
+++ b/t/t1600-index.sh
@@ -68,6 +68,9 @@  test_expect_success 'out of bounds index.version issues warning' '
 test_expect_success 'index.skipHash config option' '
 	rm -f .git/index &&
 	git -c index.skipHash=true add a &&
+	test_trailing_hash .git/index >hash &&
+	echo $(test_oid zero) >expect &&
+	test_cmp expect hash &&
 	git fsck
 '
 
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 796093a7b32..60308843f8f 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1875,3 +1875,11 @@  test_cmp_config_output () {
 	sort config-actual >sorted-actual &&
 	test_cmp sorted-expect sorted-actual
 }
+
+# Given a filename, extract its trailing hash as a hex string
+test_trailing_hash () {
+	local file="$1" &&
+	tail -c $(test_oid rawsz) "$file" |
+		test-tool hexdump |
+		sed "s/ //g"
+}