Jonathan Nieder | c74c720 | 2013-11-25 13:03:06 -0800 | [diff] [blame] | 1 | # Library of functions shared by all tests scripts, included by |
| 2 | # test-lib.sh. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 3 | # |
| 4 | # Copyright (c) 2005 Junio C Hamano |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation, either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
Josh Soref | d05b08c | 2023-11-24 03:35:13 +0000 | [diff] [blame] | 17 | # along with this program. If not, see https://www.gnu.org/licenses/ . |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 18 | |
| 19 | # The semantics of the editor variables are that of invoking |
| 20 | # sh -c "$EDITOR \"$@\"" files ... |
| 21 | # |
| 22 | # If our trash directory contains shell metacharacters, they will be |
| 23 | # interpreted if we just set $EDITOR directly, so do a little dance with |
| 24 | # environment variables to work around this. |
| 25 | # |
| 26 | # In particular, quoting isn't enough, as the path may contain the same quote |
| 27 | # that we're using. |
| 28 | test_set_editor () { |
| 29 | FAKE_EDITOR="$1" |
| 30 | export FAKE_EDITOR |
| 31 | EDITOR='"$FAKE_EDITOR"' |
| 32 | export EDITOR |
| 33 | } |
| 34 | |
Phillip Wood | 666b6e1 | 2023-02-23 20:55:01 +0000 | [diff] [blame] | 35 | # Like test_set_editor but sets GIT_SEQUENCE_EDITOR instead of EDITOR |
| 36 | test_set_sequence_editor () { |
| 37 | FAKE_SEQUENCE_EDITOR="$1" |
| 38 | export FAKE_SEQUENCE_EDITOR |
| 39 | GIT_SEQUENCE_EDITOR='"$FAKE_SEQUENCE_EDITOR"' |
| 40 | export GIT_SEQUENCE_EDITOR |
| 41 | } |
| 42 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 43 | test_decode_color () { |
| 44 | awk ' |
| 45 | function name(n) { |
| 46 | if (n == 0) return "RESET"; |
| 47 | if (n == 1) return "BOLD"; |
Stefan Beller | 991eb4f | 2018-08-13 18:41:15 -0700 | [diff] [blame] | 48 | if (n == 2) return "FAINT"; |
| 49 | if (n == 3) return "ITALIC"; |
Jeff King | 097b681 | 2017-07-13 10:58:41 -0400 | [diff] [blame] | 50 | if (n == 7) return "REVERSE"; |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 51 | if (n == 30) return "BLACK"; |
| 52 | if (n == 31) return "RED"; |
| 53 | if (n == 32) return "GREEN"; |
| 54 | if (n == 33) return "YELLOW"; |
| 55 | if (n == 34) return "BLUE"; |
| 56 | if (n == 35) return "MAGENTA"; |
| 57 | if (n == 36) return "CYAN"; |
| 58 | if (n == 37) return "WHITE"; |
| 59 | if (n == 40) return "BLACK"; |
| 60 | if (n == 41) return "BRED"; |
| 61 | if (n == 42) return "BGREEN"; |
| 62 | if (n == 43) return "BYELLOW"; |
| 63 | if (n == 44) return "BBLUE"; |
| 64 | if (n == 45) return "BMAGENTA"; |
| 65 | if (n == 46) return "BCYAN"; |
| 66 | if (n == 47) return "BWHITE"; |
| 67 | } |
| 68 | { |
| 69 | while (match($0, /\033\[[0-9;]*m/) != 0) { |
| 70 | printf "%s<", substr($0, 1, RSTART-1); |
| 71 | codes = substr($0, RSTART+2, RLENGTH-3); |
| 72 | if (length(codes) == 0) |
| 73 | printf "%s", name(0) |
| 74 | else { |
| 75 | n = split(codes, ary, ";"); |
| 76 | sep = ""; |
| 77 | for (i = 1; i <= n; i++) { |
| 78 | printf "%s%s", sep, name(ary[i]); |
| 79 | sep = ";" |
| 80 | } |
| 81 | } |
| 82 | printf ">"; |
| 83 | $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1); |
| 84 | } |
| 85 | print |
| 86 | } |
| 87 | ' |
| 88 | } |
| 89 | |
Jeff Hostetler | b249e39 | 2016-08-11 10:46:01 -0400 | [diff] [blame] | 90 | lf_to_nul () { |
| 91 | perl -pe 'y/\012/\000/' |
| 92 | } |
| 93 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 94 | nul_to_q () { |
Jeff King | 94221d2 | 2013-10-28 21:23:03 -0400 | [diff] [blame] | 95 | perl -pe 'y/\000/Q/' |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | q_to_nul () { |
Jeff King | 94221d2 | 2013-10-28 21:23:03 -0400 | [diff] [blame] | 99 | perl -pe 'y/Q/\000/' |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | q_to_cr () { |
| 103 | tr Q '\015' |
| 104 | } |
| 105 | |
| 106 | q_to_tab () { |
| 107 | tr Q '\011' |
| 108 | } |
| 109 | |
Junio C Hamano | 250b3c6 | 2013-03-22 11:10:03 -0700 | [diff] [blame] | 110 | qz_to_tab_space () { |
| 111 | tr QZ '\011\040' |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | append_cr () { |
| 115 | sed -e 's/$/Q/' | tr Q '\015' |
| 116 | } |
| 117 | |
| 118 | remove_cr () { |
| 119 | tr '\015' Q | sed -e 's/Q$//' |
| 120 | } |
| 121 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 122 | # In some bourne shell implementations, the "unset" builtin returns |
| 123 | # nonzero status when a variable to be unset was not set in the first |
| 124 | # place. |
| 125 | # |
| 126 | # Use sane_unset when that should not be considered an error. |
| 127 | |
| 128 | sane_unset () { |
| 129 | unset "$@" |
| 130 | return 0 |
| 131 | } |
| 132 | |
| 133 | test_tick () { |
| 134 | if test -z "${test_tick+set}" |
| 135 | then |
| 136 | test_tick=1112911993 |
| 137 | else |
| 138 | test_tick=$(($test_tick + 60)) |
| 139 | fi |
| 140 | GIT_COMMITTER_DATE="$test_tick -0700" |
| 141 | GIT_AUTHOR_DATE="$test_tick -0700" |
| 142 | export GIT_COMMITTER_DATE GIT_AUTHOR_DATE |
| 143 | } |
| 144 | |
SZEDER Gábor | 59210dd | 2017-03-18 17:14:00 +0100 | [diff] [blame] | 145 | # Stop execution and start a shell. This is useful for debugging tests. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 146 | # |
| 147 | # Be sure to remove all invocations of this command before submitting. |
Philippe Blain | add5240 | 2021-09-06 04:38:59 +0000 | [diff] [blame] | 148 | # WARNING: the shell invoked by this helper does not have the same environment |
| 149 | # as the one running the tests (shell variables and functions are not |
| 150 | # available, and the options below further modify the environment). As such, |
| 151 | # commands copied from a test script might behave differently than when |
| 152 | # running the test. |
| 153 | # |
| 154 | # Usage: test_pause [options] |
| 155 | # -t |
| 156 | # Use your original TERM instead of test-lib.sh's "dumb". |
| 157 | # This usually restores color output in the invoked shell. |
| 158 | # -s |
| 159 | # Invoke $SHELL instead of $TEST_SHELL_PATH. |
| 160 | # -h |
| 161 | # Use your original HOME instead of test-lib.sh's "$TRASH_DIRECTORY". |
| 162 | # This allows you to use your regular shell environment and Git aliases. |
| 163 | # CAUTION: running commands copied from a test script into the paused shell |
| 164 | # might result in files in your HOME being overwritten. |
| 165 | # -a |
| 166 | # Shortcut for -t -s -h |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 167 | |
| 168 | test_pause () { |
Philippe Blain | add5240 | 2021-09-06 04:38:59 +0000 | [diff] [blame] | 169 | PAUSE_TERM=$TERM && |
| 170 | PAUSE_SHELL=$TEST_SHELL_PATH && |
| 171 | PAUSE_HOME=$HOME && |
| 172 | while test $# != 0 |
| 173 | do |
| 174 | case "$1" in |
| 175 | -t) |
| 176 | PAUSE_TERM="$USER_TERM" |
| 177 | ;; |
| 178 | -s) |
| 179 | PAUSE_SHELL="$SHELL" |
| 180 | ;; |
| 181 | -h) |
| 182 | PAUSE_HOME="$USER_HOME" |
| 183 | ;; |
| 184 | -a) |
| 185 | PAUSE_TERM="$USER_TERM" |
| 186 | PAUSE_SHELL="$SHELL" |
| 187 | PAUSE_HOME="$USER_HOME" |
| 188 | ;; |
| 189 | *) |
| 190 | break |
| 191 | ;; |
| 192 | esac |
| 193 | shift |
| 194 | done && |
| 195 | TERM="$PAUSE_TERM" HOME="$PAUSE_HOME" "$PAUSE_SHELL" <&6 >&5 2>&7 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Elijah Newren | 8424364 | 2018-04-24 16:46:45 -0700 | [diff] [blame] | 198 | # Wrap git with a debugger. Adding this to a command can make it easier |
| 199 | # to understand what is going on in a failing test. |
Johannes Schindelin | 6a94088 | 2015-10-30 12:02:56 -0700 | [diff] [blame] | 200 | # |
Philippe Blain | 01c3810 | 2021-09-06 04:39:00 +0000 | [diff] [blame] | 201 | # Usage: debug [options] <git command> |
| 202 | # -d <debugger> |
| 203 | # --debugger=<debugger> |
| 204 | # Use <debugger> instead of GDB |
| 205 | # -t |
| 206 | # Use your original TERM instead of test-lib.sh's "dumb". |
| 207 | # This usually restores color output in the debugger. |
| 208 | # WARNING: the command being debugged might behave differently than when |
| 209 | # running the test. |
| 210 | # |
Elijah Newren | 8424364 | 2018-04-24 16:46:45 -0700 | [diff] [blame] | 211 | # Examples: |
| 212 | # debug git checkout master |
| 213 | # debug --debugger=nemiver git $ARGS |
| 214 | # debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS |
Johannes Schindelin | 6a94088 | 2015-10-30 12:02:56 -0700 | [diff] [blame] | 215 | debug () { |
Philippe Blain | 01c3810 | 2021-09-06 04:39:00 +0000 | [diff] [blame] | 216 | GIT_DEBUGGER=1 && |
| 217 | DEBUG_TERM=$TERM && |
| 218 | while test $# != 0 |
| 219 | do |
| 220 | case "$1" in |
| 221 | -t) |
| 222 | DEBUG_TERM="$USER_TERM" |
| 223 | ;; |
| 224 | -d) |
| 225 | GIT_DEBUGGER="$2" && |
| 226 | shift |
| 227 | ;; |
| 228 | --debugger=*) |
| 229 | GIT_DEBUGGER="${1#*=}" |
| 230 | ;; |
| 231 | *) |
| 232 | break |
| 233 | ;; |
| 234 | esac |
| 235 | shift |
| 236 | done && |
| 237 | |
| 238 | dotfiles=".gdbinit .lldbinit" |
| 239 | |
| 240 | for dotfile in $dotfiles |
| 241 | do |
| 242 | dotfile="$USER_HOME/$dotfile" && |
| 243 | test -f "$dotfile" && cp "$dotfile" "$HOME" || : |
| 244 | done && |
| 245 | |
| 246 | TERM="$DEBUG_TERM" GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 && |
| 247 | |
| 248 | for dotfile in $dotfiles |
| 249 | do |
| 250 | rm -f "$HOME/$dotfile" |
| 251 | done |
Johannes Schindelin | 6a94088 | 2015-10-30 12:02:56 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Patrick Steinhardt | 0497e6c | 2023-10-31 09:16:59 +0100 | [diff] [blame] | 254 | # Usage: test_ref_exists [options] <ref> |
| 255 | # |
| 256 | # -C <dir>: |
| 257 | # Run all git commands in directory <dir> |
| 258 | # |
| 259 | # This helper function checks whether a reference exists. Symrefs or object IDs |
| 260 | # will not be resolved. Can be used to check references with bad names. |
| 261 | test_ref_exists () { |
| 262 | local indir= |
| 263 | |
| 264 | while test $# != 0 |
| 265 | do |
| 266 | case "$1" in |
| 267 | -C) |
| 268 | indir="$2" |
| 269 | shift |
| 270 | ;; |
| 271 | *) |
| 272 | break |
| 273 | ;; |
| 274 | esac |
| 275 | shift |
| 276 | done && |
| 277 | |
| 278 | indir=${indir:+"$indir"/} && |
| 279 | |
| 280 | if test "$#" != 1 |
| 281 | then |
| 282 | BUG "expected exactly one reference" |
| 283 | fi && |
| 284 | |
| 285 | git ${indir:+ -C "$indir"} show-ref --exists "$1" |
| 286 | } |
| 287 | |
| 288 | # Behaves the same as test_ref_exists, except that it checks for the absence of |
| 289 | # a reference. This is preferable to `! test_ref_exists` as this function is |
| 290 | # able to distinguish actually-missing references from other, generic errors. |
| 291 | test_ref_missing () { |
| 292 | test_ref_exists "$@" |
| 293 | case "$?" in |
| 294 | 2) |
| 295 | # This is the good case. |
| 296 | return 0 |
| 297 | ;; |
| 298 | 0) |
| 299 | echo >&4 "test_ref_missing: reference exists" |
| 300 | return 1 |
| 301 | ;; |
| 302 | *) |
| 303 | echo >&4 "test_ref_missing: generic error" |
| 304 | return 1 |
| 305 | ;; |
| 306 | esac |
| 307 | } |
| 308 | |
Ævar Arnfjörð Bjarmason | f21426e | 2021-01-12 21:17:56 +0100 | [diff] [blame] | 309 | # Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]] |
| 310 | # -C <dir>: |
| 311 | # Run all git commands in directory <dir> |
Ævar Arnfjörð Bjarmason | 76b8b8d | 2021-01-12 21:17:57 +0100 | [diff] [blame] | 312 | # --notick |
| 313 | # Do not call test_tick before making a commit |
Ævar Arnfjörð Bjarmason | 3373518 | 2021-01-12 21:17:59 +0100 | [diff] [blame] | 314 | # --append |
Ævar Arnfjörð Bjarmason | cb8fb7f | 2021-05-10 16:19:02 +0200 | [diff] [blame] | 315 | # Use ">>" instead of ">" when writing "<contents>" to "<file>" |
Ævar Arnfjörð Bjarmason | 47c88d1 | 2021-05-10 16:19:06 +0200 | [diff] [blame] | 316 | # --printf |
| 317 | # Use "printf" instead of "echo" when writing "<contents>" to |
| 318 | # "<file>", use this to write escape sequences such as "\0", a |
| 319 | # trailing "\n" won't be added automatically. This option |
| 320 | # supports nothing but the FORMAT of printf(1), i.e. no custom |
| 321 | # ARGUMENT(s). |
Ævar Arnfjörð Bjarmason | 76b8b8d | 2021-01-12 21:17:57 +0100 | [diff] [blame] | 322 | # --signoff |
| 323 | # Invoke "git commit" with --signoff |
Denton Liu | f9f30a0 | 2021-01-14 15:02:40 -0800 | [diff] [blame] | 324 | # --author <author> |
| 325 | # Invoke "git commit" with --author <author> |
Ævar Arnfjörð Bjarmason | 5144219 | 2021-05-10 16:19:03 +0200 | [diff] [blame] | 326 | # --no-tag |
| 327 | # Do not tag the resulting commit |
Ævar Arnfjörð Bjarmason | 6cf8d96 | 2021-05-10 16:19:04 +0200 | [diff] [blame] | 328 | # --annotate |
| 329 | # Create an annotated tag with "--annotate -m <message>". Calls |
| 330 | # test_tick between making the commit and tag, unless --notick |
| 331 | # is given. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 332 | # |
| 333 | # This will commit a file with the given contents and the given commit |
Brandon Casey | 4c99419 | 2013-02-12 02:17:30 -0800 | [diff] [blame] | 334 | # message, and tag the resulting commit with the given tag name. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 335 | # |
Brandon Casey | 4c99419 | 2013-02-12 02:17:30 -0800 | [diff] [blame] | 336 | # <file>, <contents>, and <tag> all default to <message>. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 337 | |
| 338 | test_commit () { |
Philippe Blain | 455f0ad | 2022-10-21 15:13:31 +0000 | [diff] [blame] | 339 | local notick= && |
| 340 | local echo=echo && |
| 341 | local append= && |
| 342 | local author= && |
| 343 | local signoff= && |
| 344 | local indir= && |
| 345 | local tag=light && |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 346 | while test $# != 0 |
| 347 | do |
| 348 | case "$1" in |
| 349 | --notick) |
| 350 | notick=yes |
| 351 | ;; |
Ævar Arnfjörð Bjarmason | 47c88d1 | 2021-05-10 16:19:06 +0200 | [diff] [blame] | 352 | --printf) |
| 353 | echo=printf |
| 354 | ;; |
Ævar Arnfjörð Bjarmason | 3373518 | 2021-01-12 21:17:59 +0100 | [diff] [blame] | 355 | --append) |
| 356 | append=yes |
| 357 | ;; |
Ævar Arnfjörð Bjarmason | 999cfc4 | 2021-01-12 21:17:58 +0100 | [diff] [blame] | 358 | --author) |
| 359 | author="$2" |
| 360 | shift |
| 361 | ;; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 362 | --signoff) |
| 363 | signoff="$1" |
| 364 | ;; |
Abhishek Kumar | e8b6300 | 2021-01-16 18:11:15 +0000 | [diff] [blame] | 365 | --date) |
| 366 | notick=yes |
| 367 | GIT_COMMITTER_DATE="$2" |
| 368 | GIT_AUTHOR_DATE="$2" |
| 369 | shift |
| 370 | ;; |
Stefan Beller | 6f94351 | 2016-12-08 13:03:26 -0800 | [diff] [blame] | 371 | -C) |
| 372 | indir="$2" |
| 373 | shift |
| 374 | ;; |
Jeff King | 3803a3a | 2021-02-09 05:52:45 -0500 | [diff] [blame] | 375 | --no-tag) |
Ævar Arnfjörð Bjarmason | 6cf8d96 | 2021-05-10 16:19:04 +0200 | [diff] [blame] | 376 | tag=none |
| 377 | ;; |
| 378 | --annotate) |
| 379 | tag=annotate |
Jeff King | 3803a3a | 2021-02-09 05:52:45 -0500 | [diff] [blame] | 380 | ;; |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 381 | *) |
| 382 | break |
| 383 | ;; |
| 384 | esac |
Junio C Hamano | 9a0231b | 2012-07-22 12:54:08 -0700 | [diff] [blame] | 385 | shift |
Miklos Vajna | 5ed75e2 | 2012-09-14 08:52:03 +0200 | [diff] [blame] | 386 | done && |
Stefan Beller | 6f94351 | 2016-12-08 13:03:26 -0800 | [diff] [blame] | 387 | indir=${indir:+"$indir"/} && |
Junio C Hamano | e97f4a6 | 2024-04-05 17:09:01 -0700 | [diff] [blame] | 388 | local file="${2:-"$1.t"}" && |
Ævar Arnfjörð Bjarmason | 3373518 | 2021-01-12 21:17:59 +0100 | [diff] [blame] | 389 | if test -n "$append" |
| 390 | then |
Ævar Arnfjörð Bjarmason | 47c88d1 | 2021-05-10 16:19:06 +0200 | [diff] [blame] | 391 | $echo "${3-$1}" >>"$indir$file" |
Ævar Arnfjörð Bjarmason | 3373518 | 2021-01-12 21:17:59 +0100 | [diff] [blame] | 392 | else |
Ævar Arnfjörð Bjarmason | 47c88d1 | 2021-05-10 16:19:06 +0200 | [diff] [blame] | 393 | $echo "${3-$1}" >"$indir$file" |
Ævar Arnfjörð Bjarmason | 3373518 | 2021-01-12 21:17:59 +0100 | [diff] [blame] | 394 | fi && |
Ævar Arnfjörð Bjarmason | e3c3675 | 2022-03-17 19:08:39 +0100 | [diff] [blame] | 395 | git ${indir:+ -C "$indir"} add -- "$file" && |
Junio C Hamano | 9a0231b | 2012-07-22 12:54:08 -0700 | [diff] [blame] | 396 | if test -z "$notick" |
| 397 | then |
| 398 | test_tick |
| 399 | fi && |
Ævar Arnfjörð Bjarmason | 999cfc4 | 2021-01-12 21:17:58 +0100 | [diff] [blame] | 400 | git ${indir:+ -C "$indir"} commit \ |
| 401 | ${author:+ --author "$author"} \ |
| 402 | $signoff -m "$1" && |
Ævar Arnfjörð Bjarmason | 6cf8d96 | 2021-05-10 16:19:04 +0200 | [diff] [blame] | 403 | case "$tag" in |
| 404 | none) |
| 405 | ;; |
| 406 | light) |
Jeff King | 3803a3a | 2021-02-09 05:52:45 -0500 | [diff] [blame] | 407 | git ${indir:+ -C "$indir"} tag "${4:-$1}" |
Ævar Arnfjörð Bjarmason | 6cf8d96 | 2021-05-10 16:19:04 +0200 | [diff] [blame] | 408 | ;; |
| 409 | annotate) |
| 410 | if test -z "$notick" |
| 411 | then |
| 412 | test_tick |
| 413 | fi && |
| 414 | git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}" |
| 415 | ;; |
| 416 | esac |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | # Call test_merge with the arguments "<message> <commit>", where <commit> |
| 420 | # can be a tag pointing to the commit-to-merge. |
| 421 | |
| 422 | test_merge () { |
Denton Liu | 94ba151 | 2019-10-03 17:23:13 -0700 | [diff] [blame] | 423 | label="$1" && |
| 424 | shift && |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 425 | test_tick && |
Denton Liu | 94ba151 | 2019-10-03 17:23:13 -0700 | [diff] [blame] | 426 | git merge -m "$label" "$@" && |
| 427 | git tag "$label" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 428 | } |
| 429 | |
Jeff King | b1c36cb | 2019-07-02 01:16:49 -0400 | [diff] [blame] | 430 | # Efficiently create <nr> commits, each with a unique number (from 1 to <nr> |
| 431 | # by default) in the commit message. |
| 432 | # |
| 433 | # Usage: test_commit_bulk [options] <nr> |
| 434 | # -C <dir>: |
| 435 | # Run all git commands in directory <dir> |
| 436 | # --ref=<n>: |
| 437 | # ref on which to create commits (default: HEAD) |
| 438 | # --start=<n>: |
| 439 | # number commit messages from <n> (default: 1) |
| 440 | # --message=<msg>: |
| 441 | # use <msg> as the commit mesasge (default: "commit %s") |
| 442 | # --filename=<fn>: |
| 443 | # modify <fn> in each commit (default: %s.t) |
| 444 | # --contents=<string>: |
| 445 | # place <string> in each file (default: "content %s") |
| 446 | # --id=<string>: |
| 447 | # shorthand to use <string> and %s in message, filename, and contents |
| 448 | # |
| 449 | # The message, filename, and contents strings are evaluated by printf, with the |
| 450 | # first "%s" replaced by the current commit number. So you can do: |
| 451 | # |
| 452 | # test_commit_bulk --filename=file --contents="modification %s" |
| 453 | # |
| 454 | # to have every commit touch the same file, but with unique content. |
| 455 | # |
| 456 | test_commit_bulk () { |
| 457 | tmpfile=.bulk-commit.input |
| 458 | indir=. |
| 459 | ref=HEAD |
| 460 | n=1 |
Taylor Blau | 8e41468 | 2024-05-23 17:27:08 -0400 | [diff] [blame] | 461 | notick= |
Jeff King | b1c36cb | 2019-07-02 01:16:49 -0400 | [diff] [blame] | 462 | message='commit %s' |
| 463 | filename='%s.t' |
| 464 | contents='content %s' |
| 465 | while test $# -gt 0 |
| 466 | do |
| 467 | case "$1" in |
| 468 | -C) |
| 469 | indir=$2 |
| 470 | shift |
| 471 | ;; |
| 472 | --ref=*) |
| 473 | ref=${1#--*=} |
| 474 | ;; |
| 475 | --start=*) |
| 476 | n=${1#--*=} |
| 477 | ;; |
| 478 | --message=*) |
| 479 | message=${1#--*=} |
| 480 | ;; |
| 481 | --filename=*) |
| 482 | filename=${1#--*=} |
| 483 | ;; |
| 484 | --contents=*) |
| 485 | contents=${1#--*=} |
| 486 | ;; |
| 487 | --id=*) |
| 488 | message="${1#--*=} %s" |
| 489 | filename="${1#--*=}-%s.t" |
| 490 | contents="${1#--*=} %s" |
| 491 | ;; |
Taylor Blau | 8e41468 | 2024-05-23 17:27:08 -0400 | [diff] [blame] | 492 | --notick) |
| 493 | notick=yes |
| 494 | ;; |
Jeff King | b1c36cb | 2019-07-02 01:16:49 -0400 | [diff] [blame] | 495 | -*) |
| 496 | BUG "invalid test_commit_bulk option: $1" |
| 497 | ;; |
| 498 | *) |
| 499 | break |
| 500 | ;; |
| 501 | esac |
| 502 | shift |
| 503 | done |
| 504 | total=$1 |
| 505 | |
| 506 | add_from= |
SZEDER Gábor | fc42f20 | 2019-11-25 13:59:07 +0100 | [diff] [blame] | 507 | if git -C "$indir" rev-parse --quiet --verify "$ref" |
Jeff King | b1c36cb | 2019-07-02 01:16:49 -0400 | [diff] [blame] | 508 | then |
| 509 | add_from=t |
| 510 | fi |
| 511 | |
| 512 | while test "$total" -gt 0 |
| 513 | do |
Taylor Blau | 8e41468 | 2024-05-23 17:27:08 -0400 | [diff] [blame] | 514 | if test -z "$notick" |
| 515 | then |
| 516 | test_tick |
| 517 | fi && |
Jeff King | b1c36cb | 2019-07-02 01:16:49 -0400 | [diff] [blame] | 518 | echo "commit $ref" |
| 519 | printf 'author %s <%s> %s\n' \ |
| 520 | "$GIT_AUTHOR_NAME" \ |
| 521 | "$GIT_AUTHOR_EMAIL" \ |
| 522 | "$GIT_AUTHOR_DATE" |
| 523 | printf 'committer %s <%s> %s\n' \ |
| 524 | "$GIT_COMMITTER_NAME" \ |
| 525 | "$GIT_COMMITTER_EMAIL" \ |
| 526 | "$GIT_COMMITTER_DATE" |
| 527 | echo "data <<EOF" |
| 528 | printf "$message\n" $n |
| 529 | echo "EOF" |
| 530 | if test -n "$add_from" |
| 531 | then |
| 532 | echo "from $ref^0" |
| 533 | add_from= |
| 534 | fi |
| 535 | printf "M 644 inline $filename\n" $n |
| 536 | echo "data <<EOF" |
| 537 | printf "$contents\n" $n |
| 538 | echo "EOF" |
| 539 | echo |
| 540 | n=$((n + 1)) |
| 541 | total=$((total - 1)) |
| 542 | done >"$tmpfile" |
| 543 | |
| 544 | git -C "$indir" \ |
| 545 | -c fastimport.unpacklimit=0 \ |
| 546 | fast-import <"$tmpfile" || return 1 |
| 547 | |
| 548 | # This will be left in place on failure, which may aid debugging. |
| 549 | rm -f "$tmpfile" |
| 550 | |
| 551 | # If we updated HEAD, then be nice and update the index and working |
| 552 | # tree, too. |
| 553 | if test "$ref" = "HEAD" |
| 554 | then |
| 555 | git -C "$indir" checkout -f HEAD || return 1 |
| 556 | fi |
| 557 | |
| 558 | } |
| 559 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 560 | # This function helps systems where core.filemode=false is set. |
| 561 | # Use it instead of plain 'chmod +x' to set or unset the executable bit |
| 562 | # of a file in the working directory and add it to the index. |
| 563 | |
| 564 | test_chmod () { |
| 565 | chmod "$@" && |
| 566 | git update-index --add "--chmod=$@" |
| 567 | } |
| 568 | |
Matheus Tavares | ea8bbf2 | 2021-01-05 12:47:39 -0300 | [diff] [blame] | 569 | # Get the modebits from a file or directory, ignoring the setgid bit (g+s). |
| 570 | # This bit is inherited by subdirectories at their creation. So we remove it |
| 571 | # from the returning string to prevent callers from having to worry about the |
| 572 | # state of the bit in the test directory. |
| 573 | # |
Christian Couder | 73de1c9 | 2017-06-25 06:34:28 +0200 | [diff] [blame] | 574 | test_modebits () { |
Matheus Tavares | ea8bbf2 | 2021-01-05 12:47:39 -0300 | [diff] [blame] | 575 | ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \ |
| 576 | -e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|' |
Christian Couder | 73de1c9 | 2017-06-25 06:34:28 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 579 | # Unset a configuration variable, but don't fail if it doesn't exist. |
| 580 | test_unconfig () { |
John Keeping | 5fafc07 | 2015-09-05 14:12:47 +0100 | [diff] [blame] | 581 | config_dir= |
| 582 | if test "$1" = -C |
| 583 | then |
| 584 | shift |
| 585 | config_dir=$1 |
| 586 | shift |
| 587 | fi |
| 588 | git ${config_dir:+-C "$config_dir"} config --unset-all "$@" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 589 | config_status=$? |
| 590 | case "$config_status" in |
| 591 | 5) # ok, nothing to unset |
| 592 | config_status=0 |
| 593 | ;; |
| 594 | esac |
| 595 | return $config_status |
| 596 | } |
| 597 | |
| 598 | # Set git config, automatically unsetting it after the test is over. |
| 599 | test_config () { |
John Keeping | 5fafc07 | 2015-09-05 14:12:47 +0100 | [diff] [blame] | 600 | config_dir= |
| 601 | if test "$1" = -C |
| 602 | then |
| 603 | shift |
| 604 | config_dir=$1 |
| 605 | shift |
| 606 | fi |
Victoria Dye | 847d002 | 2023-05-26 01:32:58 +0000 | [diff] [blame] | 607 | |
| 608 | # If --worktree is provided, use it to configure/unconfigure |
| 609 | is_worktree= |
| 610 | if test "$1" = --worktree |
| 611 | then |
| 612 | is_worktree=1 |
| 613 | shift |
| 614 | fi |
| 615 | |
| 616 | test_when_finished "test_unconfig ${config_dir:+-C '$config_dir'} ${is_worktree:+--worktree} '$1'" && |
| 617 | git ${config_dir:+-C "$config_dir"} config ${is_worktree:+--worktree} "$@" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | test_config_global () { |
| 621 | test_when_finished "test_unconfig --global '$1'" && |
| 622 | git config --global "$@" |
| 623 | } |
| 624 | |
| 625 | write_script () { |
| 626 | { |
| 627 | echo "#!${2-"$SHELL_PATH"}" && |
| 628 | cat |
| 629 | } >"$1" && |
| 630 | chmod +x "$1" |
| 631 | } |
| 632 | |
Ævar Arnfjörð Bjarmason | 7da7f63 | 2022-03-17 11:13:06 +0100 | [diff] [blame] | 633 | # Usage: test_hook [options] <hook-name> <<-\EOF |
| 634 | # |
| 635 | # -C <dir>: |
| 636 | # Run all git commands in directory <dir> |
| 637 | # --setup |
| 638 | # Setup a hook for subsequent tests, i.e. don't remove it in a |
| 639 | # "test_when_finished" |
| 640 | # --clobber |
| 641 | # Overwrite an existing <hook-name>, if it exists. Implies |
| 642 | # --setup (i.e. the "test_when_finished" is assumed to have been |
| 643 | # set up already). |
Ævar Arnfjörð Bjarmason | 66865d1 | 2022-03-17 11:13:16 +0100 | [diff] [blame] | 644 | # --disable |
| 645 | # Disable (chmod -x) an existing <hook-name>, which must exist. |
| 646 | # --remove |
| 647 | # Remove (rm -f) an existing <hook-name>, which must exist. |
Ævar Arnfjörð Bjarmason | 7da7f63 | 2022-03-17 11:13:06 +0100 | [diff] [blame] | 648 | test_hook () { |
| 649 | setup= && |
| 650 | clobber= && |
Ævar Arnfjörð Bjarmason | 66865d1 | 2022-03-17 11:13:16 +0100 | [diff] [blame] | 651 | disable= && |
| 652 | remove= && |
Ævar Arnfjörð Bjarmason | 7da7f63 | 2022-03-17 11:13:06 +0100 | [diff] [blame] | 653 | indir= && |
| 654 | while test $# != 0 |
| 655 | do |
| 656 | case "$1" in |
| 657 | -C) |
| 658 | indir="$2" && |
| 659 | shift |
| 660 | ;; |
| 661 | --setup) |
| 662 | setup=t |
| 663 | ;; |
| 664 | --clobber) |
| 665 | clobber=t |
| 666 | ;; |
Ævar Arnfjörð Bjarmason | 66865d1 | 2022-03-17 11:13:16 +0100 | [diff] [blame] | 667 | --disable) |
| 668 | disable=t |
| 669 | ;; |
| 670 | --remove) |
| 671 | remove=t |
| 672 | ;; |
Ævar Arnfjörð Bjarmason | 7da7f63 | 2022-03-17 11:13:06 +0100 | [diff] [blame] | 673 | -*) |
| 674 | BUG "invalid argument: $1" |
| 675 | ;; |
| 676 | *) |
| 677 | break |
| 678 | ;; |
| 679 | esac && |
| 680 | shift |
| 681 | done && |
| 682 | |
| 683 | git_dir=$(git -C "$indir" rev-parse --absolute-git-dir) && |
| 684 | hook_dir="$git_dir/hooks" && |
| 685 | hook_file="$hook_dir/$1" && |
Ævar Arnfjörð Bjarmason | 66865d1 | 2022-03-17 11:13:16 +0100 | [diff] [blame] | 686 | if test -n "$disable$remove" |
| 687 | then |
| 688 | test_path_is_file "$hook_file" && |
| 689 | if test -n "$disable" |
| 690 | then |
| 691 | chmod -x "$hook_file" |
| 692 | elif test -n "$remove" |
| 693 | then |
| 694 | rm -f "$hook_file" |
| 695 | fi && |
| 696 | return 0 |
| 697 | fi && |
Ævar Arnfjörð Bjarmason | 7da7f63 | 2022-03-17 11:13:06 +0100 | [diff] [blame] | 698 | if test -z "$clobber" |
| 699 | then |
| 700 | test_path_is_missing "$hook_file" |
| 701 | fi && |
| 702 | if test -z "$setup$clobber" |
| 703 | then |
| 704 | test_when_finished "rm \"$hook_file\"" |
| 705 | fi && |
| 706 | write_script "$hook_file" |
| 707 | } |
| 708 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 709 | # Use test_set_prereq to tell that a particular prerequisite is available. |
| 710 | # The prerequisite can later be checked for in two ways: |
| 711 | # |
| 712 | # - Explicitly using test_have_prereq. |
| 713 | # |
| 714 | # - Implicitly by specifying the prerequisite tag in the calls to |
Ævar Arnfjörð Bjarmason | 5beca49 | 2022-07-28 01:13:37 +0200 | [diff] [blame] | 715 | # test_expect_{success,failure} |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 716 | # |
| 717 | # The single parameter is the prerequisite tag (a simple word, in all |
| 718 | # capital letters by convention). |
| 719 | |
Johannes Schindelin | 7d0ee47 | 2018-04-29 00:33:36 +0200 | [diff] [blame] | 720 | test_unset_prereq () { |
| 721 | ! test_have_prereq "$1" || |
| 722 | satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }" |
| 723 | } |
| 724 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 725 | test_set_prereq () { |
Ævar Arnfjörð Bjarmason | c740039 | 2019-06-21 12:18:12 +0200 | [diff] [blame] | 726 | if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL" |
Ævar Arnfjörð Bjarmason | dfe1a17 | 2019-05-13 20:32:42 +0200 | [diff] [blame] | 727 | then |
| 728 | case "$1" in |
| 729 | # The "!" case is handled below with |
| 730 | # test_unset_prereq() |
| 731 | !*) |
| 732 | ;; |
Derrick Stolee | 0011f94 | 2022-07-19 18:32:16 +0000 | [diff] [blame] | 733 | # List of things we can't easily pretend to not support |
Ævar Arnfjörð Bjarmason | dfe1a17 | 2019-05-13 20:32:42 +0200 | [diff] [blame] | 734 | SYMLINKS) |
| 735 | ;; |
| 736 | # Inspecting whether GIT_TEST_FAIL_PREREQS is on |
| 737 | # should be unaffected. |
| 738 | FAIL_PREREQS) |
| 739 | ;; |
| 740 | *) |
| 741 | return |
| 742 | esac |
| 743 | fi |
| 744 | |
Johannes Schindelin | 7d0ee47 | 2018-04-29 00:33:36 +0200 | [diff] [blame] | 745 | case "$1" in |
| 746 | !*) |
| 747 | test_unset_prereq "${1#!}" |
| 748 | ;; |
| 749 | *) |
| 750 | satisfied_prereq="$satisfied_prereq$1 " |
| 751 | ;; |
| 752 | esac |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 753 | } |
Junio C Hamano | f3cfc3b | 2012-07-26 13:57:56 -0700 | [diff] [blame] | 754 | satisfied_prereq=" " |
Junio C Hamano | 04083f2 | 2012-07-26 15:50:45 -0700 | [diff] [blame] | 755 | lazily_testable_prereq= lazily_tested_prereq= |
| 756 | |
| 757 | # Usage: test_lazy_prereq PREREQ 'script' |
| 758 | test_lazy_prereq () { |
| 759 | lazily_testable_prereq="$lazily_testable_prereq$1 " |
| 760 | eval test_prereq_lazily_$1=\$2 |
| 761 | } |
| 762 | |
| 763 | test_run_lazy_prereq_ () { |
| 764 | script=' |
SZEDER Gábor | 53ff3b9 | 2020-11-18 20:04:13 +0100 | [diff] [blame] | 765 | mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" && |
Junio C Hamano | 04083f2 | 2012-07-26 15:50:45 -0700 | [diff] [blame] | 766 | ( |
SZEDER Gábor | 53ff3b9 | 2020-11-18 20:04:13 +0100 | [diff] [blame] | 767 | cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"' |
Junio C Hamano | 04083f2 | 2012-07-26 15:50:45 -0700 | [diff] [blame] | 768 | )' |
| 769 | say >&3 "checking prerequisite: $1" |
| 770 | say >&3 "$script" |
| 771 | test_eval_ "$script" |
| 772 | eval_ret=$? |
SZEDER Gábor | 53ff3b9 | 2020-11-18 20:04:13 +0100 | [diff] [blame] | 773 | rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1" |
Junio C Hamano | 04083f2 | 2012-07-26 15:50:45 -0700 | [diff] [blame] | 774 | if test "$eval_ret" = 0; then |
| 775 | say >&3 "prerequisite $1 ok" |
| 776 | else |
| 777 | say >&3 "prerequisite $1 not satisfied" |
| 778 | fi |
| 779 | return $eval_ret |
| 780 | } |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 781 | |
| 782 | test_have_prereq () { |
| 783 | # prerequisites can be concatenated with ',' |
| 784 | save_IFS=$IFS |
| 785 | IFS=, |
| 786 | set -- $* |
| 787 | IFS=$save_IFS |
| 788 | |
| 789 | total_prereq=0 |
| 790 | ok_prereq=0 |
| 791 | missing_prereq= |
| 792 | |
| 793 | for prerequisite |
| 794 | do |
Jeff King | bdccd3c | 2012-11-14 16:33:25 -0800 | [diff] [blame] | 795 | case "$prerequisite" in |
| 796 | !*) |
| 797 | negative_prereq=t |
| 798 | prerequisite=${prerequisite#!} |
| 799 | ;; |
| 800 | *) |
| 801 | negative_prereq= |
| 802 | esac |
| 803 | |
Junio C Hamano | 04083f2 | 2012-07-26 15:50:45 -0700 | [diff] [blame] | 804 | case " $lazily_tested_prereq " in |
| 805 | *" $prerequisite "*) |
| 806 | ;; |
| 807 | *) |
| 808 | case " $lazily_testable_prereq " in |
| 809 | *" $prerequisite "*) |
| 810 | eval "script=\$test_prereq_lazily_$prerequisite" && |
| 811 | if test_run_lazy_prereq_ "$prerequisite" "$script" |
| 812 | then |
| 813 | test_set_prereq $prerequisite |
| 814 | fi |
| 815 | lazily_tested_prereq="$lazily_tested_prereq$prerequisite " |
| 816 | esac |
| 817 | ;; |
| 818 | esac |
| 819 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 820 | total_prereq=$(($total_prereq + 1)) |
Junio C Hamano | f3cfc3b | 2012-07-26 13:57:56 -0700 | [diff] [blame] | 821 | case "$satisfied_prereq" in |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 822 | *" $prerequisite "*) |
Jeff King | bdccd3c | 2012-11-14 16:33:25 -0800 | [diff] [blame] | 823 | satisfied_this_prereq=t |
| 824 | ;; |
| 825 | *) |
| 826 | satisfied_this_prereq= |
| 827 | esac |
| 828 | |
| 829 | case "$satisfied_this_prereq,$negative_prereq" in |
| 830 | t,|,t) |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 831 | ok_prereq=$(($ok_prereq + 1)) |
| 832 | ;; |
| 833 | *) |
Jeff King | bdccd3c | 2012-11-14 16:33:25 -0800 | [diff] [blame] | 834 | # Keep a list of missing prerequisites; restore |
| 835 | # the negative marker if necessary. |
| 836 | prerequisite=${negative_prereq:+!}$prerequisite |
Fabian Stelzer | 5024ade | 2021-11-20 16:04:00 +0100 | [diff] [blame] | 837 | |
| 838 | # Abort if this prereq was marked as required |
| 839 | if test -n "$GIT_TEST_REQUIRE_PREREQ" |
| 840 | then |
| 841 | case " $GIT_TEST_REQUIRE_PREREQ " in |
| 842 | *" $prerequisite "*) |
| 843 | BAIL_OUT "required prereq $prerequisite failed" |
| 844 | ;; |
| 845 | esac |
| 846 | fi |
| 847 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 848 | if test -z "$missing_prereq" |
| 849 | then |
| 850 | missing_prereq=$prerequisite |
| 851 | else |
| 852 | missing_prereq="$prerequisite,$missing_prereq" |
| 853 | fi |
| 854 | esac |
| 855 | done |
| 856 | |
| 857 | test $total_prereq = $ok_prereq |
| 858 | } |
| 859 | |
| 860 | test_declared_prereq () { |
| 861 | case ",$test_prereq," in |
| 862 | *,$1,*) |
| 863 | return 0 |
| 864 | ;; |
| 865 | esac |
| 866 | return 1 |
| 867 | } |
| 868 | |
Junio C Hamano | d93d5d5 | 2015-04-26 15:18:45 -0700 | [diff] [blame] | 869 | test_verify_prereq () { |
| 870 | test -z "$test_prereq" || |
| 871 | expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' || |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 872 | BUG "'$test_prereq' does not look like a prereq" |
Junio C Hamano | d93d5d5 | 2015-04-26 15:18:45 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 875 | test_expect_failure () { |
Johannes Schindelin | 0f5ae59 | 2022-05-21 22:18:51 +0000 | [diff] [blame] | 876 | test_start_ "$@" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 877 | test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= |
| 878 | test "$#" = 2 || |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 879 | BUG "not 2 or 3 parameters to test-expect-failure" |
Junio C Hamano | d93d5d5 | 2015-04-26 15:18:45 -0700 | [diff] [blame] | 880 | test_verify_prereq |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 881 | export test_prereq |
| 882 | if ! test_skip "$@" |
| 883 | then |
Victoria Dye | 110e911 | 2022-05-21 22:18:53 +0000 | [diff] [blame] | 884 | test -n "$test_skip_test_preamble" || |
SZEDER Gábor | ffe1afe | 2019-08-05 23:04:47 +0200 | [diff] [blame] | 885 | say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 886 | if test_run_ "$2" expecting_failure |
| 887 | then |
| 888 | test_known_broken_ok_ "$1" |
| 889 | else |
| 890 | test_known_broken_failure_ "$1" |
| 891 | fi |
| 892 | fi |
Thomas Rast | ae75342 | 2013-06-18 14:25:59 +0200 | [diff] [blame] | 893 | test_finish_ |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | test_expect_success () { |
Johannes Schindelin | 0f5ae59 | 2022-05-21 22:18:51 +0000 | [diff] [blame] | 897 | test_start_ "$@" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 898 | test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= |
| 899 | test "$#" = 2 || |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 900 | BUG "not 2 or 3 parameters to test-expect-success" |
Junio C Hamano | d93d5d5 | 2015-04-26 15:18:45 -0700 | [diff] [blame] | 901 | test_verify_prereq |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 902 | export test_prereq |
| 903 | if ! test_skip "$@" |
| 904 | then |
Victoria Dye | 110e911 | 2022-05-21 22:18:53 +0000 | [diff] [blame] | 905 | test -n "$test_skip_test_preamble" || |
SZEDER Gábor | ffe1afe | 2019-08-05 23:04:47 +0200 | [diff] [blame] | 906 | say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 907 | if test_run_ "$2" |
| 908 | then |
| 909 | test_ok_ "$1" |
| 910 | else |
| 911 | test_failure_ "$@" |
| 912 | fi |
| 913 | fi |
Thomas Rast | ae75342 | 2013-06-18 14:25:59 +0200 | [diff] [blame] | 914 | test_finish_ |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 915 | } |
| 916 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 917 | # debugging-friendly alternatives to "test [-f|-d|-e]" |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 918 | # The commands test the existence or non-existence of $1 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 919 | test_path_is_file () { |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 920 | test "$#" -ne 1 && BUG "1 param" |
David Aguilar | 9e8f8de | 2014-10-15 01:35:21 -0700 | [diff] [blame] | 921 | if ! test -f "$1" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 922 | then |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 923 | echo "File $1 doesn't exist" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 924 | false |
| 925 | fi |
| 926 | } |
| 927 | |
COGONI Guillaume | 456296b | 2022-02-22 22:54:29 +0100 | [diff] [blame] | 928 | test_path_is_file_not_symlink () { |
| 929 | test "$#" -ne 1 && BUG "1 param" |
| 930 | test_path_is_file "$1" && |
| 931 | if test -h "$1" |
| 932 | then |
| 933 | echo "$1 shouldn't be a symbolic link" |
| 934 | false |
| 935 | fi |
| 936 | } |
| 937 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 938 | test_path_is_dir () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 939 | test "$#" -ne 1 && BUG "1 param" |
David Aguilar | 9e8f8de | 2014-10-15 01:35:21 -0700 | [diff] [blame] | 940 | if ! test -d "$1" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 941 | then |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 942 | echo "Directory $1 doesn't exist" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 943 | false |
| 944 | fi |
| 945 | } |
| 946 | |
COGONI Guillaume | 456296b | 2022-02-22 22:54:29 +0100 | [diff] [blame] | 947 | test_path_is_dir_not_symlink () { |
| 948 | test "$#" -ne 1 && BUG "1 param" |
| 949 | test_path_is_dir "$1" && |
| 950 | if test -h "$1" |
| 951 | then |
| 952 | echo "$1 shouldn't be a symbolic link" |
| 953 | false |
| 954 | fi |
| 955 | } |
| 956 | |
Elijah Newren | 7e9055b | 2018-08-08 09:31:06 -0700 | [diff] [blame] | 957 | test_path_exists () { |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 958 | test "$#" -ne 1 && BUG "1 param" |
Elijah Newren | 7e9055b | 2018-08-08 09:31:06 -0700 | [diff] [blame] | 959 | if ! test -e "$1" |
| 960 | then |
Ævar Arnfjörð Bjarmason | 45a2686 | 2021-02-12 14:29:41 +0100 | [diff] [blame] | 961 | echo "Path $1 doesn't exist" |
Elijah Newren | 7e9055b | 2018-08-08 09:31:06 -0700 | [diff] [blame] | 962 | false |
| 963 | fi |
| 964 | } |
| 965 | |
COGONI Guillaume | 456296b | 2022-02-22 22:54:29 +0100 | [diff] [blame] | 966 | test_path_is_symlink () { |
| 967 | test "$#" -ne 1 && BUG "1 param" |
| 968 | if ! test -h "$1" |
| 969 | then |
| 970 | echo "Symbolic link $1 doesn't exist" |
| 971 | false |
| 972 | fi |
| 973 | } |
| 974 | |
brian m. carlson | d6546af | 2023-06-27 16:18:56 +0000 | [diff] [blame] | 975 | test_path_is_executable () { |
| 976 | test "$#" -ne 1 && BUG "1 param" |
| 977 | if ! test -x "$1" |
| 978 | then |
| 979 | echo "$1 is not executable" |
| 980 | false |
| 981 | fi |
| 982 | } |
| 983 | |
Jens Lehmann | 0be7d9b | 2014-06-19 22:12:23 +0200 | [diff] [blame] | 984 | # Check if the directory exists and is empty as expected, barf otherwise. |
| 985 | test_dir_is_empty () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 986 | test "$#" -ne 1 && BUG "1 param" |
Jens Lehmann | 0be7d9b | 2014-06-19 22:12:23 +0200 | [diff] [blame] | 987 | test_path_is_dir "$1" && |
Đoàn Trần Công Danh | 81580fa | 2022-09-21 20:02:31 +0700 | [diff] [blame] | 988 | if test -n "$(ls -a1 "$1" | grep -E -v '^\.\.?$')" |
Jens Lehmann | 0be7d9b | 2014-06-19 22:12:23 +0200 | [diff] [blame] | 989 | then |
| 990 | echo "Directory '$1' is not empty, it contains:" |
| 991 | ls -la "$1" |
| 992 | return 1 |
| 993 | fi |
| 994 | } |
| 995 | |
Rohit Ashiwal | 21d5ad9 | 2019-03-04 17:37:59 +0530 | [diff] [blame] | 996 | # Check if the file exists and has a size greater than zero |
| 997 | test_file_not_empty () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 998 | test "$#" = 2 && BUG "2 param" |
Rohit Ashiwal | 21d5ad9 | 2019-03-04 17:37:59 +0530 | [diff] [blame] | 999 | if ! test -s "$1" |
| 1000 | then |
| 1001 | echo "'$1' is not a non-empty file." |
| 1002 | false |
| 1003 | fi |
| 1004 | } |
| 1005 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1006 | test_path_is_missing () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 1007 | test "$#" -ne 1 && BUG "1 param" |
David Aguilar | 9e8f8de | 2014-10-15 01:35:21 -0700 | [diff] [blame] | 1008 | if test -e "$1" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1009 | then |
| 1010 | echo "Path exists:" |
| 1011 | ls -ld "$1" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1012 | false |
| 1013 | fi |
| 1014 | } |
| 1015 | |
| 1016 | # test_line_count checks that a file has the number of lines it |
| 1017 | # ought to. For example: |
| 1018 | # |
| 1019 | # test_expect_success 'produce exactly one line of output' ' |
| 1020 | # do something >output && |
| 1021 | # test_line_count = 1 output |
| 1022 | # ' |
| 1023 | # |
| 1024 | # is like "test $(wc -l <output) = 1" except that it passes the |
| 1025 | # output through when the number of lines is wrong. |
| 1026 | |
| 1027 | test_line_count () { |
| 1028 | if test $# != 3 |
| 1029 | then |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 1030 | BUG "not 3 parameters to test_line_count" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1031 | elif ! test $(wc -l <"$3") "$1" "$2" |
| 1032 | then |
| 1033 | echo "test_line_count: line count for $3 !$1 $2" |
| 1034 | cat "$3" |
| 1035 | return 1 |
| 1036 | fi |
| 1037 | } |
| 1038 | |
Đoàn Trần Công Danh | cdff1bb | 2021-07-04 12:46:10 +0700 | [diff] [blame] | 1039 | # SYNOPSIS: |
| 1040 | # test_stdout_line_count <bin-ops> <value> <cmd> [<args>...] |
| 1041 | # |
| 1042 | # test_stdout_line_count checks that the output of a command has the number |
| 1043 | # of lines it ought to. For example: |
| 1044 | # |
| 1045 | # test_stdout_line_count = 3 git ls-files -u |
| 1046 | # test_stdout_line_count -gt 10 ls |
| 1047 | test_stdout_line_count () { |
| 1048 | local ops val trashdir && |
| 1049 | if test "$#" -le 3 |
| 1050 | then |
| 1051 | BUG "expect 3 or more arguments" |
| 1052 | fi && |
| 1053 | ops="$1" && |
| 1054 | val="$2" && |
| 1055 | shift 2 && |
| 1056 | if ! trashdir="$(git rev-parse --git-dir)/trash"; then |
| 1057 | BUG "expect to be run inside a worktree" |
| 1058 | fi && |
| 1059 | mkdir -p "$trashdir" && |
| 1060 | "$@" >"$trashdir/output" && |
| 1061 | test_line_count "$ops" "$val" "$trashdir/output" |
| 1062 | } |
| 1063 | |
| 1064 | |
Johannes Schindelin | 53b67a8 | 2020-11-07 01:12:57 +0000 | [diff] [blame] | 1065 | test_file_size () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 1066 | test "$#" -ne 1 && BUG "1 param" |
Johannes Schindelin | 53b67a8 | 2020-11-07 01:12:57 +0000 | [diff] [blame] | 1067 | test-tool path-utils file-size "$1" |
| 1068 | } |
| 1069 | |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1070 | # Returns success if a comma separated string of keywords ($1) contains a |
| 1071 | # given keyword ($2). |
| 1072 | # Examples: |
| 1073 | # `list_contains "foo,bar" bar` returns 0 |
| 1074 | # `list_contains "foo" bar` returns 1 |
| 1075 | |
| 1076 | list_contains () { |
| 1077 | case ",$1," in |
| 1078 | *,$2,*) |
| 1079 | return 0 |
| 1080 | ;; |
| 1081 | esac |
| 1082 | return 1 |
| 1083 | } |
| 1084 | |
Denton Liu | 6a67c75 | 2020-07-07 02:04:38 -0400 | [diff] [blame] | 1085 | # Returns success if the arguments indicate that a command should be |
| 1086 | # accepted by test_must_fail(). If the command is run with env, the env |
| 1087 | # and its corresponding variable settings will be stripped before we |
| 1088 | # test the command being run. |
| 1089 | test_must_fail_acceptable () { |
| 1090 | if test "$1" = "env" |
| 1091 | then |
| 1092 | shift |
| 1093 | while test $# -gt 0 |
| 1094 | do |
| 1095 | case "$1" in |
| 1096 | *?=*) |
| 1097 | shift |
| 1098 | ;; |
| 1099 | *) |
| 1100 | break |
| 1101 | ;; |
| 1102 | esac |
| 1103 | done |
| 1104 | fi |
| 1105 | |
| 1106 | case "$1" in |
Derrick Stolee | 008217c | 2023-01-27 20:06:01 +0000 | [diff] [blame] | 1107 | git|__git*|scalar|test-tool|test_terminal) |
Denton Liu | 6a67c75 | 2020-07-07 02:04:38 -0400 | [diff] [blame] | 1108 | return 0 |
| 1109 | ;; |
| 1110 | *) |
| 1111 | return 1 |
| 1112 | ;; |
| 1113 | esac |
| 1114 | } |
| 1115 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1116 | # This is not among top-level (test_expect_success | test_expect_failure) |
| 1117 | # but is a prefix that can be used in the test script, like: |
| 1118 | # |
| 1119 | # test_expect_success 'complain and die' ' |
| 1120 | # do something && |
| 1121 | # do something else && |
| 1122 | # test_must_fail git checkout ../outerspace |
| 1123 | # ' |
| 1124 | # |
| 1125 | # Writing this as "! git checkout ../outerspace" is wrong, because |
| 1126 | # the failure could be due to a segv. We want a controlled failure. |
SZEDER Gábor | 12e31a6 | 2018-02-09 03:42:33 +0100 | [diff] [blame] | 1127 | # |
| 1128 | # Accepts the following options: |
| 1129 | # |
| 1130 | # ok=<signal-name>[,<...>]: |
| 1131 | # Don't treat an exit caused by the given signal as error. |
| 1132 | # Multiple signals can be specified as a comma separated list. |
| 1133 | # Currently recognized signal names are: sigpipe, success. |
| 1134 | # (Don't use 'success', use 'test_might_fail' instead.) |
Denton Liu | 6a67c75 | 2020-07-07 02:04:38 -0400 | [diff] [blame] | 1135 | # |
| 1136 | # Do not use this to run anything but "git" and other specific testable |
| 1137 | # commands (see test_must_fail_acceptable()). We are not in the |
| 1138 | # business of vetting system supplied commands -- in other words, this |
| 1139 | # is wrong: |
| 1140 | # |
| 1141 | # test_must_fail grep pattern output |
| 1142 | # |
| 1143 | # Instead use '!': |
| 1144 | # |
| 1145 | # ! grep pattern output |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1146 | |
| 1147 | test_must_fail () { |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1148 | case "$1" in |
| 1149 | ok=*) |
| 1150 | _test_ok=${1#ok=} |
| 1151 | shift |
| 1152 | ;; |
| 1153 | *) |
| 1154 | _test_ok= |
| 1155 | ;; |
| 1156 | esac |
Denton Liu | 6a67c75 | 2020-07-07 02:04:38 -0400 | [diff] [blame] | 1157 | if ! test_must_fail_acceptable "$@" |
| 1158 | then |
| 1159 | echo >&7 "test_must_fail: only 'git' is allowed: $*" |
| 1160 | return 1 |
| 1161 | fi |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1162 | "$@" 2>&7 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1163 | exit_code=$? |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1164 | if test $exit_code -eq 0 && ! list_contains "$_test_ok" success |
| 1165 | then |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1166 | echo >&4 "test_must_fail: command succeeded: $*" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1167 | return 1 |
Jeff King | 2472448 | 2016-06-24 15:45:04 -0400 | [diff] [blame] | 1168 | elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe |
Lars Schneider | 8bf4bec | 2015-11-27 10:15:14 +0100 | [diff] [blame] | 1169 | then |
| 1170 | return 0 |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1171 | elif test $exit_code -gt 129 && test $exit_code -le 192 |
| 1172 | then |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1173 | echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1174 | return 1 |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1175 | elif test $exit_code -eq 127 |
| 1176 | then |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1177 | echo >&4 "test_must_fail: command not found: $*" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1178 | return 1 |
Lars Schneider | bbfe530 | 2015-11-27 10:15:13 +0100 | [diff] [blame] | 1179 | elif test $exit_code -eq 126 |
| 1180 | then |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1181 | echo >&4 "test_must_fail: valgrind error: $*" |
Thomas Rast | eeb6913 | 2013-03-31 10:37:25 +0200 | [diff] [blame] | 1182 | return 1 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1183 | fi |
| 1184 | return 0 |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1185 | } 7>&2 2>&4 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1186 | |
| 1187 | # Similar to test_must_fail, but tolerates success, too. This is |
| 1188 | # meant to be used in contexts like: |
| 1189 | # |
| 1190 | # test_expect_success 'some command works without configuration' ' |
| 1191 | # test_might_fail git config --unset all.configuration && |
| 1192 | # do something |
| 1193 | # ' |
| 1194 | # |
| 1195 | # Writing "git config --unset all.configuration || :" would be wrong, |
| 1196 | # because we want to notice if it fails due to segv. |
SZEDER Gábor | 12e31a6 | 2018-02-09 03:42:33 +0100 | [diff] [blame] | 1197 | # |
| 1198 | # Accepts the same options as test_must_fail. |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1199 | |
| 1200 | test_might_fail () { |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1201 | test_must_fail ok=success "$@" 2>&7 |
| 1202 | } 7>&2 2>&4 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1203 | |
| 1204 | # Similar to test_must_fail and test_might_fail, but check that a |
| 1205 | # given command exited with a given exit code. Meant to be used as: |
| 1206 | # |
| 1207 | # test_expect_success 'Merge with d/f conflicts' ' |
| 1208 | # test_expect_code 1 git merge "merge msg" B master |
| 1209 | # ' |
| 1210 | |
| 1211 | test_expect_code () { |
| 1212 | want_code=$1 |
| 1213 | shift |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1214 | "$@" 2>&7 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1215 | exit_code=$? |
| 1216 | if test $exit_code = $want_code |
| 1217 | then |
| 1218 | return 0 |
| 1219 | fi |
| 1220 | |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1221 | echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1222 | return 1 |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1223 | } 7>&2 2>&4 |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1224 | |
| 1225 | # test_cmp is a helper function to compare actual and expected output. |
| 1226 | # You can use it like: |
| 1227 | # |
| 1228 | # test_expect_success 'foo works' ' |
| 1229 | # echo expected >expected && |
| 1230 | # foo >actual && |
| 1231 | # test_cmp expected actual |
| 1232 | # ' |
| 1233 | # |
| 1234 | # This could be written as either "cmp" or "diff -u", but: |
| 1235 | # - cmp's output is not nearly as easy to read as diff -u |
| 1236 | # - not all diff versions understand "-u" |
| 1237 | |
Felipe Contreras | 1ab7e00 | 2020-11-30 18:46:49 -0600 | [diff] [blame] | 1238 | test_cmp () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 1239 | test "$#" -ne 2 && BUG "2 param" |
Junio C Hamano | 262d5ad | 2020-10-16 13:51:04 -0700 | [diff] [blame] | 1240 | eval "$GIT_TEST_CMP" '"$@"' |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1241 | } |
| 1242 | |
Nguyễn Thái Ngọc Duy | a5db0b7 | 2018-10-21 16:02:27 +0200 | [diff] [blame] | 1243 | # Check that the given config key has the expected value. |
| 1244 | # |
| 1245 | # test_cmp_config [-C <dir>] <expected-value> |
| 1246 | # [<git-config-options>...] <config-key> |
| 1247 | # |
| 1248 | # for example to check that the value of core.bar is foo |
| 1249 | # |
| 1250 | # test_cmp_config foo core.bar |
| 1251 | # |
Felipe Contreras | 1ab7e00 | 2020-11-30 18:46:49 -0600 | [diff] [blame] | 1252 | test_cmp_config () { |
Nguyễn Thái Ngọc Duy | a5db0b7 | 2018-10-21 16:02:27 +0200 | [diff] [blame] | 1253 | local GD && |
| 1254 | if test "$1" = "-C" |
| 1255 | then |
| 1256 | shift && |
| 1257 | GD="-C $1" && |
| 1258 | shift |
| 1259 | fi && |
| 1260 | printf "%s\n" "$1" >expect.config && |
| 1261 | shift && |
| 1262 | git $GD config "$@" >actual.config && |
| 1263 | test_cmp expect.config actual.config |
| 1264 | } |
| 1265 | |
Stepan Kasal | b93e6e3 | 2014-06-04 17:57:52 +0200 | [diff] [blame] | 1266 | # test_cmp_bin - helper to compare binary files |
| 1267 | |
Felipe Contreras | 1ab7e00 | 2020-11-30 18:46:49 -0600 | [diff] [blame] | 1268 | test_cmp_bin () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 1269 | test "$#" -ne 2 && BUG "2 param" |
Junio C Hamano | 262d5ad | 2020-10-16 13:51:04 -0700 | [diff] [blame] | 1270 | cmp "$@" |
Stepan Kasal | b93e6e3 | 2014-06-04 17:57:52 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
SZEDER Gábor | 0f59128 | 2018-02-08 16:56:54 +0100 | [diff] [blame] | 1273 | test_i18ngrep () { |
Junio C Hamano | 381a83d | 2024-03-02 09:13:51 -0800 | [diff] [blame] | 1274 | BUG "do not use test_i18ngrep---use test_grep instead" |
Junio C Hamano | 2e87fca | 2023-10-31 14:23:29 +0900 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | test_grep () { |
SZEDER Gábor | fd29d7b | 2018-02-08 16:56:55 +0100 | [diff] [blame] | 1278 | eval "last_arg=\${$#}" |
| 1279 | |
| 1280 | test -f "$last_arg" || |
Junio C Hamano | 2e87fca | 2023-10-31 14:23:29 +0900 | [diff] [blame] | 1281 | BUG "test_grep requires a file to read as the last parameter" |
SZEDER Gábor | fd29d7b | 2018-02-08 16:56:55 +0100 | [diff] [blame] | 1282 | |
| 1283 | if test $# -lt 2 || |
| 1284 | { test "x!" = "x$1" && test $# -lt 3 ; } |
| 1285 | then |
Shreyansh Paliwal | 37e8d79 | 2023-12-03 22:47:59 +0530 | [diff] [blame] | 1286 | BUG "too few parameters to test_grep" |
SZEDER Gábor | fd29d7b | 2018-02-08 16:56:55 +0100 | [diff] [blame] | 1287 | fi |
| 1288 | |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1289 | if test "x!" = "x$1" |
SZEDER Gábor | 0f59128 | 2018-02-08 16:56:54 +0100 | [diff] [blame] | 1290 | then |
| 1291 | shift |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1292 | ! grep "$@" && return 0 |
| 1293 | |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1294 | echo >&4 "error: '! grep $@' did find a match in:" |
SZEDER Gábor | 0f59128 | 2018-02-08 16:56:54 +0100 | [diff] [blame] | 1295 | else |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1296 | grep "$@" && return 0 |
| 1297 | |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1298 | echo >&4 "error: 'grep $@' didn't find a match in:" |
SZEDER Gábor | 0f59128 | 2018-02-08 16:56:54 +0100 | [diff] [blame] | 1299 | fi |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1300 | |
| 1301 | if test -s "$last_arg" |
| 1302 | then |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1303 | cat >&4 "$last_arg" |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1304 | else |
Jeff King | 03aa378 | 2018-02-22 01:48:37 -0500 | [diff] [blame] | 1305 | echo >&4 "<File '$last_arg' is empty>" |
SZEDER Gábor | 63b1a17 | 2018-02-08 16:56:56 +0100 | [diff] [blame] | 1306 | fi |
| 1307 | |
| 1308 | return 1 |
SZEDER Gábor | 0f59128 | 2018-02-08 16:56:54 +0100 | [diff] [blame] | 1309 | } |
| 1310 | |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 1311 | # Check if the file expected to be empty is indeed empty, and barfs |
| 1312 | # otherwise. |
| 1313 | |
| 1314 | test_must_be_empty () { |
Ævar Arnfjörð Bjarmason | e7884b3 | 2021-02-12 14:29:42 +0100 | [diff] [blame] | 1315 | test "$#" -ne 1 && BUG "1 param" |
SZEDER Gábor | 9eb2308 | 2018-03-26 15:11:24 +0200 | [diff] [blame] | 1316 | test_path_is_file "$1" && |
| 1317 | if test -s "$1" |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 1318 | then |
| 1319 | echo "'$1' is not empty, it contains:" |
| 1320 | cat "$1" |
| 1321 | return 1 |
| 1322 | fi |
| 1323 | } |
| 1324 | |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 1325 | # Tests that its two parameters refer to the same revision, or if '!' is |
| 1326 | # provided first, that its other two parameters refer to different |
| 1327 | # revisions. |
Martin von Zweigbergk | 5d77298 | 2012-12-21 11:10:10 -0800 | [diff] [blame] | 1328 | test_cmp_rev () { |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 1329 | local op='=' wrong_result=different |
| 1330 | |
| 1331 | if test $# -ge 1 && test "x$1" = 'x!' |
| 1332 | then |
| 1333 | op='!=' |
| 1334 | wrong_result='the same' |
| 1335 | shift |
| 1336 | fi |
SZEDER Gábor | 30d0b6d | 2018-11-19 14:28:18 +0100 | [diff] [blame] | 1337 | if test $# != 2 |
| 1338 | then |
Ævar Arnfjörð Bjarmason | 9e9c7dd | 2021-02-09 22:41:49 +0100 | [diff] [blame] | 1339 | BUG "test_cmp_rev requires two revisions, but got $#" |
SZEDER Gábor | 30d0b6d | 2018-11-19 14:28:18 +0100 | [diff] [blame] | 1340 | else |
| 1341 | local r1 r2 |
| 1342 | r1=$(git rev-parse --verify "$1") && |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 1343 | r2=$(git rev-parse --verify "$2") || return 1 |
| 1344 | |
| 1345 | if ! test "$r1" "$op" "$r2" |
SZEDER Gábor | 30d0b6d | 2018-11-19 14:28:18 +0100 | [diff] [blame] | 1346 | then |
| 1347 | cat >&4 <<-EOF |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 1348 | error: two revisions point to $wrong_result objects: |
SZEDER Gábor | 30d0b6d | 2018-11-19 14:28:18 +0100 | [diff] [blame] | 1349 | '$1': $r1 |
| 1350 | '$2': $r2 |
| 1351 | EOF |
| 1352 | return 1 |
| 1353 | fi |
| 1354 | fi |
Martin von Zweigbergk | 5d77298 | 2012-12-21 11:10:10 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
Phillip Wood | 6ce7afe | 2023-08-03 13:09:35 +0000 | [diff] [blame] | 1357 | # Tests that a commit message matches the expected text |
| 1358 | # |
| 1359 | # Usage: test_commit_message <rev> [-m <msg> | <file>] |
| 1360 | # |
| 1361 | # When using "-m" <msg> will have a line feed appended. If the second |
| 1362 | # argument is omitted then the expected message is read from stdin. |
| 1363 | |
| 1364 | test_commit_message () { |
| 1365 | local msg_file=expect.msg |
| 1366 | |
| 1367 | case $# in |
| 1368 | 3) |
| 1369 | if test "$2" = "-m" |
| 1370 | then |
| 1371 | printf "%s\n" "$3" >"$msg_file" |
| 1372 | else |
| 1373 | BUG "Usage: test_commit_message <rev> [-m <message> | <file>]" |
| 1374 | fi |
| 1375 | ;; |
| 1376 | 2) |
| 1377 | msg_file="$2" |
| 1378 | ;; |
| 1379 | 1) |
| 1380 | cat >"$msg_file" |
| 1381 | ;; |
| 1382 | *) |
| 1383 | BUG "Usage: test_commit_message <rev> [-m <message> | <file>]" |
| 1384 | ;; |
| 1385 | esac |
| 1386 | git show --no-patch --pretty=format:%B "$1" -- >actual.msg && |
| 1387 | test_cmp "$msg_file" actual.msg |
| 1388 | } |
| 1389 | |
Johannes Schindelin | ed33bd8 | 2019-06-24 19:40:05 +0200 | [diff] [blame] | 1390 | # Compare paths respecting core.ignoreCase |
| 1391 | test_cmp_fspath () { |
| 1392 | if test "x$1" = "x$2" |
| 1393 | then |
| 1394 | return 0 |
| 1395 | fi |
| 1396 | |
| 1397 | if test true != "$(git config --get --type=bool core.ignorecase)" |
| 1398 | then |
| 1399 | return 1 |
| 1400 | fi |
| 1401 | |
| 1402 | test "x$(echo "$1" | tr A-Z a-z)" = "x$(echo "$2" | tr A-Z a-z)" |
| 1403 | } |
| 1404 | |
Junio C Hamano | 55672a3 | 2016-05-09 11:36:09 -0700 | [diff] [blame] | 1405 | # Print a sequence of integers in increasing order, either with |
| 1406 | # two arguments (start and end): |
Michał Kiedrowicz | d17cf5f | 2012-08-04 00:21:04 +0200 | [diff] [blame] | 1407 | # |
Junio C Hamano | 55672a3 | 2016-05-09 11:36:09 -0700 | [diff] [blame] | 1408 | # test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time |
| 1409 | # |
| 1410 | # or with one argument (end), in which case it starts counting |
| 1411 | # from 1. |
Michał Kiedrowicz | d17cf5f | 2012-08-04 00:21:04 +0200 | [diff] [blame] | 1412 | |
| 1413 | test_seq () { |
| 1414 | case $# in |
| 1415 | 1) set 1 "$@" ;; |
| 1416 | 2) ;; |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 1417 | *) BUG "not 1 or 2 parameters to test_seq" ;; |
Michał Kiedrowicz | d17cf5f | 2012-08-04 00:21:04 +0200 | [diff] [blame] | 1418 | esac |
Junio C Hamano | 4df4313 | 2016-05-09 12:37:01 -0700 | [diff] [blame] | 1419 | test_seq_counter__=$1 |
| 1420 | while test "$test_seq_counter__" -le "$2" |
| 1421 | do |
| 1422 | echo "$test_seq_counter__" |
| 1423 | test_seq_counter__=$(( $test_seq_counter__ + 1 )) |
| 1424 | done |
Michał Kiedrowicz | d17cf5f | 2012-08-04 00:21:04 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1427 | # This function can be used to schedule some commands to be run |
| 1428 | # unconditionally at the end of the test to restore sanity: |
| 1429 | # |
| 1430 | # test_expect_success 'test core.capslock' ' |
| 1431 | # git config core.capslock true && |
| 1432 | # test_when_finished "git config --unset core.capslock" && |
| 1433 | # hello world |
| 1434 | # ' |
| 1435 | # |
| 1436 | # That would be roughly equivalent to |
| 1437 | # |
| 1438 | # test_expect_success 'test core.capslock' ' |
| 1439 | # git config core.capslock true && |
| 1440 | # hello world |
| 1441 | # git config --unset core.capslock |
| 1442 | # ' |
| 1443 | # |
| 1444 | # except that the greeting and config --unset must both succeed for |
| 1445 | # the test to pass. |
| 1446 | # |
| 1447 | # Note that under --immediate mode, no clean-up is done to help diagnose |
| 1448 | # what went wrong. |
| 1449 | |
| 1450 | test_when_finished () { |
John Keeping | 0968f12 | 2015-09-05 14:12:49 +0100 | [diff] [blame] | 1451 | # We cannot detect when we are in a subshell in general, but by |
| 1452 | # doing so on Bash is better than nothing (the test will |
| 1453 | # silently pass on other shells). |
| 1454 | test "${BASH_SUBSHELL-0}" = 0 || |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 1455 | BUG "test_when_finished does nothing in a subshell" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1456 | test_cleanup="{ $* |
| 1457 | } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup" |
| 1458 | } |
| 1459 | |
Johannes Schindelin | 900721e | 2019-03-13 13:24:11 +0100 | [diff] [blame] | 1460 | # This function can be used to schedule some commands to be run |
| 1461 | # unconditionally at the end of the test script, e.g. to stop a daemon: |
| 1462 | # |
| 1463 | # test_expect_success 'test git daemon' ' |
| 1464 | # git daemon & |
| 1465 | # daemon_pid=$! && |
| 1466 | # test_atexit 'kill $daemon_pid' && |
| 1467 | # hello world |
| 1468 | # ' |
| 1469 | # |
| 1470 | # The commands will be executed before the trash directory is removed, |
| 1471 | # i.e. the atexit commands will still be able to access any pidfiles or |
| 1472 | # socket files. |
| 1473 | # |
| 1474 | # Note that these commands will be run even when a test script run |
| 1475 | # with '--immediate' fails. Be careful with your atexit commands to |
| 1476 | # minimize any changes to the failed state. |
| 1477 | |
| 1478 | test_atexit () { |
| 1479 | # We cannot detect when we are in a subshell in general, but by |
| 1480 | # doing so on Bash is better than nothing (the test will |
| 1481 | # silently pass on other shells). |
| 1482 | test "${BASH_SUBSHELL-0}" = 0 || |
Ævar Arnfjörð Bjarmason | 9e9c7dd | 2021-02-09 22:41:49 +0100 | [diff] [blame] | 1483 | BUG "test_atexit does nothing in a subshell" |
Johannes Schindelin | 900721e | 2019-03-13 13:24:11 +0100 | [diff] [blame] | 1484 | test_atexit_cleanup="{ $* |
| 1485 | } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup" |
| 1486 | } |
| 1487 | |
Ævar Arnfjörð Bjarmason | f0d4d39 | 2021-05-10 16:19:10 +0200 | [diff] [blame] | 1488 | # Deprecated wrapper for "git init", use "git init" directly instead |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1489 | # Usage: test_create_repo <directory> |
| 1490 | test_create_repo () { |
Ævar Arnfjörð Bjarmason | f0d4d39 | 2021-05-10 16:19:10 +0200 | [diff] [blame] | 1491 | git init "$@" |
Thomas Rast | 12a29b1 | 2012-02-17 11:25:08 +0100 | [diff] [blame] | 1492 | } |
Johannes Sixt | 9ce415d | 2013-06-07 22:53:27 +0200 | [diff] [blame] | 1493 | |
| 1494 | # This function helps on symlink challenged file systems when it is not |
| 1495 | # important that the file system entry is a symbolic link. |
| 1496 | # Use test_ln_s_add instead of "ln -s x y && git add y" to add a |
| 1497 | # symbolic link entry y to the index. |
| 1498 | |
| 1499 | test_ln_s_add () { |
| 1500 | if test_have_prereq SYMLINKS |
| 1501 | then |
| 1502 | ln -s "$1" "$2" && |
| 1503 | git update-index --add "$2" |
| 1504 | else |
| 1505 | printf '%s' "$1" >"$2" && |
| 1506 | ln_s_obj=$(git hash-object -w "$2") && |
Johannes Sixt | 817d03e | 2015-02-23 19:14:47 +0100 | [diff] [blame] | 1507 | git update-index --add --cacheinfo 120000 $ln_s_obj "$2" && |
| 1508 | # pick up stat info from the file |
| 1509 | git update-index "$2" |
Johannes Sixt | 9ce415d | 2013-06-07 22:53:27 +0200 | [diff] [blame] | 1510 | fi |
| 1511 | } |
Johannes Sixt | 4d715ac | 2013-10-26 21:17:15 +0200 | [diff] [blame] | 1512 | |
Michael S. Tsirkin | ac9afcc | 2014-04-27 21:15:47 +0300 | [diff] [blame] | 1513 | # This function writes out its parameters, one per line |
| 1514 | test_write_lines () { |
| 1515 | printf "%s\n" "$@" |
| 1516 | } |
| 1517 | |
Jeff King | a0e0ec9 | 2013-10-28 21:22:07 -0400 | [diff] [blame] | 1518 | perl () { |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1519 | command "$PERL_PATH" "$@" 2>&7 |
| 1520 | } 7>&2 2>&4 |
Junio C Hamano | a3a9cff | 2013-11-04 14:58:01 -0800 | [diff] [blame] | 1521 | |
SZEDER Gábor | 43a2afe | 2019-11-22 14:14:36 +0100 | [diff] [blame] | 1522 | # Given the name of an environment variable with a bool value, normalize |
| 1523 | # its value to a 0 (true) or 1 (false or empty string) return code. |
| 1524 | # |
| 1525 | # test_bool_env GIT_TEST_HTTPD <default-value> |
| 1526 | # |
| 1527 | # Return with code corresponding to the given default value if the variable |
| 1528 | # is unset. |
| 1529 | # Abort the test script if either the value of the variable or the default |
| 1530 | # are not valid bool values. |
| 1531 | |
| 1532 | test_bool_env () { |
| 1533 | if test $# != 2 |
| 1534 | then |
| 1535 | BUG "test_bool_env requires two parameters (variable name and default value)" |
| 1536 | fi |
| 1537 | |
Ævar Arnfjörð Bjarmason | 4a1baac | 2023-01-12 17:03:21 +0100 | [diff] [blame] | 1538 | test-tool env-helper --type=bool --default="$2" --exit-code "$1" |
SZEDER Gábor | 43a2afe | 2019-11-22 14:14:36 +0100 | [diff] [blame] | 1539 | ret=$? |
| 1540 | case $ret in |
| 1541 | 0|1) # unset or valid bool value |
| 1542 | ;; |
| 1543 | *) # invalid bool value or something unexpected |
| 1544 | error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback" |
| 1545 | ;; |
| 1546 | esac |
| 1547 | return $ret |
| 1548 | } |
| 1549 | |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 1550 | # Exit the test suite, either by skipping all remaining tests or by |
Ævar Arnfjörð Bjarmason | 3b072c5 | 2019-06-21 12:18:11 +0200 | [diff] [blame] | 1551 | # exiting with an error. If our prerequisite variable $1 falls back |
| 1552 | # on a default assume we were opportunistically trying to set up some |
| 1553 | # tests and we skip. If it is explicitly "true", then we report a failure. |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 1554 | # |
| 1555 | # The error/skip message should be given by $2. |
| 1556 | # |
| 1557 | test_skip_or_die () { |
SZEDER Gábor | 43a2afe | 2019-11-22 14:14:36 +0100 | [diff] [blame] | 1558 | if ! test_bool_env "$1" false |
Ævar Arnfjörð Bjarmason | 3b072c5 | 2019-06-21 12:18:11 +0200 | [diff] [blame] | 1559 | then |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 1560 | skip_all=$2 |
| 1561 | test_done |
Ævar Arnfjörð Bjarmason | 3b072c5 | 2019-06-21 12:18:11 +0200 | [diff] [blame] | 1562 | fi |
| 1563 | error "$2" |
Jeff King | 83d842d | 2014-02-10 16:29:37 -0500 | [diff] [blame] | 1564 | } |
| 1565 | |
Jeff King | d2554c7 | 2016-06-01 03:04:26 -0400 | [diff] [blame] | 1566 | # Like "env FOO=BAR some-program", but run inside a subshell, which means |
| 1567 | # it also works for shell functions (though those functions cannot impact |
| 1568 | # the environment outside of the test_env invocation). |
| 1569 | test_env () { |
| 1570 | ( |
| 1571 | while test $# -gt 0 |
| 1572 | do |
| 1573 | case "$1" in |
| 1574 | *=*) |
| 1575 | eval "${1%%=*}=\${1#*=}" |
| 1576 | eval "export ${1%%=*}" |
| 1577 | shift |
| 1578 | ;; |
| 1579 | *) |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1580 | "$@" 2>&7 |
Jeff King | d2554c7 | 2016-06-01 03:04:26 -0400 | [diff] [blame] | 1581 | exit |
| 1582 | ;; |
| 1583 | esac |
| 1584 | done |
| 1585 | ) |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1586 | } 7>&2 2>&4 |
Jeff King | 4886081 | 2016-06-30 05:07:54 -0400 | [diff] [blame] | 1587 | |
Jeff King | 9b67c99 | 2016-06-30 04:16:18 -0400 | [diff] [blame] | 1588 | # Returns true if the numeric exit code in "$2" represents the expected signal |
| 1589 | # in "$1". Signals should be given numerically. |
| 1590 | test_match_signal () { |
| 1591 | if test "$2" = "$((128 + $1))" |
| 1592 | then |
| 1593 | # POSIX |
| 1594 | return 0 |
| 1595 | elif test "$2" = "$((256 + $1))" |
| 1596 | then |
| 1597 | # ksh |
| 1598 | return 0 |
| 1599 | fi |
| 1600 | return 1 |
| 1601 | } |
Junio C Hamano | 39cadee | 2016-07-19 13:22:20 -0700 | [diff] [blame] | 1602 | |
Jeff King | 4886081 | 2016-06-30 05:07:54 -0400 | [diff] [blame] | 1603 | # Read up to "$1" bytes (or to EOF) from stdin and write them to stdout. |
| 1604 | test_copy_bytes () { |
| 1605 | perl -e ' |
| 1606 | my $len = $ARGV[1]; |
| 1607 | while ($len > 0) { |
| 1608 | my $s; |
| 1609 | my $nread = sysread(STDIN, $s, $len); |
| 1610 | die "cannot read: $!" unless defined($nread); |
Jeff King | f7f6dc3 | 2017-07-16 06:45:32 -0400 | [diff] [blame] | 1611 | last unless $nread; |
Jeff King | 4886081 | 2016-06-30 05:07:54 -0400 | [diff] [blame] | 1612 | print $s; |
| 1613 | $len -= $nread; |
| 1614 | } |
| 1615 | ' - "$1" |
| 1616 | } |
Jeff King | de95302 | 2016-12-15 21:30:12 -0500 | [diff] [blame] | 1617 | |
| 1618 | # run "$@" inside a non-git directory |
| 1619 | nongit () { |
| 1620 | test -d non-repo || |
| 1621 | mkdir non-repo || |
| 1622 | return 1 |
| 1623 | |
| 1624 | ( |
| 1625 | GIT_CEILING_DIRECTORIES=$(pwd) && |
| 1626 | export GIT_CEILING_DIRECTORIES && |
| 1627 | cd non-repo && |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1628 | "$@" 2>&7 |
Jeff King | de95302 | 2016-12-15 21:30:12 -0500 | [diff] [blame] | 1629 | ) |
SZEDER Gábor | a5bf824 | 2018-02-25 14:40:15 +0100 | [diff] [blame] | 1630 | } 7>&2 2>&4 |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 1631 | |
Ævar Arnfjörð Bjarmason | 64f0109 | 2021-07-16 17:41:33 +0200 | [diff] [blame] | 1632 | # These functions are historical wrappers around "test-tool pkt-line" |
| 1633 | # for older tests. Use "test-tool pkt-line" itself in new tests. |
Felipe Contreras | 1ab7e00 | 2020-11-30 18:46:49 -0600 | [diff] [blame] | 1634 | packetize () { |
Jeff King | 88124ab | 2020-03-27 04:03:00 -0400 | [diff] [blame] | 1635 | if test $# -gt 0 |
| 1636 | then |
| 1637 | packet="$*" |
| 1638 | printf '%04x%s' "$((4 + ${#packet}))" "$packet" |
| 1639 | else |
Ævar Arnfjörð Bjarmason | 64f0109 | 2021-07-16 17:41:33 +0200 | [diff] [blame] | 1640 | test-tool pkt-line pack |
Jeff King | 88124ab | 2020-03-27 04:03:00 -0400 | [diff] [blame] | 1641 | fi |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 1642 | } |
| 1643 | |
Ævar Arnfjörð Bjarmason | 64f0109 | 2021-07-16 17:41:33 +0200 | [diff] [blame] | 1644 | packetize_raw () { |
| 1645 | test-tool pkt-line pack-raw-stdin |
| 1646 | } |
| 1647 | |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 1648 | depacketize () { |
Ævar Arnfjörð Bjarmason | 64f0109 | 2021-07-16 17:41:33 +0200 | [diff] [blame] | 1649 | test-tool pkt-line unpack |
Jeff King | 4414a15 | 2018-01-24 19:58:19 -0500 | [diff] [blame] | 1650 | } |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1651 | |
Taylor Blau | 5c07647 | 2019-04-04 20:37:42 -0700 | [diff] [blame] | 1652 | # Converts base-16 data into base-8. The output is given as a sequence of |
| 1653 | # escaped octals, suitable for consumption by 'printf'. |
| 1654 | hex2oct () { |
| 1655 | perl -ne 'printf "\\%03o", hex for /../g' |
| 1656 | } |
| 1657 | |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1658 | # Set the hash algorithm in use to $1. Only useful when testing the testsuite. |
| 1659 | test_set_hash () { |
| 1660 | test_hash_algo="$1" |
| 1661 | } |
| 1662 | |
| 1663 | # Detect the hash algorithm in use. |
| 1664 | test_detect_hash () { |
Eric W. Biederman | 48b16ab | 2023-10-01 21:40:31 -0500 | [diff] [blame] | 1665 | case "$GIT_TEST_DEFAULT_HASH" in |
| 1666 | "sha256") |
| 1667 | test_hash_algo=sha256 |
| 1668 | test_compat_hash_algo=sha1 |
| 1669 | ;; |
| 1670 | *) |
| 1671 | test_hash_algo=sha1 |
| 1672 | test_compat_hash_algo=sha256 |
| 1673 | ;; |
| 1674 | esac |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Patrick Steinhardt | 58aaf59 | 2023-12-29 08:26:56 +0100 | [diff] [blame] | 1677 | # Detect the hash algorithm in use. |
| 1678 | test_detect_ref_format () { |
| 1679 | echo "${GIT_TEST_DEFAULT_REF_FORMAT:-files}" |
| 1680 | } |
| 1681 | |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1682 | # Load common hash metadata and common placeholder object IDs for use with |
| 1683 | # test_oid. |
| 1684 | test_oid_init () { |
| 1685 | test -n "$test_hash_algo" || test_detect_hash && |
| 1686 | test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" && |
| 1687 | test_oid_cache <"$TEST_DIRECTORY/oid-info/oid" |
| 1688 | } |
| 1689 | |
| 1690 | # Load key-value pairs from stdin suitable for use with test_oid. Blank lines |
| 1691 | # and lines starting with "#" are ignored. Keys must be shell identifier |
| 1692 | # characters. |
| 1693 | # |
| 1694 | # Examples: |
| 1695 | # rawsz sha1:20 |
| 1696 | # rawsz sha256:32 |
| 1697 | test_oid_cache () { |
| 1698 | local tag rest k v && |
| 1699 | |
| 1700 | { test -n "$test_hash_algo" || test_detect_hash; } && |
| 1701 | while read tag rest |
| 1702 | do |
| 1703 | case $tag in |
| 1704 | \#*) |
| 1705 | continue;; |
| 1706 | ?*) |
| 1707 | # non-empty |
| 1708 | ;; |
| 1709 | *) |
| 1710 | # blank line |
| 1711 | continue;; |
| 1712 | esac && |
| 1713 | |
| 1714 | k="${rest%:*}" && |
| 1715 | v="${rest#*:}" && |
| 1716 | |
| 1717 | if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null |
| 1718 | then |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 1719 | BUG 'bad hash algorithm' |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1720 | fi && |
| 1721 | eval "test_oid_${k}_$tag=\"\$v\"" |
| 1722 | done |
| 1723 | } |
| 1724 | |
| 1725 | # Look up a per-hash value based on a key ($1). The value must have been loaded |
| 1726 | # by test_oid_init or test_oid_cache. |
| 1727 | test_oid () { |
brian m. carlson | ceaa4b3 | 2020-07-29 23:14:23 +0000 | [diff] [blame] | 1728 | local algo="${test_hash_algo}" && |
| 1729 | |
| 1730 | case "$1" in |
Eric W. Biederman | 48b16ab | 2023-10-01 21:40:31 -0500 | [diff] [blame] | 1731 | --hash=storage) |
| 1732 | algo="$test_hash_algo" && |
| 1733 | shift;; |
| 1734 | --hash=compat) |
| 1735 | algo="$test_compat_hash_algo" && |
| 1736 | shift;; |
brian m. carlson | ceaa4b3 | 2020-07-29 23:14:23 +0000 | [diff] [blame] | 1737 | --hash=*) |
| 1738 | algo="${1#--hash=}" && |
| 1739 | shift;; |
| 1740 | *) |
| 1741 | ;; |
| 1742 | esac && |
| 1743 | |
| 1744 | local var="test_oid_${algo}_$1" && |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1745 | |
| 1746 | # If the variable is unset, we must be missing an entry for this |
| 1747 | # key-hash pair, so exit with an error. |
| 1748 | if eval "test -z \"\${$var+set}\"" |
| 1749 | then |
SZEDER Gábor | 165293a | 2018-11-19 14:13:26 +0100 | [diff] [blame] | 1750 | BUG "undefined key '$1'" |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1751 | fi && |
SZEDER Gábor | a48a880 | 2022-12-18 17:29:05 +0100 | [diff] [blame] | 1752 | eval "printf '%s\n' \"\${$var}\"" |
brian m. carlson | 2c02b11 | 2018-09-13 05:17:31 +0000 | [diff] [blame] | 1753 | } |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1754 | |
brian m. carlson | 56d8892 | 2019-06-28 22:59:19 +0000 | [diff] [blame] | 1755 | # Insert a slash into an object ID so it can be used to reference a location |
| 1756 | # under ".git/objects". For example, "deadbeef..." becomes "de/adbeef..". |
| 1757 | test_oid_to_path () { |
Junio C Hamano | e97f4a6 | 2024-04-05 17:09:01 -0700 | [diff] [blame] | 1758 | local basename="${1#??}" |
Jonathan Nieder | 1c1f6e0 | 2019-08-07 23:56:14 -0700 | [diff] [blame] | 1759 | echo "${1%$basename}/$basename" |
brian m. carlson | 56d8892 | 2019-06-28 22:59:19 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Neeraj Singh | fb2d0db | 2022-04-04 22:20:15 -0700 | [diff] [blame] | 1762 | # Parse oids from git ls-files --staged output |
| 1763 | test_parse_ls_files_stage_oids () { |
| 1764 | awk '{print $2}' - |
| 1765 | } |
| 1766 | |
| 1767 | # Parse oids from git ls-tree output |
| 1768 | test_parse_ls_tree_oids () { |
| 1769 | awk '{print $3}' - |
| 1770 | } |
| 1771 | |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1772 | # Choose a port number based on the test script's number and store it in |
| 1773 | # the given variable name, unless that variable already contains a number. |
| 1774 | test_set_port () { |
Junio C Hamano | 341aad8 | 2024-04-05 17:08:59 -0700 | [diff] [blame] | 1775 | local var="$1" port |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1776 | |
| 1777 | if test $# -ne 1 || test -z "$var" |
| 1778 | then |
| 1779 | BUG "test_set_port requires a variable name" |
| 1780 | fi |
| 1781 | |
| 1782 | eval port=\$$var |
| 1783 | case "$port" in |
| 1784 | "") |
| 1785 | # No port is set in the given env var, use the test |
| 1786 | # number as port number instead. |
| 1787 | # Remove not only the leading 't', but all leading zeros |
| 1788 | # as well, so the arithmetic below won't (mis)interpret |
| 1789 | # a test number like '0123' as an octal value. |
| 1790 | port=${this_test#${this_test%%[1-9]*}} |
| 1791 | if test "${port:-0}" -lt 1024 |
| 1792 | then |
| 1793 | # root-only port, use a larger one instead. |
| 1794 | port=$(($port + 10000)) |
| 1795 | fi |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1796 | ;; |
SZEDER Gábor | 7d661e5 | 2019-02-11 20:58:03 +0100 | [diff] [blame] | 1797 | *[!0-9]*|0*) |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1798 | error >&7 "invalid port number: $port" |
| 1799 | ;; |
| 1800 | *) |
| 1801 | # The user has specified the port. |
| 1802 | ;; |
| 1803 | esac |
SZEDER Gábor | fb7d1e3 | 2019-01-05 02:08:59 +0100 | [diff] [blame] | 1804 | |
| 1805 | # Make sure that parallel '--stress' test jobs get different |
| 1806 | # ports. |
| 1807 | port=$(($port + ${GIT_TEST_STRESS_JOB_NR:-0})) |
| 1808 | eval $var=$port |
SZEDER Gábor | fa84058 | 2019-01-05 02:08:58 +0100 | [diff] [blame] | 1809 | } |
Jeff King | ea047a8 | 2020-02-14 13:22:25 -0500 | [diff] [blame] | 1810 | |
Johannes Schindelin | 176a66a | 2020-04-11 13:40:22 +0000 | [diff] [blame] | 1811 | # Tests for the hidden file attribute on Windows |
| 1812 | test_path_is_hidden () { |
| 1813 | test_have_prereq MINGW || |
| 1814 | BUG "test_path_is_hidden can only be used on Windows" |
| 1815 | |
Johannes Schindelin | 7c2dfca | 2020-04-11 13:40:20 +0000 | [diff] [blame] | 1816 | # Use the output of `attrib`, ignore the absolute path |
Johannes Schindelin | 9814d0a | 2020-04-11 13:40:21 +0000 | [diff] [blame] | 1817 | case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac |
Johannes Schindelin | 7c2dfca | 2020-04-11 13:40:20 +0000 | [diff] [blame] | 1818 | return 1 |
| 1819 | } |
Derrick Stolee | 2057d75 | 2020-09-17 18:11:42 +0000 | [diff] [blame] | 1820 | |
Ævar Arnfjörð Bjarmason | 8f788eb | 2022-12-22 15:14:08 +0000 | [diff] [blame] | 1821 | # Poor man's URI escaping. Good enough for the test suite whose trash |
| 1822 | # directory has a space in it. See 93c3fcbe4d4 (git-svn: attempt to |
| 1823 | # mimic SVN 1.7 URL canonicalization, 2012-07-28) for prior art. |
| 1824 | test_uri_escape() { |
| 1825 | sed 's/ /%20/g' |
| 1826 | } |
| 1827 | |
Derrick Stolee | 2057d75 | 2020-09-17 18:11:42 +0000 | [diff] [blame] | 1828 | # Check that the given command was invoked as part of the |
| 1829 | # trace2-format trace on stdin. |
| 1830 | # |
| 1831 | # test_subcommand [!] <command> <args>... < <trace> |
| 1832 | # |
| 1833 | # For example, to look for an invocation of "git upload-pack |
| 1834 | # /path/to/repo" |
| 1835 | # |
| 1836 | # GIT_TRACE2_EVENT=event.log git fetch ... && |
| 1837 | # test_subcommand git upload-pack "$PATH" <event.log |
| 1838 | # |
| 1839 | # If the first parameter passed is !, this instead checks that |
| 1840 | # the given command was not called. |
| 1841 | # |
| 1842 | test_subcommand () { |
| 1843 | local negate= |
| 1844 | if test "$1" = "!" |
| 1845 | then |
| 1846 | negate=t |
| 1847 | shift |
| 1848 | fi |
| 1849 | |
Junio C Hamano | 7f9f230 | 2024-04-05 17:09:00 -0700 | [diff] [blame] | 1850 | local expr="$(printf '"%s",' "$@")" |
Derrick Stolee | 2057d75 | 2020-09-17 18:11:42 +0000 | [diff] [blame] | 1851 | expr="${expr%,}" |
| 1852 | |
| 1853 | if test -n "$negate" |
| 1854 | then |
| 1855 | ! grep "\[$expr\]" |
| 1856 | else |
| 1857 | grep "\[$expr\]" |
| 1858 | fi |
| 1859 | } |
Derrick Stolee | 3b14436 | 2021-01-23 16:07:08 -0500 | [diff] [blame] | 1860 | |
| 1861 | # Check that the given command was invoked as part of the |
Derrick Stolee | 3b14436 | 2021-01-23 16:07:08 -0500 | [diff] [blame] | 1862 | # trace2-format trace on stdin. |
| 1863 | # |
| 1864 | # test_region [!] <category> <label> git <command> <args>... |
| 1865 | # |
| 1866 | # For example, to look for trace2_region_enter("index", "do_read_index", repo) |
| 1867 | # in an invocation of "git checkout HEAD~1", run |
| 1868 | # |
| 1869 | # GIT_TRACE2_EVENT="$(pwd)/trace.txt" GIT_TRACE2_EVENT_NESTING=10 \ |
| 1870 | # git checkout HEAD~1 && |
| 1871 | # test_region index do_read_index <trace.txt |
| 1872 | # |
| 1873 | # If the first parameter passed is !, this instead checks that |
| 1874 | # the given region was not entered. |
| 1875 | # |
| 1876 | test_region () { |
| 1877 | local expect_exit=0 |
| 1878 | if test "$1" = "!" |
| 1879 | then |
| 1880 | expect_exit=1 |
| 1881 | shift |
| 1882 | fi |
| 1883 | |
| 1884 | grep -e '"region_enter".*"category":"'"$1"'","label":"'"$2"\" "$3" |
| 1885 | exitcode=$? |
| 1886 | |
| 1887 | if test $exitcode != $expect_exit |
| 1888 | then |
| 1889 | return 1 |
| 1890 | fi |
| 1891 | |
| 1892 | grep -e '"region_leave".*"category":"'"$1"'","label":"'"$2"\" "$3" |
| 1893 | exitcode=$? |
| 1894 | |
| 1895 | if test $exitcode != $expect_exit |
| 1896 | then |
| 1897 | return 1 |
| 1898 | fi |
| 1899 | |
| 1900 | return 0 |
| 1901 | } |
Jeff King | 7c0afdf | 2021-06-18 12:32:22 -0400 | [diff] [blame] | 1902 | |
Taylor Blau | 3bea0c0 | 2023-12-14 17:24:39 -0500 | [diff] [blame] | 1903 | # Check that the given data fragment was included as part of the |
| 1904 | # trace2-format trace on stdin. |
| 1905 | # |
| 1906 | # test_trace2_data <category> <key> <value> |
| 1907 | # |
| 1908 | # For example, to look for trace2_data_intmax("pack-objects", repo, |
| 1909 | # "reused", N) in an invocation of "git pack-objects", run: |
| 1910 | # |
| 1911 | # GIT_TRACE2_EVENT="$(pwd)/trace.txt" git pack-objects ... && |
| 1912 | # test_trace2_data pack-objects reused N <trace2.txt |
| 1913 | test_trace2_data () { |
| 1914 | grep -e '"category":"'"$1"'","key":"'"$2"'","value":"'"$3"'"' |
| 1915 | } |
| 1916 | |
Derrick Stolee | 7bc73e7 | 2023-01-31 13:29:11 +0000 | [diff] [blame] | 1917 | # Given a GIT_TRACE2_EVENT log over stdin, writes to stdout a list of URLs |
| 1918 | # sent to git-remote-https child processes. |
| 1919 | test_remote_https_urls() { |
| 1920 | grep -e '"event":"child_start".*"argv":\["git-remote-https",".*"\]' | |
| 1921 | sed -e 's/{"event":"child_start".*"argv":\["git-remote-https","//g' \ |
| 1922 | -e 's/"\]}//g' |
| 1923 | } |
| 1924 | |
Jeff King | 7c0afdf | 2021-06-18 12:32:22 -0400 | [diff] [blame] | 1925 | # Print the destination of symlink(s) provided as arguments. Basically |
| 1926 | # the same as the readlink command, but it's not available everywhere. |
| 1927 | test_readlink () { |
| 1928 | perl -le 'print readlink($_) for @ARGV' "$@" |
| 1929 | } |
Marc Strapetz | ab6245b | 2022-01-07 11:17:28 +0000 | [diff] [blame] | 1930 | |
| 1931 | # Set mtime to a fixed "magic" timestamp in mid February 2009, before we |
| 1932 | # run an operation that may or may not touch the file. If the file was |
| 1933 | # touched, its timestamp will not accidentally have such an old timestamp, |
| 1934 | # as long as your filesystem clock is reasonably correct. To verify the |
| 1935 | # timestamp, follow up with test_is_magic_mtime. |
| 1936 | # |
| 1937 | # An optional increment to the magic timestamp may be specified as second |
| 1938 | # argument. |
| 1939 | test_set_magic_mtime () { |
Junio C Hamano | e97f4a6 | 2024-04-05 17:09:01 -0700 | [diff] [blame] | 1940 | local inc="${2:-0}" && |
Marc Strapetz | ab6245b | 2022-01-07 11:17:28 +0000 | [diff] [blame] | 1941 | local mtime=$((1234567890 + $inc)) && |
| 1942 | test-tool chmtime =$mtime "$1" && |
| 1943 | test_is_magic_mtime "$1" $inc |
| 1944 | } |
| 1945 | |
| 1946 | # Test whether the given file has the "magic" mtime set. This is meant to |
| 1947 | # be used in combination with test_set_magic_mtime. |
| 1948 | # |
| 1949 | # An optional increment to the magic timestamp may be specified as second |
| 1950 | # argument. Usually, this should be the same increment which was used for |
| 1951 | # the associated test_set_magic_mtime. |
| 1952 | test_is_magic_mtime () { |
Junio C Hamano | e97f4a6 | 2024-04-05 17:09:01 -0700 | [diff] [blame] | 1953 | local inc="${2:-0}" && |
Marc Strapetz | ab6245b | 2022-01-07 11:17:28 +0000 | [diff] [blame] | 1954 | local mtime=$((1234567890 + $inc)) && |
| 1955 | echo $mtime >.git/test-mtime-expect && |
| 1956 | test-tool chmtime --get "$1" >.git/test-mtime-actual && |
| 1957 | test_cmp .git/test-mtime-expect .git/test-mtime-actual |
| 1958 | local ret=$? |
| 1959 | rm -f .git/test-mtime-expect |
| 1960 | rm -f .git/test-mtime-actual |
| 1961 | return $ret |
| 1962 | } |
Ævar Arnfjörð Bjarmason | d796ced | 2022-10-12 12:52:32 +0000 | [diff] [blame] | 1963 | |
| 1964 | # Given two filenames, parse both using 'git config --list --file' |
| 1965 | # and compare the sorted output of those commands. Useful when |
| 1966 | # wanting to ignore whitespace differences and sorting concerns. |
| 1967 | test_cmp_config_output () { |
| 1968 | git config --list --file="$1" >config-expect && |
| 1969 | git config --list --file="$2" >config-actual && |
| 1970 | sort config-expect >sorted-expect && |
| 1971 | sort config-actual >sorted-actual && |
| 1972 | test_cmp sorted-expect sorted-actual |
| 1973 | } |
Derrick Stolee | da9acde | 2023-01-06 16:31:55 +0000 | [diff] [blame] | 1974 | |
| 1975 | # Given a filename, extract its trailing hash as a hex string |
| 1976 | test_trailing_hash () { |
| 1977 | local file="$1" && |
| 1978 | tail -c $(test_oid rawsz) "$file" | |
| 1979 | test-tool hexdump | |
| 1980 | sed "s/ //g" |
| 1981 | } |