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. 28test_set_editor () { 29 FAKE_EDITOR="$1" 30export FAKE_EDITOR 31 EDITOR='"$FAKE_EDITOR"' 32export EDITOR 33} 34 35test_decode_color () { 36awk' 37 function name(n) { 38 if (n == 0) return "RESET"; 39 if (n == 1) return "BOLD"; 40 if (n == 30) return "BLACK"; 41 if (n == 31) return "RED"; 42 if (n == 32) return "GREEN"; 43 if (n == 33) return "YELLOW"; 44 if (n == 34) return "BLUE"; 45 if (n == 35) return "MAGENTA"; 46 if (n == 36) return "CYAN"; 47 if (n == 37) return "WHITE"; 48 if (n == 40) return "BLACK"; 49 if (n == 41) return "BRED"; 50 if (n == 42) return "BGREEN"; 51 if (n == 43) return "BYELLOW"; 52 if (n == 44) return "BBLUE"; 53 if (n == 45) return "BMAGENTA"; 54 if (n == 46) return "BCYAN"; 55 if (n == 47) return "BWHITE"; 56 } 57 { 58 while (match($0, /\033\[[0-9;]*m/) != 0) { 59 printf "%s<", substr($0, 1, RSTART-1); 60 codes = substr($0, RSTART+2, RLENGTH-3); 61 if (length(codes) == 0) 62 printf "%s", name(0) 63 else { 64 n = split(codes, ary, ";"); 65 sep = ""; 66 for (i = 1; i <= n; i++) { 67 printf "%s%s", sep, name(ary[i]); 68 sep = ";" 69 } 70 } 71 printf ">"; 72$0= substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1); 73 } 74 print 75 } 76 ' 77} 78 79nul_to_q () { 80 perl -pe'y/\000/Q/' 81} 82 83q_to_nul () { 84 perl -pe'y/Q/\000/' 85} 86 87q_to_cr () { 88tr Q '\015' 89} 90 91q_to_tab () { 92tr Q '\011' 93} 94 95qz_to_tab_space () { 96tr QZ '\011\040' 97} 98 99append_cr () { 100sed-e's/$/Q/'|tr Q '\015' 101} 102 103remove_cr () { 104tr'\015' Q |sed-e's/Q$//' 105} 106 107# In some bourne shell implementations, the "unset" builtin returns 108# nonzero status when a variable to be unset was not set in the first 109# place. 110# 111# Use sane_unset when that should not be considered an error. 112 113sane_unset () { 114unset"$@" 115return0 116} 117 118test_tick () { 119iftest -z"${test_tick+set}" 120then 121 test_tick=1112911993 122else 123 test_tick=$(($test_tick + 60)) 124fi 125 GIT_COMMITTER_DATE="$test_tick-0700" 126 GIT_AUTHOR_DATE="$test_tick-0700" 127export GIT_COMMITTER_DATE GIT_AUTHOR_DATE 128} 129 130# Stop execution and start a shell. This is useful for debugging tests and 131# only makes sense together with "-v". 132# 133# Be sure to remove all invocations of this command before submitting. 134 135test_pause () { 136iftest"$verbose"= t;then 137"$SHELL_PATH"<&6>&3 2>&4 138else 139 error >&5"test_pause requires --verbose" 140fi 141} 142 143# Call test_commit with the arguments "<message> [<file> [<contents> [<tag>]]]" 144# 145# This will commit a file with the given contents and the given commit 146# message, and tag the resulting commit with the given tag name. 147# 148# <file>, <contents>, and <tag> all default to <message>. 149 150test_commit () { 151 notick= && 152 signoff= && 153whiletest$#!=0 154do 155case"$1"in 156--notick) 157 notick=yes 158;; 159--signoff) 160 signoff="$1" 161;; 162*) 163break 164;; 165esac 166shift 167done&& 168file=${2:-"$1.t"}&& 169echo"${3-$1}">"$file"&& 170 git add "$file"&& 171iftest -z"$notick" 172then 173 test_tick 174fi&& 175 git commit $signoff-m"$1"&& 176 git tag "${4:-$1}" 177} 178 179# Call test_merge with the arguments "<message> <commit>", where <commit> 180# can be a tag pointing to the commit-to-merge. 181 182test_merge () { 183 test_tick && 184 git merge -m"$1""$2"&& 185 git tag "$1" 186} 187 188# This function helps systems where core.filemode=false is set. 189# Use it instead of plain 'chmod +x' to set or unset the executable bit 190# of a file in the working directory and add it to the index. 191 192test_chmod () { 193chmod"$@"&& 194 git update-index --add"--chmod=$@" 195} 196 197# Unset a configuration variable, but don't fail if it doesn't exist. 198test_unconfig () { 199 git config --unset-all"$@" 200 config_status=$? 201case"$config_status"in 2025)# ok, nothing to unset 203 config_status=0 204;; 205esac 206return$config_status 207} 208 209# Set git config, automatically unsetting it after the test is over. 210test_config () { 211 test_when_finished "test_unconfig '$1'"&& 212 git config "$@" 213} 214 215test_config_global () { 216 test_when_finished "test_unconfig --global '$1'"&& 217 git config --global"$@" 218} 219 220write_script () { 221{ 222echo"#!${2-"$SHELL_PATH"}"&& 223cat 224} >"$1"&& 225chmod+x "$1" 226} 227 228# Use test_set_prereq to tell that a particular prerequisite is available. 229# The prerequisite can later be checked for in two ways: 230# 231# - Explicitly using test_have_prereq. 232# 233# - Implicitly by specifying the prerequisite tag in the calls to 234# test_expect_{success,failure,code}. 235# 236# The single parameter is the prerequisite tag (a simple word, in all 237# capital letters by convention). 238 239test_set_prereq () { 240 satisfied_prereq="$satisfied_prereq$1" 241} 242satisfied_prereq=" " 243lazily_testable_prereq= lazily_tested_prereq= 244 245# Usage: test_lazy_prereq PREREQ 'script' 246test_lazy_prereq () { 247 lazily_testable_prereq="$lazily_testable_prereq$1" 248eval test_prereq_lazily_$1=\$2 249} 250 251test_run_lazy_prereq_ () { 252script=' 253mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" && 254( 255 cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"' 256)' 257 say >&3"checking prerequisite:$1" 258 say >&3"$script" 259 test_eval_ "$script" 260 eval_ret=$? 261rm-rf"$TRASH_DIRECTORY/prereq-test-dir" 262iftest"$eval_ret"=0;then 263 say >&3"prerequisite$1ok" 264else 265 say >&3"prerequisite$1not satisfied" 266fi 267return$eval_ret 268} 269 270test_have_prereq () { 271# prerequisites can be concatenated with ',' 272 save_IFS=$IFS 273 IFS=, 274set -- $* 275 IFS=$save_IFS 276 277 total_prereq=0 278 ok_prereq=0 279 missing_prereq= 280 281for prerequisite 282do 283case"$prerequisite"in 284!*) 285 negative_prereq=t 286 prerequisite=${prerequisite#!} 287;; 288*) 289 negative_prereq= 290esac 291 292case"$lazily_tested_prereq"in 293*"$prerequisite"*) 294;; 295*) 296case"$lazily_testable_prereq"in 297*"$prerequisite"*) 298eval"script=\$test_prereq_lazily_$prerequisite"&& 299if test_run_lazy_prereq_ "$prerequisite""$script" 300then 301 test_set_prereq $prerequisite 302fi 303 lazily_tested_prereq="$lazily_tested_prereq$prerequisite" 304esac 305;; 306esac 307 308 total_prereq=$(($total_prereq + 1)) 309case"$satisfied_prereq"in 310*"$prerequisite"*) 311 satisfied_this_prereq=t 312;; 313*) 314 satisfied_this_prereq= 315esac 316 317case"$satisfied_this_prereq,$negative_prereq"in 318 t,|,t) 319 ok_prereq=$(($ok_prereq + 1)) 320;; 321*) 322# Keep a list of missing prerequisites; restore 323# the negative marker if necessary. 324 prerequisite=${negative_prereq:+!}$prerequisite 325iftest -z"$missing_prereq" 326then 327 missing_prereq=$prerequisite 328else 329 missing_prereq="$prerequisite,$missing_prereq" 330fi 331esac 332done 333 334test$total_prereq=$ok_prereq 335} 336 337test_declared_prereq () { 338case",$test_prereq,"in 339*,$1,*) 340return0 341;; 342esac 343return1 344} 345 346test_expect_failure () { 347 test_start_ 348test"$#"=3&& { test_prereq=$1;shift; } || test_prereq= 349test"$#"=2|| 350 error "bug in the test script: not 2 or 3 parameters to test-expect-failure" 351export test_prereq 352if! test_skip "$@" 353then 354 say >&3"checking known breakage:$2" 355if test_run_ "$2" expecting_failure 356then 357 test_known_broken_ok_ "$1" 358else 359 test_known_broken_failure_ "$1" 360fi 361fi 362 test_finish_ 363} 364 365test_expect_success () { 366 test_start_ 367test"$#"=3&& { test_prereq=$1;shift; } || test_prereq= 368test"$#"=2|| 369 error "bug in the test script: not 2 or 3 parameters to test-expect-success" 370export test_prereq 371if! test_skip "$@" 372then 373 say >&3"expecting success:$2" 374if test_run_ "$2" 375then 376 test_ok_ "$1" 377else 378 test_failure_ "$@" 379fi 380fi 381 test_finish_ 382} 383 384# test_external runs external test scripts that provide continuous 385# test output about their progress, and succeeds/fails on 386# zero/non-zero exit code. It outputs the test output on stdout even 387# in non-verbose mode, and announces the external script with "# run 388# <n>: ..." before running it. When providing relative paths, keep in 389# mind that all scripts run in "trash directory". 390# Usage: test_external description command arguments... 391# Example: test_external 'Perl API' perl ../path/to/test.pl 392test_external () { 393test"$#"=4&& { test_prereq=$1;shift; } || test_prereq= 394test"$#"=3|| 395 error >&5"bug in the test script: not 3 or 4 parameters to test_external" 396 descr="$1" 397shift 398export test_prereq 399if! test_skip "$descr""$@" 400then 401# Announce the script to reduce confusion about the 402# test output that follows. 403 say_color """# run$test_count:$descr($*)" 404# Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG 405# to be able to use them in script 406export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG 407# Run command; redirect its stderr to &4 as in 408# test_run_, but keep its stdout on our stdout even in 409# non-verbose mode. 410"$@"2>&4 411if["$?"=0] 412then 413iftest$test_external_has_tap-eq0;then 414 test_ok_ "$descr" 415else 416 say_color """# test_external test$descrwas ok" 417 test_success=$(($test_success + 1)) 418fi 419else 420iftest$test_external_has_tap-eq0;then 421 test_failure_ "$descr""$@" 422else 423 say_color error "# test_external test$descrfailed: $@" 424 test_failure=$(($test_failure + 1)) 425fi 426fi 427fi 428} 429 430# Like test_external, but in addition tests that the command generated 431# no output on stderr. 432test_external_without_stderr () { 433# The temporary file has no (and must have no) security 434# implications. 435 tmp=${TMPDIR:-/tmp} 436 stderr="$tmp/git-external-stderr.$$.tmp" 437 test_external "$@"4>"$stderr" 438[-f"$stderr"] || error "Internal error:$stderrdisappeared." 439 descr="no stderr:$1" 440shift 441 say >&3"# expecting no stderr from previous command" 442if[ !-s"$stderr"];then 443rm"$stderr" 444 445iftest$test_external_has_tap-eq0;then 446 test_ok_ "$descr" 447else 448 say_color """# test_external_without_stderr test$descrwas ok" 449 test_success=$(($test_success + 1)) 450fi 451else 452if["$verbose"= t ];then 453 output=`echo; echo "# Stderr is:"; cat "$stderr"` 454else 455 output= 456fi 457# rm first in case test_failure exits. 458rm"$stderr" 459iftest$test_external_has_tap-eq0;then 460 test_failure_ "$descr""$@""$output" 461else 462 say_color error "# test_external_without_stderr test$descrfailed: $@:$output" 463 test_failure=$(($test_failure + 1)) 464fi 465fi 466} 467 468# debugging-friendly alternatives to "test [-f|-d|-e]" 469# The commands test the existence or non-existence of $1. $2 can be 470# given to provide a more precise diagnosis. 471test_path_is_file () { 472if! [-f"$1"] 473then 474echo"File$1doesn't exist. $*" 475 false 476fi 477} 478 479test_path_is_dir () { 480if! [-d"$1"] 481then 482echo"Directory$1doesn't exist. $*" 483 false 484fi 485} 486 487test_path_is_missing () { 488if[-e"$1"] 489then 490echo"Path exists:" 491ls-ld"$1" 492if[$#-ge1];then 493echo"$*" 494fi 495 false 496fi 497} 498 499# test_line_count checks that a file has the number of lines it 500# ought to. For example: 501# 502# test_expect_success 'produce exactly one line of output' ' 503# do something >output && 504# test_line_count = 1 output 505# ' 506# 507# is like "test $(wc -l <output) = 1" except that it passes the 508# output through when the number of lines is wrong. 509 510test_line_count () { 511iftest$#!=3 512then 513 error "bug in the test script: not 3 parameters to test_line_count" 514elif!test$(wc -l <"$3")"$1""$2" 515then 516echo"test_line_count: line count for$3!$1$2" 517cat"$3" 518return1 519fi 520} 521 522# This is not among top-level (test_expect_success | test_expect_failure) 523# but is a prefix that can be used in the test script, like: 524# 525# test_expect_success 'complain and die' ' 526# do something && 527# do something else && 528# test_must_fail git checkout ../outerspace 529# ' 530# 531# Writing this as "! git checkout ../outerspace" is wrong, because 532# the failure could be due to a segv. We want a controlled failure. 533 534test_must_fail () { 535"$@" 536 exit_code=$? 537iftest$exit_code=0;then 538echo>&2"test_must_fail: command succeeded: $*" 539return1 540eliftest$exit_code-gt129-a$exit_code-le192;then 541echo>&2"test_must_fail: died by signal: $*" 542return1 543eliftest$exit_code=127;then 544echo>&2"test_must_fail: command not found: $*" 545return1 546eliftest$exit_code=126;then 547echo>&2"test_must_fail: valgrind error: $*" 548return1 549fi 550return0 551} 552 553# Similar to test_must_fail, but tolerates success, too. This is 554# meant to be used in contexts like: 555# 556# test_expect_success 'some command works without configuration' ' 557# test_might_fail git config --unset all.configuration && 558# do something 559# ' 560# 561# Writing "git config --unset all.configuration || :" would be wrong, 562# because we want to notice if it fails due to segv. 563 564test_might_fail () { 565"$@" 566 exit_code=$? 567iftest$exit_code-gt129-a$exit_code-le192;then 568echo>&2"test_might_fail: died by signal: $*" 569return1 570eliftest$exit_code=127;then 571echo>&2"test_might_fail: command not found: $*" 572return1 573fi 574return0 575} 576 577# Similar to test_must_fail and test_might_fail, but check that a 578# given command exited with a given exit code. Meant to be used as: 579# 580# test_expect_success 'Merge with d/f conflicts' ' 581# test_expect_code 1 git merge "merge msg" B master 582# ' 583 584test_expect_code () { 585 want_code=$1 586shift 587"$@" 588 exit_code=$? 589iftest$exit_code=$want_code 590then 591return0 592fi 593 594echo>&2"test_expect_code: command exited with$exit_code, we wanted$want_code$*" 595return1 596} 597 598# test_cmp is a helper function to compare actual and expected output. 599# You can use it like: 600# 601# test_expect_success 'foo works' ' 602# echo expected >expected && 603# foo >actual && 604# test_cmp expected actual 605# ' 606# 607# This could be written as either "cmp" or "diff -u", but: 608# - cmp's output is not nearly as easy to read as diff -u 609# - not all diff versions understand "-u" 610 611test_cmp() { 612$GIT_TEST_CMP"$@" 613} 614 615# Check if the file expected to be empty is indeed empty, and barfs 616# otherwise. 617 618test_must_be_empty () { 619iftest -s"$1" 620then 621echo"'$1' is not empty, it contains:" 622cat"$1" 623return1 624fi 625} 626 627# Tests that its two parameters refer to the same revision 628test_cmp_rev () { 629 git rev-parse --verify"$1">expect.rev&& 630 git rev-parse --verify"$2">actual.rev&& 631 test_cmp expect.rev actual.rev 632} 633 634# Print a sequence of numbers or letters in increasing order. This is 635# similar to GNU seq(1), but the latter might not be available 636# everywhere (and does not do letters). It may be used like: 637# 638# for i in `test_seq 100`; do 639# for j in `test_seq 10 20`; do 640# for k in `test_seq a z`; do 641# echo $i-$j-$k 642# done 643# done 644# done 645 646test_seq () { 647case$#in 6481)set1"$@";; 6492) ;; 650*) error "bug in the test script: not 1 or 2 parameters to test_seq";; 651esac 652 perl -le'print for$ARGV[0]..$ARGV[1]'--"$@" 653} 654 655# This function can be used to schedule some commands to be run 656# unconditionally at the end of the test to restore sanity: 657# 658# test_expect_success 'test core.capslock' ' 659# git config core.capslock true && 660# test_when_finished "git config --unset core.capslock" && 661# hello world 662# ' 663# 664# That would be roughly equivalent to 665# 666# test_expect_success 'test core.capslock' ' 667# git config core.capslock true && 668# hello world 669# git config --unset core.capslock 670# ' 671# 672# except that the greeting and config --unset must both succeed for 673# the test to pass. 674# 675# Note that under --immediate mode, no clean-up is done to help diagnose 676# what went wrong. 677 678test_when_finished () { 679 test_cleanup="{ $* 680 } && (exit\"\$eval_ret\"); eval_ret=\$?;$test_cleanup" 681} 682 683# Most tests can use the created repository, but some may need to create more. 684# Usage: test_create_repo <directory> 685test_create_repo () { 686test"$#"=1|| 687 error "bug in the test script: not 1 parameter to test-create-repo" 688 repo="$1" 689mkdir-p"$repo" 690( 691cd"$repo"|| error "Cannot setup test environment" 692"$GIT_EXEC_PATH/git-init""--template=$GIT_BUILD_DIR/templates/blt/">&3 2>&4|| 693 error "cannot run git init -- have you built things yet?" 694mv .git/hooks .git/hooks-disabled 695) ||exit 696} 697 698# This function helps on symlink challenged file systems when it is not 699# important that the file system entry is a symbolic link. 700# Use test_ln_s_add instead of "ln -s x y && git add y" to add a 701# symbolic link entry y to the index. 702 703test_ln_s_add () { 704if test_have_prereq SYMLINKS 705then 706ln-s"$1""$2"&& 707 git update-index --add"$2" 708else 709printf'%s'"$1">"$2"&& 710 ln_s_obj=$(git hash-object -w "$2")&& 711 git update-index --add --cacheinfo120000$ln_s_obj"$2" 712fi 713} 714 715perl () { 716command"$PERL_PATH""$@" 717} 718 719# Is the value one of the various ways to spell a boolean true/false? 720test_normalize_bool () { 721 git -c magic.variable="$1" config --bool magic.variable 2>/dev/null 722} 723 724# Given a variable $1, normalize the value of it to one of "true", 725# "false", or "auto" and store the result to it. 726# 727# test_tristate GIT_TEST_HTTPD 728# 729# A variable set to an empty string is set to 'false'. 730# A variable set to 'false' or 'auto' keeps its value. 731# Anything else is set to 'true'. 732# An unset variable defaults to 'auto'. 733# 734# The last rule is to allow people to set the variable to an empty 735# string and export it to decline testing the particular feature 736# for versions both before and after this change. We used to treat 737# both unset and empty variable as a signal for "do not test" and 738# took any non-empty string as "please test". 739 740test_tristate () { 741ifeval"test x\"\${$1+isset}\"= xisset" 742then 743# explicitly set 744eval" 745 case\"\$$1\"in 746 '')$1=false ;; 747 auto) ;; 748 *)$1=\$(test_normalize_bool \$$1 || echo true);; 749 esac 750 " 751else 752eval"$1=auto" 753fi 754} 755 756# Exit the test suite, either by skipping all remaining tests or by 757# exiting with an error. If "$1" is "auto", we then we assume we were 758# opportunistically trying to set up some tests and we skip. If it is 759# "true", then we report a failure. 760# 761# The error/skip message should be given by $2. 762# 763test_skip_or_die () { 764case"$1"in 765 auto) 766 skip_all=$2 767 test_done 768;; 769 true) 770 error "$2" 771;; 772*) 773 error "BUG: test tristate is '$1' (real error:$2)" 774esac 775} 776 777# The following mingw_* functions obey POSIX shell syntax, but are actually 778# bash scripts, and are meant to be used only with bash on Windows. 779 780# A test_cmp function that treats LF and CRLF equal and avoids to fork 781# diff when possible. 782mingw_test_cmp () { 783# Read text into shell variables and compare them. If the results 784# are different, use regular diff to report the difference. 785local test_cmp_a= test_cmp_b= 786 787# When text came from stdin (one argument is '-') we must feed it 788# to diff. 789local stdin_for_diff= 790 791# Since it is difficult to detect the difference between an 792# empty input file and a failure to read the files, we go straight 793# to diff if one of the inputs is empty. 794iftest -s"$1"&&test -s"$2" 795then 796# regular case: both files non-empty 797 mingw_read_file_strip_cr_ test_cmp_a <"$1" 798 mingw_read_file_strip_cr_ test_cmp_b <"$2" 799eliftest -s"$1"&&test"$2"= - 800then 801# read 2nd file from stdin 802 mingw_read_file_strip_cr_ test_cmp_a <"$1" 803 mingw_read_file_strip_cr_ test_cmp_b 804 stdin_for_diff='<<<"$test_cmp_b"' 805eliftest"$1"= - &&test -s"$2" 806then 807# read 1st file from stdin 808 mingw_read_file_strip_cr_ test_cmp_a 809 mingw_read_file_strip_cr_ test_cmp_b <"$2" 810 stdin_for_diff='<<<"$test_cmp_a"' 811fi 812test -n"$test_cmp_a"&& 813test -n"$test_cmp_b"&& 814test"$test_cmp_a"="$test_cmp_b"|| 815eval"diff -u\"\$@\"$stdin_for_diff" 816} 817 818# $1 is the name of the shell variable to fill in 819mingw_read_file_strip_cr_ () { 820# Read line-wise using LF as the line separator 821# and use IFS to strip CR. 822local line 823while: 824do 825if IFS=$'\r'read -r -d $'\n' line 826then 827# good 828 line=$line$'\n' 829else 830# we get here at EOF, but also if the last line 831# was not terminated by LF; in the latter case, 832# some text was read 833iftest -z"$line" 834then 835# EOF, really 836break 837fi 838fi 839eval"$1=\$$1\$line" 840done 841}