Skip to content

Commit ef83970

Browse files
avargitster
authored andcommitted
cache-tree tests: explicitly test HEAD and index differences
The test code added in 9c4d6c0 (cache-tree: Write updated cache-tree after commit, 2014-07-13) used "ls-files" in lieu of "ls-tree" because it wanted to test the data in the index, since this test is testing the cache-tree extension. Change the test to instead use "ls-tree" for traversal, and then explicitly check how HEAD differs from the index. This is more easily understood, and less fragile as numerous past bug fixes[1][2][3] to the old code we're replacing demonstrate. As an aside this would be a bit easier if empty pathspecs hadn't been made an error in d426430 (pathspec: warn on empty strings as pathspec, 2016-06-22) and 9e4e8a6 (pathspec: die on empty strings as pathspec, 2017-06-06). If that was still allowed this code could be simplified slightly: diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh index 9bf66c9..0b02881f55 100755 --- a/t/t0090-cache-tree.sh +++ b/t/t0090-cache-tree.sh @@ -18,19 +18,18 @@ cmp_cache_tree () { # test-tool dump-cache-tree already verifies that all existing data is # correct. generate_expected_cache_tree () { - pathspec="$1" && - dir="$2${2:+/}" && + pathspec="$1${1:+/}" && git ls-tree --name-only HEAD -- "$pathspec" >files && git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees && - printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) && + printf "SHA %s (%d entries, %d subtrees)\n" "$pathspec" $(wc -l <files) $(wc -l <subtrees) && while read subtree do - generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1 + generate_expected_cache_tree "$subtree" || return 1 done <subtrees } test_cache_tree () { - generate_expected_cache_tree "." >expect && + generate_expected_cache_tree >expect && cmp_cache_tree expect && rm expect actual files subtrees && git status --porcelain -- ':!status' ':!expected.status' >status && 1. c8db708 (t0090: avoid passing empty string to printf %d, 2014-09-30) 2. d69360c (t0090: tweak awk statement for Solaris /usr/xpg4/bin/awk, 2014-12-22) 3. 9b5a9fa (t0090: stop losing return codes of git commands, 2019-11-27) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa6edee commit ef83970

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

t/t0090-cache-tree.sh

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,28 @@ cmp_cache_tree () {
1818
# test-tool dump-cache-tree already verifies that all existing data is
1919
# correct.
2020
generate_expected_cache_tree () {
21-
dir="$1${1:+/}" &&
22-
# ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux
23-
# We want to count only foo because it's the only direct child
24-
git ls-files >files &&
25-
subtrees=$(grep / files|cut -d / -f 1|uniq) &&
26-
subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
27-
entries=$(wc -l <files) &&
28-
printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
29-
for subtree in $subtrees
21+
pathspec="$1" &&
22+
dir="$2${2:+/}" &&
23+
git ls-tree --name-only HEAD -- "$pathspec" >files &&
24+
git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
25+
printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
26+
while read subtree
3027
do
31-
(
32-
cd "$subtree" &&
33-
generate_expected_cache_tree "$dir$subtree"
34-
) || return 1
35-
done
28+
generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
29+
done <subtrees
3630
}
3731

3832
test_cache_tree () {
39-
generate_expected_cache_tree >expect &&
40-
cmp_cache_tree expect
33+
generate_expected_cache_tree "." >expect &&
34+
cmp_cache_tree expect &&
35+
rm expect actual files subtrees &&
36+
git status --porcelain -- ':!status' ':!expected.status' >status &&
37+
if test -n "$1"
38+
then
39+
test_cmp "$1" status
40+
else
41+
test_must_be_empty status
42+
fi
4143
}
4244

4345
test_invalid_cache_tree () {
@@ -126,6 +128,7 @@ test_expect_success 'second commit has cache-tree' '
126128
'
127129

128130
test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
131+
test_when_finished "git reset --hard" &&
129132
cat <<-\EOT >foo.c &&
130133
int foo()
131134
{
@@ -152,7 +155,10 @@ test_expect_success PERL 'commit --interactive gives cache-tree on partial commi
152155
EOT
153156
test_write_lines p 1 "" s n y q |
154157
git commit --interactive -m foo &&
155-
test_cache_tree
158+
cat <<-\EOF >expected.status &&
159+
M foo.c
160+
EOF
161+
test_cache_tree expected.status
156162
'
157163

158164
test_expect_success PERL 'commit -p with shrinking cache-tree' '
@@ -243,7 +249,10 @@ test_expect_success 'partial commit gives cache-tree' '
243249
git add one.t &&
244250
echo "some other change" >two.t &&
245251
git commit two.t -m partial &&
246-
test_cache_tree
252+
cat <<-\EOF >expected.status &&
253+
M one.t
254+
EOF
255+
test_cache_tree expected.status
247256
'
248257

249258
test_expect_success 'no phantom error when switching trees' '

0 commit comments

Comments
 (0)