Skip to content
  • Gábor Szeder's avatar
    t: prevent '-x' tracing from interfering with test helpers' stderr · a5bf824f
    Gábor Szeder authored and Junio C Hamano's avatar Junio C Hamano committed
    Running a test script with '-x' turns on 'set -x' tracing, the output
    of which is normally sent to stderr.  This causes a lot of
    test failures, because many tests redirect and verify the stderr
    of shell functions, most frequently that of 'test_must_fail'.
    These issues were worked around somewhat in d88785e4
    
     (test-lib: set
    BASH_XTRACEFD automatically, 2016-05-11), so at least we could
    reliably run tests with '-x' tracing under a Bash version supporting
    BASH_XTRACEFD, i.e. v4.1 and later.
    
    Futhermore, redirecting the stderr of test helper functions like
    'test_must_fail' or 'test_expect_code' is the cause of a different
    issue as well.  If these functions detect something unexpected, they
    will write their error messages intended to the user to thier stderr.
    However, if their stderr is redirected in order to save and verify the
    stderr of the tested git command invoked in the function, then the
    function's error messages will be redirected as well.  Consequently,
    those messages won't reach the user, making the test's verbose output
    less useful.
    
    This patch makes it safe to redirect and verify the stderr of those
    test helper functions which are meant to run the tested command given
    as argument, even when running tests with '-x' and /bin/sh.  This is
    achieved through a couple of file descriptor redirections:
    
      - Duplicate stderr of the tested command executed in the test helper
        function from the function's fd 7 (see next point), to ensure that
        the tested command's error messages go to a different fd than the
        '-x' trace of the commands executed in the function or the
        function's error messages.
    
      - Duplicate the test helper function's fd 7 from the function's
        original stderr, meaning that, after taking a detour through fd 7,
        the error messages of the tested command do end up on the
        function's original stderr.
    
      - Duplicate stderr of the test helper function from fd 4, i.e. the
        fd connected to the test script's original stderr and the fd used
        for BASH_XTRACEFD.  This ensures that the '-x' trace of the
        commands executed in the function
    
          - doesn't go to the function's original stderr, so it won't mess
    	with callers who want to save and verify the tested command's
    	stderr.
    
          - does go to the same fd independently from the shell running
            the test script, be it /bin/sh, an older Bash without
            BASH_XTRACEFD, or a more recent Bash already supporting
            BASH_XTRACEFD.
    
        Furthermore, this also makes sure that the function's error
        messages go to this fd 4, meaning that the user will be able to
        see them even if the function's stderr is redirected in the test.
    
      - Specify the latter two redirections above in the test helper
        function's definition, so they are performed every time the
        function is invoked, without the need to modify the callsites of
        the function.
    
    Perform these redirections in those test helper functions which can be
    expected to have their stderr redirected, i.e. in the functions
    'test_must_fail', 'test_might_fail', 'test_expect_code', 'test_env',
    'nongit', 'test_terminal' and 'perl'.  Note that 'test_might_fail',
    'test_env', and 'nongit' are not involved in any test failures when
    running tests with '-x' and /bin/sh.
    
    The other test helper functions are left unchanged, because they
    either don't run commands specified as their arguments, or redirecting
    their stderr wouldn't make sense, or both.
    
    With this change the number of failures when running the test suite
    with '-x' tracing and /bin/sh goes down from 340 failed tests in 43
    test scripts to 22 failed tests in 6 scripts (or 23 in 7, if the
    system (OSX) uses an older Bash version without BASH_XTRACEFD to run
    't9903-bash-prompt.sh').
    
    Signed-off-by: default avatarSZEDER Gábor <szeder.dev@gmail.com>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    a5bf824f