]>
Commit | Line | Data |
---|---|---|
1 | # Library of functions shared by all tests scripts, included by | |
2 | # test-lib.sh. | |
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 | |
17 | # along with this program. If not, see http://www.gnu.org/licenses/ . | |
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 | ||
35 | test_decode_color () { | |
36 | awk ' | |
37 | function name(n) { | |
38 | if (n == 0) return "RESET"; | |
39 | if (n == 1) return "BOLD"; | |
40 | if (n == 2) return "FAINT"; | |
41 | if (n == 3) return "ITALIC"; | |
42 | if (n == 7) return "REVERSE"; | |
43 | if (n == 30) return "BLACK"; | |
44 | if (n == 31) return "RED"; | |
45 | if (n == 32) return "GREEN"; | |
46 | if (n == 33) return "YELLOW"; | |
47 | if (n == 34) return "BLUE"; | |
48 | if (n == 35) return "MAGENTA"; | |
49 | if (n == 36) return "CYAN"; | |
50 | if (n == 37) return "WHITE"; | |
51 | if (n == 40) return "BLACK"; | |
52 | if (n == 41) return "BRED"; | |
53 | if (n == 42) return "BGREEN"; | |
54 | if (n == 43) return "BYELLOW"; | |
55 | if (n == 44) return "BBLUE"; | |
56 | if (n == 45) return "BMAGENTA"; | |
57 | if (n == 46) return "BCYAN"; | |
58 | if (n == 47) return "BWHITE"; | |
59 | } | |
60 | { | |
61 | while (match($0, /\033\[[0-9;]*m/) != 0) { | |
62 | printf "%s<", substr($0, 1, RSTART-1); | |
63 | codes = substr($0, RSTART+2, RLENGTH-3); | |
64 | if (length(codes) == 0) | |
65 | printf "%s", name(0) | |
66 | else { | |
67 | n = split(codes, ary, ";"); | |
68 | sep = ""; | |
69 | for (i = 1; i <= n; i++) { | |
70 | printf "%s%s", sep, name(ary[i]); | |
71 | sep = ";" | |
72 | } | |
73 | } | |
74 | printf ">"; | |
75 | $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1); | |
76 | } | |
77 | ||
78 | } | |
79 | ' | |
80 | } | |
81 | ||
82 | lf_to_nul () { | |
83 | perl -pe 'y/\012/\000/' | |
84 | } | |
85 | ||
86 | nul_to_q () { | |
87 | perl -pe 'y/\000/Q/' | |
88 | } | |
89 | ||
90 | q_to_nul () { | |
91 | perl -pe 'y/Q/\000/' | |
92 | } | |
93 | ||
94 | q_to_cr () { | |
95 | tr Q '\015' | |
96 | } | |
97 | ||
98 | q_to_tab () { | |
99 | tr Q '\011' | |
100 | } | |
101 | ||
102 | qz_to_tab_space () { | |
103 | tr QZ '\011\040' | |
104 | } | |
105 | ||
106 | append_cr () { | |
107 | sed -e 's/$/Q/' | tr Q '\015' | |
108 | } | |
109 | ||
110 | remove_cr () { | |
111 | tr '\015' Q | sed -e 's/Q$//' | |
112 | } | |
113 | ||
114 | # In some bourne shell implementations, the "unset" builtin returns | |
115 | # nonzero status when a variable to be unset was not set in the first | |
116 | # place. | |
117 | # | |
118 | # Use sane_unset when that should not be considered an error. | |
119 | ||
120 | sane_unset () { | |
121 | unset "$@" | |
122 | return 0 | |
123 | } | |
124 | ||
125 | test_tick () { | |
126 | if test -z "${test_tick+set}" | |
127 | then | |
128 | test_tick=1112911993 | |
129 | else | |
130 | test_tick=$(($test_tick + 60)) | |
131 | fi | |
132 | GIT_COMMITTER_DATE="$test_tick -0700" | |
133 | GIT_AUTHOR_DATE="$test_tick -0700" | |
134 | export GIT_COMMITTER_DATE GIT_AUTHOR_DATE | |
135 | } | |
136 | ||
137 | # Stop execution and start a shell. This is useful for debugging tests. | |
138 | # | |
139 | # Be sure to remove all invocations of this command before submitting. | |
140 | ||
141 | test_pause () { | |
142 | "$SHELL_PATH" <&6 >&5 2>&7 | |
143 | } | |
144 | ||
145 | # Wrap git with a debugger. Adding this to a command can make it easier | |
146 | # to understand what is going on in a failing test. | |
147 | # | |
148 | # Examples: | |
149 | # debug git checkout master | |
150 | # debug --debugger=nemiver git $ARGS | |
151 | # debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS | |
152 | debug () { | |
153 | case "$1" in | |
154 | -d) | |
155 | GIT_DEBUGGER="$2" && | |
156 | shift 2 | |
157 | ;; | |
158 | --debugger=*) | |
159 | GIT_DEBUGGER="${1#*=}" && | |
160 | shift 1 | |
161 | ;; | |
162 | *) | |
163 | GIT_DEBUGGER=1 | |
164 | ;; | |
165 | esac && | |
166 | GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 | |
167 | } | |
168 | ||
169 | # Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]] | |
170 | # -C <dir>: | |
171 | # Run all git commands in directory <dir> | |
172 | # --notick | |
173 | # Do not call test_tick before making a commit | |
174 | # --append | |
175 | # Use "echo >>" instead of "echo >" when writing "<contents>" to | |
176 | # "<file>" | |
177 | # --signoff | |
178 | # Invoke "git commit" with --signoff | |
179 | # --author <author> | |
180 | # Invoke "git commit" with --author <author> | |
181 | # | |
182 | # This will commit a file with the given contents and the given commit | |
183 | # message, and tag the resulting commit with the given tag name. | |
184 | # | |
185 | # <file>, <contents>, and <tag> all default to <message>. | |
186 | ||
187 | test_commit () { | |
188 | notick= && | |
189 | append= && | |
190 | author= && | |
191 | signoff= && | |
192 | indir= && | |
193 | while test $# != 0 | |
194 | do | |
195 | case "$1" in | |
196 | --notick) | |
197 | notick=yes | |
198 | ;; | |
199 | --append) | |
200 | append=yes | |
201 | ;; | |
202 | --author) | |
203 | author="$2" | |
204 | shift | |
205 | ;; | |
206 | --signoff) | |
207 | signoff="$1" | |
208 | ;; | |
209 | -C) | |
210 | indir="$2" | |
211 | shift | |
212 | ;; | |
213 | *) | |
214 | break | |
215 | ;; | |
216 | esac | |
217 | shift | |
218 | done && | |
219 | indir=${indir:+"$indir"/} && | |
220 | file=${2:-"$1.t"} && | |
221 | if test -n "$append" | |
222 | then | |
223 | echo "${3-$1}" >>"$indir$file" | |
224 | else | |
225 | echo "${3-$1}" >"$indir$file" | |
226 | fi && | |
227 | git ${indir:+ -C "$indir"} add "$file" && | |
228 | if test -z "$notick" | |
229 | then | |
230 | test_tick | |
231 | fi && | |
232 | git ${indir:+ -C "$indir"} commit \ | |
233 | ${author:+ --author "$author"} \ | |
234 | $signoff -m "$1" && | |
235 | git ${indir:+ -C "$indir"} tag "${4:-$1}" | |
236 | } | |
237 | ||
238 | # Call test_merge with the arguments "<message> <commit>", where <commit> | |
239 | # can be a tag pointing to the commit-to-merge. | |
240 | ||
241 | test_merge () { | |
242 | label="$1" && | |
243 | shift && | |
244 | test_tick && | |
245 | git merge -m "$label" "$@" && | |
246 | git tag "$label" | |
247 | } | |
248 | ||
249 | # Efficiently create <nr> commits, each with a unique number (from 1 to <nr> | |
250 | # by default) in the commit message. | |
251 | # | |
252 | # Usage: test_commit_bulk [options] <nr> | |
253 | # -C <dir>: | |
254 | # Run all git commands in directory <dir> | |
255 | # --ref=<n>: | |
256 | # ref on which to create commits (default: HEAD) | |
257 | # --start=<n>: | |
258 | # number commit messages from <n> (default: 1) | |
259 | # --message=<msg>: | |
260 | # use <msg> as the commit mesasge (default: "commit %s") | |
261 | # --filename=<fn>: | |
262 | # modify <fn> in each commit (default: %s.t) | |
263 | # --contents=<string>: | |
264 | # place <string> in each file (default: "content %s") | |
265 | # --id=<string>: | |
266 | # shorthand to use <string> and %s in message, filename, and contents | |
267 | # | |
268 | # The message, filename, and contents strings are evaluated by printf, with the | |
269 | # first "%s" replaced by the current commit number. So you can do: | |
270 | # | |
271 | # test_commit_bulk --filename=file --contents="modification %s" | |
272 | # | |
273 | # to have every commit touch the same file, but with unique content. | |
274 | # | |
275 | test_commit_bulk () { | |
276 | tmpfile=.bulk-commit.input | |
277 | indir=. | |
278 | ref=HEAD | |
279 | n=1 | |
280 | message='commit %s' | |
281 | filename='%s.t' | |
282 | contents='content %s' | |
283 | while test $# -gt 0 | |
284 | do | |
285 | case "$1" in | |
286 | -C) | |
287 | indir=$2 | |
288 | shift | |
289 | ;; | |
290 | --ref=*) | |
291 | ref=${1#--*=} | |
292 | ;; | |
293 | --start=*) | |
294 | n=${1#--*=} | |
295 | ;; | |
296 | --message=*) | |
297 | message=${1#--*=} | |
298 | ;; | |
299 | --filename=*) | |
300 | filename=${1#--*=} | |
301 | ;; | |
302 | --contents=*) | |
303 | contents=${1#--*=} | |
304 | ;; | |
305 | --id=*) | |
306 | message="${1#--*=} %s" | |
307 | filename="${1#--*=}-%s.t" | |
308 | contents="${1#--*=} %s" | |
309 | ;; | |
310 | -*) | |
311 | BUG "invalid test_commit_bulk option: $1" | |
312 | ;; | |
313 | *) | |
314 | break | |
315 | ;; | |
316 | esac | |
317 | shift | |
318 | done | |
319 | total=$1 | |
320 | ||
321 | add_from= | |
322 | if git -C "$indir" rev-parse --quiet --verify "$ref" | |
323 | then | |
324 | add_from=t | |
325 | fi | |
326 | ||
327 | while test "$total" -gt 0 | |
328 | do | |
329 | test_tick && | |
330 | echo "commit $ref" | |
331 | printf 'author %s <%s> %s\n' \ | |
332 | "$GIT_AUTHOR_NAME" \ | |
333 | "$GIT_AUTHOR_EMAIL" \ | |
334 | "$GIT_AUTHOR_DATE" | |
335 | printf 'committer %s <%s> %s\n' \ | |
336 | "$GIT_COMMITTER_NAME" \ | |
337 | "$GIT_COMMITTER_EMAIL" \ | |
338 | "$GIT_COMMITTER_DATE" | |
339 | echo "data <<EOF" | |
340 | printf "$message\n" $n | |
341 | echo "EOF" | |
342 | if test -n "$add_from" | |
343 | then | |
344 | echo "from $ref^0" | |
345 | add_from= | |
346 | fi | |
347 | printf "M 644 inline $filename\n" $n | |
348 | echo "data <<EOF" | |
349 | printf "$contents\n" $n | |
350 | echo "EOF" | |
351 | echo | |
352 | n=$((n + 1)) | |
353 | total=$((total - 1)) | |
354 | done >"$tmpfile" | |
355 | ||
356 | git -C "$indir" \ | |
357 | -c fastimport.unpacklimit=0 \ | |
358 | fast-import <"$tmpfile" || return 1 | |
359 | ||
360 | # This will be left in place on failure, which may aid debugging. | |
361 | rm -f "$tmpfile" | |
362 | ||
363 | # If we updated HEAD, then be nice and update the index and working | |
364 | # tree, too. | |
365 | if test "$ref" = "HEAD" | |
366 | then | |
367 | git -C "$indir" checkout -f HEAD || return 1 | |
368 | fi | |
369 | ||
370 | } | |
371 | ||
372 | # This function helps systems where core.filemode=false is set. | |
373 | # Use it instead of plain 'chmod +x' to set or unset the executable bit | |
374 | # of a file in the working directory and add it to the index. | |
375 | ||
376 | test_chmod () { | |
377 | chmod "$@" && | |
378 | git update-index --add "--chmod=$@" | |
379 | } | |
380 | ||
381 | # Get the modebits from a file or directory, ignoring the setgid bit (g+s). | |
382 | # This bit is inherited by subdirectories at their creation. So we remove it | |
383 | # from the returning string to prevent callers from having to worry about the | |
384 | # state of the bit in the test directory. | |
385 | # | |
386 | test_modebits () { | |
387 | ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \ | |
388 | -e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|' | |
389 | } | |
390 | ||
391 | # Unset a configuration variable, but don't fail if it doesn't exist. | |
392 | test_unconfig () { | |
393 | config_dir= | |
394 | if test "$1" = -C | |
395 | then | |
396 | shift | |
397 | config_dir=$1 | |
398 | shift | |
399 | fi | |
400 | git ${config_dir:+-C "$config_dir"} config --unset-all "$@" | |
401 | config_status=$? | |
402 | case "$config_status" in | |
403 | 5) # ok, nothing to unset | |
404 | config_status=0 | |
405 | ;; | |
406 | esac | |
407 | return $config_status | |
408 | } | |
409 | ||
410 | # Set git config, automatically unsetting it after the test is over. | |
411 | test_config () { | |
412 | config_dir= | |
413 | if test "$1" = -C | |
414 | then | |
415 | shift | |
416 | config_dir=$1 | |
417 | shift | |
418 | fi | |
419 | test_when_finished "test_unconfig ${config_dir:+-C '$config_dir'} '$1'" && | |
420 | git ${config_dir:+-C "$config_dir"} config "$@" | |
421 | } | |
422 | ||
423 | test_config_global () { | |
424 | test_when_finished "test_unconfig --global '$1'" && | |
425 | git config --global "$@" | |
426 | } | |
427 | ||
428 | write_script () { | |
429 | { | |
430 | echo "#!${2-"$SHELL_PATH"}" && | |
431 | cat | |
432 | } >"$1" && | |
433 | chmod +x "$1" | |
434 | } | |
435 | ||
436 | # Use test_set_prereq to tell that a particular prerequisite is available. | |
437 | # The prerequisite can later be checked for in two ways: | |
438 | # | |
439 | # - Explicitly using test_have_prereq. | |
440 | # | |
441 | # - Implicitly by specifying the prerequisite tag in the calls to | |
442 | # test_expect_{success,failure} and test_external{,_without_stderr}. | |
443 | # | |
444 | # The single parameter is the prerequisite tag (a simple word, in all | |
445 | # capital letters by convention). | |
446 | ||
447 | test_unset_prereq () { | |
448 | ! test_have_prereq "$1" || | |
449 | satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }" | |
450 | } | |
451 | ||
452 | test_set_prereq () { | |
453 | if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL" | |
454 | then | |
455 | case "$1" in | |
456 | # The "!" case is handled below with | |
457 | # test_unset_prereq() | |
458 | !*) | |
459 | ;; | |
460 | # (Temporary?) whitelist of things we can't easily | |
461 | # pretend not to support | |
462 | SYMLINKS) | |
463 | ;; | |
464 | # Inspecting whether GIT_TEST_FAIL_PREREQS is on | |
465 | # should be unaffected. | |
466 | FAIL_PREREQS) | |
467 | ;; | |
468 | *) | |
469 | return | |
470 | esac | |
471 | fi | |
472 | ||
473 | case "$1" in | |
474 | !*) | |
475 | test_unset_prereq "${1#!}" | |
476 | ;; | |
477 | *) | |
478 | satisfied_prereq="$satisfied_prereq$1 " | |
479 | ;; | |
480 | esac | |
481 | } | |
482 | satisfied_prereq=" " | |
483 | lazily_testable_prereq= lazily_tested_prereq= | |
484 | ||
485 | # Usage: test_lazy_prereq PREREQ 'script' | |
486 | test_lazy_prereq () { | |
487 | lazily_testable_prereq="$lazily_testable_prereq$1 " | |
488 | eval test_prereq_lazily_$1=\$2 | |
489 | } | |
490 | ||
491 | test_run_lazy_prereq_ () { | |
492 | script=' | |
493 | mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" && | |
494 | ( | |
495 | cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"' | |
496 | )' | |
497 | say >&3 "checking prerequisite: $1" | |
498 | say >&3 "$script" | |
499 | test_eval_ "$script" | |
500 | eval_ret=$? | |
501 | rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1" | |
502 | if test "$eval_ret" = 0; then | |
503 | say >&3 "prerequisite $1 ok" | |
504 | else | |
505 | say >&3 "prerequisite $1 not satisfied" | |
506 | fi | |
507 | return $eval_ret | |
508 | } | |
509 | ||
510 | test_have_prereq () { | |
511 | # prerequisites can be concatenated with ',' | |
512 | save_IFS=$IFS | |
513 | IFS=, | |
514 | set -- $* | |
515 | IFS=$save_IFS | |
516 | ||
517 | total_prereq=0 | |
518 | ok_prereq=0 | |
519 | missing_prereq= | |
520 | ||
521 | for prerequisite | |
522 | do | |
523 | case "$prerequisite" in | |
524 | !*) | |
525 | negative_prereq=t | |
526 | prerequisite=${prerequisite#!} | |
527 | ;; | |
528 | *) | |
529 | negative_prereq= | |
530 | esac | |
531 | ||
532 | case " $lazily_tested_prereq " in | |
533 | *" $prerequisite "*) | |
534 | ;; | |
535 | *) | |
536 | case " $lazily_testable_prereq " in | |
537 | *" $prerequisite "*) | |
538 | eval "script=\$test_prereq_lazily_$prerequisite" && | |
539 | if test_run_lazy_prereq_ "$prerequisite" "$script" | |
540 | then | |
541 | test_set_prereq $prerequisite | |
542 | fi | |
543 | lazily_tested_prereq="$lazily_tested_prereq$prerequisite " | |
544 | esac | |
545 | ;; | |
546 | esac | |
547 | ||
548 | total_prereq=$(($total_prereq + 1)) | |
549 | case "$satisfied_prereq" in | |
550 | *" $prerequisite "*) | |
551 | satisfied_this_prereq=t | |
552 | ;; | |
553 | *) | |
554 | satisfied_this_prereq= | |
555 | esac | |
556 | ||
557 | case "$satisfied_this_prereq,$negative_prereq" in | |
558 | t,|,t) | |
559 | ok_prereq=$(($ok_prereq + 1)) | |
560 | ;; | |
561 | *) | |
562 | # Keep a list of missing prerequisites; restore | |
563 | # the negative marker if necessary. | |
564 | prerequisite=${negative_prereq:+!}$prerequisite | |
565 | if test -z "$missing_prereq" | |
566 | then | |
567 | missing_prereq=$prerequisite | |
568 | else | |
569 | missing_prereq="$prerequisite,$missing_prereq" | |
570 | fi | |
571 | esac | |
572 | done | |
573 | ||
574 | test $total_prereq = $ok_prereq | |
575 | } | |
576 | ||
577 | test_declared_prereq () { | |
578 | case ",$test_prereq," in | |
579 | *,$1,*) | |
580 | return 0 | |
581 | ;; | |
582 | esac | |
583 | return 1 | |
584 | } | |
585 | ||
586 | test_verify_prereq () { | |
587 | test -z "$test_prereq" || | |
588 | expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' || | |
589 | BUG "'$test_prereq' does not look like a prereq" | |
590 | } | |
591 | ||
592 | test_expect_failure () { | |
593 | test_start_ | |
594 | test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= | |
595 | test "$#" = 2 || | |
596 | BUG "not 2 or 3 parameters to test-expect-failure" | |
597 | test_verify_prereq | |
598 | export test_prereq | |
599 | if ! test_skip "$@" | |
600 | then | |
601 | say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2" | |
602 | if test_run_ "$2" expecting_failure | |
603 | then | |
604 | test_known_broken_ok_ "$1" | |
605 | else | |
606 | test_known_broken_failure_ "$1" | |
607 | fi | |
608 | fi | |
609 | test_finish_ | |
610 | } | |
611 | ||
612 | test_expect_success () { | |
613 | test_start_ | |
614 | test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq= | |
615 | test "$#" = 2 || | |
616 | BUG "not 2 or 3 parameters to test-expect-success" | |
617 | test_verify_prereq | |
618 | export test_prereq | |
619 | if ! test_skip "$@" | |
620 | then | |
621 | say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2" | |
622 | if test_run_ "$2" | |
623 | then | |
624 | test_ok_ "$1" | |
625 | else | |
626 | test_failure_ "$@" | |
627 | fi | |
628 | fi | |
629 | test_finish_ | |
630 | } | |
631 | ||
632 | # test_external runs external test scripts that provide continuous | |
633 | # test output about their progress, and succeeds/fails on | |
634 | # zero/non-zero exit code. It outputs the test output on stdout even | |
635 | # in non-verbose mode, and announces the external script with "# run | |
636 | # <n>: ..." before running it. When providing relative paths, keep in | |
637 | # mind that all scripts run in "trash directory". | |
638 | # Usage: test_external description command arguments... | |
639 | # Example: test_external 'Perl API' perl ../path/to/test.pl | |
640 | test_external () { | |
641 | test "$#" = 4 && { test_prereq=$1; shift; } || test_prereq= | |
642 | test "$#" = 3 || | |
643 | BUG "not 3 or 4 parameters to test_external" | |
644 | descr="$1" | |
645 | shift | |
646 | test_verify_prereq | |
647 | export test_prereq | |
648 | if ! test_skip "$descr" "$@" | |
649 | then | |
650 | # Announce the script to reduce confusion about the | |
651 | # test output that follows. | |
652 | say_color "" "# run $test_count: $descr ($*)" | |
653 | # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG | |
654 | # to be able to use them in script | |
655 | export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG | |
656 | # Run command; redirect its stderr to &4 as in | |
657 | # test_run_, but keep its stdout on our stdout even in | |
658 | # non-verbose mode. | |
659 | "$@" 2>&4 | |
660 | if test "$?" = 0 | |
661 | then | |
662 | if test $test_external_has_tap -eq 0; then | |
663 | test_ok_ "$descr" | |
664 | else | |
665 | say_color "" "# test_external test $descr was ok" | |
666 | test_success=$(($test_success + 1)) | |
667 | fi | |
668 | else | |
669 | if test $test_external_has_tap -eq 0; then | |
670 | test_failure_ "$descr" "$@" | |
671 | else | |
672 | say_color error "# test_external test $descr failed: $@" | |
673 | test_failure=$(($test_failure + 1)) | |
674 | fi | |
675 | fi | |
676 | fi | |
677 | } | |
678 | ||
679 | # Like test_external, but in addition tests that the command generated | |
680 | # no output on stderr. | |
681 | test_external_without_stderr () { | |
682 | # The temporary file has no (and must have no) security | |
683 | # implications. | |
684 | tmp=${TMPDIR:-/tmp} | |
685 | stderr="$tmp/git-external-stderr.$$.tmp" | |
686 | test_external "$@" 4> "$stderr" | |
687 | test -f "$stderr" || error "Internal error: $stderr disappeared." | |
688 | descr="no stderr: $1" | |
689 | shift | |
690 | say >&3 "# expecting no stderr from previous command" | |
691 | if test ! -s "$stderr" | |
692 | then | |
693 | rm "$stderr" | |
694 | ||
695 | if test $test_external_has_tap -eq 0; then | |
696 | test_ok_ "$descr" | |
697 | else | |
698 | say_color "" "# test_external_without_stderr test $descr was ok" | |
699 | test_success=$(($test_success + 1)) | |
700 | fi | |
701 | else | |
702 | if test "$verbose" = t | |
703 | then | |
704 | output=$(echo; echo "# Stderr is:"; cat "$stderr") | |
705 | else | |
706 | output= | |
707 | fi | |
708 | # rm first in case test_failure exits. | |
709 | rm "$stderr" | |
710 | if test $test_external_has_tap -eq 0; then | |
711 | test_failure_ "$descr" "$@" "$output" | |
712 | else | |
713 | say_color error "# test_external_without_stderr test $descr failed: $@: $output" | |
714 | test_failure=$(($test_failure + 1)) | |
715 | fi | |
716 | fi | |
717 | } | |
718 | ||
719 | # debugging-friendly alternatives to "test [-f|-d|-e]" | |
720 | # The commands test the existence or non-existence of $1. $2 can be | |
721 | # given to provide a more precise diagnosis. | |
722 | test_path_is_file () { | |
723 | if ! test -f "$1" | |
724 | then | |
725 | echo "File $1 doesn't exist. $2" | |
726 | false | |
727 | fi | |
728 | } | |
729 | ||
730 | test_path_is_dir () { | |
731 | if ! test -d "$1" | |
732 | then | |
733 | echo "Directory $1 doesn't exist. $2" | |
734 | false | |
735 | fi | |
736 | } | |
737 | ||
738 | test_path_exists () { | |
739 | if ! test -e "$1" | |
740 | then | |
741 | echo "Path $1 doesn't exist. $2" | |
742 | false | |
743 | fi | |
744 | } | |
745 | ||
746 | # Check if the directory exists and is empty as expected, barf otherwise. | |
747 | test_dir_is_empty () { | |
748 | test_path_is_dir "$1" && | |
749 | if test -n "$(ls -a1 "$1" | egrep -v '^\.\.?$')" | |
750 | then | |
751 | echo "Directory '$1' is not empty, it contains:" | |
752 | ls -la "$1" | |
753 | return 1 | |
754 | fi | |
755 | } | |
756 | ||
757 | # Check if the file exists and has a size greater than zero | |
758 | test_file_not_empty () { | |
759 | if ! test -s "$1" | |
760 | then | |
761 | echo "'$1' is not a non-empty file." | |
762 | false | |
763 | fi | |
764 | } | |
765 | ||
766 | test_path_is_missing () { | |
767 | if test -e "$1" | |
768 | then | |
769 | echo "Path exists:" | |
770 | ls -ld "$1" | |
771 | if test $# -ge 1 | |
772 | then | |
773 | echo "$*" | |
774 | fi | |
775 | false | |
776 | fi | |
777 | } | |
778 | ||
779 | # test_line_count checks that a file has the number of lines it | |
780 | # ought to. For example: | |
781 | # | |
782 | # test_expect_success 'produce exactly one line of output' ' | |
783 | # do something >output && | |
784 | # test_line_count = 1 output | |
785 | # ' | |
786 | # | |
787 | # is like "test $(wc -l <output) = 1" except that it passes the | |
788 | # output through when the number of lines is wrong. | |
789 | ||
790 | test_line_count () { | |
791 | if test $# != 3 | |
792 | then | |
793 | BUG "not 3 parameters to test_line_count" | |
794 | elif ! test $(wc -l <"$3") "$1" "$2" | |
795 | then | |
796 | echo "test_line_count: line count for $3 !$1 $2" | |
797 | cat "$3" | |
798 | return 1 | |
799 | fi | |
800 | } | |
801 | ||
802 | test_file_size () { | |
803 | test-tool path-utils file-size "$1" | |
804 | } | |
805 | ||
806 | # Returns success if a comma separated string of keywords ($1) contains a | |
807 | # given keyword ($2). | |
808 | # Examples: | |
809 | # `list_contains "foo,bar" bar` returns 0 | |
810 | # `list_contains "foo" bar` returns 1 | |
811 | ||
812 | list_contains () { | |
813 | case ",$1," in | |
814 | *,$2,*) | |
815 | return 0 | |
816 | ;; | |
817 | esac | |
818 | return 1 | |
819 | } | |
820 | ||
821 | # Returns success if the arguments indicate that a command should be | |
822 | # accepted by test_must_fail(). If the command is run with env, the env | |
823 | # and its corresponding variable settings will be stripped before we | |
824 | # test the command being run. | |
825 | test_must_fail_acceptable () { | |
826 | if test "$1" = "env" | |
827 | then | |
828 | shift | |
829 | while test $# -gt 0 | |
830 | do | |
831 | case "$1" in | |
832 | *?=*) | |
833 | shift | |
834 | ;; | |
835 | *) | |
836 | break | |
837 | ;; | |
838 | esac | |
839 | done | |
840 | fi | |
841 | ||
842 | case "$1" in | |
843 | git|__git*|test-tool|test_terminal) | |
844 | return 0 | |
845 | ;; | |
846 | *) | |
847 | return 1 | |
848 | ;; | |
849 | esac | |
850 | } | |
851 | ||
852 | # This is not among top-level (test_expect_success | test_expect_failure) | |
853 | # but is a prefix that can be used in the test script, like: | |
854 | # | |
855 | # test_expect_success 'complain and die' ' | |
856 | # do something && | |
857 | # do something else && | |
858 | # test_must_fail git checkout ../outerspace | |
859 | # ' | |
860 | # | |
861 | # Writing this as "! git checkout ../outerspace" is wrong, because | |
862 | # the failure could be due to a segv. We want a controlled failure. | |
863 | # | |
864 | # Accepts the following options: | |
865 | # | |
866 | # ok=<signal-name>[,<...>]: | |
867 | # Don't treat an exit caused by the given signal as error. | |
868 | # Multiple signals can be specified as a comma separated list. | |
869 | # Currently recognized signal names are: sigpipe, success. | |
870 | # (Don't use 'success', use 'test_might_fail' instead.) | |
871 | # | |
872 | # Do not use this to run anything but "git" and other specific testable | |
873 | # commands (see test_must_fail_acceptable()). We are not in the | |
874 | # business of vetting system supplied commands -- in other words, this | |
875 | # is wrong: | |
876 | # | |
877 | # test_must_fail grep pattern output | |
878 | # | |
879 | # Instead use '!': | |
880 | # | |
881 | # ! grep pattern output | |
882 | ||
883 | test_must_fail () { | |
884 | case "$1" in | |
885 | ok=*) | |
886 | _test_ok=${1#ok=} | |
887 | shift | |
888 | ;; | |
889 | *) | |
890 | _test_ok= | |
891 | ;; | |
892 | esac | |
893 | if ! test_must_fail_acceptable "$@" | |
894 | then | |
895 | echo >&7 "test_must_fail: only 'git' is allowed: $*" | |
896 | return 1 | |
897 | fi | |
898 | "$@" 2>&7 | |
899 | exit_code=$? | |
900 | if test $exit_code -eq 0 && ! list_contains "$_test_ok" success | |
901 | then | |
902 | echo >&4 "test_must_fail: command succeeded: $*" | |
903 | return 1 | |
904 | elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe | |
905 | then | |
906 | return 0 | |
907 | elif test $exit_code -gt 129 && test $exit_code -le 192 | |
908 | then | |
909 | echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*" | |
910 | return 1 | |
911 | elif test $exit_code -eq 127 | |
912 | then | |
913 | echo >&4 "test_must_fail: command not found: $*" | |
914 | return 1 | |
915 | elif test $exit_code -eq 126 | |
916 | then | |
917 | echo >&4 "test_must_fail: valgrind error: $*" | |
918 | return 1 | |
919 | fi | |
920 | return 0 | |
921 | } 7>&2 2>&4 | |
922 | ||
923 | # Similar to test_must_fail, but tolerates success, too. This is | |
924 | # meant to be used in contexts like: | |
925 | # | |
926 | # test_expect_success 'some command works without configuration' ' | |
927 | # test_might_fail git config --unset all.configuration && | |
928 | # do something | |
929 | # ' | |
930 | # | |
931 | # Writing "git config --unset all.configuration || :" would be wrong, | |
932 | # because we want to notice if it fails due to segv. | |
933 | # | |
934 | # Accepts the same options as test_must_fail. | |
935 | ||
936 | test_might_fail () { | |
937 | test_must_fail ok=success "$@" 2>&7 | |
938 | } 7>&2 2>&4 | |
939 | ||
940 | # Similar to test_must_fail and test_might_fail, but check that a | |
941 | # given command exited with a given exit code. Meant to be used as: | |
942 | # | |
943 | # test_expect_success 'Merge with d/f conflicts' ' | |
944 | # test_expect_code 1 git merge "merge msg" B master | |
945 | # ' | |
946 | ||
947 | test_expect_code () { | |
948 | want_code=$1 | |
949 | shift | |
950 | "$@" 2>&7 | |
951 | exit_code=$? | |
952 | if test $exit_code = $want_code | |
953 | then | |
954 | return 0 | |
955 | fi | |
956 | ||
957 | echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*" | |
958 | return 1 | |
959 | } 7>&2 2>&4 | |
960 | ||
961 | # test_cmp is a helper function to compare actual and expected output. | |
962 | # You can use it like: | |
963 | # | |
964 | # test_expect_success 'foo works' ' | |
965 | # echo expected >expected && | |
966 | # foo >actual && | |
967 | # test_cmp expected actual | |
968 | # ' | |
969 | # | |
970 | # This could be written as either "cmp" or "diff -u", but: | |
971 | # - cmp's output is not nearly as easy to read as diff -u | |
972 | # - not all diff versions understand "-u" | |
973 | ||
974 | test_cmp () { | |
975 | eval "$GIT_TEST_CMP" '"$@"' | |
976 | } | |
977 | ||
978 | # Check that the given config key has the expected value. | |
979 | # | |
980 | # test_cmp_config [-C <dir>] <expected-value> | |
981 | # [<git-config-options>...] <config-key> | |
982 | # | |
983 | # for example to check that the value of core.bar is foo | |
984 | # | |
985 | # test_cmp_config foo core.bar | |
986 | # | |
987 | test_cmp_config () { | |
988 | local GD && | |
989 | if test "$1" = "-C" | |
990 | then | |
991 | shift && | |
992 | GD="-C $1" && | |
993 | shift | |
994 | fi && | |
995 | printf "%s\n" "$1" >expect.config && | |
996 | shift && | |
997 | git $GD config "$@" >actual.config && | |
998 | test_cmp expect.config actual.config | |
999 | } | |
1000 | ||
1001 | # test_cmp_bin - helper to compare binary files | |
1002 | ||
1003 | test_cmp_bin () { | |
1004 | cmp "$@" | |
1005 | } | |
1006 | ||
1007 | # Use this instead of test_cmp to compare files that contain expected and | |
1008 | # actual output from git commands that can be translated. When running | |
1009 | # under GIT_TEST_GETTEXT_POISON this pretends that the command produced expected | |
1010 | # results. | |
1011 | test_i18ncmp () { | |
1012 | ! test_have_prereq C_LOCALE_OUTPUT || test_cmp "$@" | |
1013 | } | |
1014 | ||
1015 | # Use this instead of "grep expected-string actual" to see if the | |
1016 | # output from a git command that can be translated either contains an | |
1017 | # expected string, or does not contain an unwanted one. When running | |
1018 | # under GIT_TEST_GETTEXT_POISON this pretends that the command produced expected | |
1019 | # results. | |
1020 | test_i18ngrep () { | |
1021 | eval "last_arg=\${$#}" | |
1022 | ||
1023 | test -f "$last_arg" || | |
1024 | BUG "test_i18ngrep requires a file to read as the last parameter" | |
1025 | ||
1026 | if test $# -lt 2 || | |
1027 | { test "x!" = "x$1" && test $# -lt 3 ; } | |
1028 | then | |
1029 | BUG "too few parameters to test_i18ngrep" | |
1030 | fi | |
1031 | ||
1032 | if test_have_prereq !C_LOCALE_OUTPUT | |
1033 | then | |
1034 | # pretend success | |
1035 | return 0 | |
1036 | fi | |
1037 | ||
1038 | if test "x!" = "x$1" | |
1039 | then | |
1040 | shift | |
1041 | ! grep "$@" && return 0 | |
1042 | ||
1043 | echo >&4 "error: '! grep $@' did find a match in:" | |
1044 | else | |
1045 | grep "$@" && return 0 | |
1046 | ||
1047 | echo >&4 "error: 'grep $@' didn't find a match in:" | |
1048 | fi | |
1049 | ||
1050 | if test -s "$last_arg" | |
1051 | then | |
1052 | cat >&4 "$last_arg" | |
1053 | else | |
1054 | echo >&4 "<File '$last_arg' is empty>" | |
1055 | fi | |
1056 | ||
1057 | return 1 | |
1058 | } | |
1059 | ||
1060 | # Call any command "$@" but be more verbose about its | |
1061 | # failure. This is handy for commands like "test" which do | |
1062 | # not output anything when they fail. | |
1063 | verbose () { | |
1064 | "$@" && return 0 | |
1065 | echo >&4 "command failed: $(git rev-parse --sq-quote "$@")" | |
1066 | return 1 | |
1067 | } | |
1068 | ||
1069 | # Check if the file expected to be empty is indeed empty, and barfs | |
1070 | # otherwise. | |
1071 | ||
1072 | test_must_be_empty () { | |
1073 | test_path_is_file "$1" && | |
1074 | if test -s "$1" | |
1075 | then | |
1076 | echo "'$1' is not empty, it contains:" | |
1077 | cat "$1" | |
1078 | return 1 | |
1079 | fi | |
1080 | } | |
1081 | ||
1082 | # Tests that its two parameters refer to the same revision, or if '!' is | |
1083 | # provided first, that its other two parameters refer to different | |
1084 | # revisions. | |
1085 | test_cmp_rev () { | |
1086 | local op='=' wrong_result=different | |
1087 | ||
1088 | if test $# -ge 1 && test "x$1" = 'x!' | |
1089 | then | |
1090 | op='!=' | |
1091 | wrong_result='the same' | |
1092 | shift | |
1093 | fi | |
1094 | if test $# != 2 | |
1095 | then | |
1096 | BUG "test_cmp_rev requires two revisions, but got $#" | |
1097 | else | |
1098 | local r1 r2 | |
1099 | r1=$(git rev-parse --verify "$1") && | |
1100 | r2=$(git rev-parse --verify "$2") || return 1 | |
1101 | ||
1102 | if ! test "$r1" "$op" "$r2" | |
1103 | then | |
1104 | cat >&4 <<-EOF | |
1105 | error: two revisions point to $wrong_result objects: | |
1106 | '$1': $r1 | |
1107 | '$2': $r2 | |
1108 | EOF | |
1109 | return 1 | |
1110 | fi | |
1111 | fi | |
1112 | } | |
1113 | ||
1114 | # Compare paths respecting core.ignoreCase | |
1115 | test_cmp_fspath () { | |
1116 | if test "x$1" = "x$2" | |
1117 | then | |
1118 | return 0 | |
1119 | fi | |
1120 | ||
1121 | if test true != "$(git config --get --type=bool core.ignorecase)" | |
1122 | then | |
1123 | return 1 | |
1124 | fi | |
1125 | ||
1126 | test "x$(echo "$1" | tr A-Z a-z)" = "x$(echo "$2" | tr A-Z a-z)" | |
1127 | } | |
1128 | ||
1129 | # Print a sequence of integers in increasing order, either with | |
1130 | # two arguments (start and end): | |
1131 | # | |
1132 | # test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time | |
1133 | # | |
1134 | # or with one argument (end), in which case it starts counting | |
1135 | # from 1. | |
1136 | ||
1137 | test_seq () { | |
1138 | case $# in | |
1139 | 1) set 1 "$@" ;; | |
1140 | 2) ;; | |
1141 | *) BUG "not 1 or 2 parameters to test_seq" ;; | |
1142 | esac | |
1143 | test_seq_counter__=$1 | |
1144 | while test "$test_seq_counter__" -le "$2" | |
1145 | do | |
1146 | echo "$test_seq_counter__" | |
1147 | test_seq_counter__=$(( $test_seq_counter__ + 1 )) | |
1148 | done | |
1149 | } | |
1150 | ||
1151 | # This function can be used to schedule some commands to be run | |
1152 | # unconditionally at the end of the test to restore sanity: | |
1153 | # | |
1154 | # test_expect_success 'test core.capslock' ' | |
1155 | # git config core.capslock true && | |
1156 | # test_when_finished "git config --unset core.capslock" && | |
1157 | # hello world | |
1158 | # ' | |
1159 | # | |
1160 | # That would be roughly equivalent to | |
1161 | # | |
1162 | # test_expect_success 'test core.capslock' ' | |
1163 | # git config core.capslock true && | |
1164 | # hello world | |
1165 | # git config --unset core.capslock | |
1166 | # ' | |
1167 | # | |
1168 | # except that the greeting and config --unset must both succeed for | |
1169 | # the test to pass. | |
1170 | # | |
1171 | # Note that under --immediate mode, no clean-up is done to help diagnose | |
1172 | # what went wrong. | |
1173 | ||
1174 | test_when_finished () { | |
1175 | # We cannot detect when we are in a subshell in general, but by | |
1176 | # doing so on Bash is better than nothing (the test will | |
1177 | # silently pass on other shells). | |
1178 | test "${BASH_SUBSHELL-0}" = 0 || | |
1179 | BUG "test_when_finished does nothing in a subshell" | |
1180 | test_cleanup="{ $* | |
1181 | } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup" | |
1182 | } | |
1183 | ||
1184 | # This function can be used to schedule some commands to be run | |
1185 | # unconditionally at the end of the test script, e.g. to stop a daemon: | |
1186 | # | |
1187 | # test_expect_success 'test git daemon' ' | |
1188 | # git daemon & | |
1189 | # daemon_pid=$! && | |
1190 | # test_atexit 'kill $daemon_pid' && | |
1191 | # hello world | |
1192 | # ' | |
1193 | # | |
1194 | # The commands will be executed before the trash directory is removed, | |
1195 | # i.e. the atexit commands will still be able to access any pidfiles or | |
1196 | # socket files. | |
1197 | # | |
1198 | # Note that these commands will be run even when a test script run | |
1199 | # with '--immediate' fails. Be careful with your atexit commands to | |
1200 | # minimize any changes to the failed state. | |
1201 | ||
1202 | test_atexit () { | |
1203 | # We cannot detect when we are in a subshell in general, but by | |
1204 | # doing so on Bash is better than nothing (the test will | |
1205 | # silently pass on other shells). | |
1206 | test "${BASH_SUBSHELL-0}" = 0 || | |
1207 | BUG "test_atexit does nothing in a subshell" | |
1208 | test_atexit_cleanup="{ $* | |
1209 | } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup" | |
1210 | } | |
1211 | ||
1212 | # Most tests can use the created repository, but some may need to create more. | |
1213 | # Usage: test_create_repo <directory> | |
1214 | test_create_repo () { | |
1215 | test "$#" = 1 || | |
1216 | BUG "not 1 parameter to test-create-repo" | |
1217 | repo="$1" | |
1218 | mkdir -p "$repo" | |
1219 | ( | |
1220 | cd "$repo" || error "Cannot setup test environment" | |
1221 | "${GIT_TEST_INSTALLED:-$GIT_EXEC_PATH}/git$X" -c \ | |
1222 | init.defaultBranch="${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME-master}" \ | |
1223 | init \ | |
1224 | "--template=$GIT_BUILD_DIR/templates/blt/" >&3 2>&4 || | |
1225 | error "cannot run git init -- have you built things yet?" | |
1226 | mv .git/hooks .git/hooks-disabled | |
1227 | ) || exit | |
1228 | } | |
1229 | ||
1230 | # This function helps on symlink challenged file systems when it is not | |
1231 | # important that the file system entry is a symbolic link. | |
1232 | # Use test_ln_s_add instead of "ln -s x y && git add y" to add a | |
1233 | # symbolic link entry y to the index. | |
1234 | ||
1235 | test_ln_s_add () { | |
1236 | if test_have_prereq SYMLINKS | |
1237 | then | |
1238 | ln -s "$1" "$2" && | |
1239 | git update-index --add "$2" | |
1240 | else | |
1241 | printf '%s' "$1" >"$2" && | |
1242 | ln_s_obj=$(git hash-object -w "$2") && | |
1243 | git update-index --add --cacheinfo 120000 $ln_s_obj "$2" && | |
1244 | # pick up stat info from the file | |
1245 | git update-index "$2" | |
1246 | fi | |
1247 | } | |
1248 | ||
1249 | # This function writes out its parameters, one per line | |
1250 | test_write_lines () { | |
1251 | printf "%s\n" "$@" | |
1252 | } | |
1253 | ||
1254 | perl () { | |
1255 | command "$PERL_PATH" "$@" 2>&7 | |
1256 | } 7>&2 2>&4 | |
1257 | ||
1258 | # Given the name of an environment variable with a bool value, normalize | |
1259 | # its value to a 0 (true) or 1 (false or empty string) return code. | |
1260 | # | |
1261 | # test_bool_env GIT_TEST_HTTPD <default-value> | |
1262 | # | |
1263 | # Return with code corresponding to the given default value if the variable | |
1264 | # is unset. | |
1265 | # Abort the test script if either the value of the variable or the default | |
1266 | # are not valid bool values. | |
1267 | ||
1268 | test_bool_env () { | |
1269 | if test $# != 2 | |
1270 | then | |
1271 | BUG "test_bool_env requires two parameters (variable name and default value)" | |
1272 | fi | |
1273 | ||
1274 | git env--helper --type=bool --default="$2" --exit-code "$1" | |
1275 | ret=$? | |
1276 | case $ret in | |
1277 | 0|1) # unset or valid bool value | |
1278 | ;; | |
1279 | *) # invalid bool value or something unexpected | |
1280 | error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback" | |
1281 | ;; | |
1282 | esac | |
1283 | return $ret | |
1284 | } | |
1285 | ||
1286 | # Exit the test suite, either by skipping all remaining tests or by | |
1287 | # exiting with an error. If our prerequisite variable $1 falls back | |
1288 | # on a default assume we were opportunistically trying to set up some | |
1289 | # tests and we skip. If it is explicitly "true", then we report a failure. | |
1290 | # | |
1291 | # The error/skip message should be given by $2. | |
1292 | # | |
1293 | test_skip_or_die () { | |
1294 | if ! test_bool_env "$1" false | |
1295 | then | |
1296 | skip_all=$2 | |
1297 | test_done | |
1298 | fi | |
1299 | error "$2" | |
1300 | } | |
1301 | ||
1302 | # The following mingw_* functions obey POSIX shell syntax, but are actually | |
1303 | # bash scripts, and are meant to be used only with bash on Windows. | |
1304 | ||
1305 | # A test_cmp function that treats LF and CRLF equal and avoids to fork | |
1306 | # diff when possible. | |
1307 | mingw_test_cmp () { | |
1308 | # Read text into shell variables and compare them. If the results | |
1309 | # are different, use regular diff to report the difference. | |
1310 | local test_cmp_a= test_cmp_b= | |
1311 | ||
1312 | # When text came from stdin (one argument is '-') we must feed it | |
1313 | # to diff. | |
1314 | local stdin_for_diff= | |
1315 | ||
1316 | # Since it is difficult to detect the difference between an | |
1317 | # empty input file and a failure to read the files, we go straight | |
1318 | # to diff if one of the inputs is empty. | |
1319 | if test -s "$1" && test -s "$2" | |
1320 | then | |
1321 | # regular case: both files non-empty | |
1322 | mingw_read_file_strip_cr_ test_cmp_a <"$1" | |
1323 | mingw_read_file_strip_cr_ test_cmp_b <"$2" | |
1324 | elif test -s "$1" && test "$2" = - | |
1325 | then | |
1326 | # read 2nd file from stdin | |
1327 | mingw_read_file_strip_cr_ test_cmp_a <"$1" | |
1328 | mingw_read_file_strip_cr_ test_cmp_b | |
1329 | stdin_for_diff='<<<"$test_cmp_b"' | |
1330 | elif test "$1" = - && test -s "$2" | |
1331 | then | |
1332 | # read 1st file from stdin | |
1333 | mingw_read_file_strip_cr_ test_cmp_a | |
1334 | mingw_read_file_strip_cr_ test_cmp_b <"$2" | |
1335 | stdin_for_diff='<<<"$test_cmp_a"' | |
1336 | fi | |
1337 | test -n "$test_cmp_a" && | |
1338 | test -n "$test_cmp_b" && | |
1339 | test "$test_cmp_a" = "$test_cmp_b" || | |
1340 | eval "diff -u \"\$@\" $stdin_for_diff" | |
1341 | } | |
1342 | ||
1343 | # $1 is the name of the shell variable to fill in | |
1344 | mingw_read_file_strip_cr_ () { | |
1345 | # Read line-wise using LF as the line separator | |
1346 | # and use IFS to strip CR. | |
1347 | local line | |
1348 | while : | |
1349 | do | |
1350 | if IFS=$'\r' read -r -d $'\n' line | |
1351 | then | |
1352 | # good | |
1353 | line=$line$'\n' | |
1354 | else | |
1355 | # we get here at EOF, but also if the last line | |
1356 | # was not terminated by LF; in the latter case, | |
1357 | # some text was read | |
1358 | if test -z "$line" | |
1359 | then | |
1360 | # EOF, really | |
1361 | break | |
1362 | fi | |
1363 | fi | |
1364 | eval "$1=\$$1\$line" | |
1365 | done | |
1366 | } | |
1367 | ||
1368 | # Like "env FOO=BAR some-program", but run inside a subshell, which means | |
1369 | # it also works for shell functions (though those functions cannot impact | |
1370 | # the environment outside of the test_env invocation). | |
1371 | test_env () { | |
1372 | ( | |
1373 | while test $# -gt 0 | |
1374 | do | |
1375 | case "$1" in | |
1376 | *=*) | |
1377 | eval "${1%%=*}=\${1#*=}" | |
1378 | eval "export ${1%%=*}" | |
1379 | shift | |
1380 | ;; | |
1381 | *) | |
1382 | "$@" 2>&7 | |
1383 | exit | |
1384 | ;; | |
1385 | esac | |
1386 | done | |
1387 | ) | |
1388 | } 7>&2 2>&4 | |
1389 | ||
1390 | # Returns true if the numeric exit code in "$2" represents the expected signal | |
1391 | # in "$1". Signals should be given numerically. | |
1392 | test_match_signal () { | |
1393 | if test "$2" = "$((128 + $1))" | |
1394 | then | |
1395 | # POSIX | |
1396 | return 0 | |
1397 | elif test "$2" = "$((256 + $1))" | |
1398 | then | |
1399 | # ksh | |
1400 | return 0 | |
1401 | fi | |
1402 | return 1 | |
1403 | } | |
1404 | ||
1405 | # Read up to "$1" bytes (or to EOF) from stdin and write them to stdout. | |
1406 | test_copy_bytes () { | |
1407 | perl -e ' | |
1408 | my $len = $ARGV[1]; | |
1409 | while ($len > 0) { | |
1410 | my $s; | |
1411 | my $nread = sysread(STDIN, $s, $len); | |
1412 | die "cannot read: $!" unless defined($nread); | |
1413 | last unless $nread; | |
1414 | print $s; | |
1415 | $len -= $nread; | |
1416 | } | |
1417 | ' - "$1" | |
1418 | } | |
1419 | ||
1420 | # run "$@" inside a non-git directory | |
1421 | nongit () { | |
1422 | test -d non-repo || | |
1423 | mkdir non-repo || | |
1424 | return 1 | |
1425 | ||
1426 | ( | |
1427 | GIT_CEILING_DIRECTORIES=$(pwd) && | |
1428 | export GIT_CEILING_DIRECTORIES && | |
1429 | cd non-repo && | |
1430 | "$@" 2>&7 | |
1431 | ) | |
1432 | } 7>&2 2>&4 | |
1433 | ||
1434 | # convert function arguments or stdin (if not arguments given) to pktline | |
1435 | # representation. If multiple arguments are given, they are separated by | |
1436 | # whitespace and put in a single packet. Note that data containing NULs must be | |
1437 | # given on stdin, and that empty input becomes an empty packet, not a flush | |
1438 | # packet (for that you can just print 0000 yourself). | |
1439 | packetize () { | |
1440 | if test $# -gt 0 | |
1441 | then | |
1442 | packet="$*" | |
1443 | printf '%04x%s' "$((4 + ${#packet}))" "$packet" | |
1444 | else | |
1445 | perl -e ' | |
1446 | my $packet = do { local $/; <STDIN> }; | |
1447 | printf "%04x%s", 4 + length($packet), $packet; | |
1448 | ' | |
1449 | fi | |
1450 | } | |
1451 | ||
1452 | # Parse the input as a series of pktlines, writing the result to stdout. | |
1453 | # Sideband markers are removed automatically, and the output is routed to | |
1454 | # stderr if appropriate. | |
1455 | # | |
1456 | # NUL bytes are converted to "\\0" for ease of parsing with text tools. | |
1457 | depacketize () { | |
1458 | perl -e ' | |
1459 | while (read(STDIN, $len, 4) == 4) { | |
1460 | if ($len eq "0000") { | |
1461 | print "FLUSH\n"; | |
1462 | } else { | |
1463 | read(STDIN, $buf, hex($len) - 4); | |
1464 | $buf =~ s/\0/\\0/g; | |
1465 | if ($buf =~ s/^[\x2\x3]//) { | |
1466 | print STDERR $buf; | |
1467 | } else { | |
1468 | $buf =~ s/^\x1//; | |
1469 | print $buf; | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | ' | |
1474 | } | |
1475 | ||
1476 | # Converts base-16 data into base-8. The output is given as a sequence of | |
1477 | # escaped octals, suitable for consumption by 'printf'. | |
1478 | hex2oct () { | |
1479 | perl -ne 'printf "\\%03o", hex for /../g' | |
1480 | } | |
1481 | ||
1482 | # Set the hash algorithm in use to $1. Only useful when testing the testsuite. | |
1483 | test_set_hash () { | |
1484 | test_hash_algo="$1" | |
1485 | } | |
1486 | ||
1487 | # Detect the hash algorithm in use. | |
1488 | test_detect_hash () { | |
1489 | test_hash_algo="${GIT_TEST_DEFAULT_HASH:-sha1}" | |
1490 | } | |
1491 | ||
1492 | # Load common hash metadata and common placeholder object IDs for use with | |
1493 | # test_oid. | |
1494 | test_oid_init () { | |
1495 | test -n "$test_hash_algo" || test_detect_hash && | |
1496 | test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" && | |
1497 | test_oid_cache <"$TEST_DIRECTORY/oid-info/oid" | |
1498 | } | |
1499 | ||
1500 | # Load key-value pairs from stdin suitable for use with test_oid. Blank lines | |
1501 | # and lines starting with "#" are ignored. Keys must be shell identifier | |
1502 | # characters. | |
1503 | # | |
1504 | # Examples: | |
1505 | # rawsz sha1:20 | |
1506 | # rawsz sha256:32 | |
1507 | test_oid_cache () { | |
1508 | local tag rest k v && | |
1509 | ||
1510 | { test -n "$test_hash_algo" || test_detect_hash; } && | |
1511 | while read tag rest | |
1512 | do | |
1513 | case $tag in | |
1514 | \#*) | |
1515 | continue;; | |
1516 | ?*) | |
1517 | # non-empty | |
1518 | ;; | |
1519 | *) | |
1520 | # blank line | |
1521 | continue;; | |
1522 | esac && | |
1523 | ||
1524 | k="${rest%:*}" && | |
1525 | v="${rest#*:}" && | |
1526 | ||
1527 | if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null | |
1528 | then | |
1529 | BUG 'bad hash algorithm' | |
1530 | fi && | |
1531 | eval "test_oid_${k}_$tag=\"\$v\"" | |
1532 | done | |
1533 | } | |
1534 | ||
1535 | # Look up a per-hash value based on a key ($1). The value must have been loaded | |
1536 | # by test_oid_init or test_oid_cache. | |
1537 | test_oid () { | |
1538 | local algo="${test_hash_algo}" && | |
1539 | ||
1540 | case "$1" in | |
1541 | --hash=*) | |
1542 | algo="${1#--hash=}" && | |
1543 | shift;; | |
1544 | *) | |
1545 | ;; | |
1546 | esac && | |
1547 | ||
1548 | local var="test_oid_${algo}_$1" && | |
1549 | ||
1550 | # If the variable is unset, we must be missing an entry for this | |
1551 | # key-hash pair, so exit with an error. | |
1552 | if eval "test -z \"\${$var+set}\"" | |
1553 | then | |
1554 | BUG "undefined key '$1'" | |
1555 | fi && | |
1556 | eval "printf '%s' \"\${$var}\"" | |
1557 | } | |
1558 | ||
1559 | # Insert a slash into an object ID so it can be used to reference a location | |
1560 | # under ".git/objects". For example, "deadbeef..." becomes "de/adbeef..". | |
1561 | test_oid_to_path () { | |
1562 | local basename=${1#??} | |
1563 | echo "${1%$basename}/$basename" | |
1564 | } | |
1565 | ||
1566 | # Choose a port number based on the test script's number and store it in | |
1567 | # the given variable name, unless that variable already contains a number. | |
1568 | test_set_port () { | |
1569 | local var=$1 port | |
1570 | ||
1571 | if test $# -ne 1 || test -z "$var" | |
1572 | then | |
1573 | BUG "test_set_port requires a variable name" | |
1574 | fi | |
1575 | ||
1576 | eval port=\$$var | |
1577 | case "$port" in | |
1578 | "") | |
1579 | # No port is set in the given env var, use the test | |
1580 | # number as port number instead. | |
1581 | # Remove not only the leading 't', but all leading zeros | |
1582 | # as well, so the arithmetic below won't (mis)interpret | |
1583 | # a test number like '0123' as an octal value. | |
1584 | port=${this_test#${this_test%%[1-9]*}} | |
1585 | if test "${port:-0}" -lt 1024 | |
1586 | then | |
1587 | # root-only port, use a larger one instead. | |
1588 | port=$(($port + 10000)) | |
1589 | fi | |
1590 | ;; | |
1591 | *[!0-9]*|0*) | |
1592 | error >&7 "invalid port number: $port" | |
1593 | ;; | |
1594 | *) | |
1595 | # The user has specified the port. | |
1596 | ;; | |
1597 | esac | |
1598 | ||
1599 | # Make sure that parallel '--stress' test jobs get different | |
1600 | # ports. | |
1601 | port=$(($port + ${GIT_TEST_STRESS_JOB_NR:-0})) | |
1602 | eval $var=$port | |
1603 | } | |
1604 | ||
1605 | # Tests for the hidden file attribute on Windows | |
1606 | test_path_is_hidden () { | |
1607 | test_have_prereq MINGW || | |
1608 | BUG "test_path_is_hidden can only be used on Windows" | |
1609 | ||
1610 | # Use the output of `attrib`, ignore the absolute path | |
1611 | case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac | |
1612 | return 1 | |
1613 | } | |
1614 | ||
1615 | # Check that the given command was invoked as part of the | |
1616 | # trace2-format trace on stdin. | |
1617 | # | |
1618 | # test_subcommand [!] <command> <args>... < <trace> | |
1619 | # | |
1620 | # For example, to look for an invocation of "git upload-pack | |
1621 | # /path/to/repo" | |
1622 | # | |
1623 | # GIT_TRACE2_EVENT=event.log git fetch ... && | |
1624 | # test_subcommand git upload-pack "$PATH" <event.log | |
1625 | # | |
1626 | # If the first parameter passed is !, this instead checks that | |
1627 | # the given command was not called. | |
1628 | # | |
1629 | test_subcommand () { | |
1630 | local negate= | |
1631 | if test "$1" = "!" | |
1632 | then | |
1633 | negate=t | |
1634 | shift | |
1635 | fi | |
1636 | ||
1637 | local expr=$(printf '"%s",' "$@") | |
1638 | expr="${expr%,}" | |
1639 | ||
1640 | if test -n "$negate" | |
1641 | then | |
1642 | ! grep "\[$expr\]" | |
1643 | else | |
1644 | grep "\[$expr\]" | |
1645 | fi | |
1646 | } |