PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/t/test-lib.sh

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