]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cache-tree tests: explicitly test HEAD and index differences
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Sat, 23 Jan 2021 13:00:40 +0000 (14:00 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 23 Jan 2021 21:25:12 +0000 (13:25 -0800)
The test code added in 9c4d6c0297 (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 d426430e6e (pathspec: warn on empty strings as
pathspec, 2016-06-22) and 9e4e8a64c2 (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 9bf66c9e68..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. c8db708d5d (t0090: avoid passing empty string to printf %d,
   2014-09-30)
2. d69360c6b1 (t0090: tweak awk statement for Solaris
   /usr/xpg4/bin/awk, 2014-12-22)
3. 9b5a9fa60a (t0090: stop losing return codes of git commands,
   2019-11-27)

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t0090-cache-tree.sh

index 5bb4f7544316ac8c0d4c4a75803ca7fb29ae42a7..9bf66c9e68dd7b99434dfeaa19a0cc8a7618f039 100755 (executable)
@@ -18,26 +18,28 @@ cmp_cache_tree () {
 # test-tool dump-cache-tree already verifies that all existing data is
 # correct.
 generate_expected_cache_tree () {
-       dir="$1${1:+/}" &&
-       # ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux
-       # We want to count only foo because it's the only direct child
-       git ls-files >files &&
-       subtrees=$(grep / files|cut -d / -f 1|uniq) &&
-       subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') &&
-       entries=$(wc -l <files) &&
-       printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" &&
-       for subtree in $subtrees
+       pathspec="$1" &&
+       dir="$2${2:+/}" &&
+       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) &&
+       while read subtree
        do
-               (
-                       cd "$subtree" &&
-                       generate_expected_cache_tree "$dir$subtree"
-               ) || return 1
-       done
+               generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+       done <subtrees
 }
 
 test_cache_tree () {
-       generate_expected_cache_tree >expect &&
-       cmp_cache_tree expect
+       generate_expected_cache_tree "." >expect &&
+       cmp_cache_tree expect &&
+       rm expect actual files subtrees &&
+       git status --porcelain -- ':!status' ':!expected.status' >status &&
+       if test -n "$1"
+       then
+               test_cmp "$1" status
+       else
+               test_must_be_empty status
+       fi
 }
 
 test_invalid_cache_tree () {
@@ -126,6 +128,7 @@ test_expect_success 'second commit has cache-tree' '
 '
 
 test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
+       test_when_finished "git reset --hard" &&
        cat <<-\EOT >foo.c &&
        int foo()
        {
@@ -152,7 +155,10 @@ test_expect_success PERL 'commit --interactive gives cache-tree on partial commi
        EOT
        test_write_lines p 1 "" s n y q |
        git commit --interactive -m foo &&
-       test_cache_tree
+       cat <<-\EOF >expected.status &&
+        M foo.c
+       EOF
+       test_cache_tree expected.status
 '
 
 test_expect_success PERL 'commit -p with shrinking cache-tree' '
@@ -243,7 +249,10 @@ test_expect_success 'partial commit gives cache-tree' '
        git add one.t &&
        echo "some other change" >two.t &&
        git commit two.t -m partial &&
-       test_cache_tree
+       cat <<-\EOF >expected.status &&
+       M  one.t
+       EOF
+       test_cache_tree expected.status
 '
 
 test_expect_success 'no phantom error when switching trees' '