1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5# This program is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 2 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program. If not, see http://www.gnu.org/licenses/ . 17 18# if --tee was passed, write the output not only to the terminal, but 19# additionally to the file test-results/$BASENAME.out, too. 20case"$GIT_TEST_TEE_STARTED, $* "in 21done,*) 22# do not redirect again 23;; 24*' --tee '*|*' --va'*) 25mkdir-p test-results 26 BASE=test-results/$(basename "$0" .sh) 27(GIT_TEST_TEE_STARTED=done${SHELL-sh}"$0""$@"2>&1; 28echo $? >$BASE.exit) |tee$BASE.out 29test"$(cat $BASE.exit)"=0 30exit 31;; 32esac 33 34# Keep the original TERM for say_color 35ORIGINAL_TERM=$TERM 36 37# Test the binaries we have just built. The tests are kept in 38# t/ subdirectory and are run in 'trash directory' subdirectory. 39iftest -z"$TEST_DIRECTORY" 40then 41# We allow tests to override this, in case they want to run tests 42# outside of t/, e.g. for running tests on the test library 43# itself. 44 TEST_DIRECTORY=$(pwd) 45fi 46iftest -z"$TEST_OUTPUT_DIRECTORY" 47then 48# Similarly, override this to store the test-results subdir 49# elsewhere 50 TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY 51fi 52GIT_BUILD_DIR="$TEST_DIRECTORY"/.. 53 54. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS 55export PERL_PATH SHELL_PATH 56 57# For repeatability, reset the environment to known value. 58LANG=C 59LC_ALL=C 60PAGER=cat 61TZ=UTC 62TERM=dumb 63export LANG LC_ALL PAGER TERM TZ 64EDITOR=: 65# A call to "unset" with no arguments causes at least Solaris 10 66# /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets 67# deriving from the command substitution clustered with the other 68# ones. 69unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH"-e' 70 my @env = keys %ENV; 71 my$ok= join("|", qw( 72 TRACE 73 DEBUG 74 USE_LOOKUP 75 TEST 76 .*_TEST 77 PROVE 78 VALGRIND 79 PERF_AGGREGATING_LATER 80 )); 81 my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env); 82 print join("\n", @vars); 83') 84GIT_AUTHOR_EMAIL=author@example.com 85GIT_AUTHOR_NAME='A U Thor' 86GIT_COMMITTER_EMAIL=committer@example.com 87GIT_COMMITTER_NAME='C O Mitter' 88GIT_MERGE_VERBOSITY=5 89GIT_MERGE_AUTOEDIT=no 90export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT 91export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME 92export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME 93export EDITOR 94 95# Protect ourselves from common misconfiguration to export 96# CDPATH into the environment 97unset CDPATH 98 99unset GREP_OPTIONS 100 101case$(echo $GIT_TRACE |tr "[A-Z]" "[a-z]")in 1021|2|true) 103echo"* warning: Some tests will not work if GIT_TRACE" \ 104"is set as to trace on STDERR ! *" 105echo"* warning: Please set GIT_TRACE to something" \ 106"other than 1, 2 or true ! *" 107;; 108esac 109 110# Convenience 111# 112# A regexp to match 5 and 40 hexdigits 113_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' 114_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05" 115 116# Zero SHA-1 117_z40=0000000000000000000000000000000000000000 118 119# Line feed 120LF=' 121' 122 123export _x05 _x40 _z40 LF 124 125# Each test should start with something like this, after copyright notices: 126# 127# test_description='Description of this test... 128# This test checks if command xyzzy does the right thing... 129# ' 130# . ./test-lib.sh 131["x$ORIGINAL_TERM"!="xdumb"] && ( 132 TERM=$ORIGINAL_TERM&& 133export TERM && 134[-t1] && 135tput bold >/dev/null 2>&1&& 136tput setaf 1>/dev/null 2>&1&& 137tput sgr0 >/dev/null 2>&1 138) && 139 color=t 140 141whiletest"$#"-ne0 142do 143case"$1"in 144-d|--d|--de|--deb|--debu|--debug) 145 debug=t;shift;; 146-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate) 147 immediate=t;shift;; 148-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests) 149 GIT_TEST_LONG=t;export GIT_TEST_LONG;shift;; 150-h|--h|--he|--hel|--help) 151help=t;shift;; 152-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose) 153 verbose=t;shift;; 154-q|--q|--qu|--qui|--quie|--quiet) 155# Ignore --quiet under a TAP::Harness. Saying how many tests 156# passed without the ok/not ok details is always an error. 157test -z"$HARNESS_ACTIVE"&& quiet=t;shift;; 158--with-dashes) 159 with_dashes=t;shift;; 160--no-color) 161 color=;shift;; 162--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind) 163 valgrind=t; verbose=t;shift;; 164--tee) 165shift;;# was handled already 166--root=*) 167 root=$(expr "z$1" : 'z[^=]*=\(.*\)') 168 shift ;; 169 *) 170 echo "error: unknown test option '$1'" >&2; exit 1 ;; 171 esac 172done 173 174if test -n "$color"; then 175 say_color () { 176 ( 177 TERM=$ORIGINAL_TERM 178 export TERM 179 case "$1" in 180 error) tput bold; tput setaf 1;; # bold red 181 skip) tput bold; tput setaf 2;; # bold green 182 pass) tput setaf 2;; # green 183 info) tput setaf 3;; # brown 184 *) test -n "$quiet" && return;; 185 esac 186 shift 187 printf "%s" "$*" 188 tput sgr0 189 echo 190 ) 191 } 192else 193 say_color() { 194 test -z "$1" && test -n "$quiet" && return 195 shift 196 echo "$*" 197 } 198fi 199 200error () { 201 say_color error "error: $*" 202 GIT_EXIT_OK=t 203 exit 1 204} 205 206say () { 207 say_color info "$*" 208} 209 210test "${test_description}" != "" || 211error "Test script did not set test_description." 212 213if test "$help" = "t" 214then 215 echo "$test_description" 216 exit 0 217fi 218 219exec 5>&1 220exec 6<&0 221if test "$verbose" = "t" 222then 223 exec 4>&2 3>&1 224else 225 exec 4>/dev/null 3>/dev/null 226fi 227 228test_failure=0 229test_count=0 230test_fixed=0 231test_broken=0 232test_success=0 233 234test_external_has_tap=0 235 236die () { 237 code=$? 238 if test -n "$GIT_EXIT_OK" 239 then 240 exit$code 241 else 242 echo >&5 "FATAL: Unexpected exit with code$code" 243 exit 1 244 fi 245} 246 247GIT_EXIT_OK= 248trap 'die' EXIT 249 250# The user-facing functions are loaded from a separate file so that 251# test_perf subshells can have them too 252. "$TEST_DIRECTORY/test-lib-functions.sh" 253 254# You are not expected to call test_ok_ and test_failure_ directly, use 255# the text_expect_* functions instead. 256 257test_ok_ () { 258 test_success=$(($test_success + 1)) 259 say_color "" "ok$test_count- $@" 260} 261 262test_failure_ () { 263 test_failure=$(($test_failure + 1)) 264 say_color error "not ok -$test_count$1" 265 shift 266 echo "$@" | sed -e 's/^/# /' 267test"$immediate"=""|| { GIT_EXIT_OK=t;exit1; } 268} 269 270test_known_broken_ok_ () { 271 test_fixed=$(($test_fixed+1)) 272 say_color """ok$test_count- $@ # TODO known breakage" 273} 274 275test_known_broken_failure_ () { 276 test_broken=$(($test_broken+1)) 277 say_color skip "not ok$test_count- $@ # TODO known breakage" 278} 279 280test_debug () { 281test"$debug"=""||eval"$1" 282} 283 284test_eval_ () { 285# This is a separate function because some tests use 286# "return" to end a test_expect_success block early. 287eval</dev/null >&3 2>&4"$*" 288} 289 290test_run_ () { 291 test_cleanup=: 292 expecting_failure=$2 293 test_eval_ "$1" 294 eval_ret=$? 295 296iftest -z"$immediate"||test$eval_ret=0||test -n"$expecting_failure" 297then 298 test_eval_ "$test_cleanup" 299fi 300iftest"$verbose"="t"&&test -n"$HARNESS_ACTIVE";then 301echo"" 302fi 303return"$eval_ret" 304} 305 306test_skip () { 307 test_count=$(($test_count+1)) 308 to_skip= 309for skp in$GIT_SKIP_TESTS 310do 311case$this_test.$test_countin 312$skp) 313 to_skip=t 314break 315esac 316done 317iftest -z"$to_skip"&&test -n"$test_prereq"&& 318! test_have_prereq "$test_prereq" 319then 320 to_skip=t 321fi 322case"$to_skip"in 323 t) 324 of_prereq= 325iftest"$missing_prereq"!="$test_prereq" 326then 327 of_prereq=" of$test_prereq" 328fi 329 330 say_color skip >&3"skipping test: $@" 331 say_color skip "ok$test_count# skip$1(missing$missing_prereq${of_prereq})" 332: true 333;; 334*) 335 false 336;; 337esac 338} 339 340# stub; perf-lib overrides it 341test_at_end_hook_ () { 342: 343} 344 345test_done () { 346 GIT_EXIT_OK=t 347 348iftest -z"$HARNESS_ACTIVE";then 349 test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results" 350mkdir-p"$test_results_dir" 351 test_results_path="$test_results_dir/${0%.sh}-$$.counts" 352 353cat>>"$test_results_path"<<-EOF 354 total$test_count 355 success$test_success 356 fixed$test_fixed 357 broken$test_broken 358 failed$test_failure 359 360 EOF 361fi 362 363iftest"$test_fixed"!=0 364then 365 say_color pass "# fixed$test_fixedknown breakage(s)" 366fi 367iftest"$test_broken"!=0 368then 369 say_color error "# still have$test_brokenknown breakage(s)" 370 msg="remaining$(($test_count-$test_broken)) test(s)" 371else 372 msg="$test_counttest(s)" 373fi 374case"$test_failure"in 3750) 376# Maybe print SKIP message 377[-z"$skip_all"] || skip_all=" # SKIP$skip_all" 378 379iftest$test_external_has_tap-eq0;then 380 say_color pass "# passed all$msg" 381 say "1..$test_count$skip_all" 382fi 383 384test -d"$remove_trash"&& 385cd"$(dirname "$remove_trash")"&& 386rm-rf"$(basename "$remove_trash")" 387 388 test_at_end_hook_ 389 390exit0;; 391 392*) 393iftest$test_external_has_tap-eq0;then 394 say_color error "# failed$test_failureamong$msg" 395 say "1..$test_count" 396fi 397 398exit1;; 399 400esac 401} 402 403iftest -n"$valgrind" 404then 405 make_symlink () { 406test -h"$2"&& 407test"$1"="$(readlink "$2")"|| { 408# be super paranoid 409ifmkdir"$2".lock 410then 411rm-f"$2"&& 412ln-s"$1""$2"&& 413rm-r"$2".lock 414else 415whiletest -d"$2".lock 416do 417 say "Waiting for lock on$2." 418sleep1 419done 420fi 421} 422} 423 424 make_valgrind_symlink () { 425# handle only executables, unless they are shell libraries that 426# need to be in the exec-path. We will just use "#!" as a 427# guess for a shell-script, since we have no idea what the user 428# may have configured as the shell path. 429test -x"$1"|| 430test"#!"="$(head -c 2 <"$1")"|| 431return; 432 433 base=$(basename "$1") 434 symlink_target=$GIT_BUILD_DIR/$base 435# do not override scripts 436iftest -x"$symlink_target"&& 437test!-d"$symlink_target"&& 438test"#!"!="$(head -c 2 < "$symlink_target")" 439then 440 symlink_target=../valgrind.sh 441fi 442case"$base"in 443*.sh|*.perl) 444 symlink_target=../unprocessed-script 445esac 446# create the link, or replace it if it is out of date 447 make_symlink "$symlink_target""$GIT_VALGRIND/bin/$base"||exit 448} 449 450# override all git executables in TEST_DIRECTORY/.. 451 GIT_VALGRIND=$TEST_DIRECTORY/valgrind 452mkdir-p"$GIT_VALGRIND"/bin 453forfilein$GIT_BUILD_DIR/git*$GIT_BUILD_DIR/test-* 454do 455 make_valgrind_symlink $file 456done 457# special-case the mergetools loadables 458 make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools" 459 OLDIFS=$IFS 460 IFS=: 461for path in$PATH 462do 463ls"$path"/git-*2> /dev/null | 464whilereadfile 465do 466 make_valgrind_symlink "$file" 467done 468done 469 IFS=$OLDIFS 470 PATH=$GIT_VALGRIND/bin:$PATH 471 GIT_EXEC_PATH=$GIT_VALGRIND/bin 472export GIT_VALGRIND 473eliftest -n"$GIT_TEST_INSTALLED";then 474 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)|| 475 error "Cannot run git from$GIT_TEST_INSTALLED." 476 PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH 477 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} 478else# normal case, use ../bin-wrappers only unless $with_dashes: 479 git_bin_dir="$GIT_BUILD_DIR/bin-wrappers" 480if!test -x"$git_bin_dir/git";then 481iftest -z"$with_dashes";then 482 say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH" 483fi 484 with_dashes=t 485fi 486 PATH="$git_bin_dir:$PATH" 487 GIT_EXEC_PATH=$GIT_BUILD_DIR 488iftest -n"$with_dashes";then 489 PATH="$GIT_BUILD_DIR:$PATH" 490fi 491fi 492GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt 493unset GIT_CONFIG 494GIT_CONFIG_NOSYSTEM=1 495GIT_ATTR_NOSYSTEM=1 496export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM 497 498iftest -z"$GIT_TEST_CMP" 499then 500iftest -n"$GIT_TEST_CMP_USE_COPIED_CONTEXT" 501then 502 GIT_TEST_CMP="$DIFF-c" 503else 504 GIT_TEST_CMP="$DIFF-u" 505fi 506fi 507 508GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git 509export GITPERLLIB 510test -d"$GIT_BUILD_DIR"/templates/blt || { 511 error "You haven't built things yet, have you?" 512} 513 514iftest -z"$GIT_TEST_INSTALLED"&&test -z"$NO_PYTHON" 515then 516 GITPYTHONLIB="$GIT_BUILD_DIR/git_remote_helpers/build/lib" 517export GITPYTHONLIB 518test -d"$GIT_BUILD_DIR"/git_remote_helpers/build || { 519 error "You haven't built git_remote_helpers yet, have you?" 520} 521fi 522 523if!test -x"$GIT_BUILD_DIR"/test-chmtime;then 524echo>&2'You need to build test-chmtime:' 525echo>&2'Run "make test-chmtime" in the source (toplevel) directory' 526exit1 527fi 528 529# Test repository 530test="trash directory.$(basename "$0" .sh)" 531test -n"$root"&&test="$root/$test" 532case"$test"in 533/*) TRASH_DIRECTORY="$test";; 534*) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$test";; 535esac 536test!-z"$debug"|| remove_trash=$TRASH_DIRECTORY 537rm-fr"$test"|| { 538 GIT_EXIT_OK=t 539echo>&5"FATAL: Cannot prepare test area" 540exit1 541} 542 543HOME="$TRASH_DIRECTORY" 544export HOME 545 546iftest -z"$TEST_NO_CREATE_REPO";then 547 test_create_repo "$test" 548else 549mkdir-p"$test" 550fi 551# Use -P to resolve symlinks in our working directory so that the cwd 552# in subprocesses like git equals our $PWD (for pathname comparisons). 553cd -P"$test"||exit1 554 555this_test=${0##*/} 556this_test=${this_test%%-*} 557for skp in$GIT_SKIP_TESTS 558do 559case"$this_test"in 560$skp) 561 say_color skip >&3"skipping test$this_testaltogether" 562 skip_all="skip all tests in$this_test" 563 test_done 564esac 565done 566 567# Provide an implementation of the 'yes' utility 568yes() { 569iftest$#=0 570then 571 y=y 572else 573 y="$*" 574fi 575 576whileecho"$y" 577do 578: 579done 580} 581 582# Fix some commands on Windows 583case$(uname -s)in 584*MINGW*) 585# Windows has its own (incompatible) sort and find 586sort() { 587/usr/bin/sort"$@" 588} 589find() { 590/usr/bin/find"$@" 591} 592sum() { 593md5sum"$@" 594} 595# git sees Windows-style pwd 596pwd() { 597builtin pwd -W 598} 599# no POSIX permissions 600# backslashes in pathspec are converted to '/' 601# exec does not inherit the PID 602 test_set_prereq MINGW 603 test_set_prereq SED_STRIPS_CR 604;; 605*CYGWIN*) 606 test_set_prereq POSIXPERM 607 test_set_prereq EXECKEEPSPID 608 test_set_prereq NOT_MINGW 609 test_set_prereq SED_STRIPS_CR 610;; 611*) 612 test_set_prereq POSIXPERM 613 test_set_prereq BSLASHPSPEC 614 test_set_prereq EXECKEEPSPID 615 test_set_prereq NOT_MINGW 616;; 617esac 618 619( COLUMNS=1&&test$COLUMNS=1) && test_set_prereq COLUMNS_CAN_BE_1 620test -z"$NO_PERL"&& test_set_prereq PERL 621test -z"$NO_PYTHON"&& test_set_prereq PYTHON 622test -n"$USE_LIBPCRE"&& test_set_prereq LIBPCRE 623test -z"$NO_GETTEXT"&& test_set_prereq GETTEXT 624 625# Can we rely on git's output in the C locale? 626iftest -n"$GETTEXT_POISON" 627then 628 GIT_GETTEXT_POISON=YesPlease 629export GIT_GETTEXT_POISON 630 test_set_prereq GETTEXT_POISON 631else 632 test_set_prereq C_LOCALE_OUTPUT 633fi 634 635# Use this instead of test_cmp to compare files that contain expected and 636# actual output from git commands that can be translated. When running 637# under GETTEXT_POISON this pretends that the command produced expected 638# results. 639test_i18ncmp () { 640test -n"$GETTEXT_POISON"|| test_cmp "$@" 641} 642 643# Use this instead of "grep expected-string actual" to see if the 644# output from a git command that can be translated either contains an 645# expected string, or does not contain an unwanted one. When running 646# under GETTEXT_POISON this pretends that the command produced expected 647# results. 648test_i18ngrep () { 649iftest -n"$GETTEXT_POISON" 650then 651:# pretend success 652eliftest"x!"="x$1" 653then 654shift 655!grep"$@" 656else 657grep"$@" 658fi 659} 660 661# test whether the filesystem supports symbolic links 662ln-s x y 2>/dev/null &&test -h y 2>/dev/null && test_set_prereq SYMLINKS 663rm-f y 664 665# When the tests are run as root, permission tests will report that 666# things are writable when they shouldn't be. 667test -w/ || test_set_prereq SANITY