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# The semantics of the editor variables are that of invoking 19# sh -c "$EDITOR \"$@\"" files ... 20# 21# If our trash directory contains shell metacharacters, they will be 22# interpreted if we just set $EDITOR directly, so do a little dance with 23# environment variables to work around this. 24# 25# In particular, quoting isn't enough, as the path may contain the same quote 26# that we're using. 27test_set_editor () { 28 FAKE_EDITOR="$1" 29export FAKE_EDITOR 30 EDITOR='"$FAKE_EDITOR"' 31export EDITOR 32} 33 34test_decode_color () { 35awk' 36 function name(n) { 37 if (n == 0) return "RESET"; 38 if (n == 1) return "BOLD"; 39 if (n == 30) return "BLACK"; 40 if (n == 31) return "RED"; 41 if (n == 32) return "GREEN"; 42 if (n == 33) return "YELLOW"; 43 if (n == 34) return "BLUE"; 44 if (n == 35) return "MAGENTA"; 45 if (n == 36) return "CYAN"; 46 if (n == 37) return "WHITE"; 47 if (n == 40) return "BLACK"; 48 if (n == 41) return "BRED"; 49 if (n == 42) return "BGREEN"; 50 if (n == 43) return "BYELLOW"; 51 if (n == 44) return "BBLUE"; 52 if (n == 45) return "BMAGENTA"; 53 if (n == 46) return "BCYAN"; 54 if (n == 47) return "BWHITE"; 55 } 56 { 57 while (match($0, /\033\[[0-9;]*m/) != 0) { 58 printf "%s<", substr($0, 1, RSTART-1); 59 codes = substr($0, RSTART+2, RLENGTH-3); 60 if (length(codes) == 0) 61 printf "%s", name(0) 62 else { 63 n = split(codes, ary, ";"); 64 sep = ""; 65 for (i = 1; i <= n; i++) { 66 printf "%s%s", sep, name(ary[i]); 67 sep = ";" 68 } 69 } 70 printf ">"; 71$0= substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1); 72 } 73 print 74 } 75 ' 76} 77 78nul_to_q () { 79"$PERL_PATH"-pe'y/\000/Q/' 80} 81 82q_to_nul () { 83"$PERL_PATH"-pe'y/Q/\000/' 84} 85 86q_to_cr () { 87tr Q '\015' 88} 89 90q_to_tab () { 91tr Q '\011' 92} 93 94qz_to_tab_space () { 95tr QZ '\011\040' 96} 97 98append_cr () { 99sed-e's/$/Q/'|tr Q '\015' 100} 101 102remove_cr () { 103tr'\015' Q |sed-e's/Q$//' 104} 105 106# In some bourne shell implementations, the "unset" builtin returns 107# nonzero status when a variable to be unset was not set in the first 108# place. 109# 110# Use sane_unset when that should not be considered an error. 111 112sane_unset () { 113unset"$@" 114return0 115} 116 117test_tick () { 118iftest -z"${test_tick+set}" 119then 120 test_tick=1112911993 121else 122 test_tick=$(($test_tick + 60)) 123fi 124 GIT_COMMITTER_DATE="$test_tick-0700" 125 GIT_AUTHOR_DATE="$test_tick-0700" 126export GIT_COMMITTER_DATE GIT_AUTHOR_DATE 127} 128 129# Stop execution and start a shell. This is useful for debugging tests and 130# only makes sense together with "-v". 131# 132# Be sure to remove all invocations of this command before submitting. 133 134test_pause () { 135iftest"$verbose"= t;then 136"$SHELL_PATH"<&6>&3 2>&4 137else 138 error >&5"test_pause requires --verbose" 139fi 140} 141 142# Call test_commit with the arguments "<message> [<file> [<contents>]]" 143# 144# This will commit a file with the given contents and the given commit 145# message. It will also add a tag with <message> as name. 146# 147# Both <file> and <contents> default to <message>. 148 149test_commit () { 150 notick= && 151 signoff= && 152whiletest$#!=0 153do 154case"$1"in 155--notick) 156 notick=yes 157;; 158--signoff) 159 signoff="$1" 160;; 161*) 162break 163;; 164esac 165shift 166done&& 167file=${2:-"$1.t"}&& 168echo"${3-$1}">"$file"&& 169 git add "$file"&& 170iftest -z"$notick" 171then 172 test_tick 173fi&& 174 git commit $signoff-m"$1"&& 175 git tag "$1" 176} 177 178# Call test_merge with the arguments "<message> <commit>", where <commit> 179# can be a tag pointing to the commit-to-merge. 180 181test_merge () { 182 test_tick && 183 git merge -m"$1""$2"&& 184 git tag "$1" 185} 186 187# This function helps systems where core.filemode=false is set. 188# Use it instead of plain 'chmod +x' to set or unset the executable bit 189# of a file in the working directory and add it to the index. 190 191test_chmod () { 192chmod"$@"&& 193 git update-index --add"--chmod=$@" 194} 195 196# Unset a configuration variable, but don't fail if it doesn't exist. 197test_unconfig () { 198 git config --unset-all"$@" 199 config_status=$? 200case"$config_status"in 2015)# ok, nothing to unset 202 config_status=0 203;; 204esac 205return$config_status 206} 207 208# Set git config, automatically unsetting it after the test is over. 209test_config () { 210 test_when_finished "test_unconfig '$1'"&& 211 git config "$@" 212} 213 214test_config_global () { 215 test_when_finished "test_unconfig --global '$1'"&& 216 git config --global"$@" 217} 218 219write_script () { 220{ 221echo"#!${2-"$SHELL_PATH"}"&& 222cat 223} >"$1"&& 224chmod+x "$1" 225} 226 227# Use test_set_prereq to tell that a particular prerequisite is available. 228# The prerequisite can later be checked for in two ways: 229# 230# - Explicitly using test_have_prereq. 231# 232# - Implicitly by specifying the prerequisite tag in the calls to 233# test_expect_{success,failure,code}. 234# 235# The single parameter is the prerequisite tag (a simple word, in all 236# capital letters by convention). 237 238test_set_prereq () { 239 satisfied_prereq="$satisfied_prereq$1" 240} 241satisfied_prereq=" " 242lazily_testable_prereq= lazily_tested_prereq= 243 244# Usage: test_lazy_prereq PREREQ 'script' 245test_lazy_prereq () { 246 lazily_testable_prereq="$lazily_testable_prereq$1" 247eval test_prereq_lazily_$1=\$2 248} 249 250test_run_lazy_prereq_ () { 251script=' 252mkdir -p "$TRASH_DIRECTORY/prereq-test-dir" && 253( 254 cd "$TRASH_DIRECTORY/prereq-test-dir" &&'"$2"' 255)' 256 say >&3"checking prerequisite:$1" 257 say >&3"$script" 258 test_eval_ "$script" 259 eval_ret=$? 260rm-rf"$TRASH_DIRECTORY/prereq-test-dir" 261iftest"$eval_ret"=0;then 262 say >&3"prerequisite$1ok" 263else 264 say >&3"prerequisite$1not satisfied" 265fi 266return$eval_ret 267} 268 269test_have_prereq () { 270# prerequisites can be concatenated with ',' 271 save_IFS=$IFS 272 IFS=, 273set -- $* 274 IFS=$save_IFS 275 276 total_prereq=0 277 ok_prereq=0 278 missing_prereq= 279 280for prerequisite 281do 282case"$lazily_tested_prereq"in 283*"$prerequisite"*) 284;; 285*) 286case"$lazily_testable_prereq"in 287*"$prerequisite"*) 288eval"script=\$test_prereq_lazily_$prerequisite"&& 289if test_run_lazy_prereq_ "$prerequisite""$script" 290then 291 test_set_prereq $prerequisite 292fi 293 lazily_tested_prereq="$lazily_tested_prereq$prerequisite" 294esac 295;; 296esac 297 298 total_prereq=$(($total_prereq + 1)) 299case"$satisfied_prereq"in 300*"$prerequisite"*) 301 ok_prereq=$(($ok_prereq + 1)) 302;; 303*) 304# Keep a list of missing prerequisites 305iftest -z"$missing_prereq" 306then 307 missing_prereq=$prerequisite 308else 309 missing_prereq="$prerequisite,$missing_prereq" 310fi 311esac 312done 313 314test$total_prereq=$ok_prereq 315} 316 317test_declared_prereq () { 318case",$test_prereq,"in 319*,$1,*) 320return0 321;; 322esac 323return1 324} 325 326test_expect_failure () { 327test"$#"=3&& { test_prereq=$1;shift; } || test_prereq= 328test"$#"=2|| 329 error "bug in the test script: not 2 or 3 parameters to test-expect-failure" 330export test_prereq 331if! test_skip "$@" 332then 333 say >&3"checking known breakage:$2" 334if test_run_ "$2" expecting_failure 335then 336 test_known_broken_ok_ "$1" 337else 338 test_known_broken_failure_ "$1" 339fi 340fi 341echo>&3"" 342} 343 344test_expect_success () { 345test"$#"=3&& { test_prereq=$1;shift; } || test_prereq= 346test"$#"=2|| 347 error "bug in the test script: not 2 or 3 parameters to test-expect-success" 348export test_prereq 349if! test_skip "$@" 350then 351 say >&3"expecting success:$2" 352if test_run_ "$2" 353then 354 test_ok_ "$1" 355else 356 test_failure_ "$@" 357fi 358fi 359echo>&3"" 360} 361 362# test_external runs external test scripts that provide continuous 363# test output about their progress, and succeeds/fails on 364# zero/non-zero exit code. It outputs the test output on stdout even 365# in non-verbose mode, and announces the external script with "# run 366# <n>: ..." before running it. When providing relative paths, keep in 367# mind that all scripts run in "trash directory". 368# Usage: test_external description command arguments... 369# Example: test_external 'Perl API' perl ../path/to/test.pl 370test_external () { 371test"$#"=4&& { test_prereq=$1;shift; } || test_prereq= 372test"$#"=3|| 373 error >&5"bug in the test script: not 3 or 4 parameters to test_external" 374 descr="$1" 375shift 376export test_prereq 377if! test_skip "$descr""$@" 378then 379# Announce the script to reduce confusion about the 380# test output that follows. 381 say_color """# run$test_count:$descr($*)" 382# Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG 383# to be able to use them in script 384export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG 385# Run command; redirect its stderr to &4 as in 386# test_run_, but keep its stdout on our stdout even in 387# non-verbose mode. 388"$@"2>&4 389if["$?"=0] 390then 391iftest$test_external_has_tap-eq0;then 392 test_ok_ "$descr" 393else 394 say_color """# test_external test$descrwas ok" 395 test_success=$(($test_success + 1)) 396fi 397else 398iftest$test_external_has_tap-eq0;then 399 test_failure_ "$descr""$@" 400else 401 say_color error "# test_external test$descrfailed: $@" 402 test_failure=$(($test_failure + 1)) 403fi 404fi 405fi 406} 407 408# Like test_external, but in addition tests that the command generated 409# no output on stderr. 410test_external_without_stderr () { 411# The temporary file has no (and must have no) security 412# implications. 413 tmp=${TMPDIR:-/tmp} 414 stderr="$tmp/git-external-stderr.$$.tmp" 415 test_external "$@"4>"$stderr" 416[-f"$stderr"] || error "Internal error:$stderrdisappeared." 417 descr="no stderr:$1" 418shift 419 say >&3"# expecting no stderr from previous command" 420if[ !-s"$stderr"];then 421rm"$stderr" 422 423iftest$test_external_has_tap-eq0;then 424 test_ok_ "$descr" 425else 426 say_color """# test_external_without_stderr test$descrwas ok" 427 test_success=$(($test_success + 1)) 428fi 429else 430if["$verbose"= t ];then 431 output=`echo; echo "# Stderr is:"; cat "$stderr"` 432else 433 output= 434fi 435# rm first in case test_failure exits. 436rm"$stderr" 437iftest$test_external_has_tap-eq0;then 438 test_failure_ "$descr""$@""$output" 439else 440 say_color error "# test_external_without_stderr test$descrfailed: $@:$output" 441 test_failure=$(($test_failure + 1)) 442fi 443fi 444} 445 446# debugging-friendly alternatives to "test [-f|-d|-e]" 447# The commands test the existence or non-existence of $1. $2 can be 448# given to provide a more precise diagnosis. 449test_path_is_file () { 450if! [-f"$1"] 451then 452echo"File$1doesn't exist. $*" 453 false 454fi 455} 456 457test_path_is_dir () { 458if! [-d"$1"] 459then 460echo"Directory$1doesn't exist. $*" 461 false 462fi 463} 464 465test_path_is_missing () { 466if[-e"$1"] 467then 468echo"Path exists:" 469ls-ld"$1" 470if[$#-ge1];then 471echo"$*" 472fi 473 false 474fi 475} 476 477# test_line_count checks that a file has the number of lines it 478# ought to. For example: 479# 480# test_expect_success 'produce exactly one line of output' ' 481# do something >output && 482# test_line_count = 1 output 483# ' 484# 485# is like "test $(wc -l <output) = 1" except that it passes the 486# output through when the number of lines is wrong. 487 488test_line_count () { 489iftest$#!=3 490then 491 error "bug in the test script: not 3 parameters to test_line_count" 492elif!test$(wc -l <"$3")"$1""$2" 493then 494echo"test_line_count: line count for$3!$1$2" 495cat"$3" 496return1 497fi 498} 499 500# This is not among top-level (test_expect_success | test_expect_failure) 501# but is a prefix that can be used in the test script, like: 502# 503# test_expect_success 'complain and die' ' 504# do something && 505# do something else && 506# test_must_fail git checkout ../outerspace 507# ' 508# 509# Writing this as "! git checkout ../outerspace" is wrong, because 510# the failure could be due to a segv. We want a controlled failure. 511 512test_must_fail () { 513"$@" 514 exit_code=$? 515iftest$exit_code=0;then 516echo>&2"test_must_fail: command succeeded: $*" 517return1 518eliftest$exit_code-gt129-a$exit_code-le192;then 519echo>&2"test_must_fail: died by signal: $*" 520return1 521eliftest$exit_code=127;then 522echo>&2"test_must_fail: command not found: $*" 523return1 524fi 525return0 526} 527 528# Similar to test_must_fail, but tolerates success, too. This is 529# meant to be used in contexts like: 530# 531# test_expect_success 'some command works without configuration' ' 532# test_might_fail git config --unset all.configuration && 533# do something 534# ' 535# 536# Writing "git config --unset all.configuration || :" would be wrong, 537# because we want to notice if it fails due to segv. 538 539test_might_fail () { 540"$@" 541 exit_code=$? 542iftest$exit_code-gt129-a$exit_code-le192;then 543echo>&2"test_might_fail: died by signal: $*" 544return1 545eliftest$exit_code=127;then 546echo>&2"test_might_fail: command not found: $*" 547return1 548fi 549return0 550} 551 552# Similar to test_must_fail and test_might_fail, but check that a 553# given command exited with a given exit code. Meant to be used as: 554# 555# test_expect_success 'Merge with d/f conflicts' ' 556# test_expect_code 1 git merge "merge msg" B master 557# ' 558 559test_expect_code () { 560 want_code=$1 561shift 562"$@" 563 exit_code=$? 564iftest$exit_code=$want_code 565then 566return0 567fi 568 569echo>&2"test_expect_code: command exited with$exit_code, we wanted$want_code$*" 570return1 571} 572 573# test_cmp is a helper function to compare actual and expected output. 574# You can use it like: 575# 576# test_expect_success 'foo works' ' 577# echo expected >expected && 578# foo >actual && 579# test_cmp expected actual 580# ' 581# 582# This could be written as either "cmp" or "diff -u", but: 583# - cmp's output is not nearly as easy to read as diff -u 584# - not all diff versions understand "-u" 585 586test_cmp() { 587$GIT_TEST_CMP"$@" 588} 589 590# Print a sequence of numbers or letters in increasing order. This is 591# similar to GNU seq(1), but the latter might not be available 592# everywhere (and does not do letters). It may be used like: 593# 594# for i in `test_seq 100`; do 595# for j in `test_seq 10 20`; do 596# for k in `test_seq a z`; do 597# echo $i-$j-$k 598# done 599# done 600# done 601 602test_seq () { 603case$#in 6041)set1"$@";; 6052) ;; 606*) error "bug in the test script: not 1 or 2 parameters to test_seq";; 607esac 608"$PERL_PATH"-le'print for$ARGV[0]..$ARGV[1]'--"$@" 609} 610 611# This function can be used to schedule some commands to be run 612# unconditionally at the end of the test to restore sanity: 613# 614# test_expect_success 'test core.capslock' ' 615# git config core.capslock true && 616# test_when_finished "git config --unset core.capslock" && 617# hello world 618# ' 619# 620# That would be roughly equivalent to 621# 622# test_expect_success 'test core.capslock' ' 623# git config core.capslock true && 624# hello world 625# git config --unset core.capslock 626# ' 627# 628# except that the greeting and config --unset must both succeed for 629# the test to pass. 630# 631# Note that under --immediate mode, no clean-up is done to help diagnose 632# what went wrong. 633 634test_when_finished () { 635 test_cleanup="{ $* 636 } && (exit\"\$eval_ret\"); eval_ret=\$?;$test_cleanup" 637} 638 639# Most tests can use the created repository, but some may need to create more. 640# Usage: test_create_repo <directory> 641test_create_repo () { 642test"$#"=1|| 643 error "bug in the test script: not 1 parameter to test-create-repo" 644 repo="$1" 645mkdir-p"$repo" 646( 647cd"$repo"|| error "Cannot setup test environment" 648"$GIT_EXEC_PATH/git-init""--template=$GIT_BUILD_DIR/templates/blt/">&3 2>&4|| 649 error "cannot run git init -- have you built things yet?" 650mv .git/hooks .git/hooks-disabled 651) ||exit 652}