t5312: create bogus ref as necessary

Some tests in t5312 create an illegally-named ref, and then see how various operations handle it. But between those operations, we also do some more setup (e.g., repacking), and we are subtly depending on how those setup steps react to the illegal ref. To future-proof us against those behaviors changing, let's instead create and clean up our bogus ref on demand in the tests that need it. This has two small extra advantages: - the tests are more stand-alone; we do not need an extra test to clean up the ref before moving on to other parts of the script - the creation and cleanup is together in one helper function. Because these depend on touching the refs in the filesystem directly, they may need to be tweaked for a world with alternate backends (they have not been noticed so far in the reftable work because with a non-file backend the tests don't fail; they simply become uninteresting noops because the broken ref isn't read at all). Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2021 at 14:36 UTC f805844676ac4d6f5def5c2ace1d0430c410e21e
1 file changed +7 -6
t/t5312-prune-corruption.sh
+7 -6
@@ -18,18 +18,23 @@ test_expect_success 'disable reflogs' '
18 git reflog expire --expire=all --all
19 '
20
21 +create_bogus_ref () {
22 + test_when_finished 'rm -f .git/refs/heads/bogus..name' &&
23 + echo $bogus >.git/refs/heads/bogus..name
24 +}
25 +
26 test_expect_success 'create history reachable only from a bogus-named ref' '
27 test_tick && git commit --allow-empty -m main &&
28 base=$(git rev-parse HEAD) &&
29 test_tick && git commit --allow-empty -m bogus &&
30 bogus=$(git rev-parse HEAD) &&
31 git cat-file commit $bogus >saved &&
27 - echo $bogus >.git/refs/heads/bogus..name &&
32 git reset --hard HEAD^
33 '
34
35 test_expect_success 'pruning does not drop bogus object' '
36 test_when_finished "git hash-object -w -t commit saved" &&
37 + create_bogus_ref &&
38 test_might_fail git prune --expire=now &&
39 git cat-file -e $bogus
40 '
@@ -42,17 +47,13 @@ test_expect_success 'put bogus object into pack' '
47 '
48
49 test_expect_success 'destructive repack keeps packed object' '
50 + create_bogus_ref &&
51 test_might_fail git repack -Ad --unpack-unreachable=now &&
52 git cat-file -e $bogus &&
53 test_might_fail git repack -ad &&
54 git cat-file -e $bogus
55 '
56
51 -# subsequent tests will have different corruptions
52 -test_expect_success 'clean up bogus ref' '
53 - rm .git/refs/heads/bogus..name
54 -'
55 -
57 # We create two new objects here, "one" and "two". Our
58 # main branch points to "two", which is deleted,
59 # corrupting the repository. But we'd like to make sure