blob: 427b375b39235d6b4c1dfe8a1e2c1ff032fe3788 [file] [log] [blame]
Jonathan Niederc74c7202013-11-25 13:03:06 -08001# Library of functions shared by all tests scripts, included by
2# test-lib.sh.
Thomas Rast12a29b12012-02-17 11:25:08 +01003#
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 Sorefd05b08c2023-11-24 03:35:13 +000017# along with this program. If not, see https://www.gnu.org/licenses/ .
Thomas Rast12a29b12012-02-17 11:25:08 +010018
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.
28test_set_editor () {
29 FAKE_EDITOR="$1"
30 export FAKE_EDITOR
31 EDITOR='"$FAKE_EDITOR"'
32 export EDITOR
33}
34
Phillip Wood666b6e12023-02-23 20:55:01 +000035# Like test_set_editor but sets GIT_SEQUENCE_EDITOR instead of EDITOR
36test_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 Rast12a29b12012-02-17 11:25:08 +010043test_decode_color () {
44 awk '
45 function name(n) {
46 if (n == 0) return "RESET";
47 if (n == 1) return "BOLD";
Stefan Beller991eb4f2018-08-13 18:41:15 -070048 if (n == 2) return "FAINT";
49 if (n == 3) return "ITALIC";
Jeff King097b6812017-07-13 10:58:41 -040050 if (n == 7) return "REVERSE";
Thomas Rast12a29b12012-02-17 11:25:08 +010051 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 Hostetlerb249e392016-08-11 10:46:01 -040090lf_to_nul () {
91 perl -pe 'y/\012/\000/'
92}
93
Thomas Rast12a29b12012-02-17 11:25:08 +010094nul_to_q () {
Jeff King94221d22013-10-28 21:23:03 -040095 perl -pe 'y/\000/Q/'
Thomas Rast12a29b12012-02-17 11:25:08 +010096}
97
98q_to_nul () {
Jeff King94221d22013-10-28 21:23:03 -040099 perl -pe 'y/Q/\000/'
Thomas Rast12a29b12012-02-17 11:25:08 +0100100}
101
102q_to_cr () {
103 tr Q '\015'
104}
105
106q_to_tab () {
107 tr Q '\011'
108}
109
Junio C Hamano250b3c62013-03-22 11:10:03 -0700110qz_to_tab_space () {
111 tr QZ '\011\040'
Thomas Rast12a29b12012-02-17 11:25:08 +0100112}
113
114append_cr () {
115 sed -e 's/$/Q/' | tr Q '\015'
116}
117
118remove_cr () {
119 tr '\015' Q | sed -e 's/Q$//'
120}
121
Thomas Rast12a29b12012-02-17 11:25:08 +0100122# 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
128sane_unset () {
129 unset "$@"
130 return 0
131}
132
133test_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ábor59210dd2017-03-18 17:14:00 +0100145# Stop execution and start a shell. This is useful for debugging tests.
Thomas Rast12a29b12012-02-17 11:25:08 +0100146#
147# Be sure to remove all invocations of this command before submitting.
Philippe Blainadd52402021-09-06 04:38:59 +0000148# 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 Rast12a29b12012-02-17 11:25:08 +0100167
168test_pause () {
Philippe Blainadd52402021-09-06 04:38:59 +0000169 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 Rast12a29b12012-02-17 11:25:08 +0100196}
197
Elijah Newren84243642018-04-24 16:46:45 -0700198# 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 Schindelin6a940882015-10-30 12:02:56 -0700200#
Philippe Blain01c38102021-09-06 04:39:00 +0000201# 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 Newren84243642018-04-24 16:46:45 -0700211# Examples:
212# debug git checkout master
213# debug --debugger=nemiver git $ARGS
214# debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS
Johannes Schindelin6a940882015-10-30 12:02:56 -0700215debug () {
Philippe Blain01c38102021-09-06 04:39:00 +0000216 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 Schindelin6a940882015-10-30 12:02:56 -0700252}
253
Patrick Steinhardt0497e6c2023-10-31 09:16:59 +0100254# 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.
261test_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.
291test_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ð Bjarmasonf21426e2021-01-12 21:17:56 +0100309# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]
310# -C <dir>:
311# Run all git commands in directory <dir>
Ævar Arnfjörð Bjarmason76b8b8d2021-01-12 21:17:57 +0100312# --notick
313# Do not call test_tick before making a commit
Ævar Arnfjörð Bjarmason33735182021-01-12 21:17:59 +0100314# --append
Ævar Arnfjörð Bjarmasoncb8fb7f2021-05-10 16:19:02 +0200315# Use ">>" instead of ">" when writing "<contents>" to "<file>"
Ævar Arnfjörð Bjarmason47c88d12021-05-10 16:19:06 +0200316# --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ð Bjarmason76b8b8d2021-01-12 21:17:57 +0100322# --signoff
323# Invoke "git commit" with --signoff
Denton Liuf9f30a02021-01-14 15:02:40 -0800324# --author <author>
325# Invoke "git commit" with --author <author>
Ævar Arnfjörð Bjarmason51442192021-05-10 16:19:03 +0200326# --no-tag
327# Do not tag the resulting commit
Ævar Arnfjörð Bjarmason6cf8d962021-05-10 16:19:04 +0200328# --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 Rast12a29b12012-02-17 11:25:08 +0100332#
333# This will commit a file with the given contents and the given commit
Brandon Casey4c994192013-02-12 02:17:30 -0800334# message, and tag the resulting commit with the given tag name.
Thomas Rast12a29b12012-02-17 11:25:08 +0100335#
Brandon Casey4c994192013-02-12 02:17:30 -0800336# <file>, <contents>, and <tag> all default to <message>.
Thomas Rast12a29b12012-02-17 11:25:08 +0100337
338test_commit () {
Philippe Blain455f0ad2022-10-21 15:13:31 +0000339 local notick= &&
340 local echo=echo &&
341 local append= &&
342 local author= &&
343 local signoff= &&
344 local indir= &&
345 local tag=light &&
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200346 while test $# != 0
347 do
348 case "$1" in
349 --notick)
350 notick=yes
351 ;;
Ævar Arnfjörð Bjarmason47c88d12021-05-10 16:19:06 +0200352 --printf)
353 echo=printf
354 ;;
Ævar Arnfjörð Bjarmason33735182021-01-12 21:17:59 +0100355 --append)
356 append=yes
357 ;;
Ævar Arnfjörð Bjarmason999cfc42021-01-12 21:17:58 +0100358 --author)
359 author="$2"
360 shift
361 ;;
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200362 --signoff)
363 signoff="$1"
364 ;;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000365 --date)
366 notick=yes
367 GIT_COMMITTER_DATE="$2"
368 GIT_AUTHOR_DATE="$2"
369 shift
370 ;;
Stefan Beller6f943512016-12-08 13:03:26 -0800371 -C)
372 indir="$2"
373 shift
374 ;;
Jeff King3803a3a2021-02-09 05:52:45 -0500375 --no-tag)
Ævar Arnfjörð Bjarmason6cf8d962021-05-10 16:19:04 +0200376 tag=none
377 ;;
378 --annotate)
379 tag=annotate
Jeff King3803a3a2021-02-09 05:52:45 -0500380 ;;
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200381 *)
382 break
383 ;;
384 esac
Junio C Hamano9a0231b2012-07-22 12:54:08 -0700385 shift
Miklos Vajna5ed75e22012-09-14 08:52:03 +0200386 done &&
Stefan Beller6f943512016-12-08 13:03:26 -0800387 indir=${indir:+"$indir"/} &&
Junio C Hamanoe97f4a62024-04-05 17:09:01 -0700388 local file="${2:-"$1.t"}" &&
Ævar Arnfjörð Bjarmason33735182021-01-12 21:17:59 +0100389 if test -n "$append"
390 then
Ævar Arnfjörð Bjarmason47c88d12021-05-10 16:19:06 +0200391 $echo "${3-$1}" >>"$indir$file"
Ævar Arnfjörð Bjarmason33735182021-01-12 21:17:59 +0100392 else
Ævar Arnfjörð Bjarmason47c88d12021-05-10 16:19:06 +0200393 $echo "${3-$1}" >"$indir$file"
Ævar Arnfjörð Bjarmason33735182021-01-12 21:17:59 +0100394 fi &&
Ævar Arnfjörð Bjarmasone3c36752022-03-17 19:08:39 +0100395 git ${indir:+ -C "$indir"} add -- "$file" &&
Junio C Hamano9a0231b2012-07-22 12:54:08 -0700396 if test -z "$notick"
397 then
398 test_tick
399 fi &&
Ævar Arnfjörð Bjarmason999cfc42021-01-12 21:17:58 +0100400 git ${indir:+ -C "$indir"} commit \
401 ${author:+ --author "$author"} \
402 $signoff -m "$1" &&
Ævar Arnfjörð Bjarmason6cf8d962021-05-10 16:19:04 +0200403 case "$tag" in
404 none)
405 ;;
406 light)
Jeff King3803a3a2021-02-09 05:52:45 -0500407 git ${indir:+ -C "$indir"} tag "${4:-$1}"
Ævar Arnfjörð Bjarmason6cf8d962021-05-10 16:19:04 +0200408 ;;
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 Rast12a29b12012-02-17 11:25:08 +0100417}
418
419# Call test_merge with the arguments "<message> <commit>", where <commit>
420# can be a tag pointing to the commit-to-merge.
421
422test_merge () {
Denton Liu94ba1512019-10-03 17:23:13 -0700423 label="$1" &&
424 shift &&
Thomas Rast12a29b12012-02-17 11:25:08 +0100425 test_tick &&
Denton Liu94ba1512019-10-03 17:23:13 -0700426 git merge -m "$label" "$@" &&
427 git tag "$label"
Thomas Rast12a29b12012-02-17 11:25:08 +0100428}
429
Jeff Kingb1c36cb2019-07-02 01:16:49 -0400430# 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#
456test_commit_bulk () {
457 tmpfile=.bulk-commit.input
458 indir=.
459 ref=HEAD
460 n=1
Taylor Blau8e414682024-05-23 17:27:08 -0400461 notick=
Jeff Kingb1c36cb2019-07-02 01:16:49 -0400462 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 Blau8e414682024-05-23 17:27:08 -0400492 --notick)
493 notick=yes
494 ;;
Jeff Kingb1c36cb2019-07-02 01:16:49 -0400495 -*)
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áborfc42f202019-11-25 13:59:07 +0100507 if git -C "$indir" rev-parse --quiet --verify "$ref"
Jeff Kingb1c36cb2019-07-02 01:16:49 -0400508 then
509 add_from=t
510 fi
511
512 while test "$total" -gt 0
513 do
Taylor Blau8e414682024-05-23 17:27:08 -0400514 if test -z "$notick"
515 then
516 test_tick
517 fi &&
Jeff Kingb1c36cb2019-07-02 01:16:49 -0400518 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 Rast12a29b12012-02-17 11:25:08 +0100560# 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
564test_chmod () {
565 chmod "$@" &&
566 git update-index --add "--chmod=$@"
567}
568
Matheus Tavaresea8bbf22021-01-05 12:47:39 -0300569# 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 Couder73de1c92017-06-25 06:34:28 +0200574test_modebits () {
Matheus Tavaresea8bbf22021-01-05 12:47:39 -0300575 ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \
576 -e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|'
Christian Couder73de1c92017-06-25 06:34:28 +0200577}
578
Thomas Rast12a29b12012-02-17 11:25:08 +0100579# Unset a configuration variable, but don't fail if it doesn't exist.
580test_unconfig () {
John Keeping5fafc072015-09-05 14:12:47 +0100581 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 Rast12a29b12012-02-17 11:25:08 +0100589 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.
599test_config () {
John Keeping5fafc072015-09-05 14:12:47 +0100600 config_dir=
601 if test "$1" = -C
602 then
603 shift
604 config_dir=$1
605 shift
606 fi
Victoria Dye847d0022023-05-26 01:32:58 +0000607
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 Rast12a29b12012-02-17 11:25:08 +0100618}
619
620test_config_global () {
621 test_when_finished "test_unconfig --global '$1'" &&
622 git config --global "$@"
623}
624
625write_script () {
626 {
627 echo "#!${2-"$SHELL_PATH"}" &&
628 cat
629 } >"$1" &&
630 chmod +x "$1"
631}
632
Ævar Arnfjörð Bjarmason7da7f632022-03-17 11:13:06 +0100633# 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ð Bjarmason66865d12022-03-17 11:13:16 +0100644# --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ð Bjarmason7da7f632022-03-17 11:13:06 +0100648test_hook () {
649 setup= &&
650 clobber= &&
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100651 disable= &&
652 remove= &&
Ævar Arnfjörð Bjarmason7da7f632022-03-17 11:13:06 +0100653 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ð Bjarmason66865d12022-03-17 11:13:16 +0100667 --disable)
668 disable=t
669 ;;
670 --remove)
671 remove=t
672 ;;
Ævar Arnfjörð Bjarmason7da7f632022-03-17 11:13:06 +0100673 -*)
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ð Bjarmason66865d12022-03-17 11:13:16 +0100686 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ð Bjarmason7da7f632022-03-17 11:13:06 +0100698 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 Rast12a29b12012-02-17 11:25:08 +0100709# 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ð Bjarmason5beca492022-07-28 01:13:37 +0200715# test_expect_{success,failure}
Thomas Rast12a29b12012-02-17 11:25:08 +0100716#
717# The single parameter is the prerequisite tag (a simple word, in all
718# capital letters by convention).
719
Johannes Schindelin7d0ee472018-04-29 00:33:36 +0200720test_unset_prereq () {
721 ! test_have_prereq "$1" ||
722 satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }"
723}
724
Thomas Rast12a29b12012-02-17 11:25:08 +0100725test_set_prereq () {
Ævar Arnfjörð Bjarmasonc7400392019-06-21 12:18:12 +0200726 if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL"
Ævar Arnfjörð Bjarmasondfe1a172019-05-13 20:32:42 +0200727 then
728 case "$1" in
729 # The "!" case is handled below with
730 # test_unset_prereq()
731 !*)
732 ;;
Derrick Stolee0011f942022-07-19 18:32:16 +0000733 # List of things we can't easily pretend to not support
Ævar Arnfjörð Bjarmasondfe1a172019-05-13 20:32:42 +0200734 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 Schindelin7d0ee472018-04-29 00:33:36 +0200745 case "$1" in
746 !*)
747 test_unset_prereq "${1#!}"
748 ;;
749 *)
750 satisfied_prereq="$satisfied_prereq$1 "
751 ;;
752 esac
Thomas Rast12a29b12012-02-17 11:25:08 +0100753}
Junio C Hamanof3cfc3b2012-07-26 13:57:56 -0700754satisfied_prereq=" "
Junio C Hamano04083f22012-07-26 15:50:45 -0700755lazily_testable_prereq= lazily_tested_prereq=
756
757# Usage: test_lazy_prereq PREREQ 'script'
758test_lazy_prereq () {
759 lazily_testable_prereq="$lazily_testable_prereq$1 "
760 eval test_prereq_lazily_$1=\$2
761}
762
763test_run_lazy_prereq_ () {
764 script='
SZEDER Gábor53ff3b92020-11-18 20:04:13 +0100765mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&
Junio C Hamano04083f22012-07-26 15:50:45 -0700766(
SZEDER Gábor53ff3b92020-11-18 20:04:13 +0100767 cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"'
Junio C Hamano04083f22012-07-26 15:50:45 -0700768)'
769 say >&3 "checking prerequisite: $1"
770 say >&3 "$script"
771 test_eval_ "$script"
772 eval_ret=$?
SZEDER Gábor53ff3b92020-11-18 20:04:13 +0100773 rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1"
Junio C Hamano04083f22012-07-26 15:50:45 -0700774 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 Rast12a29b12012-02-17 11:25:08 +0100781
782test_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 Kingbdccd3c2012-11-14 16:33:25 -0800795 case "$prerequisite" in
796 !*)
797 negative_prereq=t
798 prerequisite=${prerequisite#!}
799 ;;
800 *)
801 negative_prereq=
802 esac
803
Junio C Hamano04083f22012-07-26 15:50:45 -0700804 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 Rast12a29b12012-02-17 11:25:08 +0100820 total_prereq=$(($total_prereq + 1))
Junio C Hamanof3cfc3b2012-07-26 13:57:56 -0700821 case "$satisfied_prereq" in
Thomas Rast12a29b12012-02-17 11:25:08 +0100822 *" $prerequisite "*)
Jeff Kingbdccd3c2012-11-14 16:33:25 -0800823 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 Rast12a29b12012-02-17 11:25:08 +0100831 ok_prereq=$(($ok_prereq + 1))
832 ;;
833 *)
Jeff Kingbdccd3c2012-11-14 16:33:25 -0800834 # Keep a list of missing prerequisites; restore
835 # the negative marker if necessary.
836 prerequisite=${negative_prereq:+!}$prerequisite
Fabian Stelzer5024ade2021-11-20 16:04:00 +0100837
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 Rast12a29b12012-02-17 11:25:08 +0100848 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
860test_declared_prereq () {
861 case ",$test_prereq," in
862 *,$1,*)
863 return 0
864 ;;
865 esac
866 return 1
867}
868
Junio C Hamanod93d5d52015-04-26 15:18:45 -0700869test_verify_prereq () {
870 test -z "$test_prereq" ||
871 expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100872 BUG "'$test_prereq' does not look like a prereq"
Junio C Hamanod93d5d52015-04-26 15:18:45 -0700873}
874
Thomas Rast12a29b12012-02-17 11:25:08 +0100875test_expect_failure () {
Johannes Schindelin0f5ae592022-05-21 22:18:51 +0000876 test_start_ "$@"
Thomas Rast12a29b12012-02-17 11:25:08 +0100877 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
878 test "$#" = 2 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100879 BUG "not 2 or 3 parameters to test-expect-failure"
Junio C Hamanod93d5d52015-04-26 15:18:45 -0700880 test_verify_prereq
Thomas Rast12a29b12012-02-17 11:25:08 +0100881 export test_prereq
882 if ! test_skip "$@"
883 then
Victoria Dye110e9112022-05-21 22:18:53 +0000884 test -n "$test_skip_test_preamble" ||
SZEDER Gáborffe1afe2019-08-05 23:04:47 +0200885 say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2"
Thomas Rast12a29b12012-02-17 11:25:08 +0100886 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 Rastae753422013-06-18 14:25:59 +0200893 test_finish_
Thomas Rast12a29b12012-02-17 11:25:08 +0100894}
895
896test_expect_success () {
Johannes Schindelin0f5ae592022-05-21 22:18:51 +0000897 test_start_ "$@"
Thomas Rast12a29b12012-02-17 11:25:08 +0100898 test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
899 test "$#" = 2 ||
SZEDER Gábor165293a2018-11-19 14:13:26 +0100900 BUG "not 2 or 3 parameters to test-expect-success"
Junio C Hamanod93d5d52015-04-26 15:18:45 -0700901 test_verify_prereq
Thomas Rast12a29b12012-02-17 11:25:08 +0100902 export test_prereq
903 if ! test_skip "$@"
904 then
Victoria Dye110e9112022-05-21 22:18:53 +0000905 test -n "$test_skip_test_preamble" ||
SZEDER Gáborffe1afe2019-08-05 23:04:47 +0200906 say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2"
Thomas Rast12a29b12012-02-17 11:25:08 +0100907 if test_run_ "$2"
908 then
909 test_ok_ "$1"
910 else
911 test_failure_ "$@"
912 fi
913 fi
Thomas Rastae753422013-06-18 14:25:59 +0200914 test_finish_
Thomas Rast12a29b12012-02-17 11:25:08 +0100915}
916
Thomas Rast12a29b12012-02-17 11:25:08 +0100917# debugging-friendly alternatives to "test [-f|-d|-e]"
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100918# The commands test the existence or non-existence of $1
Thomas Rast12a29b12012-02-17 11:25:08 +0100919test_path_is_file () {
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100920 test "$#" -ne 1 && BUG "1 param"
David Aguilar9e8f8de2014-10-15 01:35:21 -0700921 if ! test -f "$1"
Thomas Rast12a29b12012-02-17 11:25:08 +0100922 then
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100923 echo "File $1 doesn't exist"
Thomas Rast12a29b12012-02-17 11:25:08 +0100924 false
925 fi
926}
927
COGONI Guillaume456296b2022-02-22 22:54:29 +0100928test_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 Rast12a29b12012-02-17 11:25:08 +0100938test_path_is_dir () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +0100939 test "$#" -ne 1 && BUG "1 param"
David Aguilar9e8f8de2014-10-15 01:35:21 -0700940 if ! test -d "$1"
Thomas Rast12a29b12012-02-17 11:25:08 +0100941 then
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100942 echo "Directory $1 doesn't exist"
Thomas Rast12a29b12012-02-17 11:25:08 +0100943 false
944 fi
945}
946
COGONI Guillaume456296b2022-02-22 22:54:29 +0100947test_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 Newren7e9055b2018-08-08 09:31:06 -0700957test_path_exists () {
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100958 test "$#" -ne 1 && BUG "1 param"
Elijah Newren7e9055b2018-08-08 09:31:06 -0700959 if ! test -e "$1"
960 then
Ævar Arnfjörð Bjarmason45a26862021-02-12 14:29:41 +0100961 echo "Path $1 doesn't exist"
Elijah Newren7e9055b2018-08-08 09:31:06 -0700962 false
963 fi
964}
965
COGONI Guillaume456296b2022-02-22 22:54:29 +0100966test_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. carlsond6546af2023-06-27 16:18:56 +0000975test_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 Lehmann0be7d9b2014-06-19 22:12:23 +0200984# Check if the directory exists and is empty as expected, barf otherwise.
985test_dir_is_empty () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +0100986 test "$#" -ne 1 && BUG "1 param"
Jens Lehmann0be7d9b2014-06-19 22:12:23 +0200987 test_path_is_dir "$1" &&
Đoàn Trần Công Danh81580fa2022-09-21 20:02:31 +0700988 if test -n "$(ls -a1 "$1" | grep -E -v '^\.\.?$')"
Jens Lehmann0be7d9b2014-06-19 22:12:23 +0200989 then
990 echo "Directory '$1' is not empty, it contains:"
991 ls -la "$1"
992 return 1
993 fi
994}
995
Rohit Ashiwal21d5ad92019-03-04 17:37:59 +0530996# Check if the file exists and has a size greater than zero
997test_file_not_empty () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +0100998 test "$#" = 2 && BUG "2 param"
Rohit Ashiwal21d5ad92019-03-04 17:37:59 +0530999 if ! test -s "$1"
1000 then
1001 echo "'$1' is not a non-empty file."
1002 false
1003 fi
1004}
1005
Thomas Rast12a29b12012-02-17 11:25:08 +01001006test_path_is_missing () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +01001007 test "$#" -ne 1 && BUG "1 param"
David Aguilar9e8f8de2014-10-15 01:35:21 -07001008 if test -e "$1"
Thomas Rast12a29b12012-02-17 11:25:08 +01001009 then
1010 echo "Path exists:"
1011 ls -ld "$1"
Thomas Rast12a29b12012-02-17 11:25:08 +01001012 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
1027test_line_count () {
1028 if test $# != 3
1029 then
SZEDER Gábor165293a2018-11-19 14:13:26 +01001030 BUG "not 3 parameters to test_line_count"
Thomas Rast12a29b12012-02-17 11:25:08 +01001031 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 Danhcdff1bb2021-07-04 12:46:10 +07001039# 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
1047test_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 Schindelin53b67a82020-11-07 01:12:57 +00001065test_file_size () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +01001066 test "$#" -ne 1 && BUG "1 param"
Johannes Schindelin53b67a82020-11-07 01:12:57 +00001067 test-tool path-utils file-size "$1"
1068}
1069
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001070# 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
1076list_contains () {
1077 case ",$1," in
1078 *,$2,*)
1079 return 0
1080 ;;
1081 esac
1082 return 1
1083}
1084
Denton Liu6a67c752020-07-07 02:04:38 -04001085# 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.
1089test_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 Stolee008217c2023-01-27 20:06:01 +00001107 git|__git*|scalar|test-tool|test_terminal)
Denton Liu6a67c752020-07-07 02:04:38 -04001108 return 0
1109 ;;
1110 *)
1111 return 1
1112 ;;
1113 esac
1114}
1115
Thomas Rast12a29b12012-02-17 11:25:08 +01001116# 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ábor12e31a62018-02-09 03:42:33 +01001127#
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 Liu6a67c752020-07-07 02:04:38 -04001135#
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 Rast12a29b12012-02-17 11:25:08 +01001146
1147test_must_fail () {
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001148 case "$1" in
1149 ok=*)
1150 _test_ok=${1#ok=}
1151 shift
1152 ;;
1153 *)
1154 _test_ok=
1155 ;;
1156 esac
Denton Liu6a67c752020-07-07 02:04:38 -04001157 if ! test_must_fail_acceptable "$@"
1158 then
1159 echo >&7 "test_must_fail: only 'git' is allowed: $*"
1160 return 1
1161 fi
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001162 "$@" 2>&7
Thomas Rast12a29b12012-02-17 11:25:08 +01001163 exit_code=$?
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001164 if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
1165 then
Jeff King03aa3782018-02-22 01:48:37 -05001166 echo >&4 "test_must_fail: command succeeded: $*"
Thomas Rast12a29b12012-02-17 11:25:08 +01001167 return 1
Jeff King24724482016-06-24 15:45:04 -04001168 elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
Lars Schneider8bf4bec2015-11-27 10:15:14 +01001169 then
1170 return 0
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001171 elif test $exit_code -gt 129 && test $exit_code -le 192
1172 then
Jeff King03aa3782018-02-22 01:48:37 -05001173 echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*"
Thomas Rast12a29b12012-02-17 11:25:08 +01001174 return 1
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001175 elif test $exit_code -eq 127
1176 then
Jeff King03aa3782018-02-22 01:48:37 -05001177 echo >&4 "test_must_fail: command not found: $*"
Thomas Rast12a29b12012-02-17 11:25:08 +01001178 return 1
Lars Schneiderbbfe5302015-11-27 10:15:13 +01001179 elif test $exit_code -eq 126
1180 then
Jeff King03aa3782018-02-22 01:48:37 -05001181 echo >&4 "test_must_fail: valgrind error: $*"
Thomas Rasteeb69132013-03-31 10:37:25 +02001182 return 1
Thomas Rast12a29b12012-02-17 11:25:08 +01001183 fi
1184 return 0
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001185} 7>&2 2>&4
Thomas Rast12a29b12012-02-17 11:25:08 +01001186
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ábor12e31a62018-02-09 03:42:33 +01001197#
1198# Accepts the same options as test_must_fail.
Thomas Rast12a29b12012-02-17 11:25:08 +01001199
1200test_might_fail () {
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001201 test_must_fail ok=success "$@" 2>&7
1202} 7>&2 2>&4
Thomas Rast12a29b12012-02-17 11:25:08 +01001203
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
1211test_expect_code () {
1212 want_code=$1
1213 shift
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001214 "$@" 2>&7
Thomas Rast12a29b12012-02-17 11:25:08 +01001215 exit_code=$?
1216 if test $exit_code = $want_code
1217 then
1218 return 0
1219 fi
1220
Jeff King03aa3782018-02-22 01:48:37 -05001221 echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
Thomas Rast12a29b12012-02-17 11:25:08 +01001222 return 1
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001223} 7>&2 2>&4
Thomas Rast12a29b12012-02-17 11:25:08 +01001224
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 Contreras1ab7e002020-11-30 18:46:49 -06001238test_cmp () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +01001239 test "$#" -ne 2 && BUG "2 param"
Junio C Hamano262d5ad2020-10-16 13:51:04 -07001240 eval "$GIT_TEST_CMP" '"$@"'
Thomas Rast12a29b12012-02-17 11:25:08 +01001241}
1242
Nguyễn Thái Ngọc Duya5db0b72018-10-21 16:02:27 +02001243# 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 Contreras1ab7e002020-11-30 18:46:49 -06001252test_cmp_config () {
Nguyễn Thái Ngọc Duya5db0b72018-10-21 16:02:27 +02001253 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 Kasalb93e6e32014-06-04 17:57:52 +02001266# test_cmp_bin - helper to compare binary files
1267
Felipe Contreras1ab7e002020-11-30 18:46:49 -06001268test_cmp_bin () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +01001269 test "$#" -ne 2 && BUG "2 param"
Junio C Hamano262d5ad2020-10-16 13:51:04 -07001270 cmp "$@"
Stepan Kasalb93e6e32014-06-04 17:57:52 +02001271}
1272
SZEDER Gábor0f591282018-02-08 16:56:54 +01001273test_i18ngrep () {
Junio C Hamano381a83d2024-03-02 09:13:51 -08001274 BUG "do not use test_i18ngrep---use test_grep instead"
Junio C Hamano2e87fca2023-10-31 14:23:29 +09001275}
1276
1277test_grep () {
SZEDER Gáborfd29d7b2018-02-08 16:56:55 +01001278 eval "last_arg=\${$#}"
1279
1280 test -f "$last_arg" ||
Junio C Hamano2e87fca2023-10-31 14:23:29 +09001281 BUG "test_grep requires a file to read as the last parameter"
SZEDER Gáborfd29d7b2018-02-08 16:56:55 +01001282
1283 if test $# -lt 2 ||
1284 { test "x!" = "x$1" && test $# -lt 3 ; }
1285 then
Shreyansh Paliwal37e8d792023-12-03 22:47:59 +05301286 BUG "too few parameters to test_grep"
SZEDER Gáborfd29d7b2018-02-08 16:56:55 +01001287 fi
1288
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001289 if test "x!" = "x$1"
SZEDER Gábor0f591282018-02-08 16:56:54 +01001290 then
1291 shift
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001292 ! grep "$@" && return 0
1293
Jeff King03aa3782018-02-22 01:48:37 -05001294 echo >&4 "error: '! grep $@' did find a match in:"
SZEDER Gábor0f591282018-02-08 16:56:54 +01001295 else
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001296 grep "$@" && return 0
1297
Jeff King03aa3782018-02-22 01:48:37 -05001298 echo >&4 "error: 'grep $@' didn't find a match in:"
SZEDER Gábor0f591282018-02-08 16:56:54 +01001299 fi
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001300
1301 if test -s "$last_arg"
1302 then
Jeff King03aa3782018-02-22 01:48:37 -05001303 cat >&4 "$last_arg"
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001304 else
Jeff King03aa3782018-02-22 01:48:37 -05001305 echo >&4 "<File '$last_arg' is empty>"
SZEDER Gábor63b1a172018-02-08 16:56:56 +01001306 fi
1307
1308 return 1
SZEDER Gábor0f591282018-02-08 16:56:54 +01001309}
1310
Junio C Hamanoca8d1482013-06-09 11:29:20 -07001311# Check if the file expected to be empty is indeed empty, and barfs
1312# otherwise.
1313
1314test_must_be_empty () {
Ævar Arnfjörð Bjarmasone7884b32021-02-12 14:29:42 +01001315 test "$#" -ne 1 && BUG "1 param"
SZEDER Gábor9eb23082018-03-26 15:11:24 +02001316 test_path_is_file "$1" &&
1317 if test -s "$1"
Junio C Hamanoca8d1482013-06-09 11:29:20 -07001318 then
1319 echo "'$1' is not empty, it contains:"
1320 cat "$1"
1321 return 1
1322 fi
1323}
1324
Denton Liu2c9e1252019-11-12 15:07:45 -08001325# 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 Zweigbergk5d772982012-12-21 11:10:10 -08001328test_cmp_rev () {
Denton Liu2c9e1252019-11-12 15:07:45 -08001329 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ábor30d0b6d2018-11-19 14:28:18 +01001337 if test $# != 2
1338 then
Ævar Arnfjörð Bjarmason9e9c7dd2021-02-09 22:41:49 +01001339 BUG "test_cmp_rev requires two revisions, but got $#"
SZEDER Gábor30d0b6d2018-11-19 14:28:18 +01001340 else
1341 local r1 r2
1342 r1=$(git rev-parse --verify "$1") &&
Denton Liu2c9e1252019-11-12 15:07:45 -08001343 r2=$(git rev-parse --verify "$2") || return 1
1344
1345 if ! test "$r1" "$op" "$r2"
SZEDER Gábor30d0b6d2018-11-19 14:28:18 +01001346 then
1347 cat >&4 <<-EOF
Denton Liu2c9e1252019-11-12 15:07:45 -08001348 error: two revisions point to $wrong_result objects:
SZEDER Gábor30d0b6d2018-11-19 14:28:18 +01001349 '$1': $r1
1350 '$2': $r2
1351 EOF
1352 return 1
1353 fi
1354 fi
Martin von Zweigbergk5d772982012-12-21 11:10:10 -08001355}
1356
Phillip Wood6ce7afe2023-08-03 13:09:35 +00001357# 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
1364test_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 Schindelined33bd82019-06-24 19:40:05 +02001390# Compare paths respecting core.ignoreCase
1391test_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 Hamano55672a32016-05-09 11:36:09 -07001405# Print a sequence of integers in increasing order, either with
1406# two arguments (start and end):
Michał Kiedrowiczd17cf5f2012-08-04 00:21:04 +02001407#
Junio C Hamano55672a32016-05-09 11:36:09 -07001408# 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ł Kiedrowiczd17cf5f2012-08-04 00:21:04 +02001412
1413test_seq () {
1414 case $# in
1415 1) set 1 "$@" ;;
1416 2) ;;
SZEDER Gábor165293a2018-11-19 14:13:26 +01001417 *) BUG "not 1 or 2 parameters to test_seq" ;;
Michał Kiedrowiczd17cf5f2012-08-04 00:21:04 +02001418 esac
Junio C Hamano4df43132016-05-09 12:37:01 -07001419 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ł Kiedrowiczd17cf5f2012-08-04 00:21:04 +02001425}
1426
Thomas Rast12a29b12012-02-17 11:25:08 +01001427# 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
1450test_when_finished () {
John Keeping0968f122015-09-05 14:12:49 +01001451 # 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ábor165293a2018-11-19 14:13:26 +01001455 BUG "test_when_finished does nothing in a subshell"
Thomas Rast12a29b12012-02-17 11:25:08 +01001456 test_cleanup="{ $*
1457 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
1458}
1459
Johannes Schindelin900721e2019-03-13 13:24:11 +01001460# 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
1478test_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ð Bjarmason9e9c7dd2021-02-09 22:41:49 +01001483 BUG "test_atexit does nothing in a subshell"
Johannes Schindelin900721e2019-03-13 13:24:11 +01001484 test_atexit_cleanup="{ $*
1485 } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
1486}
1487
Ævar Arnfjörð Bjarmasonf0d4d392021-05-10 16:19:10 +02001488# Deprecated wrapper for "git init", use "git init" directly instead
Thomas Rast12a29b12012-02-17 11:25:08 +01001489# Usage: test_create_repo <directory>
1490test_create_repo () {
Ævar Arnfjörð Bjarmasonf0d4d392021-05-10 16:19:10 +02001491 git init "$@"
Thomas Rast12a29b12012-02-17 11:25:08 +01001492}
Johannes Sixt9ce415d2013-06-07 22:53:27 +02001493
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
1499test_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 Sixt817d03e2015-02-23 19:14:47 +01001507 git update-index --add --cacheinfo 120000 $ln_s_obj "$2" &&
1508 # pick up stat info from the file
1509 git update-index "$2"
Johannes Sixt9ce415d2013-06-07 22:53:27 +02001510 fi
1511}
Johannes Sixt4d715ac2013-10-26 21:17:15 +02001512
Michael S. Tsirkinac9afcc2014-04-27 21:15:47 +03001513# This function writes out its parameters, one per line
1514test_write_lines () {
1515 printf "%s\n" "$@"
1516}
1517
Jeff Kinga0e0ec92013-10-28 21:22:07 -04001518perl () {
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001519 command "$PERL_PATH" "$@" 2>&7
1520} 7>&2 2>&4
Junio C Hamanoa3a9cff2013-11-04 14:58:01 -08001521
SZEDER Gábor43a2afe2019-11-22 14:14:36 +01001522# 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
1532test_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ð Bjarmason4a1baac2023-01-12 17:03:21 +01001538 test-tool env-helper --type=bool --default="$2" --exit-code "$1"
SZEDER Gábor43a2afe2019-11-22 14:14:36 +01001539 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 King83d842d2014-02-10 16:29:37 -05001550# Exit the test suite, either by skipping all remaining tests or by
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +02001551# 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 King83d842d2014-02-10 16:29:37 -05001554#
1555# The error/skip message should be given by $2.
1556#
1557test_skip_or_die () {
SZEDER Gábor43a2afe2019-11-22 14:14:36 +01001558 if ! test_bool_env "$1" false
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +02001559 then
Jeff King83d842d2014-02-10 16:29:37 -05001560 skip_all=$2
1561 test_done
Ævar Arnfjörð Bjarmason3b072c52019-06-21 12:18:11 +02001562 fi
1563 error "$2"
Jeff King83d842d2014-02-10 16:29:37 -05001564}
1565
Jeff Kingd2554c72016-06-01 03:04:26 -04001566# 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).
1569test_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ábora5bf8242018-02-25 14:40:15 +01001580 "$@" 2>&7
Jeff Kingd2554c72016-06-01 03:04:26 -04001581 exit
1582 ;;
1583 esac
1584 done
1585 )
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001586} 7>&2 2>&4
Jeff King48860812016-06-30 05:07:54 -04001587
Jeff King9b67c992016-06-30 04:16:18 -04001588# Returns true if the numeric exit code in "$2" represents the expected signal
1589# in "$1". Signals should be given numerically.
1590test_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 Hamano39cadee2016-07-19 13:22:20 -07001602
Jeff King48860812016-06-30 05:07:54 -04001603# Read up to "$1" bytes (or to EOF) from stdin and write them to stdout.
1604test_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 Kingf7f6dc32017-07-16 06:45:32 -04001611 last unless $nread;
Jeff King48860812016-06-30 05:07:54 -04001612 print $s;
1613 $len -= $nread;
1614 }
1615 ' - "$1"
1616}
Jeff Kingde953022016-12-15 21:30:12 -05001617
1618# run "$@" inside a non-git directory
1619nongit () {
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ábora5bf8242018-02-25 14:40:15 +01001628 "$@" 2>&7
Jeff Kingde953022016-12-15 21:30:12 -05001629 )
SZEDER Gábora5bf8242018-02-25 14:40:15 +01001630} 7>&2 2>&4
Jeff King4414a152018-01-24 19:58:19 -05001631
Ævar Arnfjörð Bjarmason64f01092021-07-16 17:41:33 +02001632# These functions are historical wrappers around "test-tool pkt-line"
1633# for older tests. Use "test-tool pkt-line" itself in new tests.
Felipe Contreras1ab7e002020-11-30 18:46:49 -06001634packetize () {
Jeff King88124ab2020-03-27 04:03:00 -04001635 if test $# -gt 0
1636 then
1637 packet="$*"
1638 printf '%04x%s' "$((4 + ${#packet}))" "$packet"
1639 else
Ævar Arnfjörð Bjarmason64f01092021-07-16 17:41:33 +02001640 test-tool pkt-line pack
Jeff King88124ab2020-03-27 04:03:00 -04001641 fi
Jeff King4414a152018-01-24 19:58:19 -05001642}
1643
Ævar Arnfjörð Bjarmason64f01092021-07-16 17:41:33 +02001644packetize_raw () {
1645 test-tool pkt-line pack-raw-stdin
1646}
1647
Jeff King4414a152018-01-24 19:58:19 -05001648depacketize () {
Ævar Arnfjörð Bjarmason64f01092021-07-16 17:41:33 +02001649 test-tool pkt-line unpack
Jeff King4414a152018-01-24 19:58:19 -05001650}
brian m. carlson2c02b112018-09-13 05:17:31 +00001651
Taylor Blau5c076472019-04-04 20:37:42 -07001652# Converts base-16 data into base-8. The output is given as a sequence of
1653# escaped octals, suitable for consumption by 'printf'.
1654hex2oct () {
1655 perl -ne 'printf "\\%03o", hex for /../g'
1656}
1657
brian m. carlson2c02b112018-09-13 05:17:31 +00001658# Set the hash algorithm in use to $1. Only useful when testing the testsuite.
1659test_set_hash () {
1660 test_hash_algo="$1"
1661}
1662
1663# Detect the hash algorithm in use.
1664test_detect_hash () {
Eric W. Biederman48b16ab2023-10-01 21:40:31 -05001665 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. carlson2c02b112018-09-13 05:17:31 +00001675}
1676
Patrick Steinhardt58aaf592023-12-29 08:26:56 +01001677# Detect the hash algorithm in use.
1678test_detect_ref_format () {
1679 echo "${GIT_TEST_DEFAULT_REF_FORMAT:-files}"
1680}
1681
brian m. carlson2c02b112018-09-13 05:17:31 +00001682# Load common hash metadata and common placeholder object IDs for use with
1683# test_oid.
1684test_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
1697test_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ábor165293a2018-11-19 14:13:26 +01001719 BUG 'bad hash algorithm'
brian m. carlson2c02b112018-09-13 05:17:31 +00001720 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.
1727test_oid () {
brian m. carlsonceaa4b32020-07-29 23:14:23 +00001728 local algo="${test_hash_algo}" &&
1729
1730 case "$1" in
Eric W. Biederman48b16ab2023-10-01 21:40:31 -05001731 --hash=storage)
1732 algo="$test_hash_algo" &&
1733 shift;;
1734 --hash=compat)
1735 algo="$test_compat_hash_algo" &&
1736 shift;;
brian m. carlsonceaa4b32020-07-29 23:14:23 +00001737 --hash=*)
1738 algo="${1#--hash=}" &&
1739 shift;;
1740 *)
1741 ;;
1742 esac &&
1743
1744 local var="test_oid_${algo}_$1" &&
brian m. carlson2c02b112018-09-13 05:17:31 +00001745
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ábor165293a2018-11-19 14:13:26 +01001750 BUG "undefined key '$1'"
brian m. carlson2c02b112018-09-13 05:17:31 +00001751 fi &&
SZEDER Gábora48a8802022-12-18 17:29:05 +01001752 eval "printf '%s\n' \"\${$var}\""
brian m. carlson2c02b112018-09-13 05:17:31 +00001753}
SZEDER Gáborfa840582019-01-05 02:08:58 +01001754
brian m. carlson56d88922019-06-28 22:59:19 +00001755# 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..".
1757test_oid_to_path () {
Junio C Hamanoe97f4a62024-04-05 17:09:01 -07001758 local basename="${1#??}"
Jonathan Nieder1c1f6e02019-08-07 23:56:14 -07001759 echo "${1%$basename}/$basename"
brian m. carlson56d88922019-06-28 22:59:19 +00001760}
1761
Neeraj Singhfb2d0db2022-04-04 22:20:15 -07001762# Parse oids from git ls-files --staged output
1763test_parse_ls_files_stage_oids () {
1764 awk '{print $2}' -
1765}
1766
1767# Parse oids from git ls-tree output
1768test_parse_ls_tree_oids () {
1769 awk '{print $3}' -
1770}
1771
SZEDER Gáborfa840582019-01-05 02:08:58 +01001772# 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.
1774test_set_port () {
Junio C Hamano341aad82024-04-05 17:08:59 -07001775 local var="$1" port
SZEDER Gáborfa840582019-01-05 02:08:58 +01001776
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áborfa840582019-01-05 02:08:58 +01001796 ;;
SZEDER Gábor7d661e52019-02-11 20:58:03 +01001797 *[!0-9]*|0*)
SZEDER Gáborfa840582019-01-05 02:08:58 +01001798 error >&7 "invalid port number: $port"
1799 ;;
1800 *)
1801 # The user has specified the port.
1802 ;;
1803 esac
SZEDER Gáborfb7d1e32019-01-05 02:08:59 +01001804
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áborfa840582019-01-05 02:08:58 +01001809}
Jeff Kingea047a82020-02-14 13:22:25 -05001810
Johannes Schindelin176a66a2020-04-11 13:40:22 +00001811# Tests for the hidden file attribute on Windows
1812test_path_is_hidden () {
1813 test_have_prereq MINGW ||
1814 BUG "test_path_is_hidden can only be used on Windows"
1815
Johannes Schindelin7c2dfca2020-04-11 13:40:20 +00001816 # Use the output of `attrib`, ignore the absolute path
Johannes Schindelin9814d0a2020-04-11 13:40:21 +00001817 case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
Johannes Schindelin7c2dfca2020-04-11 13:40:20 +00001818 return 1
1819}
Derrick Stolee2057d752020-09-17 18:11:42 +00001820
Ævar Arnfjörð Bjarmason8f788eb2022-12-22 15:14:08 +00001821# 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.
1824test_uri_escape() {
1825 sed 's/ /%20/g'
1826}
1827
Derrick Stolee2057d752020-09-17 18:11:42 +00001828# 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#
1842test_subcommand () {
1843 local negate=
1844 if test "$1" = "!"
1845 then
1846 negate=t
1847 shift
1848 fi
1849
Junio C Hamano7f9f2302024-04-05 17:09:00 -07001850 local expr="$(printf '"%s",' "$@")"
Derrick Stolee2057d752020-09-17 18:11:42 +00001851 expr="${expr%,}"
1852
1853 if test -n "$negate"
1854 then
1855 ! grep "\[$expr\]"
1856 else
1857 grep "\[$expr\]"
1858 fi
1859}
Derrick Stolee3b144362021-01-23 16:07:08 -05001860
1861# Check that the given command was invoked as part of the
Derrick Stolee3b144362021-01-23 16:07:08 -05001862# 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#
1876test_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 King7c0afdf2021-06-18 12:32:22 -04001902
Taylor Blau3bea0c02023-12-14 17:24:39 -05001903# 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
1913test_trace2_data () {
1914 grep -e '"category":"'"$1"'","key":"'"$2"'","value":"'"$3"'"'
1915}
1916
Derrick Stolee7bc73e72023-01-31 13:29:11 +00001917# Given a GIT_TRACE2_EVENT log over stdin, writes to stdout a list of URLs
1918# sent to git-remote-https child processes.
1919test_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 King7c0afdf2021-06-18 12:32:22 -04001925# Print the destination of symlink(s) provided as arguments. Basically
1926# the same as the readlink command, but it's not available everywhere.
1927test_readlink () {
1928 perl -le 'print readlink($_) for @ARGV' "$@"
1929}
Marc Strapetzab6245b2022-01-07 11:17:28 +00001930
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.
1939test_set_magic_mtime () {
Junio C Hamanoe97f4a62024-04-05 17:09:01 -07001940 local inc="${2:-0}" &&
Marc Strapetzab6245b2022-01-07 11:17:28 +00001941 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.
1952test_is_magic_mtime () {
Junio C Hamanoe97f4a62024-04-05 17:09:01 -07001953 local inc="${2:-0}" &&
Marc Strapetzab6245b2022-01-07 11:17:28 +00001954 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ð Bjarmasond796ced2022-10-12 12:52:32 +00001963
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.
1967test_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 Stoleeda9acde2023-01-06 16:31:55 +00001974
1975# Given a filename, extract its trailing hash as a hex string
1976test_trailing_hash () {
1977 local file="$1" &&
1978 tail -c $(test_oid rawsz) "$file" |
1979 test-tool hexdump |
1980 sed "s/ //g"
1981}