1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano 4# 5 6# if --tee was passed, write the output not only to the terminal, but 7# additionally to the file test-results/$BASENAME.out, too. 8case"$GIT_TEST_TEE_STARTED, $* "in 9done,*) 10# do not redirect again 11;; 12*' --tee '*|*' --va'*) 13mkdir-p test-results 14 BASE=test-results/$(basename "$0" .sh) 15(GIT_TEST_TEE_STARTED=done${SHELL-sh}"$0""$@"2>&1; 16echo $? >$BASE.exit) |tee$BASE.out 17test"$(cat $BASE.exit)"=0 18exit 19;; 20esac 21 22# Keep the original TERM for say_color 23ORIGINAL_TERM=$TERM 24 25# For repeatability, reset the environment to known value. 26LANG=C 27LC_ALL=C 28PAGER=cat 29TZ=UTC 30TERM=dumb 31export LANG LC_ALL PAGER TERM TZ 32EDITOR=: 33unset VISUAL 34unset GIT_EDITOR 35unset AUTHOR_DATE 36unset AUTHOR_EMAIL 37unset AUTHOR_NAME 38unset COMMIT_AUTHOR_EMAIL 39unset COMMIT_AUTHOR_NAME 40unset EMAIL 41unset GIT_ALTERNATE_OBJECT_DIRECTORIES 42unset GIT_AUTHOR_DATE 43GIT_AUTHOR_EMAIL=author@example.com 44GIT_AUTHOR_NAME='A U Thor' 45unset GIT_COMMITTER_DATE 46GIT_COMMITTER_EMAIL=committer@example.com 47GIT_COMMITTER_NAME='C O Mitter' 48unset GIT_DIFF_OPTS 49unset GIT_DIR 50unset GIT_WORK_TREE 51unset GIT_EXTERNAL_DIFF 52unset GIT_INDEX_FILE 53unset GIT_OBJECT_DIRECTORY 54unset GIT_CEILING_DIRECTORIES 55unset SHA1_FILE_DIRECTORIES 56unset SHA1_FILE_DIRECTORY 57GIT_MERGE_VERBOSITY=5 58export GIT_MERGE_VERBOSITY 59export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME 60export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME 61export EDITOR 62GIT_TEST_CMP=${GIT_TEST_CMP:-diff -u} 63 64# Protect ourselves from common misconfiguration to export 65# CDPATH into the environment 66unset CDPATH 67 68case$(echo $GIT_TRACE |tr "[A-Z]" "[a-z]")in 691|2|true) 70echo"* warning: Some tests will not work if GIT_TRACE" \ 71"is set as to trace on STDERR ! *" 72echo"* warning: Please set GIT_TRACE to something" \ 73"other than 1, 2 or true ! *" 74;; 75esac 76 77# Convenience 78# 79# A regexp to match 5 and 40 hexdigits 80_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' 81_x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05" 82 83# Each test should start with something like this, after copyright notices: 84# 85# test_description='Description of this test... 86# This test checks if command xyzzy does the right thing... 87# ' 88# . ./test-lib.sh 89["x$ORIGINAL_TERM"!="xdumb"] && ( 90 TERM=$ORIGINAL_TERM&& 91export TERM && 92[-t1] && 93tput bold >/dev/null 2>&1&& 94tput setaf 1>/dev/null 2>&1&& 95tput sgr0 >/dev/null 2>&1 96) && 97 color=t 98 99whiletest"$#"-ne0 100do 101case"$1"in 102-d|--d|--de|--deb|--debu|--debug) 103 debug=t;shift;; 104-i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate) 105 immediate=t;shift;; 106-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests) 107 GIT_TEST_LONG=t;export GIT_TEST_LONG;shift;; 108-h|--h|--he|--hel|--help) 109help=t;shift;; 110-v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose) 111 verbose=t;shift;; 112-q|--q|--qu|--qui|--quie|--quiet) 113 quiet=t;shift;; 114--no-color) 115 color=;shift;; 116--no-python) 117# noop now... 118shift;; 119--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind) 120 valgrind=t; verbose=t;shift;; 121--tee) 122shift;;# was handled already 123--root=*) 124 root=$(expr "z$1" : 'z[^=]*=\(.*\)') 125 shift ;; 126 *) 127 echo "error: unknown test option '$1'" >&2; exit 1 ;; 128 esac 129done 130 131if test -n "$color"; then 132 say_color () { 133 ( 134 TERM=$ORIGINAL_TERM 135 export TERM 136 case "$1" in 137 error) tput bold; tput setaf 1;; # bold red 138 skip) tput bold; tput setaf 2;; # bold green 139 pass) tput setaf 2;; # green 140 info) tput setaf 3;; # brown 141 *) test -n "$quiet" && return;; 142 esac 143 shift 144 printf "* %s" "$*" 145 tput sgr0 146 echo 147 ) 148 } 149else 150 say_color() { 151 test -z "$1" && test -n "$quiet" && return 152 shift 153 echo "* $*" 154 } 155fi 156 157error () { 158 say_color error "error: $*" 159 GIT_EXIT_OK=t 160 exit 1 161} 162 163say () { 164 say_color info "$*" 165} 166 167test "${test_description}" != "" || 168error "Test script did not set test_description." 169 170if test "$help" = "t" 171then 172 echo "$test_description" 173 exit 0 174fi 175 176exec 5>&1 177if test "$verbose" = "t" 178then 179 exec 4>&2 3>&1 180else 181 exec 4>/dev/null 3>/dev/null 182fi 183 184test_failure=0 185test_count=0 186test_fixed=0 187test_broken=0 188test_success=0 189 190die () { 191 code=$? 192 if test -n "$GIT_EXIT_OK" 193 then 194 exit$code 195 else 196 echo >&5 "FATAL: Unexpected exit with code$code" 197 exit 1 198 fi 199} 200 201GIT_EXIT_OK= 202trap 'die' EXIT 203 204# The semantics of the editor variables are that of invoking 205# sh -c "$EDITOR\"$@\"" files ... 206# 207# If our trash directory contains shell metacharacters, they will be 208# interpreted if we just set$EDITORdirectly, so do a little dance with 209# environment variables to work around this. 210# 211# In particular, quoting isn't enough, as the path may contain the same quote 212# that we're using. 213test_set_editor () { 214 FAKE_EDITOR="$1" 215export FAKE_EDITOR 216 EDITOR='"$FAKE_EDITOR"' 217export EDITOR 218} 219 220test_tick () { 221iftest -z"${test_tick+set}" 222then 223 test_tick=1112911993 224else 225 test_tick=$(($test_tick + 60)) 226fi 227 GIT_COMMITTER_DATE="$test_tick-0700" 228 GIT_AUTHOR_DATE="$test_tick-0700" 229export GIT_COMMITTER_DATE GIT_AUTHOR_DATE 230} 231 232# Call test_commit with the arguments "<message> [<file> [<contents>]]" 233# 234# This will commit a file with the given contents and the given commit 235# message. It will also add a tag with <message> as name. 236# 237# Both <file> and <contents> default to <message>. 238 239test_commit () { 240file=${2:-"$1.t"} 241echo"${3-$1}">"$file"&& 242 git add "$file"&& 243 test_tick && 244 git commit -m"$1"&& 245 git tag "$1" 246} 247 248# Call test_merge with the arguments "<message> <commit>", where <commit> 249# can be a tag pointing to the commit-to-merge. 250 251test_merge () { 252 test_tick && 253 git merge -m"$1""$2"&& 254 git tag "$1" 255} 256 257# This function helps systems where core.filemode=false is set. 258# Use it instead of plain 'chmod +x' to set or unset the executable bit 259# of a file in the working directory and add it to the index. 260 261test_chmod () { 262chmod"$@"&& 263 git update-index --add"--chmod=$@" 264} 265 266# Use test_set_prereq to tell that a particular prerequisite is available. 267# The prerequisite can later be checked for in two ways: 268# 269# - Explicitly using test_have_prereq. 270# 271# - Implicitly by specifying the prerequisite tag in the calls to 272# test_expect_{success,failure,code}. 273# 274# The single parameter is the prerequisite tag (a simple word, in all 275# capital letters by convention). 276 277test_set_prereq () { 278 satisfied="$satisfied$1" 279} 280satisfied=" " 281 282test_have_prereq () { 283case$satisfiedin 284*"$1"*) 285:yes, have it ;; 286*) 287! : nope ;; 288esac 289} 290 291# You are not expected to call test_ok_ and test_failure_ directly, use 292# the text_expect_* functions instead. 293 294test_ok_ () { 295 test_success=$(($test_success + 1)) 296 say_color """ ok$test_count: $@" 297} 298 299test_failure_ () { 300 test_failure=$(($test_failure + 1)) 301 say_color error "FAIL$test_count:$1" 302shift 303echo"$@"|sed-e's/^/ /' 304test"$immediate"=""|| { GIT_EXIT_OK=t;exit1; } 305} 306 307test_known_broken_ok_ () { 308 test_fixed=$(($test_fixed+1)) 309 say_color """ FIXED$test_count: $@" 310} 311 312test_known_broken_failure_ () { 313 test_broken=$(($test_broken+1)) 314 say_color skip " still broken$test_count: $@" 315} 316 317test_debug () { 318test"$debug"=""||eval"$1" 319} 320 321test_run_ () { 322eval>&3 2>&4"$1" 323 eval_ret="$?" 324return0 325} 326 327test_skip () { 328 test_count=$(($test_count+1)) 329 to_skip= 330for skp in$GIT_SKIP_TESTS 331do 332case$this_test.$test_countin 333$skp) 334 to_skip=t 335esac 336done 337iftest -z"$to_skip"&&test -n"$prereq"&& 338! test_have_prereq "$prereq" 339then 340 to_skip=t 341fi 342case"$to_skip"in 343 t) 344 say_color skip >&3"skipping test: $@" 345 say_color skip "skip$test_count:$1" 346: true 347;; 348*) 349 false 350;; 351esac 352} 353 354test_expect_failure () { 355test"$#"=3&& { prereq=$1;shift; } || prereq= 356test"$#"=2|| 357 error "bug in the test script: not 2 or 3 parameters to test-expect-failure" 358if! test_skip "$@" 359then 360 say >&3"checking known breakage:$2" 361 test_run_ "$2" 362if["$?"=0-a"$eval_ret"=0] 363then 364 test_known_broken_ok_ "$1" 365else 366 test_known_broken_failure_ "$1" 367fi 368fi 369echo>&3"" 370} 371 372test_expect_success () { 373test"$#"=3&& { prereq=$1;shift; } || prereq= 374test"$#"=2|| 375 error "bug in the test script: not 2 or 3 parameters to test-expect-success" 376if! test_skip "$@" 377then 378 say >&3"expecting success:$2" 379 test_run_ "$2" 380if["$?"=0-a"$eval_ret"=0] 381then 382 test_ok_ "$1" 383else 384 test_failure_ "$@" 385fi 386fi 387echo>&3"" 388} 389 390test_expect_code () { 391test"$#"=4&& { prereq=$1;shift; } || prereq= 392test"$#"=3|| 393 error "bug in the test script: not 3 or 4 parameters to test-expect-code" 394if! test_skip "$@" 395then 396 say >&3"expecting exit code$1:$3" 397 test_run_ "$3" 398if["$?"=0-a"$eval_ret"="$1"] 399then 400 test_ok_ "$2" 401else 402 test_failure_ "$@" 403fi 404fi 405echo>&3"" 406} 407 408# test_external runs external test scripts that provide continuous 409# test output about their progress, and succeeds/fails on 410# zero/non-zero exit code. It outputs the test output on stdout even 411# in non-verbose mode, and announces the external script with "* run 412# <n>: ..." before running it. When providing relative paths, keep in 413# mind that all scripts run in "trash directory". 414# Usage: test_external description command arguments... 415# Example: test_external 'Perl API' perl ../path/to/test.pl 416test_external () { 417test"$#"=4&& { prereq=$1;shift; } || prereq= 418test"$#"=3|| 419 error >&5"bug in the test script: not 3 or 4 parameters to test_external" 420 descr="$1" 421shift 422if! test_skip "$descr""$@" 423then 424# Announce the script to reduce confusion about the 425# test output that follows. 426 say_color """ run$test_count:$descr($*)" 427# Run command; redirect its stderr to &4 as in 428# test_run_, but keep its stdout on our stdout even in 429# non-verbose mode. 430"$@"2>&4 431if["$?"=0] 432then 433 test_ok_ "$descr" 434else 435 test_failure_ "$descr""$@" 436fi 437fi 438} 439 440# Like test_external, but in addition tests that the command generated 441# no output on stderr. 442test_external_without_stderr () { 443# The temporary file has no (and must have no) security 444# implications. 445 tmp="$TMPDIR";if[-z"$tmp"];then tmp=/tmp;fi 446 stderr="$tmp/git-external-stderr.$$.tmp" 447 test_external "$@"4>"$stderr" 448[-f"$stderr"] || error "Internal error:$stderrdisappeared." 449 descr="no stderr:$1" 450shift 451 say >&3"expecting no stderr from previous command" 452if[ !-s"$stderr"];then 453rm"$stderr" 454 test_ok_ "$descr" 455else 456if["$verbose"= t ];then 457 output=`echo; echo Stderr is:; cat "$stderr"` 458else 459 output= 460fi 461# rm first in case test_failure exits. 462rm"$stderr" 463 test_failure_ "$descr""$@""$output" 464fi 465} 466 467# This is not among top-level (test_expect_success | test_expect_failure) 468# but is a prefix that can be used in the test script, like: 469# 470# test_expect_success 'complain and die' ' 471# do something && 472# do something else && 473# test_must_fail git checkout ../outerspace 474# ' 475# 476# Writing this as "! git checkout ../outerspace" is wrong, because 477# the failure could be due to a segv. We want a controlled failure. 478 479test_must_fail () { 480"$@" 481test $? -gt0-a $? -le129-o $? -gt192 482} 483 484# test_cmp is a helper function to compare actual and expected output. 485# You can use it like: 486# 487# test_expect_success 'foo works' ' 488# echo expected >expected && 489# foo >actual && 490# test_cmp expected actual 491# ' 492# 493# This could be written as either "cmp" or "diff -u", but: 494# - cmp's output is not nearly as easy to read as diff -u 495# - not all diff versions understand "-u" 496 497test_cmp() { 498$GIT_TEST_CMP"$@" 499} 500 501# Most tests can use the created repository, but some may need to create more. 502# Usage: test_create_repo <directory> 503test_create_repo () { 504test"$#"=1|| 505 error "bug in the test script: not 1 parameter to test-create-repo" 506 owd=`pwd` 507 repo="$1" 508mkdir-p"$repo" 509cd"$repo"|| error "Cannot setup test environment" 510"$GIT_EXEC_PATH/git-init""--template=$TEST_DIRECTORY/../templates/blt/">&3 2>&4|| 511 error "cannot run git init -- have you built things yet?" 512mv .git/hooks .git/hooks-disabled 513cd"$owd" 514} 515 516test_done () { 517 GIT_EXIT_OK=t 518 test_results_dir="$TEST_DIRECTORY/test-results" 519mkdir-p"$test_results_dir" 520 test_results_path="$test_results_dir/${0%.sh}-$$" 521 522echo"total$test_count">>$test_results_path 523echo"success$test_success">>$test_results_path 524echo"fixed$test_fixed">>$test_results_path 525echo"broken$test_broken">>$test_results_path 526echo"failed$test_failure">>$test_results_path 527echo"">>$test_results_path 528 529iftest"$test_fixed"!=0 530then 531 say_color pass "fixed$test_fixedknown breakage(s)" 532fi 533iftest"$test_broken"!=0 534then 535 say_color error "still have$test_brokenknown breakage(s)" 536 msg="remaining$(($test_count-$test_broken)) test(s)" 537else 538 msg="$test_counttest(s)" 539fi 540case"$test_failure"in 5410) 542 say_color pass "passed all$msg" 543 544test -d"$remove_trash"&& 545cd"$(dirname "$remove_trash")"&& 546rm-rf"$(basename "$remove_trash")" 547 548exit0;; 549 550*) 551 say_color error "failed$test_failureamong$msg" 552exit1;; 553 554esac 555} 556 557# Test the binaries we have just built. The tests are kept in 558# t/ subdirectory and are run in 'trash directory' subdirectory. 559TEST_DIRECTORY=$(pwd) 560iftest -z"$valgrind" 561then 562iftest -z"$GIT_TEST_INSTALLED" 563then 564 PATH=$TEST_DIRECTORY/..:$PATH 565 GIT_EXEC_PATH=$TEST_DIRECTORY/.. 566else 567 GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path)|| 568 error "Cannot run git from$GIT_TEST_INSTALLED." 569 PATH=$GIT_TEST_INSTALLED:$TEST_DIRECTORY/..:$PATH 570 GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH} 571fi 572else 573 make_symlink () { 574test -h"$2"&& 575test"$1"="$(readlink "$2")"|| { 576# be super paranoid 577ifmkdir"$2".lock 578then 579rm-f"$2"&& 580ln-s"$1""$2"&& 581rm-r"$2".lock 582else 583whiletest -d"$2".lock 584do 585 say "Waiting for lock on$2." 586sleep1 587done 588fi 589} 590} 591 592 make_valgrind_symlink () { 593# handle only executables 594test -x"$1"||return 595 596 base=$(basename "$1") 597 symlink_target=$TEST_DIRECTORY/../$base 598# do not override scripts 599iftest -x"$symlink_target"&& 600test!-d"$symlink_target"&& 601test"#!"!="$(head -c 2 < "$symlink_target")" 602then 603 symlink_target=../valgrind.sh 604fi 605case"$base"in 606*.sh|*.perl) 607 symlink_target=../unprocessed-script 608esac 609# create the link, or replace it if it is out of date 610 make_symlink "$symlink_target""$GIT_VALGRIND/bin/$base"||exit 611} 612 613# override all git executables in TEST_DIRECTORY/.. 614 GIT_VALGRIND=$TEST_DIRECTORY/valgrind 615mkdir-p"$GIT_VALGRIND"/bin 616forfilein$TEST_DIRECTORY/../git*$TEST_DIRECTORY/../test-* 617do 618 make_valgrind_symlink $file 619done 620 OLDIFS=$IFS 621 IFS=: 622for path in$PATH 623do 624ls"$path"/git-*2> /dev/null | 625whilereadfile 626do 627 make_valgrind_symlink "$file" 628done 629done 630 IFS=$OLDIFS 631 PATH=$GIT_VALGRIND/bin:$PATH 632 GIT_EXEC_PATH=$GIT_VALGRIND/bin 633export GIT_VALGRIND 634fi 635GIT_TEMPLATE_DIR=$(pwd)/../templates/blt 636unset GIT_CONFIG 637GIT_CONFIG_NOSYSTEM=1 638GIT_CONFIG_NOGLOBAL=1 639export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_CONFIG_NOGLOBAL 640 641GITPERLLIB=$(pwd)/../perl/blib/lib:$(pwd)/../perl/blib/arch/auto/Git 642export GITPERLLIB 643test -d ../templates/blt || { 644 error "You haven't built things yet, have you?" 645} 646 647if!test -x ../test-chmtime;then 648echo>&2'You need to build test-chmtime:' 649echo>&2'Run "make test-chmtime" in the source (toplevel) directory' 650exit1 651fi 652 653. ../GIT-BUILD-OPTIONS 654 655# Test repository 656test="trash directory.$(basename "$0" .sh)" 657test -n"$root"&&test="$root/$test" 658case"$test"in 659/*) TRASH_DIRECTORY="$test";; 660*) TRASH_DIRECTORY="$TEST_DIRECTORY/$test";; 661esac 662test!-z"$debug"|| remove_trash=$TRASH_DIRECTORY 663rm-fr"$test"|| { 664 GIT_EXIT_OK=t 665echo>&5"FATAL: Cannot prepare test area" 666exit1 667} 668 669test_create_repo "$test" 670# Use -P to resolve symlinks in our working directory so that the cwd 671# in subprocesses like git equals our $PWD (for pathname comparisons). 672cd -P"$test"||exit1 673 674this_test=${0##*/} 675this_test=${this_test%%-*} 676for skp in$GIT_SKIP_TESTS 677do 678 to_skip= 679for skp in$GIT_SKIP_TESTS 680do 681case"$this_test"in 682$skp) 683 to_skip=t 684esac 685done 686case"$to_skip"in 687 t) 688 say_color skip >&3"skipping test$this_testaltogether" 689 say_color skip "skip all tests in$this_test" 690 test_done 691esac 692done 693 694# Provide an implementation of the 'yes' utility 695yes() { 696iftest$#=0 697then 698 y=y 699else 700 y="$*" 701fi 702 703whileecho"$y" 704do 705: 706done 707} 708 709# Fix some commands on Windows 710case$(uname -s)in 711*MINGW*) 712# Windows has its own (incompatible) sort and find 713sort() { 714/usr/bin/sort"$@" 715} 716find() { 717/usr/bin/find"$@" 718} 719sum() { 720md5sum"$@" 721} 722# git sees Windows-style pwd 723pwd() { 724builtin pwd -W 725} 726# no POSIX permissions 727# backslashes in pathspec are converted to '/' 728# exec does not inherit the PID 729;; 730*) 731 test_set_prereq POSIXPERM 732 test_set_prereq BSLASHPSPEC 733 test_set_prereq EXECKEEPSPID 734;; 735esac 736 737test -z"$NO_PERL"&& test_set_prereq PERL 738 739# test whether the filesystem supports symbolic links 740ln-s x y 2>/dev/null &&test -h y 2>/dev/null && test_set_prereq SYMLINKS 741rm-f y