diff mbox series

[v2,05/21] t1401-symbolic-ref: avoid direct filesystem access

Message ID 248d9ffe79272c7a6efef64512d72ccb9a91349a.1619519903.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Prepare tests for reftable backend | expand

Commit Message

Han-Wen Nienhuys April 27, 2021, 10:38 a.m. UTC
From: Han-Wen Nienhuys <hanwen@google.com>

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
---
 t/t1401-symbolic-ref.sh | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

Comments

Ævar Arnfjörð Bjarmason May 20, 2021, 3:20 p.m. UTC | #1
On Tue, Apr 27 2021, Han-Wen Nienhuys via GitGitGadget wrote:

> From: Han-Wen Nienhuys <hanwen@google.com>
>
> Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
> ---
>  t/t1401-symbolic-ref.sh | 34 +++++++++++++++++-----------------
>  1 file changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/t/t1401-symbolic-ref.sh b/t/t1401-symbolic-ref.sh
> index a4ebb0b65fec..315a62f78019 100755
> --- a/t/t1401-symbolic-ref.sh
> +++ b/t/t1401-symbolic-ref.sh
> @@ -3,21 +3,20 @@
>  test_description='basic symbolic-ref tests'
>  . ./test-lib.sh
>  
> -# If the tests munging HEAD fail, they can break detection of
> -# the git repo, meaning that further tests will operate on
> -# the surrounding git repo instead of the trash directory.
> -reset_to_sane() {
> -	echo ref: refs/heads/foo >.git/HEAD
> -}
> -
> -test_expect_success 'symbolic-ref writes HEAD' '
> +test_expect_success 'setup' '
>  	git symbolic-ref HEAD refs/heads/foo &&
> -	echo ref: refs/heads/foo >expect &&
> -	test_cmp expect .git/HEAD
> +	test_commit file &&
> +	"$TAR" cf .git.tar .git/
>  '
>  
> -test_expect_success 'symbolic-ref reads HEAD' '
> -	echo refs/heads/foo >expect &&
> +reset_to_sane() {
> +	rm -rf .git &&
> +	"$TAR" xf .git.tar
> +}
> +
> +test_expect_success 'symbolic-ref read/write roundtrip' '
> +	git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
> +	echo refs/heads/read-write-roundtrip >expect &&
>  	git symbolic-ref HEAD >actual &&
>  	test_cmp expect actual
>  '
> @@ -25,12 +24,13 @@ test_expect_success 'symbolic-ref reads HEAD' '
>  test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
>  	test_must_fail git symbolic-ref HEAD foo
>  '
> +
>  reset_to_sane
>  
>  test_expect_success 'symbolic-ref refuses bare sha1' '
> -	echo content >file && git add file && git commit -m one &&
>  	test_must_fail git symbolic-ref HEAD $(git rev-parse HEAD)
>  '
> +
>  reset_to_sane
>  
>  test_expect_success 'HEAD cannot be removed' '
> @@ -42,16 +42,16 @@ reset_to_sane
>  test_expect_success 'symbolic-ref can be deleted' '
>  	git symbolic-ref NOTHEAD refs/heads/foo &&
>  	git symbolic-ref -d NOTHEAD &&
> -	test_path_is_file .git/refs/heads/foo &&
> -	test_path_is_missing .git/NOTHEAD
> +	git rev-parse refs/heads/foo &&
> +	test_must_fail git symbolic-ref NOTHEAD
>  '
>  reset_to_sane
>  
>  test_expect_success 'symbolic-ref can delete dangling symref' '
>  	git symbolic-ref NOTHEAD refs/heads/missing &&
>  	git symbolic-ref -d NOTHEAD &&
> -	test_path_is_missing .git/refs/heads/missing &&
> -	test_path_is_missing .git/NOTHEAD
> +	test_must_fail git rev-parse refs/heads/missing &&
> +	test_must_fail git symbolic-ref NOTHEAD
>  '
>  reset_to_sane

You do end up needing to refactor some rather nasty patterns in this
series, so this isn't on you initially...

But since we're encountering this "reset_to_sane" pattern, can't we just
as easily fix it up with something more obvious than replacing an echo
to a specific ref with a tarring up and untarring of the whole .git each
time?

I.e. something like:

   # setup the .git initially

Then:

   test_when_finished "rm -rf copy" &&
   git clone . copy &&
   # munge the repo and use "git -C copy" for the tests"

?
Han-Wen Nienhuys May 31, 2021, 1:40 p.m. UTC | #2
On Thu, May 20, 2021 at 5:21 PM Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
> But since we're encountering this "reset_to_sane" pattern, can't we just
> as easily fix it up with something more obvious than replacing an echo
> to a specific ref with a tarring up and untarring of the whole .git each
> time?
>
> I.e. something like:
>
>    # setup the .git initially
>
> Then:
>
>    test_when_finished "rm -rf copy" &&
>    git clone . copy &&
>    # munge the repo and use "git -C copy" for the tests"

I think this is actually worse than tarring up the tree. By using
clone, your initial state will be affected by any behavior that
git-clone does that makes sense when the user is a human. For example,
refs/heads/x is renamed to refs/remotes/origin/x. Also, all the
timestamps will be off, which might be a problem if there is
infrastructure (index refreshes?) that uses timestamps. By contrast,
using tar (re)creates a precise snapshot of the initial state.
diff mbox series

Patch

diff --git a/t/t1401-symbolic-ref.sh b/t/t1401-symbolic-ref.sh
index a4ebb0b65fec..315a62f78019 100755
--- a/t/t1401-symbolic-ref.sh
+++ b/t/t1401-symbolic-ref.sh
@@ -3,21 +3,20 @@ 
 test_description='basic symbolic-ref tests'
 . ./test-lib.sh
 
-# If the tests munging HEAD fail, they can break detection of
-# the git repo, meaning that further tests will operate on
-# the surrounding git repo instead of the trash directory.
-reset_to_sane() {
-	echo ref: refs/heads/foo >.git/HEAD
-}
-
-test_expect_success 'symbolic-ref writes HEAD' '
+test_expect_success 'setup' '
 	git symbolic-ref HEAD refs/heads/foo &&
-	echo ref: refs/heads/foo >expect &&
-	test_cmp expect .git/HEAD
+	test_commit file &&
+	"$TAR" cf .git.tar .git/
 '
 
-test_expect_success 'symbolic-ref reads HEAD' '
-	echo refs/heads/foo >expect &&
+reset_to_sane() {
+	rm -rf .git &&
+	"$TAR" xf .git.tar
+}
+
+test_expect_success 'symbolic-ref read/write roundtrip' '
+	git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
+	echo refs/heads/read-write-roundtrip >expect &&
 	git symbolic-ref HEAD >actual &&
 	test_cmp expect actual
 '
@@ -25,12 +24,13 @@  test_expect_success 'symbolic-ref reads HEAD' '
 test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
 	test_must_fail git symbolic-ref HEAD foo
 '
+
 reset_to_sane
 
 test_expect_success 'symbolic-ref refuses bare sha1' '
-	echo content >file && git add file && git commit -m one &&
 	test_must_fail git symbolic-ref HEAD $(git rev-parse HEAD)
 '
+
 reset_to_sane
 
 test_expect_success 'HEAD cannot be removed' '
@@ -42,16 +42,16 @@  reset_to_sane
 test_expect_success 'symbolic-ref can be deleted' '
 	git symbolic-ref NOTHEAD refs/heads/foo &&
 	git symbolic-ref -d NOTHEAD &&
-	test_path_is_file .git/refs/heads/foo &&
-	test_path_is_missing .git/NOTHEAD
+	git rev-parse refs/heads/foo &&
+	test_must_fail git symbolic-ref NOTHEAD
 '
 reset_to_sane
 
 test_expect_success 'symbolic-ref can delete dangling symref' '
 	git symbolic-ref NOTHEAD refs/heads/missing &&
 	git symbolic-ref -d NOTHEAD &&
-	test_path_is_missing .git/refs/heads/missing &&
-	test_path_is_missing .git/NOTHEAD
+	test_must_fail git rev-parse refs/heads/missing &&
+	test_must_fail git symbolic-ref NOTHEAD
 '
 reset_to_sane