/tests/test-lib.sh

https://code.google.com/ · Shell · 1007 lines · 704 code · 106 blank · 197 comment · 100 complexity · 3dd93e97b58820ba52c99b38b6558490 MD5 · raw file

  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. # if --tee was passed, write the output not only to the terminal, but
  18. # additionally to the file test-results/$BASENAME.out, too.
  19. test_name=$(basename "$0" .sh)
  20. case "$GIT_TEST_TEE_STARTED, $* " in
  21. done,*)
  22. # do not redirect again
  23. ;;
  24. *' --tee '*|*' --va'*)
  25. mkdir -p test-results
  26. BASE=test-results/$test_name
  27. (GIT_TEST_TEE_STARTED=done ${SHELL-sh} "$0" "$@" 2>&1;
  28. echo $? > $BASE.exit) | tee $BASE.out
  29. test "$(cat $BASE.exit)" = 0
  30. exit
  31. ;;
  32. esac
  33. # Keep the original TERM for say_color
  34. ORIGINAL_TERM=$TERM
  35. # For repeatability, reset the environment to known value.
  36. LANG=C
  37. LC_ALL=C
  38. PAGER=cat
  39. TZ=UTC
  40. TERM=dumb
  41. export LANG LC_ALL PAGER TERM TZ
  42. EDITOR=:
  43. export EDITOR
  44. #
  45. # If SHELL_PATH is not set, use a default of /bin/sh
  46. #
  47. SHELL_PATH=${SHELL_PATH:-/bin/sh}
  48. export SHELL_PATH
  49. unset VISUAL
  50. #
  51. # Pdsh variables
  52. #
  53. unset WCOLL
  54. unset PDSH_RCMD_TYPE
  55. unset PDSH_MISC_MODULES
  56. unset PDSH_MODULE_DIR
  57. unset DSHPATH
  58. unset FANOUT
  59. unset PDSH_GENDERS_FILE
  60. unset PDSH_GENDERS_DIR
  61. unset PDSH_SSH_ARGS
  62. unset PDSH_SSH_ARGS_APPEND
  63. unset SLUMR_JOBID
  64. unset PBS_JOBID
  65. #
  66. # PDSH_BUILD_DIR and PDSH_SRC_DIR are set to build and src paths
  67. #
  68. if test -z "$PDSH_BUILD_DIR"; then
  69. if test -z "${builddir}"; then
  70. PDSH_BUILD_DIR=$(pwd)/..
  71. else
  72. PDSH_BUILD_DIR=$(cd ${builddir} && pwd)/..
  73. fi
  74. export PDSH_BUILD_DIR
  75. fi
  76. #
  77. if test -z "$PDSH_SRC_DIR"; then
  78. if test -z "$srcdir"; then
  79. PDSH_SRC_DIR=$(pwd)/..
  80. else
  81. PDSH_SRC_DIR=$(cd ${srcdir} && pwd)/..
  82. fi
  83. export PDSH_SRC_DIR
  84. fi
  85. # Protect ourselves from common misconfiguration to export
  86. # CDPATH into the environment
  87. unset CDPATH
  88. unset GREP_OPTIONS
  89. # Convenience
  90. #
  91. # A regexp to match 5 and 40 hexdigits
  92. _x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
  93. _x40="$_x05$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
  94. # Each test should start with something like this, after copyright notices:
  95. #
  96. # test_description='Description of this test...
  97. # This test checks if command xyzzy does the right thing...
  98. # '
  99. # . ./test-lib.sh
  100. [ "x$ORIGINAL_TERM" != "xdumb" ] && (
  101. TERM=$ORIGINAL_TERM &&
  102. export TERM &&
  103. [ -t 1 ] &&
  104. tput bold >/dev/null 2>&1 &&
  105. tput setaf 1 >/dev/null 2>&1 &&
  106. tput sgr0 >/dev/null 2>&1
  107. ) &&
  108. color=t
  109. while test "$#" -ne 0
  110. do
  111. case "$1" in
  112. -d|--d|--de|--deb|--debu|--debug)
  113. debug=t; shift ;;
  114. -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
  115. immediate=t; shift ;;
  116. -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
  117. PDSH_TEST_LONG=t; export PDSH_TEST_LONG; shift ;;
  118. -h|--h|--he|--hel|--help)
  119. help=t; shift ;;
  120. -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
  121. verbose=t; shift ;;
  122. -q|--q|--qu|--qui|--quie|--quiet)
  123. # Ignore --quiet under a TAP::Harness. Saying how many tests
  124. # passed without the ok/not ok details is always an error.
  125. test -z "$HARNESS_ACTIVE" && quiet=t; shift ;;
  126. --with-dashes)
  127. with_dashes=t; shift ;;
  128. --no-color)
  129. color=; shift ;;
  130. --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
  131. valgrind=t; verbose=t; shift ;;
  132. --tee)
  133. shift ;; # was handled already
  134. --root=*)
  135. root=$(expr "z$1" : 'z[^=]*=\(.*\)')
  136. shift ;;
  137. *)
  138. echo "error: unknown test option '$1'" >&2; exit 1 ;;
  139. esac
  140. done
  141. if test -n "$color"; then
  142. say_color () {
  143. (
  144. TERM=$ORIGINAL_TERM
  145. export TERM
  146. case "$1" in
  147. error) tput bold; tput setaf 1;; # bold red
  148. skip) tput bold; tput setaf 2;; # bold green
  149. pass) tput setaf 2;; # green
  150. info) tput setaf 3;; # brown
  151. *) test -n "$quiet" && return;;
  152. esac
  153. shift
  154. printf "%s" "$*"
  155. tput sgr0
  156. echo
  157. )
  158. }
  159. else
  160. say_color() {
  161. test -z "$1" && test -n "$quiet" && return
  162. shift
  163. echo "$*"
  164. }
  165. fi
  166. error () {
  167. say_color error "error: $*"
  168. GIT_EXIT_OK=t
  169. exit 1
  170. }
  171. say () {
  172. say_color info "$*"
  173. }
  174. test "${test_description}" != "" ||
  175. error "Test script did not set test_description."
  176. if test "$help" = "t"
  177. then
  178. echo "$test_description"
  179. exit 0
  180. fi
  181. exec 5>&1
  182. if test "$verbose" = "t"
  183. then
  184. exec 4>&2 3>&1
  185. else
  186. exec 4>/dev/null 3>/dev/null
  187. fi
  188. test_failure=0
  189. test_count=0
  190. test_fixed=0
  191. test_broken=0
  192. test_success=0
  193. test_external_has_tap=0
  194. die () {
  195. code=$?
  196. if test -n "$GIT_EXIT_OK"
  197. then
  198. exit $code
  199. else
  200. echo >&5 "FATAL: Unexpected exit with code $code"
  201. exit 1
  202. fi
  203. }
  204. GIT_EXIT_OK=
  205. trap 'die' EXIT
  206. # The semantics of the editor variables are that of invoking
  207. # sh -c "$EDITOR \"$@\"" files ...
  208. #
  209. # If our trash directory contains shell metacharacters, they will be
  210. # interpreted if we just set $EDITOR directly, so do a little dance with
  211. # environment variables to work around this.
  212. #
  213. # In particular, quoting isn't enough, as the path may contain the same quote
  214. # that we're using.
  215. test_set_editor () {
  216. FAKE_EDITOR="$1"
  217. export FAKE_EDITOR
  218. EDITOR='"$FAKE_EDITOR"'
  219. export EDITOR
  220. }
  221. test_decode_color () {
  222. sed -e 's/.\[1m/<WHITE>/g' \
  223. -e 's/.\[31m/<RED>/g' \
  224. -e 's/.\[32m/<GREEN>/g' \
  225. -e 's/.\[33m/<YELLOW>/g' \
  226. -e 's/.\[34m/<BLUE>/g' \
  227. -e 's/.\[35m/<MAGENTA>/g' \
  228. -e 's/.\[36m/<CYAN>/g' \
  229. -e 's/.\[m/<RESET>/g'
  230. }
  231. q_to_nul () {
  232. perl -pe 'y/Q/\000/'
  233. }
  234. q_to_cr () {
  235. tr Q '\015'
  236. }
  237. q_to_tab () {
  238. tr Q '\011'
  239. }
  240. append_cr () {
  241. sed -e 's/$/Q/' | tr Q '\015'
  242. }
  243. remove_cr () {
  244. tr '\015' Q | sed -e 's/Q$//'
  245. }
  246. test_tick () {
  247. if test -z "${test_tick+set}"
  248. then
  249. test_tick=1112911993
  250. else
  251. test_tick=$(($test_tick + 60))
  252. fi
  253. GIT_COMMITTER_DATE="$test_tick -0700"
  254. GIT_AUTHOR_DATE="$test_tick -0700"
  255. export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
  256. }
  257. # Use test_set_prereq to tell that a particular prerequisite is available.
  258. # The prerequisite can later be checked for in two ways:
  259. #
  260. # - Explicitly using test_have_prereq.
  261. #
  262. # - Implicitly by specifying the prerequisite tag in the calls to
  263. # test_expect_{success,failure,code}.
  264. #
  265. # The single parameter is the prerequisite tag (a simple word, in all
  266. # capital letters by convention).
  267. test_set_prereq () {
  268. satisfied="$satisfied$1 "
  269. }
  270. satisfied=" "
  271. test_have_prereq () {
  272. # prerequisites can be concatenated with ','
  273. save_IFS=$IFS
  274. IFS=,
  275. set -- $*
  276. IFS=$save_IFS
  277. total_prereq=0
  278. ok_prereq=0
  279. missing_prereq=
  280. for prerequisite
  281. do
  282. total_prereq=$(($total_prereq + 1))
  283. case $satisfied in
  284. *" $prerequisite "*)
  285. ok_prereq=$(($ok_prereq + 1))
  286. ;;
  287. *)
  288. # Keep a list of missing prerequisites
  289. if test -z "$missing_prereq"
  290. then
  291. missing_prereq=$prerequisite
  292. else
  293. missing_prereq="$prerequisite,$missing_prereq"
  294. fi
  295. esac
  296. done
  297. test $total_prereq = $ok_prereq
  298. }
  299. # You are not expected to call test_ok_ and test_failure_ directly, use
  300. # the text_expect_* functions instead.
  301. test_ok_ () {
  302. test_success=$(($test_success + 1))
  303. say_color pass "ok $test_count - $@"
  304. }
  305. test_failure_ () {
  306. test_failure=$(($test_failure + 1))
  307. say_color error "not ok - $test_count $1"
  308. shift
  309. echo "$@" | sed -e 's/^/# /'
  310. test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
  311. }
  312. test_known_broken_ok_ () {
  313. test_fixed=$(($test_fixed+1))
  314. say_color pass "ok $test_count - $@ # TODO known breakage"
  315. }
  316. test_known_broken_failure_ () {
  317. test_broken=$(($test_broken+1))
  318. say_color skip "not ok $test_count - $@ # TODO known breakage"
  319. }
  320. test_debug () {
  321. test "$debug" = "" || eval "$1"
  322. }
  323. test_run_ () {
  324. test_cleanup=:
  325. eval >&3 2>&4 "$1"
  326. eval_ret=$?
  327. eval >&3 2>&4 "$test_cleanup"
  328. if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
  329. echo ""
  330. fi
  331. return 0
  332. }
  333. test_skip () {
  334. test_count=$(($test_count+1))
  335. to_skip=
  336. for skp in $PDSH_SKIP_TESTS
  337. do
  338. case $this_test.$test_count in
  339. $skp)
  340. to_skip=t
  341. break
  342. esac
  343. done
  344. if test -z "$to_skip" && test -n "$prereq" &&
  345. ! test_have_prereq "$prereq"
  346. then
  347. to_skip=t
  348. fi
  349. case "$to_skip" in
  350. t)
  351. of_prereq=
  352. if test "$missing_prereq" != "$prereq"
  353. then
  354. of_prereq=" of $prereq"
  355. fi
  356. say_color skip >&3 "skipping test: $@"
  357. say_color skip "ok $test_count # skip $1 (missing $missing_prereq${of_prereq})"
  358. : true
  359. ;;
  360. *)
  361. false
  362. ;;
  363. esac
  364. }
  365. test_expect_failure () {
  366. test "$#" = 3 && { prereq=$1; shift; } || prereq=
  367. test "$#" = 2 ||
  368. error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
  369. if ! test_skip "$@"
  370. then
  371. say >&3 "checking known breakage: $2"
  372. test_run_ "$2"
  373. if [ "$?" = 0 -a "$eval_ret" = 0 ]
  374. then
  375. test_known_broken_ok_ "$1"
  376. else
  377. test_known_broken_failure_ "$1"
  378. fi
  379. fi
  380. echo >&3 ""
  381. }
  382. test_expect_success () {
  383. test "$#" = 3 && { prereq=$1; shift; } || prereq=
  384. test "$#" = 2 ||
  385. error "bug in the test script: not 2 or 3 parameters to test-expect-success"
  386. if ! test_skip "$@"
  387. then
  388. say >&3 "expecting success: $2"
  389. test_run_ "$2"
  390. if [ "$?" = 0 -a "$eval_ret" = 0 ]
  391. then
  392. test_ok_ "$1"
  393. else
  394. test_failure_ "$@"
  395. fi
  396. fi
  397. echo >&3 ""
  398. }
  399. # Similar to test_must_fail and test_might_fail, but check that a
  400. # given command exited with a given exit code. Meant to be used as:
  401. #
  402. # test_expect_success 'Merge with d/f conflicts' '
  403. # test_expect_code 1 git merge "merge msg" B master
  404. # '
  405. test_expect_code () {
  406. want_code=$1
  407. shift
  408. "$@"
  409. exit_code=$?
  410. if test $exit_code = $want_code
  411. then
  412. echo >&2 "test_expect_code: command exited with $exit_code: $*"
  413. return 0
  414. else
  415. echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
  416. return 1
  417. fi
  418. }
  419. # test_external runs external test scripts that provide continuous
  420. # test output about their progress, and succeeds/fails on
  421. # zero/non-zero exit code. It outputs the test output on stdout even
  422. # in non-verbose mode, and announces the external script with "# run
  423. # <n>: ..." before running it. When providing relative paths, keep in
  424. # mind that all scripts run in "trash directory".
  425. # Usage: test_external description command arguments...
  426. # Example: test_external 'Perl API' perl ../path/to/test.pl
  427. test_external () {
  428. test "$#" = 4 && { prereq=$1; shift; } || prereq=
  429. test "$#" = 3 ||
  430. error >&5 "bug in the test script: not 3 or 4 parameters to test_external"
  431. descr="$1"
  432. shift
  433. if ! test_skip "$descr" "$@"
  434. then
  435. # Announce the script to reduce confusion about the
  436. # test output that follows.
  437. say_color "" "# run $test_count: $descr ($*)"
  438. # Export TEST_DIRECTORY, TRASH_DIRECTORY and GIT_TEST_LONG
  439. # to be able to use them in script
  440. export TEST_DIRECTORY TRASH_DIRECTORY GIT_TEST_LONG
  441. # Run command; redirect its stderr to &4 as in
  442. # test_run_, but keep its stdout on our stdout even in
  443. # non-verbose mode.
  444. "$@" 2>&4
  445. if [ "$?" = 0 ]
  446. then
  447. if test $test_external_has_tap -eq 0; then
  448. test_ok_ "$descr"
  449. else
  450. say_color "" "# test_external test $descr was ok"
  451. test_success=$(($test_success + 1))
  452. fi
  453. else
  454. if test $test_external_has_tap -eq 0; then
  455. test_failure_ "$descr" "$@"
  456. else
  457. say_color error "# test_external test $descr failed: $@"
  458. test_failure=$(($test_failure + 1))
  459. fi
  460. fi
  461. fi
  462. }
  463. # Like test_external, but in addition tests that the command generated
  464. # no output on stderr.
  465. test_external_without_stderr () {
  466. # The temporary file has no (and must have no) security
  467. # implications.
  468. tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi
  469. stderr="$tmp/git-external-stderr.$$.tmp"
  470. test_external "$@" 4> "$stderr"
  471. [ -f "$stderr" ] || error "Internal error: $stderr disappeared."
  472. descr="no stderr: $1"
  473. shift
  474. say >&3 "# expecting no stderr from previous command"
  475. if [ ! -s "$stderr" ]; then
  476. rm "$stderr"
  477. if test $test_external_has_tap -eq 0; then
  478. test_ok_ "$descr"
  479. else
  480. say_color "" "# test_external_without_stderr test $descr was ok"
  481. test_success=$(($test_success + 1))
  482. fi
  483. else
  484. if [ "$verbose" = t ]; then
  485. output=`echo; echo "# Stderr is:"; cat "$stderr"`
  486. else
  487. output=
  488. fi
  489. # rm first in case test_failure exits.
  490. rm "$stderr"
  491. if test $test_external_has_tap -eq 0; then
  492. test_failure_ "$descr" "$@" "$output"
  493. else
  494. say_color error "# test_external_without_stderr test $descr failed: $@: $output"
  495. test_failure=$(($test_failure + 1))
  496. fi
  497. fi
  498. }
  499. # debugging-friendly alternatives to "test [-f|-d|-e]"
  500. # The commands test the existence or non-existence of $1. $2 can be
  501. # given to provide a more precise diagnosis.
  502. test_path_is_file () {
  503. if ! [ -f "$1" ]
  504. then
  505. echo "File $1 doesn't exist. $*"
  506. false
  507. fi
  508. }
  509. test_path_is_dir () {
  510. if ! [ -d "$1" ]
  511. then
  512. echo "Directory $1 doesn't exist. $*"
  513. false
  514. fi
  515. }
  516. test_path_is_missing () {
  517. if [ -e "$1" ]
  518. then
  519. echo "Path exists:"
  520. ls -ld "$1"
  521. if [ $# -ge 1 ]; then
  522. echo "$*"
  523. fi
  524. false
  525. fi
  526. }
  527. # This is not among top-level (test_expect_success | test_expect_failure)
  528. # but is a prefix that can be used in the test script, like:
  529. #
  530. # test_expect_success 'complain and die' '
  531. # do something &&
  532. # do something else &&
  533. # test_must_fail git checkout ../outerspace
  534. # '
  535. #
  536. # Writing this as "! git checkout ../outerspace" is wrong, because
  537. # the failure could be due to a segv. We want a controlled failure.
  538. test_must_fail () {
  539. "$@"
  540. exit_code=$?
  541. if test $exit_code = 0; then
  542. echo >&2 "test_must_fail: command succeeded: $*"
  543. return 1
  544. elif test $exit_code -gt 129 -a $exit_code -le 192; then
  545. echo >&2 "test_must_fail: died by signal: $*"
  546. return 1
  547. elif test $exit_code = 127; then
  548. echo >&2 "test_must_fail: command not found: $*"
  549. return 1
  550. fi
  551. return 0
  552. }
  553. # Similar to test_must_fail, but tolerates success, too. This is
  554. # meant to be used in contexts like:
  555. #
  556. # test_expect_success 'some command works without configuration' '
  557. # test_might_fail git config --unset all.configuration &&
  558. # do something
  559. # '
  560. #
  561. # Writing "git config --unset all.configuration || :" would be wrong,
  562. # because we want to notice if it fails due to segv.
  563. test_might_fail () {
  564. "$@"
  565. exit_code=$?
  566. if test $exit_code -gt 129 -a $exit_code -le 192; then
  567. echo >&2 "test_might_fail: died by signal: $*"
  568. return 1
  569. elif test $exit_code = 127; then
  570. echo >&2 "test_might_fail: command not found: $*"
  571. return 1
  572. fi
  573. return 0
  574. }
  575. # test_cmp is a helper function to compare actual and expected output.
  576. # You can use it like:
  577. #
  578. # test_expect_success 'foo works' '
  579. # echo expected >expected &&
  580. # foo >actual &&
  581. # test_cmp expected actual
  582. # '
  583. #
  584. # This could be written as either "cmp" or "diff -u", but:
  585. # - cmp's output is not nearly as easy to read as diff -u
  586. # - not all diff versions understand "-u"
  587. test_cmp() {
  588. $GIT_TEST_CMP "$@"
  589. }
  590. # This function can be used to schedule some commands to be run
  591. # unconditionally at the end of the test to restore sanity:
  592. #
  593. # test_expect_success 'test core.capslock' '
  594. # git config core.capslock true &&
  595. # test_when_finished "git config --unset core.capslock" &&
  596. # hello world
  597. # '
  598. #
  599. # That would be roughly equivalent to
  600. #
  601. # test_expect_success 'test core.capslock' '
  602. # git config core.capslock true &&
  603. # hello world
  604. # git config --unset core.capslock
  605. # '
  606. #
  607. # except that the greeting and config --unset must both succeed for
  608. # the test to pass.
  609. test_when_finished () {
  610. test_cleanup="{ $*
  611. } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
  612. }
  613. #
  614. # pdsh convenience functions
  615. #
  616. test_output_is_expected() {
  617. OUTPUT="$1"
  618. EXPECTED="$2"
  619. if ! test "$OUTPUT" = "$EXPECTED"; then
  620. say_color error "Error: Output \'$OUTPUT\' != \'$EXPECTED\'"
  621. false
  622. fi
  623. }
  624. # Most tests can use the created repository, but some may need to create more.
  625. # Usage: test_create_repo <directory>
  626. test_create_repo () {
  627. test "$#" = 1 ||
  628. error "bug in the test script: not 1 parameter to test-create-repo"
  629. repo="$1"
  630. mkdir -p "$repo"
  631. }
  632. test_done () {
  633. GIT_EXIT_OK=t
  634. if test -z "$HARNESS_ACTIVE"; then
  635. test_results_dir="$TEST_DIRECTORY/test-results"
  636. mkdir -p "$test_results_dir"
  637. test_results_path="$test_results_dir/$test_name-$$.counts"
  638. echo "total $test_count" >> $test_results_path
  639. echo "success $test_success" >> $test_results_path
  640. echo "fixed $test_fixed" >> $test_results_path
  641. echo "broken $test_broken" >> $test_results_path
  642. echo "failed $test_failure" >> $test_results_path
  643. echo "" >> $test_results_path
  644. fi
  645. if test "$test_fixed" != 0
  646. then
  647. say_color pass "# fixed $test_fixed known breakage(s)"
  648. fi
  649. if test "$test_broken" != 0
  650. then
  651. say_color error "# still have $test_broken known breakage(s)"
  652. msg="remaining $(($test_count-$test_broken)) test(s)"
  653. else
  654. msg="$test_count test(s)"
  655. fi
  656. case "$test_failure" in
  657. 0)
  658. # Maybe print SKIP message
  659. [ -z "$skip_all" ] || skip_all=" # SKIP $skip_all"
  660. if test $test_external_has_tap -eq 0; then
  661. say_color pass "# passed all $msg"
  662. say "1..$test_count$skip_all"
  663. fi
  664. test -d "$remove_trash" &&
  665. cd "$(dirname "$remove_trash")" &&
  666. rm -rf "$(basename "$remove_trash")"
  667. exit 0 ;;
  668. *)
  669. if test $test_external_has_tap -eq 0; then
  670. say_color error "# failed $test_failure among $msg"
  671. say "1..$test_count"
  672. fi
  673. exit 1 ;;
  674. esac
  675. }
  676. # Test the binaries we have just built. The tests are kept in
  677. # t/ subdirectory and are run in 'trash directory' subdirectory.
  678. if test -z "$TEST_DIRECTORY"
  679. then
  680. # We allow tests to override this, in case they want to run tests
  681. # outside of t/, e.g. for running tests on the test library
  682. # itself.
  683. TEST_DIRECTORY=$(pwd)
  684. fi
  685. if test -n "$valgrind"
  686. then
  687. make_symlink () {
  688. test -h "$2" &&
  689. test "$1" = "$(readlink "$2")" || {
  690. # be super paranoid
  691. if mkdir "$2".lock
  692. then
  693. rm -f "$2" &&
  694. ln -s "$1" "$2" &&
  695. rm -r "$2".lock
  696. else
  697. while test -d "$2".lock
  698. do
  699. say "Waiting for lock on $2."
  700. sleep 1
  701. done
  702. fi
  703. }
  704. }
  705. make_valgrind_symlink () {
  706. # handle only executables
  707. test -x "$1" || return
  708. base=$(basename "$1")
  709. symlink_target=$GIT_BUILD_DIR/$base
  710. # do not override scripts
  711. if test -x "$symlink_target" &&
  712. test ! -d "$symlink_target" &&
  713. test "#!" != "$(head -c 2 < "$symlink_target")"
  714. then
  715. symlink_target=../valgrind.sh
  716. fi
  717. case "$base" in
  718. *.sh|*.perl)
  719. symlink_target=../unprocessed-script
  720. esac
  721. # create the link, or replace it if it is out of date
  722. make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
  723. }
  724. # override all executables in TEST_DIRECTORY/..
  725. GIT_VALGRIND=$TEST_DIRECTORY/valgrind
  726. mkdir -p "$GIT_VALGRIND"/bin
  727. make_valgrind_symlink $PDSH_BUILD_DIR/src/pdsh/pdsh
  728. IFS=$OLDIFS
  729. PATH=$GIT_VALGRIND/bin:$PATH
  730. export GIT_VALGRIND
  731. elif test -n "$PDSH_TEST_INSTALLED" ; then
  732. PATH=$PDSH_TEST_INSTALLED:$PDSH_BUILD_DIR/src/pdsh:$PATH
  733. GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
  734. else # normal case, use ../bin-wrappers only unless $with_dashes:
  735. pdsh_path=$PDSH_BUILD_DIR/src/pdsh
  736. dshbak_path=$PDSH_SRC_DIR/scripts
  737. test -n "$dshbak_path" && PATH="$dshbak_path:$PATH"
  738. test -n "$pdsh_path" && PATH="$pdsh_path:$PATH"
  739. fi
  740. export PATH
  741. if test -z "$GIT_TEST_CMP"
  742. then
  743. if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
  744. then
  745. GIT_TEST_CMP="diff -c"
  746. else
  747. GIT_TEST_CMP="diff -u"
  748. fi
  749. fi
  750. test="trash-directory.$test_name"
  751. test -n "$root" && test="$root/$test"
  752. case "$test" in
  753. /*) TRASH_DIRECTORY="$test" ;;
  754. *) TRASH_DIRECTORY="$TEST_DIRECTORY/$test" ;;
  755. esac
  756. test ! -z "$debug" || remove_trash=$TRASH_DIRECTORY
  757. rm -fr "$test" || {
  758. GIT_EXIT_OK=t
  759. echo >&5 "FATAL: Cannot prepare test area"
  760. exit 1
  761. }
  762. test_create_repo "$test"
  763. # Use -P to resolve symlinks in our working directory so that the cwd
  764. # in subprocesses like git equals our $PWD (for pathname comparisons).
  765. cd -P "$test" || exit 1
  766. HOME=$(pwd)
  767. export HOME
  768. this_test=${0##*/}
  769. this_test=${this_test%%-*}
  770. for skp in $PDSH_SKIP_TESTS
  771. do
  772. case "$this_test" in
  773. $skp)
  774. say_color skip >&3 "skipping test $this_test altogether"
  775. skip_all="skip all tests in $this_test"
  776. test_done
  777. esac
  778. done
  779. # Provide an implementation of the 'yes' utility
  780. yes () {
  781. if test $# = 0
  782. then
  783. y=y
  784. else
  785. y="$*"
  786. fi
  787. while echo "$y"
  788. do
  789. :
  790. done
  791. }
  792. # Fix some commands on Windows
  793. case $(uname -s) in
  794. *MINGW*)
  795. # Windows has its own (incompatible) sort and find
  796. sort () {
  797. /usr/bin/sort "$@"
  798. }
  799. find () {
  800. /usr/bin/find "$@"
  801. }
  802. sum () {
  803. md5sum "$@"
  804. }
  805. # git sees Windows-style pwd
  806. pwd () {
  807. builtin pwd -W
  808. }
  809. # no POSIX permissions
  810. # backslashes in pathspec are converted to '/'
  811. # exec does not inherit the PID
  812. ;;
  813. *)
  814. test_set_prereq POSIXPERM
  815. test_set_prereq BSLASHPSPEC
  816. test_set_prereq EXECKEEPSPID
  817. ;;
  818. esac
  819. test -z "$NO_PERL" && test_set_prereq PERL
  820. test -z "$NO_PYTHON" && test_set_prereq PYTHON
  821. # test whether the filesystem supports symbolic links
  822. ln -s x y 2>/dev/null && test -h y 2>/dev/null && test_set_prereq SYMLINKS
  823. rm -f y
  824. # When the tests are run as root, permission tests will report that
  825. # things are writable when they shouldn't be.
  826. #
  827. # Additionally, for pdsh, some tests require non-root, esp those
  828. # that use PDSH_MODULE_DIR, which doesn't work when run setuid or
  829. # as root
  830. test -w / || test_set_prereq SANITY
  831. test "$USER" = "root" || test_set_prereq NOTROOT
  832. # Set some prereqs for common commands
  833. #
  834. test "$(expr 9 - 2)" = "7" && test_set_prereq EXPR
  835. # Function to generate a random number
  836. #
  837. random() {
  838. R=$RANDOM
  839. if test -z "$R"; then
  840. if test -r /dev/urandom; then
  841. R=$(dd if=/dev/urandom count=1 2>/dev/null | cksum | cut -d' ' -f1)
  842. else
  843. R=$( (echo $$; ps; date +%s) 2>&1 | cksum | cut -d' ' -f1)
  844. fi
  845. fi
  846. if test -n "${1}"; then
  847. R=$(expr $R % $1)
  848. fi
  849. echo $R
  850. }
  851. # Shell implementation of seq(1)
  852. #
  853. seq() {
  854. if [ $# -eq 1 ]; then
  855. i=1
  856. end=$1
  857. else
  858. i=$1
  859. end=$2
  860. fi
  861. while [ $i -le $end ]; do
  862. echo $i
  863. i=$((i+1))
  864. done
  865. }
  866. #
  867. # Ensure that pdsh has been built.
  868. #
  869. if ! test -x $PDSH_BUILD_DIR/src/pdsh/pdsh; then
  870. say_color error 'Can not find a pdsh binary to test'
  871. say_color error 'Do you need to run the build system?'
  872. exit 1
  873. fi
  874. #
  875. # If the pdsh build directory owner and the pdsh binary have
  876. # different ownership, abort the test because pdsh will not
  877. # be able to load any modules, and almost no tests will work.
  878. #
  879. path_owner() { ls -ld $1 | awk '{print $3}'; }
  880. pdsh_owner=$(path_owner $PDSH_BUILD_DIR/src/pdsh/pdsh)
  881. builddir_owner=$(path_owner $PDSH_BUILD_DIR/src)
  882. if test "$pdsh_owner" != "$builddir_owner"; then
  883. say_color error 'Build directory owner and pdsh binary owner are different'
  884. say_color error 'The testsuite will not work in this configuration'
  885. exit 1
  886. fi
  887. #
  888. # Set loaded modules as prereqs
  889. #
  890. for mod in $(pdsh -L 2>&1 | \
  891. sed -n 's/^Module: *\(.*\)\/\(.*\)/\1_\2/p' | \
  892. tr a-z A-Z); do
  893. test_set_prereq MOD_${mod}
  894. done
  895. if [ -n "$PDSH_TEST_LONG" ]; then
  896. test_set_prereq LONGTESTS
  897. fi
  898. if pdsh -V | head -1 | grep -qv +static-modules; then
  899. test_set_prereq DYNAMIC_MODULES
  900. fi