Skip to content

Commit dbedf8b

Browse files
jrngitster
authored andcommitted
t1450 (fsck): remove dangling objects
The fsck test is generally careful to remove the corrupt objects it inserts, but dangling objects are left behind due to some typos and omissions. It is better to clean up more completely, to simplify the addition of later tests. So: - guard setup and cleanup with test_expect_success to catch typos and errors; - check both stdout and stderr when checking for empty fsck output; - use test_cmp empty file in place of test $(wc -l <file) = 0, for better debugging output when running tests with -v; - add a remove_object () helper and use it to replace broken object removal code that forgot about the fanout in .git/objects; - disable gc.auto, to avoid tripping up object removal if the number of objects ever reaches that threshold. - use test_when_finished to ensure cleanup tasks are run and succeed when tests fail; - add a new final test that no breakage or dangling objects was left behind. While at it, add a brief description to test_description of the history that is expected to persist between tests. Part of a campaign to clean up subshell usage in tests. Cc: Thomas Rast <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 18a8269 commit dbedf8b

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

t/t1450-fsck.sh

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#!/bin/sh
22

3-
test_description='git fsck random collection of tests'
3+
test_description='git fsck random collection of tests
4+
5+
* (HEAD) B
6+
* (master) A
7+
'
48

59
. ./test-lib.sh
610

711
test_expect_success setup '
12+
git config gc.auto 0 &&
813
git config i18n.commitencoding ISO-8859-1 &&
914
test_commit A fileA one &&
1015
git config --unset i18n.commitencoding &&
1116
git checkout HEAD^0 &&
1217
test_commit B fileB two &&
1318
git tag -d A B &&
14-
git reflog expire --expire=now --all
15-
'
16-
17-
test_expect_success 'HEAD is part of refs' '
18-
test 0 = $(git fsck | wc -l)
19+
git reflog expire --expire=now --all &&
20+
>empty
1921
'
2022

2123
test_expect_success 'loose objects borrowed from alternate are not missing' '
@@ -25,110 +27,132 @@ test_expect_success 'loose objects borrowed from alternate are not missing' '
2527
git init &&
2628
echo ../../../.git/objects >.git/objects/info/alternates &&
2729
test_commit C fileC one &&
28-
git fsck >out &&
29-
! grep "missing blob" out
30-
)
30+
git fsck >../out 2>&1
31+
) &&
32+
{
33+
grep -v dangling out >actual ||
34+
:
35+
} &&
36+
test_cmp empty actual
3137
'
3238

33-
test_expect_success 'valid objects appear valid' '
34-
{ git fsck 2>out; true; } &&
35-
! grep error out &&
36-
! grep fatal out
39+
test_expect_success 'HEAD is part of refs, valid objects appear valid' '
40+
git fsck >actual 2>&1 &&
41+
test_cmp empty actual
3742
'
3843

3944
# Corruption tests follow. Make sure to remove all traces of the
4045
# specific corruption you test afterwards, lest a later test trip over
4146
# it.
4247

48+
test_expect_success 'setup: helpers for corruption tests' '
49+
sha1_file() {
50+
echo "$*" | sed "s#..#.git/objects/&/#"
51+
} &&
52+
53+
remove_object() {
54+
file=$(sha1_file "$*") &&
55+
test -e "$file" &&
56+
rm -f "$file"
57+
}
58+
'
59+
4360
test_expect_success 'object with bad sha1' '
4461
sha=$(echo blob | git hash-object -w --stdin) &&
45-
echo $sha &&
4662
old=$(echo $sha | sed "s+^..+&/+") &&
4763
new=$(dirname $old)/ffffffffffffffffffffffffffffffffffffff &&
4864
sha="$(dirname $new)$(basename $new)"
4965
mv .git/objects/$old .git/objects/$new &&
66+
test_when_finished "remove_object $sha" &&
5067
git update-index --add --cacheinfo 100644 $sha foo &&
68+
test_when_finished "git read-tree -u --reset HEAD" &&
5169
tree=$(git write-tree) &&
70+
test_when_finished "remove_object $tree" &&
5271
cmt=$(echo bogus | git commit-tree $tree) &&
72+
test_when_finished "remove_object $cmt" &&
5373
git update-ref refs/heads/bogus $cmt &&
54-
(git fsck 2>out; true) &&
55-
grep "$sha.*corrupt" out &&
56-
rm -f .git/objects/$new &&
57-
git update-ref -d refs/heads/bogus &&
58-
git read-tree -u --reset HEAD
74+
test_when_finished "git update-ref -d refs/heads/bogus" &&
75+
76+
test_might_fail git fsck 2>out &&
77+
cat out &&
78+
grep "$sha.*corrupt" out
5979
'
6080

6181
test_expect_success 'branch pointing to non-commit' '
62-
git rev-parse HEAD^{tree} > .git/refs/heads/invalid &&
82+
git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
83+
test_when_finished "git update-ref -d refs/heads/invalid" &&
6384
git fsck 2>out &&
64-
grep "not a commit" out &&
65-
git update-ref -d refs/heads/invalid
85+
cat out &&
86+
grep "not a commit" out
6687
'
6788

68-
new=nothing
6989
test_expect_success 'email without @ is okay' '
7090
git cat-file commit HEAD >basis &&
7191
sed "s/@/AT/" basis >okay &&
7292
new=$(git hash-object -t commit -w --stdin <okay) &&
73-
echo "$new" &&
93+
test_when_finished "remove_object $new" &&
7494
git update-ref refs/heads/bogus "$new" &&
95+
test_when_finished "git update-ref -d refs/heads/bogus" &&
7596
git fsck 2>out &&
7697
cat out &&
77-
! grep "error in commit $new" out
98+
! grep "commit $new" out
7899
'
79-
git update-ref -d refs/heads/bogus
80-
rm -f ".git/objects/$new"
81100

82-
new=nothing
83101
test_expect_success 'email with embedded > is not okay' '
84102
git cat-file commit HEAD >basis &&
85103
sed "s/@[a-z]/&>/" basis >bad-email &&
86104
new=$(git hash-object -t commit -w --stdin <bad-email) &&
87-
echo "$new" &&
105+
test_when_finished "remove_object $new" &&
88106
git update-ref refs/heads/bogus "$new" &&
107+
test_when_finished "git update-ref -d refs/heads/bogus" &&
89108
git fsck 2>out &&
90109
cat out &&
91110
grep "error in commit $new" out
92111
'
93-
git update-ref -d refs/heads/bogus
94-
rm -f ".git/objects/$new"
95-
96-
cat > invalid-tag <<EOF
97-
object ffffffffffffffffffffffffffffffffffffffff
98-
type commit
99-
tag invalid
100-
tagger T A Gger <[email protected]> 1234567890 -0000
101-
102-
This is an invalid tag.
103-
EOF
104112

105113
test_expect_success 'tag pointing to nonexistent' '
106-
tag=$(git hash-object -t tag -w --stdin < invalid-tag) &&
107-
echo $tag > .git/refs/tags/invalid &&
114+
cat >invalid-tag <<-\EOF
115+
object ffffffffffffffffffffffffffffffffffffffff
116+
type commit
117+
tag invalid
118+
tagger T A Gger <[email protected]> 1234567890 -0000
119+
120+
This is an invalid tag.
121+
EOF
122+
123+
tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
124+
test_when_finished "remove_object $tag" &&
125+
echo $tag >.git/refs/tags/invalid &&
126+
test_when_finished "git update-ref -d refs/tags/invalid" &&
108127
test_must_fail git fsck --tags >out &&
109128
cat out &&
110-
grep "broken link" out &&
111-
rm .git/refs/tags/invalid
129+
grep "broken link" out
112130
'
113131

114-
cat > wrong-tag <<EOF
115-
object $(echo blob | git hash-object -w --stdin)
116-
type commit
117-
tag wrong
118-
tagger T A Gger <[email protected]> 1234567890 -0000
119-
120-
This is an invalid tag.
121-
EOF
122-
123132
test_expect_success 'tag pointing to something else than its type' '
124-
tag=$(git hash-object -t tag -w --stdin < wrong-tag) &&
125-
echo $tag > .git/refs/tags/wrong &&
133+
sha=$(echo blob | git hash-object -w --stdin) &&
134+
test_when_finished "remove_object $sha" &&
135+
cat >wrong-tag <<-EOF &&
136+
object $sha
137+
type commit
138+
tag wrong
139+
tagger T A Gger <[email protected]> 1234567890 -0000
140+
141+
This is an invalid tag.
142+
EOF
143+
144+
tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
145+
test_when_finished "remove_object $tag" &&
146+
echo $tag >.git/refs/tags/wrong &&
147+
test_when_finished "git update-ref -d refs/tags/wrong" &&
126148
test_must_fail git fsck --tags 2>out &&
127149
cat out &&
128-
grep "error in tag.*broken links" out &&
129-
rm .git/refs/tags/wrong
150+
grep "error in tag.*broken links" out
130151
'
131152

132-
153+
test_expect_success 'cleaned up' '
154+
git fsck >actual 2>&1 &&
155+
test_cmp empty actual
156+
'
133157

134158
test_done

0 commit comments

Comments
 (0)