PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/t/test-lib.sh

https://bitbucket.org/ssaasen/git
Shell | 1100 lines | 874 code | 97 blank | 129 comment | 104 complexity | b5ff1987b0bf6259f51cabd23ab835f3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause
  1. # Test framework for git. See t/README for usage.
  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. # Test the binaries we have just built. The tests are kept in
  18. # t/ subdirectory and are run in 'trash directory' subdirectory.
  19. if test -z "$TEST_DIRECTORY"
  20. then
  21. # We allow tests to override this, in case they want to run tests
  22. # outside of t/, e.g. for running tests on the test library
  23. # itself.
  24. TEST_DIRECTORY=$(pwd)
  25. else
  26. # ensure that TEST_DIRECTORY is an absolute path so that it
  27. # is valid even if the current working directory is changed
  28. TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
  29. fi
  30. if test -z "$TEST_OUTPUT_DIRECTORY"
  31. then
  32. # Similarly, override this to store the test-results subdir
  33. # elsewhere
  34. TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
  35. fi
  36. GIT_BUILD_DIR="$TEST_DIRECTORY"/..
  37. ################################################################
  38. # It appears that people try to run tests without building...
  39. "$GIT_BUILD_DIR/git" >/dev/null
  40. if test $? != 1
  41. then
  42. echo >&2 'error: you do not seem to have built git yet.'
  43. exit 1
  44. fi
  45. . "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
  46. export PERL_PATH SHELL_PATH
  47. # if --tee was passed, write the output not only to the terminal, but
  48. # additionally to the file test-results/$BASENAME.out, too.
  49. case "$GIT_TEST_TEE_STARTED, $* " in
  50. done,*)
  51. # do not redirect again
  52. ;;
  53. *' --tee '*|*' --va'*)
  54. mkdir -p "$TEST_OUTPUT_DIRECTORY/test-results"
  55. BASE="$TEST_OUTPUT_DIRECTORY/test-results/$(basename "$0" .sh)"
  56. (GIT_TEST_TEE_STARTED=done ${SHELL_PATH} "$0" "$@" 2>&1;
  57. echo $? > $BASE.exit) | tee $BASE.out
  58. test "$(cat $BASE.exit)" = 0
  59. exit
  60. ;;
  61. esac
  62. # For repeatability, reset the environment to known value.
  63. # TERM is sanitized below, after saving color control sequences.
  64. LANG=C
  65. LC_ALL=C
  66. PAGER=cat
  67. TZ=UTC
  68. export LANG LC_ALL PAGER TZ
  69. EDITOR=:
  70. # A call to "unset" with no arguments causes at least Solaris 10
  71. # /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
  72. # deriving from the command substitution clustered with the other
  73. # ones.
  74. unset VISUAL EMAIL LANGUAGE COLUMNS $("$PERL_PATH" -e '
  75. my @env = keys %ENV;
  76. my $ok = join("|", qw(
  77. TRACE
  78. DEBUG
  79. USE_LOOKUP
  80. TEST
  81. .*_TEST
  82. PROVE
  83. VALGRIND
  84. UNZIP
  85. PERF_
  86. CURL_VERBOSE
  87. ));
  88. my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
  89. print join("\n", @vars);
  90. ')
  91. unset XDG_CONFIG_HOME
  92. unset GITPERLLIB
  93. GIT_AUTHOR_EMAIL=author@example.com
  94. GIT_AUTHOR_NAME='A U Thor'
  95. GIT_COMMITTER_EMAIL=committer@example.com
  96. GIT_COMMITTER_NAME='C O Mitter'
  97. GIT_MERGE_VERBOSITY=5
  98. GIT_MERGE_AUTOEDIT=no
  99. export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
  100. export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
  101. export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
  102. export EDITOR
  103. # Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
  104. GIT_TRACE_BARE=1
  105. export GIT_TRACE_BARE
  106. if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
  107. then
  108. GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
  109. export GIT_INDEX_VERSION
  110. fi
  111. # Add libc MALLOC and MALLOC_PERTURB test
  112. # only if we are not executing the test with valgrind
  113. if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||
  114. test -n "$TEST_NO_MALLOC_CHECK"
  115. then
  116. setup_malloc_check () {
  117. : nothing
  118. }
  119. teardown_malloc_check () {
  120. : nothing
  121. }
  122. else
  123. setup_malloc_check () {
  124. MALLOC_CHECK_=3 MALLOC_PERTURB_=165
  125. export MALLOC_CHECK_ MALLOC_PERTURB_
  126. }
  127. teardown_malloc_check () {
  128. unset MALLOC_CHECK_ MALLOC_PERTURB_
  129. }
  130. fi
  131. : ${ASAN_OPTIONS=detect_leaks=0}
  132. export ASAN_OPTIONS
  133. # Protect ourselves from common misconfiguration to export
  134. # CDPATH into the environment
  135. unset CDPATH
  136. unset GREP_OPTIONS
  137. unset UNZIP
  138. case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
  139. 1|2|true)
  140. GIT_TRACE=4
  141. ;;
  142. esac
  143. # Convenience
  144. #
  145. # A regexp to match 5 and 40 hexdigits
  146. _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
  147. _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
  148. # Zero SHA-1
  149. _z40=0000000000000000000000000000000000000000
  150. # Line feed
  151. LF='
  152. '
  153. # UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
  154. # when case-folding filenames
  155. u200c=$(printf '\342\200\214')
  156. export _x05 _x40 _z40 LF u200c
  157. # Each test should start with something like this, after copyright notices:
  158. #
  159. # test_description='Description of this test...
  160. # This test checks if command xyzzy does the right thing...
  161. # '
  162. # . ./test-lib.sh
  163. test "x$TERM" != "xdumb" && (
  164. test -t 1 &&
  165. tput bold >/dev/null 2>&1 &&
  166. tput setaf 1 >/dev/null 2>&1 &&
  167. tput sgr0 >/dev/null 2>&1
  168. ) &&
  169. color=t
  170. while test "$#" -ne 0
  171. do
  172. case "$1" in
  173. -d|--d|--de|--deb|--debu|--debug)
  174. debug=t; shift ;;
  175. -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  176. immediate=t; shift ;;
  177. -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
  178. GIT_TEST_LONG=t; export GIT_TEST_LONG; shift ;;
  179. -r)
  180. shift; test "$#" -ne 0 || {
  181. echo 'error: -r requires an argument' >&2;
  182. exit 1;
  183. }
  184. run_list=$1; shift ;;
  185. --run=*)
  186. run_list=${1#--*=}; shift ;;
  187. -h|--h|--he|--hel|--help)
  188. help=t; shift ;;
  189. -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  190. verbose=t; shift ;;
  191. --verbose-only=*)
  192. verbose_only=${1#--*=}
  193. shift ;;
  194. -q|--q|--qu|--qui|--quie|--quiet)
  195. # Ignore --quiet under a TAP::Harness. Saying how many tests
  196. # passed without the ok/not ok details is always an error.
  197. test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
  198. --with-dashes)
  199. with_dashes=t; shift ;;
  200. --no-color)
  201. color=; shift ;;
  202. --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
  203. valgrind=memcheck
  204. shift ;;
  205. --valgrind=*)
  206. valgrind=${1#--*=}
  207. shift ;;
  208. --valgrind-only=*)
  209. valgrind_only=${1#--*=}
  210. shift ;;
  211. --tee)
  212. shift ;; # was handled already
  213. --root=*)
  214. root=${1#--*=}
  215. shift ;;
  216. --chain-lint)
  217. GIT_TEST_CHAIN_LINT=1
  218. shift ;;
  219. --no-chain-lint)
  220. GIT_TEST_CHAIN_LINT=0
  221. shift ;;
  222. -x)
  223. trace=t
  224. verbose=t
  225. shift ;;
  226. *)
  227. echo "error: unknown test option '$1'" >&2; exit 1 ;;
  228. esac
  229. done
  230. if test -n "$valgrind_only"
  231. then
  232. test -z "$valgrind" && valgrind=memcheck
  233. test -z "$verbose" && verbose_only="$valgrind_only"
  234. elif test -n "$valgrind"
  235. then
  236. verbose=t
  237. fi
  238. if test -n "$color"
  239. then
  240. # Save the color control sequences now rather than run tput
  241. # each time say_color() is called. This is done for two
  242. # reasons:
  243. # * TERM will be changed to dumb
  244. # * HOME will be changed to a temporary directory and tput
  245. # might need to read ~/.terminfo from the original HOME
  246. # directory to get the control sequences
  247. # Note: This approach assumes the control sequences don't end
  248. # in a newline for any terminal of interest (command
  249. # substitutions strip trailing newlines). Given that most
  250. # (all?) terminals in common use are related to ECMA-48, this
  251. # shouldn't be a problem.
  252. say_color_error=$(tput bold; tput setaf 1) # bold red
  253. say_color_skip=$(tput setaf 4) # blue
  254. say_color_warn=$(tput setaf 3) # brown/yellow
  255. say_color_pass=$(tput setaf 2) # green
  256. say_color_info=$(tput setaf 6) # cyan
  257. say_color_reset=$(tput sgr0)
  258. say_color_="" # no formatting for normal text
  259. say_color () {
  260. test -z "$1" && test -n "$quiet" && return
  261. eval "say_color_color=\$say_color_$1"
  262. shift
  263. printf "%s\\n" "$say_color_color$*$say_color_reset"
  264. }
  265. else
  266. say_color() {
  267. test -z "$1" && test -n "$quiet" && return
  268. shift
  269. printf "%s\n" "$*"
  270. }
  271. fi
  272. TERM=dumb
  273. export TERM
  274. error () {
  275. say_color error "error: $*"
  276. GIT_EXIT_OK=t
  277. exit 1
  278. }
  279. say () {
  280. say_color info "$*"
  281. }
  282. test "${test_description}" != "" ||
  283. error "Test script did not set test_description."
  284. if test "$help" = "t"
  285. then
  286. printf '%s\n' "$test_description"
  287. exit 0
  288. fi
  289. exec 5>&1
  290. exec 6<&0
  291. if test "$verbose" = "t"
  292. then
  293. exec 4>&2 3>&1
  294. else
  295. exec 4>/dev/null 3>/dev/null
  296. fi
  297. test_failure=0
  298. test_count=0
  299. test_fixed=0
  300. test_broken=0
  301. test_success=0
  302. test_external_has_tap=0
  303. die () {
  304. code=$?
  305. if test -n "$GIT_EXIT_OK"
  306. then
  307. exit $code
  308. else
  309. echo >&5 "FATAL: Unexpected exit with code $code"
  310. exit 1
  311. fi
  312. }
  313. GIT_EXIT_OK=
  314. trap 'die' EXIT
  315. trap 'exit $?' INT
  316. # The user-facing functions are loaded from a separate file so that
  317. # test_perf subshells can have them too
  318. . "$TEST_DIRECTORY/test-lib-functions.sh"
  319. # You are not expected to call test_ok_ and test_failure_ directly, use
  320. # the test_expect_* functions instead.
  321. test_ok_ () {
  322. test_success=$(($test_success + 1))
  323. say_color "" "ok $test_count - $@"
  324. }
  325. test_failure_ () {
  326. test_failure=$(($test_failure + 1))
  327. say_color error "not ok $test_count - $1"
  328. shift
  329. printf '%s\n' "$*" | sed -e 's/^/# /'
  330. test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
  331. }
  332. test_known_broken_ok_ () {
  333. test_fixed=$(($test_fixed+1))
  334. say_color error "ok $test_count - $@ # TODO known breakage vanished"
  335. }
  336. test_known_broken_failure_ () {
  337. test_broken=$(($test_broken+1))
  338. say_color warn "not ok $test_count - $@ # TODO known breakage"
  339. }
  340. test_debug () {
  341. test "$debug" = "" || eval "$1"
  342. }
  343. match_pattern_list () {
  344. arg="$1"
  345. shift
  346. test -z "$*" && return 1
  347. for pattern_
  348. do
  349. case "$arg" in
  350. $pattern_)
  351. return 0
  352. esac
  353. done
  354. return 1
  355. }
  356. match_test_selector_list () {
  357. title="$1"
  358. shift
  359. arg="$1"
  360. shift
  361. test -z "$1" && return 0
  362. # Both commas and whitespace are accepted as separators.
  363. OLDIFS=$IFS
  364. IFS=' ,'
  365. set -- $1
  366. IFS=$OLDIFS
  367. # If the first selector is negative we include by default.
  368. include=
  369. case "$1" in
  370. !*) include=t ;;
  371. esac
  372. for selector
  373. do
  374. orig_selector=$selector
  375. positive=t
  376. case "$selector" in
  377. !*)
  378. positive=
  379. selector=${selector##?}
  380. ;;
  381. esac
  382. test -z "$selector" && continue
  383. case "$selector" in
  384. *-*)
  385. if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
  386. then
  387. echo "error: $title: invalid non-numeric in range" \
  388. "start: '$orig_selector'" >&2
  389. exit 1
  390. fi
  391. if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
  392. then
  393. echo "error: $title: invalid non-numeric in range" \
  394. "end: '$orig_selector'" >&2
  395. exit 1
  396. fi
  397. ;;
  398. *)
  399. if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
  400. then
  401. echo "error: $title: invalid non-numeric in test" \
  402. "selector: '$orig_selector'" >&2
  403. exit 1
  404. fi
  405. esac
  406. # Short cut for "obvious" cases
  407. test -z "$include" && test -z "$positive" && continue
  408. test -n "$include" && test -n "$positive" && continue
  409. case "$selector" in
  410. -*)
  411. if test $arg -le ${selector#-}
  412. then
  413. include=$positive
  414. fi
  415. ;;
  416. *-)
  417. if test $arg -ge ${selector%-}
  418. then
  419. include=$positive
  420. fi
  421. ;;
  422. *-*)
  423. if test ${selector%%-*} -le $arg \
  424. && test $arg -le ${selector#*-}
  425. then
  426. include=$positive
  427. fi
  428. ;;
  429. *)
  430. if test $arg -eq $selector
  431. then
  432. include=$positive
  433. fi
  434. ;;
  435. esac
  436. done
  437. test -n "$include"
  438. }
  439. maybe_teardown_verbose () {
  440. test -z "$verbose_only" && return
  441. exec 4>/dev/null 3>/dev/null
  442. verbose=
  443. }
  444. last_verbose=t
  445. maybe_setup_verbose () {
  446. test -z "$verbose_only" && return
  447. if match_pattern_list $test_count $verbose_only
  448. then
  449. exec 4>&2 3>&1
  450. # Emit a delimiting blank line when going from
  451. # non-verbose to verbose. Within verbose mode the
  452. # delimiter is printed by test_expect_*. The choice
  453. # of the initial $last_verbose is such that before
  454. # test 1, we do not print it.
  455. test -z "$last_verbose" && echo >&3 ""
  456. verbose=t
  457. else
  458. exec 4>/dev/null 3>/dev/null
  459. verbose=
  460. fi
  461. last_verbose=$verbose
  462. }
  463. maybe_teardown_valgrind () {
  464. test -z "$GIT_VALGRIND" && return
  465. GIT_VALGRIND_ENABLED=
  466. }
  467. maybe_setup_valgrind () {
  468. test -z "$GIT_VALGRIND" && return
  469. if test -z "$valgrind_only"
  470. then
  471. GIT_VALGRIND_ENABLED=t
  472. return
  473. fi
  474. GIT_VALGRIND_ENABLED=
  475. if match_pattern_list $test_count $valgrind_only
  476. then
  477. GIT_VALGRIND_ENABLED=t
  478. fi
  479. }
  480. want_trace () {
  481. test "$trace" = t && test "$verbose" = t
  482. }
  483. # This is a separate function because some tests use
  484. # "return" to end a test_expect_success block early
  485. # (and we want to make sure we run any cleanup like
  486. # "set +x").
  487. test_eval_inner_ () {
  488. # Do not add anything extra (including LF) after '$*'
  489. eval "
  490. want_trace && set -x
  491. $*"
  492. }
  493. test_eval_ () {
  494. # We run this block with stderr redirected to avoid extra cruft
  495. # during a "-x" trace. Once in "set -x" mode, we cannot prevent
  496. # the shell from printing the "set +x" to turn it off (nor the saving
  497. # of $? before that). But we can make sure that the output goes to
  498. # /dev/null.
  499. #
  500. # The test itself is run with stderr put back to &4 (so either to
  501. # /dev/null, or to the original stderr if --verbose was used).
  502. {
  503. test_eval_inner_ "$@" </dev/null >&3 2>&4
  504. test_eval_ret_=$?
  505. if want_trace
  506. then
  507. set +x
  508. if test "$test_eval_ret_" != 0
  509. then
  510. say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
  511. fi
  512. fi
  513. } 2>/dev/null
  514. return $test_eval_ret_
  515. }
  516. test_run_ () {
  517. test_cleanup=:
  518. expecting_failure=$2
  519. if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
  520. # turn off tracing for this test-eval, as it simply creates
  521. # confusing noise in the "-x" output
  522. trace_tmp=$trace
  523. trace=
  524. # 117 is magic because it is unlikely to match the exit
  525. # code of other programs
  526. test_eval_ "(exit 117) && $1"
  527. if test "$?" != 117; then
  528. error "bug in the test script: broken &&-chain: $1"
  529. fi
  530. trace=$trace_tmp
  531. fi
  532. setup_malloc_check
  533. test_eval_ "$1"
  534. eval_ret=$?
  535. teardown_malloc_check
  536. if test -z "$immediate" || test $eval_ret = 0 ||
  537. test -n "$expecting_failure" && test "$test_cleanup" != ":"
  538. then
  539. setup_malloc_check
  540. test_eval_ "$test_cleanup"
  541. teardown_malloc_check
  542. fi
  543. if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
  544. then
  545. echo ""
  546. fi
  547. return "$eval_ret"
  548. }
  549. test_start_ () {
  550. test_count=$(($test_count+1))
  551. maybe_setup_verbose
  552. maybe_setup_valgrind
  553. }
  554. test_finish_ () {
  555. echo >&3 ""
  556. maybe_teardown_valgrind
  557. maybe_teardown_verbose
  558. }
  559. test_skip () {
  560. to_skip=
  561. skipped_reason=
  562. if match_pattern_list $this_test.$test_count $GIT_SKIP_TESTS
  563. then
  564. to_skip=t
  565. skipped_reason="GIT_SKIP_TESTS"
  566. fi
  567. if test -z "$to_skip" && test -n "$test_prereq" &&
  568. ! test_have_prereq "$test_prereq"
  569. then
  570. to_skip=t
  571. of_prereq=
  572. if test "$missing_prereq" != "$test_prereq"
  573. then
  574. of_prereq=" of $test_prereq"
  575. fi
  576. skipped_reason="missing $missing_prereq${of_prereq}"
  577. fi
  578. if test -z "$to_skip" && test -n "$run_list" &&
  579. ! match_test_selector_list '--run' $test_count "$run_list"
  580. then
  581. to_skip=t
  582. skipped_reason="--run"
  583. fi
  584. case "$to_skip" in
  585. t)
  586. say_color skip >&3 "skipping test: $@"
  587. say_color skip "ok $test_count # skip $1 ($skipped_reason)"
  588. : true
  589. ;;
  590. *)
  591. false
  592. ;;
  593. esac
  594. }
  595. # stub; perf-lib overrides it
  596. test_at_end_hook_ () {
  597. :
  598. }
  599. test_done () {
  600. GIT_EXIT_OK=t
  601. if test -z "$HARNESS_ACTIVE"
  602. then
  603. test_results_dir="$TEST_OUTPUT_DIRECTORY/test-results"
  604. mkdir -p "$test_results_dir"
  605. base=${0##*/}
  606. test_results_path="$test_results_dir/${base%.sh}-$$.counts"
  607. cat >>"$test_results_path" <<-EOF
  608. total $test_count
  609. success $test_success
  610. fixed $test_fixed
  611. broken $test_broken
  612. failed $test_failure
  613. EOF
  614. fi
  615. if test "$test_fixed" != 0
  616. then
  617. say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
  618. fi
  619. if test "$test_broken" != 0
  620. then
  621. say_color warn "# still have $test_broken known breakage(s)"
  622. fi
  623. if test "$test_broken" != 0 || test "$test_fixed" != 0
  624. then
  625. test_remaining=$(( $test_count - $test_broken - $test_fixed ))
  626. msg="remaining $test_remaining test(s)"
  627. else
  628. test_remaining=$test_count
  629. msg="$test_count test(s)"
  630. fi
  631. case "$test_failure" in
  632. 0)
  633. # Maybe print SKIP message
  634. if test -n "$skip_all" && test $test_count -gt 0
  635. then
  636. error "Can't use skip_all after running some tests"
  637. fi
  638. test -z "$skip_all" || skip_all=" # SKIP $skip_all"
  639. if test $test_external_has_tap -eq 0
  640. then
  641. if test $test_remaining -gt 0
  642. then
  643. say_color pass "# passed all $msg"
  644. fi
  645. say "1..$test_count$skip_all"
  646. fi
  647. test -d "$remove_trash" &&
  648. cd "$(dirname "$remove_trash")" &&
  649. rm -rf "$(basename "$remove_trash")"
  650. test_at_end_hook_
  651. exit 0 ;;
  652. *)
  653. if test $test_external_has_tap -eq 0
  654. then
  655. say_color error "# failed $test_failure among $msg"
  656. say "1..$test_count"
  657. fi
  658. exit 1 ;;
  659. esac
  660. }
  661. if test -n "$valgrind"
  662. then
  663. make_symlink () {
  664. test -h "$2" &&
  665. test "$1" = "$(readlink "$2")" || {
  666. # be super paranoid
  667. if mkdir "$2".lock
  668. then
  669. rm -f "$2" &&
  670. ln -s "$1" "$2" &&
  671. rm -r "$2".lock
  672. else
  673. while test -d "$2".lock
  674. do
  675. say "Waiting for lock on $2."
  676. sleep 1
  677. done
  678. fi
  679. }
  680. }
  681. make_valgrind_symlink () {
  682. # handle only executables, unless they are shell libraries that
  683. # need to be in the exec-path.
  684. test -x "$1" ||
  685. test "# " = "$(head -c 2 <"$1")" ||
  686. return;
  687. base=$(basename "$1")
  688. symlink_target=$GIT_BUILD_DIR/$base
  689. # do not override scripts
  690. if test -x "$symlink_target" &&
  691. test ! -d "$symlink_target" &&
  692. test "#!" != "$(head -c 2 < "$symlink_target")"
  693. then
  694. symlink_target=../valgrind.sh
  695. fi
  696. case "$base" in
  697. *.sh|*.perl)
  698. symlink_target=../unprocessed-script
  699. esac
  700. # create the link, or replace it if it is out of date
  701. make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
  702. }
  703. # override all git executables in TEST_DIRECTORY/..
  704. GIT_VALGRIND=$TEST_DIRECTORY/valgrind
  705. mkdir -p "$GIT_VALGRIND"/bin
  706. for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/test-*
  707. do
  708. make_valgrind_symlink $file
  709. done
  710. # special-case the mergetools loadables
  711. make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
  712. OLDIFS=$IFS
  713. IFS=:
  714. for path in $PATH
  715. do
  716. ls "$path"/git-* 2> /dev/null |
  717. while read file
  718. do
  719. make_valgrind_symlink "$file"
  720. done
  721. done
  722. IFS=$OLDIFS
  723. PATH=$GIT_VALGRIND/bin:$PATH
  724. GIT_EXEC_PATH=$GIT_VALGRIND/bin
  725. export GIT_VALGRIND
  726. GIT_VALGRIND_MODE="$valgrind"
  727. export GIT_VALGRIND_MODE
  728. GIT_VALGRIND_ENABLED=t
  729. test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
  730. export GIT_VALGRIND_ENABLED
  731. elif test -n "$GIT_TEST_INSTALLED"
  732. then
  733. GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
  734. error "Cannot run git from $GIT_TEST_INSTALLED."
  735. PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR:$PATH
  736. GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
  737. else # normal case, use ../bin-wrappers only unless $with_dashes:
  738. git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
  739. if ! test -x "$git_bin_dir/git"
  740. then
  741. if test -z "$with_dashes"
  742. then
  743. say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
  744. fi
  745. with_dashes=t
  746. fi
  747. PATH="$git_bin_dir:$PATH"
  748. GIT_EXEC_PATH=$GIT_BUILD_DIR
  749. if test -n "$with_dashes"
  750. then
  751. PATH="$GIT_BUILD_DIR:$PATH"
  752. fi
  753. fi
  754. GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
  755. GIT_CONFIG_NOSYSTEM=1
  756. GIT_ATTR_NOSYSTEM=1
  757. export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
  758. if test -z "$GIT_TEST_CMP"
  759. then
  760. if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
  761. then
  762. GIT_TEST_CMP="$DIFF -c"
  763. else
  764. GIT_TEST_CMP="$DIFF -u"
  765. fi
  766. fi
  767. GITPERLLIB="$GIT_BUILD_DIR"/perl/blib/lib:"$GIT_BUILD_DIR"/perl/blib/arch/auto/Git
  768. export GITPERLLIB
  769. test -d "$GIT_BUILD_DIR"/templates/blt || {
  770. error "You haven't built things yet, have you?"
  771. }
  772. if ! test -x "$GIT_BUILD_DIR"/t/helper/test-chmtime
  773. then
  774. echo >&2 'You need to build test-chmtime:'
  775. echo >&2 'Run "make t/helper/test-chmtime" in the source (toplevel) directory'
  776. exit 1
  777. fi
  778. # Test repository
  779. TRASH_DIRECTORY="trash directory.$(basename "$0" .sh)"
  780. test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
  781. case "$TRASH_DIRECTORY" in
  782. /*) ;; # absolute path is good
  783. *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
  784. esac
  785. test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
  786. rm -fr "$TRASH_DIRECTORY" || {
  787. GIT_EXIT_OK=t
  788. echo >&5 "FATAL: Cannot prepare test area"
  789. exit 1
  790. }
  791. HOME="$TRASH_DIRECTORY"
  792. GNUPGHOME="$HOME/gnupg-home-not-used"
  793. export HOME GNUPGHOME
  794. if test -z "$TEST_NO_CREATE_REPO"
  795. then
  796. test_create_repo "$TRASH_DIRECTORY"
  797. else
  798. mkdir -p "$TRASH_DIRECTORY"
  799. fi
  800. # Use -P to resolve symlinks in our working directory so that the cwd
  801. # in subprocesses like git equals our $PWD (for pathname comparisons).
  802. cd -P "$TRASH_DIRECTORY" || exit 1
  803. this_test=${0##*/}
  804. this_test=${this_test%%-*}
  805. if match_pattern_list "$this_test" $GIT_SKIP_TESTS
  806. then
  807. say_color info >&3 "skipping test $this_test altogether"
  808. skip_all="skip all tests in $this_test"
  809. test_done
  810. fi
  811. # Provide an implementation of the 'yes' utility
  812. yes () {
  813. if test $# = 0
  814. then
  815. y=y
  816. else
  817. y="$*"
  818. fi
  819. i=0
  820. while test $i -lt 99
  821. do
  822. echo "$y"
  823. i=$(($i+1))
  824. done
  825. }
  826. # Fix some commands on Windows
  827. case $(uname -s) in
  828. *MINGW*)
  829. # Windows has its own (incompatible) sort and find
  830. sort () {
  831. /usr/bin/sort "$@"
  832. }
  833. find () {
  834. /usr/bin/find "$@"
  835. }
  836. sum () {
  837. md5sum "$@"
  838. }
  839. # git sees Windows-style pwd
  840. pwd () {
  841. builtin pwd -W
  842. }
  843. # no POSIX permissions
  844. # backslashes in pathspec are converted to '/'
  845. # exec does not inherit the PID
  846. test_set_prereq MINGW
  847. test_set_prereq NATIVE_CRLF
  848. test_set_prereq SED_STRIPS_CR
  849. test_set_prereq GREP_STRIPS_CR
  850. GIT_TEST_CMP=mingw_test_cmp
  851. ;;
  852. *CYGWIN*)
  853. test_set_prereq POSIXPERM
  854. test_set_prereq EXECKEEPSPID
  855. test_set_prereq CYGWIN
  856. test_set_prereq SED_STRIPS_CR
  857. test_set_prereq GREP_STRIPS_CR
  858. ;;
  859. *)
  860. test_set_prereq POSIXPERM
  861. test_set_prereq BSLASHPSPEC
  862. test_set_prereq EXECKEEPSPID
  863. ;;
  864. esac
  865. ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
  866. test -z "$NO_PERL" && test_set_prereq PERL
  867. test -z "$NO_PYTHON" && test_set_prereq PYTHON
  868. test -n "$USE_LIBPCRE" && test_set_prereq LIBPCRE
  869. test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
  870. # Can we rely on git's output in the C locale?
  871. if test -n "$GETTEXT_POISON"
  872. then
  873. GIT_GETTEXT_POISON=YesPlease
  874. export GIT_GETTEXT_POISON
  875. test_set_prereq GETTEXT_POISON
  876. else
  877. test_set_prereq C_LOCALE_OUTPUT
  878. fi
  879. # Use this instead of test_cmp to compare files that contain expected and
  880. # actual output from git commands that can be translated. When running
  881. # under GETTEXT_POISON this pretends that the command produced expected
  882. # results.
  883. test_i18ncmp () {
  884. test -n "$GETTEXT_POISON" || test_cmp "$@"
  885. }
  886. # Use this instead of "grep expected-string actual" to see if the
  887. # output from a git command that can be translated either contains an
  888. # expected string, or does not contain an unwanted one. When running
  889. # under GETTEXT_POISON this pretends that the command produced expected
  890. # results.
  891. test_i18ngrep () {
  892. if test -n "$GETTEXT_POISON"
  893. then
  894. : # pretend success
  895. elif test "x!" = "x$1"
  896. then
  897. shift
  898. ! grep "$@"
  899. else
  900. grep "$@"
  901. fi
  902. }
  903. test_lazy_prereq PIPE '
  904. # test whether the filesystem supports FIFOs
  905. case $(uname -s) in
  906. CYGWIN*|MINGW*)
  907. false
  908. ;;
  909. *)
  910. rm -f testfifo && mkfifo testfifo
  911. ;;
  912. esac
  913. '
  914. test_lazy_prereq SYMLINKS '
  915. # test whether the filesystem supports symbolic links
  916. ln -s x y && test -h y
  917. '
  918. test_lazy_prereq FILEMODE '
  919. test "$(git config --bool core.filemode)" = true
  920. '
  921. test_lazy_prereq CASE_INSENSITIVE_FS '
  922. echo good >CamelCase &&
  923. echo bad >camelcase &&
  924. test "$(cat CamelCase)" != good
  925. '
  926. test_lazy_prereq UTF8_NFD_TO_NFC '
  927. # check whether FS converts nfd unicode to nfc
  928. auml=$(printf "\303\244")
  929. aumlcdiar=$(printf "\141\314\210")
  930. >"$auml" &&
  931. case "$(echo *)" in
  932. "$aumlcdiar")
  933. true ;;
  934. *)
  935. false ;;
  936. esac
  937. '
  938. test_lazy_prereq AUTOIDENT '
  939. sane_unset GIT_AUTHOR_NAME &&
  940. sane_unset GIT_AUTHOR_EMAIL &&
  941. git var GIT_AUTHOR_IDENT
  942. '
  943. test_lazy_prereq EXPENSIVE '
  944. test -n "$GIT_TEST_LONG"
  945. '
  946. test_lazy_prereq USR_BIN_TIME '
  947. test -x /usr/bin/time
  948. '
  949. test_lazy_prereq NOT_ROOT '
  950. uid=$(id -u) &&
  951. test "$uid" != 0
  952. '
  953. # SANITY is about "can you correctly predict what the filesystem would
  954. # do by only looking at the permission bits of the files and
  955. # directories?" A typical example of !SANITY is running the test
  956. # suite as root, where a test may expect "chmod -r file && cat file"
  957. # to fail because file is supposed to be unreadable after a successful
  958. # chmod. In an environment (i.e. combination of what filesystem is
  959. # being used and who is running the tests) that lacks SANITY, you may
  960. # be able to delete or create a file when the containing directory
  961. # doesn't have write permissions, or access a file even if the
  962. # containing directory doesn't have read or execute permissions.
  963. test_lazy_prereq SANITY '
  964. mkdir SANETESTD.1 SANETESTD.2 &&
  965. chmod +w SANETESTD.1 SANETESTD.2 &&
  966. >SANETESTD.1/x 2>SANETESTD.2/x &&
  967. chmod -w SANETESTD.1 &&
  968. chmod -r SANETESTD.1/x &&
  969. chmod -rx SANETESTD.2 ||
  970. error "bug in test sript: cannot prepare SANETESTD"
  971. ! test -r SANETESTD.1/x &&
  972. ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
  973. status=$?
  974. chmod +rwx SANETESTD.1 SANETESTD.2 &&
  975. rm -rf SANETESTD.1 SANETESTD.2 ||
  976. error "bug in test sript: cannot clean SANETESTD"
  977. return $status
  978. '
  979. GIT_UNZIP=${GIT_UNZIP:-unzip}
  980. test_lazy_prereq UNZIP '
  981. "$GIT_UNZIP" -v
  982. test $? -ne 127
  983. '
  984. run_with_limited_cmdline () {
  985. (ulimit -s 128 && "$@")
  986. }
  987. test_lazy_prereq CMDLINE_LIMIT 'run_with_limited_cmdline true'