Merge branch 'sg/test-x'

Running test scripts under -x option of the shell is often not a
useful way to debug them, because the error messages from the
commands tests try to capture and inspect are contaminated by the
tracing output by the shell.  An earlier work done to make it more
pleasant to run tests under -x with recent versions of bash is
extended to cover posix shells that do not support BASH_XTRACEFD.

* sg/test-x:
  travis-ci: run tests with '-x' tracing
  t/README: add a note about don't saving stderr of compound commands
  t1510-repo-setup: mark as untraceable with '-x'
  t9903-bash-prompt: don't check the stderr of __git_ps1()
  t5570-git-daemon: don't check the stderr of a subshell
  t5526: use $TRASH_DIRECTORY to specify the path of GIT_TRACE log file
  t5500-fetch-pack: don't check the stderr of a subshell
  t3030-merge-recursive: don't check the stderr of a subshell
  t1507-rev-parse-upstream: don't check the stderr of a shell function
  t: add means to disable '-x' tracing for individual test scripts
  t: prevent '-x' tracing from interfering with test helpers' stderr
This commit is contained in:
Junio C Hamano
2018-03-14 12:01:03 -07:00
12 changed files with 94 additions and 62 deletions

View File

@ -97,7 +97,7 @@ fi
export DEVELOPER=1 export DEVELOPER=1
export DEFAULT_TEST_TARGET=prove export DEFAULT_TEST_TARGET=prove
export GIT_PROVE_OPTS="--timer --jobs 3 --state=failed,slow,save" export GIT_PROVE_OPTS="--timer --jobs 3 --state=failed,slow,save"
export GIT_TEST_OPTS="--verbose-log" export GIT_TEST_OPTS="--verbose-log -x"
export GIT_TEST_CLONE_2GB=YesPlease export GIT_TEST_CLONE_2GB=YesPlease
case "$jobname" in case "$jobname" in

View File

@ -84,9 +84,10 @@ appropriately before running "make".
-x:: -x::
Turn on shell tracing (i.e., `set -x`) during the tests Turn on shell tracing (i.e., `set -x`) during the tests
themselves. Implies `--verbose`. Note that in non-bash shells, themselves. Implies `--verbose`.
this can cause failures in some tests which redirect and test Ignored in test scripts that set the variable 'test_untraceable'
the output of shell functions. Use with caution. to a non-empty value, unless it's run with a Bash version
supporting BASH_XTRACEFD, i.e. v4.1 or later.
-d:: -d::
--debug:: --debug::
@ -452,6 +453,22 @@ Don't:
causing the next test to start in an unexpected directory. Do so causing the next test to start in an unexpected directory. Do so
inside a subshell if necessary. inside a subshell if necessary.
- save and verify the standard error of compound commands, i.e. group
commands, subshells, and shell functions (except test helper
functions like 'test_must_fail') like this:
( cd dir && git cmd ) 2>error &&
test_cmp expect error
When running the test with '-x' tracing, then the trace of commands
executed in the compound command will be included in standard error
as well, quite possibly throwing off the subsequent checks examining
the output. Instead, save only the relevant git command's standard
error:
( cd dir && git cmd 2>../error ) &&
test_cmp expect error
- Break the TAP output - Break the TAP output
The raw output from your test may be interpreted by a TAP harness. TAP The raw output from your test may be interpreted by a TAP harness. TAP

View File

@ -9,8 +9,8 @@ test_terminal () {
echo >&4 "test_terminal: need to declare TTY prerequisite" echo >&4 "test_terminal: need to declare TTY prerequisite"
return 127 return 127
fi fi
perl "$TEST_DIRECTORY"/test-terminal.perl "$@" perl "$TEST_DIRECTORY"/test-terminal.perl "$@" 2>&7
} } 7>&2 2>&4
test_lazy_prereq TTY ' test_lazy_prereq TTY '
test_have_prereq PERL && test_have_prereq PERL &&

View File

@ -42,7 +42,7 @@ commit_subject () {
error_message () { error_message () {
(cd clone && (cd clone &&
test_must_fail git rev-parse --verify "$@") test_must_fail git rev-parse --verify "$@" 2>../error)
} }
test_expect_success '@{upstream} resolves to correct full name' ' test_expect_success '@{upstream} resolves to correct full name' '
@ -159,8 +159,8 @@ test_expect_success 'branch@{u} error message when no upstream' '
cat >expect <<-EOF && cat >expect <<-EOF &&
fatal: no upstream configured for branch ${sq}non-tracking${sq} fatal: no upstream configured for branch ${sq}non-tracking${sq}
EOF EOF
error_message non-tracking@{u} 2>actual && error_message non-tracking@{u} &&
test_i18ncmp expect actual test_i18ncmp expect error
' '
test_expect_success '@{u} error message when no upstream' ' test_expect_success '@{u} error message when no upstream' '
@ -175,8 +175,8 @@ test_expect_success 'branch@{u} error message with misspelt branch' '
cat >expect <<-EOF && cat >expect <<-EOF &&
fatal: no such branch: ${sq}no-such-branch${sq} fatal: no such branch: ${sq}no-such-branch${sq}
EOF EOF
error_message no-such-branch@{u} 2>actual && error_message no-such-branch@{u} &&
test_i18ncmp expect actual test_i18ncmp expect error
' '
test_expect_success '@{u} error message when not on a branch' ' test_expect_success '@{u} error message when not on a branch' '
@ -192,8 +192,8 @@ test_expect_success 'branch@{u} error message if upstream branch not fetched' '
cat >expect <<-EOF && cat >expect <<-EOF &&
fatal: upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch fatal: upstream branch ${sq}refs/heads/side${sq} not stored as a remote-tracking branch
EOF EOF
error_message bad-upstream@{u} 2>actual && error_message bad-upstream@{u} &&
test_i18ncmp expect actual test_i18ncmp expect error
' '
test_expect_success 'pull works when tracking a local branch' ' test_expect_success 'pull works when tracking a local branch' '

View File

@ -39,6 +39,10 @@ A few rules for repo setup:
11. When user's cwd is outside worktree, cwd remains unchanged, 11. When user's cwd is outside worktree, cwd remains unchanged,
prefix is NULL. prefix is NULL.
" "
# This test heavily relies on the standard error of nested function calls.
test_untraceable=UnfortunatelyYes
. ./test-lib.sh . ./test-lib.sh
here=$(pwd) here=$(pwd)

View File

@ -525,20 +525,22 @@ test_expect_success 'merge-recursive w/ empty work tree - ours has rename' '
GIT_INDEX_FILE="$PWD/ours-has-rename-index" && GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
export GIT_INDEX_FILE && export GIT_INDEX_FILE &&
mkdir "$GIT_WORK_TREE" && mkdir "$GIT_WORK_TREE" &&
git read-tree -i -m $c7 && git read-tree -i -m $c7 2>actual-err &&
git update-index --ignore-missing --refresh && test_must_be_empty actual-err &&
git merge-recursive $c0 -- $c7 $c3 && git update-index --ignore-missing --refresh 2>actual-err &&
git ls-files -s >actual-files test_must_be_empty actual-err &&
) 2>actual-err && git merge-recursive $c0 -- $c7 $c3 2>actual-err &&
>expected-err && test_must_be_empty actual-err &&
git ls-files -s >actual-files 2>actual-err &&
test_must_be_empty actual-err
) &&
cat >expected-files <<-EOF && cat >expected-files <<-EOF &&
100644 $o3 0 b/c 100644 $o3 0 b/c
100644 $o0 0 c 100644 $o0 0 c
100644 $o0 0 d/e 100644 $o0 0 d/e
100644 $o0 0 e 100644 $o0 0 e
EOF EOF
test_cmp expected-files actual-files && test_cmp expected-files actual-files
test_cmp expected-err actual-err
' '
test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' ' test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' '
@ -548,20 +550,22 @@ test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' '
GIT_INDEX_FILE="$PWD/theirs-has-rename-index" && GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
export GIT_INDEX_FILE && export GIT_INDEX_FILE &&
mkdir "$GIT_WORK_TREE" && mkdir "$GIT_WORK_TREE" &&
git read-tree -i -m $c3 && git read-tree -i -m $c3 2>actual-err &&
git update-index --ignore-missing --refresh && test_must_be_empty actual-err &&
git merge-recursive $c0 -- $c3 $c7 && git update-index --ignore-missing --refresh 2>actual-err &&
git ls-files -s >actual-files test_must_be_empty actual-err &&
) 2>actual-err && git merge-recursive $c0 -- $c3 $c7 2>actual-err &&
>expected-err && test_must_be_empty actual-err &&
git ls-files -s >actual-files 2>actual-err &&
test_must_be_empty actual-err
) &&
cat >expected-files <<-EOF && cat >expected-files <<-EOF &&
100644 $o3 0 b/c 100644 $o3 0 b/c
100644 $o0 0 c 100644 $o0 0 c
100644 $o0 0 d/e 100644 $o0 0 d/e
100644 $o0 0 e 100644 $o0 0 e
EOF EOF
test_cmp expected-files actual-files && test_cmp expected-files actual-files
test_cmp expected-err actual-err
' '
test_expect_success 'merge removes empty directories' ' test_expect_success 'merge removes empty directories' '

View File

@ -482,24 +482,24 @@ test_expect_success 'set up tests of missing reference' '
test_expect_success 'test lonely missing ref' ' test_expect_success 'test lonely missing ref' '
( (
cd client && cd client &&
test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy 2>../error-m
) >/dev/null 2>error-m && ) &&
test_i18ncmp expect-error error-m test_i18ncmp expect-error error-m
' '
test_expect_success 'test missing ref after existing' ' test_expect_success 'test missing ref after existing' '
( (
cd client && cd client &&
test_must_fail git fetch-pack --no-progress .. refs/heads/A refs/heads/xyzzy test_must_fail git fetch-pack --no-progress .. refs/heads/A refs/heads/xyzzy 2>../error-em
) >/dev/null 2>error-em && ) &&
test_i18ncmp expect-error error-em test_i18ncmp expect-error error-em
' '
test_expect_success 'test missing ref before existing' ' test_expect_success 'test missing ref before existing' '
( (
cd client && cd client &&
test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy refs/heads/A test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy refs/heads/A 2>../error-me
) >/dev/null 2>error-me && ) &&
test_i18ncmp expect-error error-me test_i18ncmp expect-error error-me
' '

View File

@ -85,7 +85,7 @@ test_expect_success "fetch --recurse-submodules -j2 has the same output behaviou
add_upstream_commit && add_upstream_commit &&
( (
cd downstream && cd downstream &&
GIT_TRACE=$(pwd)/../trace.out git fetch --recurse-submodules -j2 2>../actual.err GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
) && ) &&
test_must_be_empty actual.out && test_must_be_empty actual.out &&
test_i18ncmp expect.err actual.err && test_i18ncmp expect.err actual.err &&

View File

@ -50,7 +50,7 @@ test_expect_success 'no-op fetch -v stderr is as expected' '
' '
test_expect_success 'no-op fetch without "-v" is quiet' ' test_expect_success 'no-op fetch without "-v" is quiet' '
(cd clone && git fetch) 2>stderr && (cd clone && git fetch 2>../stderr) &&
! test -s stderr ! test -s stderr
' '

View File

@ -735,22 +735,12 @@ test_expect_success 'prompt - hide if pwd ignored - env var set, config unset, p
test_cmp expected "$actual" test_cmp expected "$actual"
' '
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stdout)' ' test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
printf " (GIT_DIR!)" >expected && printf " (GIT_DIR!)" >expected &&
( (
GIT_PS1_HIDE_IF_PWD_IGNORED=y && GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
cd .git && cd .git &&
__git_ps1 >"$actual" 2>/dev/null __git_ps1 >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stderr)' '
printf "" >expected &&
(
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
cd .git &&
__git_ps1 >/dev/null 2>"$actual"
) && ) &&
test_cmp expected "$actual" test_cmp expected "$actual"
' '

View File

@ -629,7 +629,7 @@ test_must_fail () {
_test_ok= _test_ok=
;; ;;
esac esac
"$@" "$@" 2>&7
exit_code=$? exit_code=$?
if test $exit_code -eq 0 && ! list_contains "$_test_ok" success if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
then then
@ -652,7 +652,7 @@ test_must_fail () {
return 1 return 1
fi fi
return 0 return 0
} } 7>&2 2>&4
# Similar to test_must_fail, but tolerates success, too. This is # Similar to test_must_fail, but tolerates success, too. This is
# meant to be used in contexts like: # meant to be used in contexts like:
@ -668,8 +668,8 @@ test_must_fail () {
# Accepts the same options as test_must_fail. # Accepts the same options as test_must_fail.
test_might_fail () { test_might_fail () {
test_must_fail ok=success "$@" test_must_fail ok=success "$@" 2>&7
} } 7>&2 2>&4
# Similar to test_must_fail and test_might_fail, but check that a # Similar to test_must_fail and test_might_fail, but check that a
# given command exited with a given exit code. Meant to be used as: # given command exited with a given exit code. Meant to be used as:
@ -681,7 +681,7 @@ test_might_fail () {
test_expect_code () { test_expect_code () {
want_code=$1 want_code=$1
shift shift
"$@" "$@" 2>&7
exit_code=$? exit_code=$?
if test $exit_code = $want_code if test $exit_code = $want_code
then then
@ -690,7 +690,7 @@ test_expect_code () {
echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*" echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
return 1 return 1
} } 7>&2 2>&4
# test_cmp is a helper function to compare actual and expected output. # test_cmp is a helper function to compare actual and expected output.
# You can use it like: # You can use it like:
@ -896,8 +896,8 @@ test_write_lines () {
} }
perl () { perl () {
command "$PERL_PATH" "$@" command "$PERL_PATH" "$@" 2>&7
} } 7>&2 2>&4
# Is the value one of the various ways to spell a boolean true/false? # Is the value one of the various ways to spell a boolean true/false?
test_normalize_bool () { test_normalize_bool () {
@ -1037,13 +1037,13 @@ test_env () {
shift shift
;; ;;
*) *)
"$@" "$@" 2>&7
exit exit
;; ;;
esac esac
done done
) )
} } 7>&2 2>&4
# Returns true if the numeric exit code in "$2" represents the expected signal # Returns true if the numeric exit code in "$2" represents the expected signal
# in "$1". Signals should be given numerically. # in "$1". Signals should be given numerically.
@ -1085,9 +1085,9 @@ nongit () {
GIT_CEILING_DIRECTORIES=$(pwd) && GIT_CEILING_DIRECTORIES=$(pwd) &&
export GIT_CEILING_DIRECTORIES && export GIT_CEILING_DIRECTORIES &&
cd non-repo && cd non-repo &&
"$@" "$@" 2>&7
) )
} } 7>&2 2>&4
# convert stdin to pktline representation; note that empty input becomes an # convert stdin to pktline representation; note that empty input becomes an
# empty packet, not a flush packet (for that you can just print 0000 yourself). # empty packet, not a flush packet (for that you can just print 0000 yourself).

View File

@ -264,7 +264,24 @@ do
GIT_TEST_CHAIN_LINT=0 GIT_TEST_CHAIN_LINT=0
shift ;; shift ;;
-x) -x)
trace=t # Some test scripts can't be reliably traced with '-x',
# unless the test is run with a Bash version supporting
# BASH_XTRACEFD (introduced in Bash v4.1). Check whether
# this test is marked as such, and ignore '-x' if it
# isn't executed with a suitable Bash version.
if test -z "$test_untraceable" || {
test -n "$BASH_VERSION" && {
test ${BASH_VERSINFO[0]} -gt 4 || {
test ${BASH_VERSINFO[0]} -eq 4 &&
test ${BASH_VERSINFO[1]} -ge 1
}
}
}
then
trace=t
else
echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
fi
shift ;; shift ;;
--verbose-log) --verbose-log)
verbose_log=t verbose_log=t