t: convert tests to not write references via the filesystem

Some of our tests manually create, update or delete references by writing the respective new values into the filesystem directly. While this works with the current files reference backend, this will break once we have a second reference backend implementation in our codebase. Refactor these tests to instead use git-update-ref(1) or our `ref-store` test tool. The latter is required in some cases where safety checks of git-update-ref(1) would otherwise reject a reference update. While at it, refactor some of the tests to schedule the cleanup command via `test_when_finished` before modifying the repository. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 2, 2023 at 09:46 UTC 1c6667cb9dab6f90dcc3381676b50b1f131aeda5
4 files changed +23 -24
t/t1400-update-ref.sh
+8 -9
@@ -9,8 +9,6 @@ test_description='Test git update-ref and basic ref logging'
9 Z=$ZERO_OID
10
11 m=refs/heads/main
12 -n_dir=refs/heads/gu
13 -n=$n_dir/fixes
12 outside=refs/foo
13 bare=bare-repo
14
@@ -62,10 +60,10 @@ test_expect_success "delete $m without oldvalue verification" '
60 test_must_fail git show-ref --verify -q $m
61 '
62
65 -test_expect_success "fail to create $n" '
66 - test_when_finished "rm -f .git/$n_dir" &&
67 - touch .git/$n_dir &&
68 - test_must_fail git update-ref $n $A
63 +test_expect_success "fail to create $n due to file/directory conflict" '
64 + test_when_finished "git update-ref -d refs/heads/gu" &&
65 + git update-ref refs/heads/gu $A &&
66 + test_must_fail git update-ref refs/heads/gu/fixes $A
67 '
68
69 test_expect_success "create $m (by HEAD)" '
@@ -221,16 +219,16 @@ test_expect_success 'delete symref without dereference when the referred ref is
219 '
220
221 test_expect_success 'update-ref -d is not confused by self-reference' '
222 + test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
223 git symbolic-ref refs/heads/self refs/heads/self &&
225 - test_when_finished "rm -f .git/refs/heads/self" &&
224 test_path_is_file .git/refs/heads/self &&
225 test_must_fail git update-ref -d refs/heads/self &&
226 test_path_is_file .git/refs/heads/self
227 '
228
229 test_expect_success 'update-ref --no-deref -d can delete self-reference' '
230 + test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF refs/heads/self" &&
231 git symbolic-ref refs/heads/self refs/heads/self &&
233 - test_when_finished "rm -f .git/refs/heads/self" &&
232 test_path_is_file .git/refs/heads/self &&
233 git update-ref --no-deref -d refs/heads/self &&
234 test_must_fail git show-ref --verify -q refs/heads/self
@@ -434,7 +432,8 @@ test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
432 test_i18ngrep -F "warning: log for ref $m unexpectedly ended on $ld" e
433 '
434
437 -rm -f .git/$m .git/logs/$m expect
435 +rm -f expect
436 +git update-ref -d $m
437
438 test_expect_success 'creating initial files' '
439 test_when_finished rm -f M &&
t/t1450-fsck.sh
+13 -13
@@ -15,6 +15,7 @@ test_expect_success setup '
15 git config --unset i18n.commitencoding &&
16 git checkout HEAD^0 &&
17 test_commit B fileB two &&
18 + orig_head=$(git rev-parse HEAD) &&
19 git tag -d A B &&
20 git reflog expire --expire=now --all
21 '
@@ -115,15 +116,15 @@ test_expect_success 'zlib corrupt loose object output ' '
116 '
117
118 test_expect_success 'branch pointing to non-commit' '
118 - git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
119 + tree_oid=$(git rev-parse --verify HEAD^{tree}) &&
120 test_when_finished "git update-ref -d refs/heads/invalid" &&
121 + test-tool ref-store main update-ref msg refs/heads/invalid $tree_oid $ZERO_OID REF_SKIP_OID_VERIFICATION &&
122 test_must_fail git fsck 2>out &&
123 test_i18ngrep "not a commit" out
124 '
125
126 test_expect_success 'HEAD link pointing at a funny object' '
125 - test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
126 - mv .git/HEAD .git/SAVED_HEAD &&
127 + test_when_finished "git update-ref HEAD $orig_head" &&
128 echo $ZERO_OID >.git/HEAD &&
129 # avoid corrupt/broken HEAD from interfering with repo discovery
130 test_must_fail env GIT_DIR=.git git fsck 2>out &&
@@ -131,8 +132,7 @@ test_expect_success 'HEAD link pointing at a funny object' '
132 '
133
134 test_expect_success 'HEAD link pointing at a funny place' '
134 - test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
135 - mv .git/HEAD .git/SAVED_HEAD &&
135 + test_when_finished "git update-ref --no-deref HEAD $orig_head" &&
136 echo "ref: refs/funny/place" >.git/HEAD &&
137 # avoid corrupt/broken HEAD from interfering with repo discovery
138 test_must_fail env GIT_DIR=.git git fsck 2>out &&
@@ -140,10 +140,9 @@ test_expect_success 'HEAD link pointing at a funny place' '
140 '
141
142 test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
143 - test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
143 + test_when_finished "git update-ref HEAD $orig_head" &&
144 test_when_finished "rm -rf .git/worktrees wt" &&
145 git worktree add wt &&
146 - mv .git/HEAD .git/SAVED_HEAD &&
146 echo $ZERO_OID >.git/HEAD &&
147 # avoid corrupt/broken HEAD from interfering with repo discovery
148 test_must_fail git -C wt fsck 2>out &&
@@ -161,7 +160,8 @@ test_expect_success 'other worktree HEAD link pointing at a funny object' '
160 test_expect_success 'other worktree HEAD link pointing at missing object' '
161 test_when_finished "rm -rf .git/worktrees other" &&
162 git worktree add other &&
164 - echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
163 + object_id=$(echo "Contents missing from repo" | git hash-object --stdin) &&
164 + test-tool -C other ref-store main update-ref msg HEAD $object_id "" REF_NO_DEREF,REF_SKIP_OID_VERIFICATION &&
165 test_must_fail git fsck 2>out &&
166 test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
167 '
@@ -391,7 +391,7 @@ test_expect_success 'tag pointing to nonexistent' '
391
392 tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
393 test_when_finished "remove_object $tag" &&
394 - echo $tag >.git/refs/tags/invalid &&
394 + git update-ref refs/tags/invalid $tag &&
395 test_when_finished "git update-ref -d refs/tags/invalid" &&
396 test_must_fail git fsck --tags >out &&
397 test_i18ngrep "broken link" out
@@ -411,7 +411,7 @@ test_expect_success 'tag pointing to something else than its type' '
411
412 tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
413 test_when_finished "remove_object $tag" &&
414 - echo $tag >.git/refs/tags/wrong &&
414 + git update-ref refs/tags/wrong $tag &&
415 test_when_finished "git update-ref -d refs/tags/wrong" &&
416 test_must_fail git fsck --tags
417 '
@@ -428,7 +428,7 @@ test_expect_success 'tag with incorrect tag name & missing tagger' '
428
429 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
430 test_when_finished "remove_object $tag" &&
431 - echo $tag >.git/refs/tags/wrong &&
431 + git update-ref refs/tags/wrong $tag &&
432 test_when_finished "git update-ref -d refs/tags/wrong" &&
433 git fsck --tags 2>out &&
434
@@ -452,7 +452,7 @@ test_expect_success 'tag with bad tagger' '
452
453 tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
454 test_when_finished "remove_object $tag" &&
455 - echo $tag >.git/refs/tags/wrong &&
455 + git update-ref refs/tags/wrong $tag &&
456 test_when_finished "git update-ref -d refs/tags/wrong" &&
457 test_must_fail git fsck --tags 2>out &&
458 test_i18ngrep "error in tag .*: invalid author/committer" out
@@ -471,7 +471,7 @@ test_expect_success 'tag with NUL in header' '
471
472 tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
473 test_when_finished "remove_object $tag" &&
474 - echo $tag >.git/refs/tags/wrong &&
474 + git update-ref refs/tags/wrong $tag &&
475 test_when_finished "git update-ref -d refs/tags/wrong" &&
476 test_must_fail git fsck --tags 2>out &&
477 test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out
t/t3404-rebase-interactive.sh
+1 -1
@@ -2160,7 +2160,7 @@ test_expect_success '--update-refs: check failed ref update' '
2160 # recorded in the update-refs file. We will force-update the
2161 # "second" ref, but "git branch -f" will not work because of
2162 # the lock in the update-refs file.
2163 - git rev-parse third >.git/refs/heads/second &&
2163 + git update-ref refs/heads/second third &&
2164
2165 test_must_fail git rebase --continue 2>err &&
2166 grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
t/t5526-fetch-submodules.sh
+1 -1
@@ -771,7 +771,7 @@ test_expect_success 'fetching submodule into a broken repository' '
771 git -C dst fetch --recurse-submodules &&
772
773 # Break the receiving submodule
774 - rm -f dst/sub/.git/HEAD &&
774 + test-tool -C dst/sub ref-store main delete-refs REF_NO_DEREF msg HEAD &&
775
776 # NOTE: without the fix the following tests will recurse forever!
777 # They should terminate with an error.