Skip to content

Commit 1c6667c

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9ddd5b8 commit 1c6667c

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

t/t1400-update-ref.sh

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ test_description='Test git update-ref and basic ref logging'
99
Z=$ZERO_OID
1010

1111
m=refs/heads/main
12-
n_dir=refs/heads/gu
13-
n=$n_dir/fixes
1412
outside=refs/foo
1513
bare=bare-repo
1614

@@ -62,10 +60,10 @@ test_expect_success "delete $m without oldvalue verification" '
6260
test_must_fail git show-ref --verify -q $m
6361
'
6462

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
6967
'
7068

7169
test_expect_success "create $m (by HEAD)" '
@@ -221,16 +219,16 @@ test_expect_success 'delete symref without dereference when the referred ref is
221219
'
222220

223221
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" &&
224223
git symbolic-ref refs/heads/self refs/heads/self &&
225-
test_when_finished "rm -f .git/refs/heads/self" &&
226224
test_path_is_file .git/refs/heads/self &&
227225
test_must_fail git update-ref -d refs/heads/self &&
228226
test_path_is_file .git/refs/heads/self
229227
'
230228

231229
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" &&
232231
git symbolic-ref refs/heads/self refs/heads/self &&
233-
test_when_finished "rm -f .git/refs/heads/self" &&
234232
test_path_is_file .git/refs/heads/self &&
235233
git update-ref --no-deref -d refs/heads/self &&
236234
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)' '
434432
test_i18ngrep -F "warning: log for ref $m unexpectedly ended on $ld" e
435433
'
436434

437-
rm -f .git/$m .git/logs/$m expect
435+
rm -f expect
436+
git update-ref -d $m
438437

439438
test_expect_success 'creating initial files' '
440439
test_when_finished rm -f M &&

t/t1450-fsck.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test_expect_success setup '
1515
git config --unset i18n.commitencoding &&
1616
git checkout HEAD^0 &&
1717
test_commit B fileB two &&
18+
orig_head=$(git rev-parse HEAD) &&
1819
git tag -d A B &&
1920
git reflog expire --expire=now --all
2021
'
@@ -115,35 +116,33 @@ test_expect_success 'zlib corrupt loose object output ' '
115116
'
116117

117118
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}) &&
119120
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 &&
120122
test_must_fail git fsck 2>out &&
121123
test_i18ngrep "not a commit" out
122124
'
123125

124126
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" &&
127128
echo $ZERO_OID >.git/HEAD &&
128129
# avoid corrupt/broken HEAD from interfering with repo discovery
129130
test_must_fail env GIT_DIR=.git git fsck 2>out &&
130131
test_i18ngrep "detached HEAD points" out
131132
'
132133

133134
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" &&
136136
echo "ref: refs/funny/place" >.git/HEAD &&
137137
# avoid corrupt/broken HEAD from interfering with repo discovery
138138
test_must_fail env GIT_DIR=.git git fsck 2>out &&
139139
test_i18ngrep "HEAD points to something strange" out
140140
'
141141

142142
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" &&
144144
test_when_finished "rm -rf .git/worktrees wt" &&
145145
git worktree add wt &&
146-
mv .git/HEAD .git/SAVED_HEAD &&
147146
echo $ZERO_OID >.git/HEAD &&
148147
# avoid corrupt/broken HEAD from interfering with repo discovery
149148
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' '
161160
test_expect_success 'other worktree HEAD link pointing at missing object' '
162161
test_when_finished "rm -rf .git/worktrees other" &&
163162
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 &&
165165
test_must_fail git fsck 2>out &&
166166
test_i18ngrep "worktrees/other/HEAD: invalid sha1 pointer" out
167167
'
@@ -391,7 +391,7 @@ test_expect_success 'tag pointing to nonexistent' '
391391
392392
tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
393393
test_when_finished "remove_object $tag" &&
394-
echo $tag >.git/refs/tags/invalid &&
394+
git update-ref refs/tags/invalid $tag &&
395395
test_when_finished "git update-ref -d refs/tags/invalid" &&
396396
test_must_fail git fsck --tags >out &&
397397
test_i18ngrep "broken link" out
@@ -411,7 +411,7 @@ test_expect_success 'tag pointing to something else than its type' '
411411
412412
tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
413413
test_when_finished "remove_object $tag" &&
414-
echo $tag >.git/refs/tags/wrong &&
414+
git update-ref refs/tags/wrong $tag &&
415415
test_when_finished "git update-ref -d refs/tags/wrong" &&
416416
test_must_fail git fsck --tags
417417
'
@@ -428,7 +428,7 @@ test_expect_success 'tag with incorrect tag name & missing tagger' '
428428
429429
tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
430430
test_when_finished "remove_object $tag" &&
431-
echo $tag >.git/refs/tags/wrong &&
431+
git update-ref refs/tags/wrong $tag &&
432432
test_when_finished "git update-ref -d refs/tags/wrong" &&
433433
git fsck --tags 2>out &&
434434
@@ -452,7 +452,7 @@ test_expect_success 'tag with bad tagger' '
452452
453453
tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
454454
test_when_finished "remove_object $tag" &&
455-
echo $tag >.git/refs/tags/wrong &&
455+
git update-ref refs/tags/wrong $tag &&
456456
test_when_finished "git update-ref -d refs/tags/wrong" &&
457457
test_must_fail git fsck --tags 2>out &&
458458
test_i18ngrep "error in tag .*: invalid author/committer" out
@@ -471,7 +471,7 @@ test_expect_success 'tag with NUL in header' '
471471
472472
tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
473473
test_when_finished "remove_object $tag" &&
474-
echo $tag >.git/refs/tags/wrong &&
474+
git update-ref refs/tags/wrong $tag &&
475475
test_when_finished "git update-ref -d refs/tags/wrong" &&
476476
test_must_fail git fsck --tags 2>out &&
477477
test_i18ngrep "error in tag $tag.*unterminated header: NUL at offset" out

t/t3404-rebase-interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ test_expect_success '--update-refs: check failed ref update' '
21602160
# recorded in the update-refs file. We will force-update the
21612161
# "second" ref, but "git branch -f" will not work because of
21622162
# the lock in the update-refs file.
2163-
git rev-parse third >.git/refs/heads/second &&
2163+
git update-ref refs/heads/second third &&
21642164
21652165
test_must_fail git rebase --continue 2>err &&
21662166
grep "update_ref failed for ref '\''refs/heads/second'\''" err &&

t/t5526-fetch-submodules.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ test_expect_success 'fetching submodule into a broken repository' '
771771
git -C dst fetch --recurse-submodules &&
772772
773773
# 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 &&
775775
776776
# NOTE: without the fix the following tests will recurse forever!
777777
# They should terminate with an error.

0 commit comments

Comments
 (0)