Merge branch 'bc/hash-independent-tests'
authorJunio C Hamano <gitster@pobox.com>
Wed, 10 Oct 2018 03:37:16 +0000 (12:37 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Oct 2018 03:37:16 +0000 (12:37 +0900)
Various tests have been updated to make it easier to swap the
hash function used for object identification.

* bc/hash-independent-tests:
t5318: use test_oid for HASH_LEN
t1407: make hash size independent
t1406: make hash-size independent
t1405: make hash size independent
t1400: switch hard-coded object ID to variable
t1006: make hash size independent
t0064: make hash size independent
t0002: abstract away SHA-1 specific constants
t0000: update tests for SHA-256
t0000: use hash translation table
t: add test functions to translate hash-related values

1  2 
t/README
t/t1400-update-ref.sh
t/test-lib-functions.sh
diff --combined t/README
index 3ea6c854606056e1a7d9431dd59518d817768527,eed574dd9b55656cb171a1dc5224ddf850b37fc4..5e48a043cef01b10935574fa302e44679980a3b5
+++ b/t/README
@@@ -319,14 -319,6 +319,14 @@@ GIT_TEST_OE_DELTA_SIZE=<n> exercises th
  path where deltas larger than this limit require extra memory
  allocation for bookkeeping.
  
 +GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES=<boolean> checks that cache-tree
 +records are valid when the index is written out or after a merge. This
 +is mostly to catch missing invalidation. Default is true.
 +
 +GIT_TEST_COMMIT_GRAPH=<boolean>, when true, forces the commit-graph to
 +be written after every 'git commit' command, and overrides the
 +'core.commitGraph' setting to true.
 +
  Naming Tests
  ------------
  
@@@ -814,6 -806,28 +814,28 @@@ library for your script to use
     the symbolic link in the file system and a part that does; then only
     the latter part need be protected by a SYMLINKS prerequisite (see below).
  
+  - test_oid_init
+    This function loads facts and useful object IDs related to the hash
+    algorithm(s) in use from the files in t/oid-info.
+  - test_oid_cache
+    This function reads per-hash algorithm information from standard
+    input (usually a heredoc) in the format described in
+    t/oid-info/README.  This is useful for test-specific values, such as
+    object IDs, which must vary based on the hash algorithm.
+    Certain fixed values, such as hash sizes and common placeholder
+    object IDs, can be loaded with test_oid_init (described above).
+  - test_oid <key>
+    This function looks up a value for the hash algorithm in use, based
+    on the key given.  The value must have been loaded using
+    test_oid_init or test_oid_cache.  Providing an unknown key is an
+    error.
  Prerequisites
  -------------
  
diff --combined t/t1400-update-ref.sh
index 02493f14ba2071bb79d520c1d9c262ccf63c5207,60726506869369cb11e59a0b5faae677e9dfce0f..663f17c5fe4757a9eea8c749cf1a158a05f4a8df
@@@ -346,7 -346,7 +346,7 @@@ test_expect_success "verifying $m's lo
  
  git update-ref $m $D
  cat >.git/logs/$m <<EOF
0000000000000000000000000000000000000000 $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
$Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
  $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
  $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
  $F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
@@@ -807,37 -807,6 +807,37 @@@ test_expect_success 'stdin delete symre
        test_cmp expect actual
  '
  
 +test_expect_success 'stdin update symref works flag --no-deref' '
 +      git symbolic-ref TESTSYMREFONE $b &&
 +      git symbolic-ref TESTSYMREFTWO $b &&
 +      cat >stdin <<-EOF &&
 +      update TESTSYMREFONE $a $b
 +      update TESTSYMREFTWO $a $b
 +      EOF
 +      git update-ref --no-deref --stdin <stdin &&
 +      git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
 +      git rev-parse $a $a >actual &&
 +      test_cmp expect actual &&
 +      git rev-parse $m~1 >expect &&
 +      git rev-parse $b >actual &&
 +      test_cmp expect actual
 +'
 +
 +test_expect_success 'stdin delete symref works flag --no-deref' '
 +      git symbolic-ref TESTSYMREFONE $b &&
 +      git symbolic-ref TESTSYMREFTWO $b &&
 +      cat >stdin <<-EOF &&
 +      delete TESTSYMREFONE $b
 +      delete TESTSYMREFTWO $b
 +      EOF
 +      git update-ref --no-deref --stdin <stdin &&
 +      test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
 +      test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
 +      git rev-parse $m~1 >expect &&
 +      git rev-parse $b >actual &&
 +      test_cmp expect actual
 +'
 +
  test_expect_success 'stdin delete ref works with right old value' '
        echo "delete $b $m~1" >stdin &&
        git update-ref --stdin <stdin &&
diff --combined t/test-lib-functions.sh
index d82fac9d790b9349df908f684e61a03780ac58c8,7293041b1e555d8d133afa1537c3d668e5b1bbba..78d8c3783bd301a3dd2bf7061f04e08368d7ca37
@@@ -42,8 -42,6 +42,8 @@@ test_decode_color () 
                function name(n) {
                        if (n == 0) return "RESET";
                        if (n == 1) return "BOLD";
 +                      if (n == 2) return "FAINT";
 +                      if (n == 3) return "ITALIC";
                        if (n == 7) return "REVERSE";
                        if (n == 30) return "BLACK";
                        if (n == 31) return "RED";
@@@ -1157,3 -1155,72 +1157,72 @@@ depacketize () 
                }
        '
  }
+ # Set the hash algorithm in use to $1.  Only useful when testing the testsuite.
+ test_set_hash () {
+       test_hash_algo="$1"
+ }
+ # Detect the hash algorithm in use.
+ test_detect_hash () {
+       # Currently we only support SHA-1, but in the future this function will
+       # actually detect the algorithm in use.
+       test_hash_algo='sha1'
+ }
+ # Load common hash metadata and common placeholder object IDs for use with
+ # test_oid.
+ test_oid_init () {
+       test -n "$test_hash_algo" || test_detect_hash &&
+       test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
+       test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
+ }
+ # Load key-value pairs from stdin suitable for use with test_oid.  Blank lines
+ # and lines starting with "#" are ignored.  Keys must be shell identifier
+ # characters.
+ #
+ # Examples:
+ # rawsz sha1:20
+ # rawsz sha256:32
+ test_oid_cache () {
+       local tag rest k v &&
+       { test -n "$test_hash_algo" || test_detect_hash; } &&
+       while read tag rest
+       do
+               case $tag in
+               \#*)
+                       continue;;
+               ?*)
+                       # non-empty
+                       ;;
+               *)
+                       # blank line
+                       continue;;
+               esac &&
+               k="${rest%:*}" &&
+               v="${rest#*:}" &&
+               if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null
+               then
+                       error 'bug in the test script: bad hash algorithm'
+               fi &&
+               eval "test_oid_${k}_$tag=\"\$v\""
+       done
+ }
+ # Look up a per-hash value based on a key ($1).  The value must have been loaded
+ # by test_oid_init or test_oid_cache.
+ test_oid () {
+       local var="test_oid_${test_hash_algo}_$1" &&
+       # If the variable is unset, we must be missing an entry for this
+       # key-hash pair, so exit with an error.
+       if eval "test -z \"\${$var+set}\""
+       then
+               error "bug in the test script: undefined key '$1'" >&2
+       fi &&
+       eval "printf '%s' \"\${$var}\""
+ }