From: "SZEDER Gábor" <szeder.dev@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Jeff King <peff@peff.net>, Luke Diamand <luke@diamand.org>,
Lars Schneider <larsxschneider@gmail.com>
Subject: Re: [PATCH 03/11] test-lib: introduce 'test_atexit'
Date: Thu, 14 Mar 2019 18:40:24 +0100 [thread overview]
Message-ID: <20190314174024.GH28939@szeder.dev> (raw)
In-Reply-To: <xmqqh8c6b0pv.fsf@gitster-ct.c.googlers.com>
On Thu, Mar 14, 2019 at 12:21:00PM +0900, Junio C Hamano wrote:
> SZEDER Gábor <szeder.dev@gmail.com> writes:
>
> > +test_atexit () {
> > + # We cannot detect when we are in a subshell in general, but by
> > + # doing so on Bash is better than nothing (the test will
> > + # silently pass on other shells).
> > + test "${BASH_SUBSHELL-0}" = 0 ||
> > + error "bug in test script: test_atexit does nothing in a subshell"
> > + test_atexit_cleanup="{ $*
> > + } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
> > +}
>
> This chaining is modelled after how $test_cleaup is built and
> maintained by test_when_finished. Use of eval_ret makes sense in
> that original context as eval_ret _is_ used to keep track of the
> result of 'test_eval_ "$1"' in test_run_ that executed the body
> of a single test_expect_$something, and $test_cleanup would want to
> keep the resulting status from that body when clean-up succeeds (or
> otherwise, make that failure to clean-up visible as $eval_ret).
>
> But does it make sense in the context of the whole test script to
> try maintaining $eval_ret?
Right, it doesn't, as 'die' preserves the last seen exit code, and any
exit codes from the atexit commands are ignored anyway.
> > # Most tests can use the created repository, but some may need to create more.
> > # Usage: test_create_repo <directory>
> > test_create_repo () {
> > diff --git a/t/test-lib.sh b/t/test-lib.sh
> > index db3875d1e4..b35881696f 100644
> > --- a/t/test-lib.sh
> > +++ b/t/test-lib.sh
> > @@ -620,6 +620,10 @@ test_external_has_tap=0
> >
> > die () {
> > code=$?
> > + # This is responsible for running the atexit commands even when a
> > + # test script run with '--immediate' fails, or when the user hits
> > + # ctrl-C, i.e. when 'test_done' is not invoked at all.
> > + test_atexit_handler || code=$?
> > if test -n "$GIT_EXIT_OK"
> > then
> > exit $code
> > @@ -1045,9 +1049,28 @@ write_junit_xml_testcase () {
> > junit_have_testcase=t
> > }
> >
> > +test_atexit_cleanup=:
> > +test_atexit_handler () {
> > + # In a succeeding test script 'test_atexit_handler' is invoked
> > + # twice: first from 'test_done', then from 'die' in the trap on
> > + # EXIT.
>
> We are guaranteed to still have the trash directory when we are run
> in the exit handler after getting interrupted or test_failure_()
> exits under the "-i" option, and when test_done() calls us. What
> will cause us trouble is the exit handler at the end of a successful
> run after test_done() finishes. At that point, test_done would have
> already cleared the trash directory, so we may not have enough state
> to allow us to clean-up at exit.
>
> Clearing the exit trap in test_done after it calls us might be an
> alternative, but I think it is equivalent to clearing the
> test_atexit_cleanup variable, and it is cleaner, so I think I agree
> with the approach this patch uses.
>
> > + # This condition and resetting 'test_atexit_cleanup' below makes
> > + # sure that the registered cleanup commands are run only once.
> > + test : != "$test_atexit_cleanup" || return 0
>
> I think test_when_finished uses a special value in $test_cleanup in
> a similar way
That's right.
> but it even skips when there is no point doing the
> test_eval_ of the "accumulated" scriptlet when it is empty.
But this is not, because $test_cleanup is initialized to this special
value and it can never be empty, and indeed 'test_eval_' uses this
condition:
if test -z "$immediate" || test $eval_ret = 0 ||
test -n "$expecting_failure" && test "$test_cleanup" != ":"
and it never checks $test_cleanup's emptiness.
> Shouldn't we be doing the same thing, i.e.
>
> if test -z "$test_atexit_cleanup"
> then
> return 0
> fi
> ... do the heavy lifting ...
> test_atexit_cleanup=
>
> That will make the handler truly a no-op when there is no atexit
> defined.
$test_atexit_cleanup is used the same way as $test_cleanup; it's
initialized to the same special value and can never be empty, so there
is no need to check for its emptiness either.
> > + setup_malloc_check
> > + test_eval_ "$test_atexit_cleanup"
> > + test_atexit_cleanup=:
> > + teardown_malloc_check
> > +}
> > +
> > test_done () {
> > GIT_EXIT_OK=t
> >
> > + # Run the atexit commands _before_ the trash directory is
> > + # removed, so the commands can access pidfiles and socket files.
> > + test_atexit_handler
> > +
> > if test -n "$write_junit_xml" && test -n "$junit_xml_path"
> > then
> > test -n "$junit_have_testcase" || {
next prev parent reply other threads:[~2019-03-14 17:40 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-13 12:24 [PATCH 00/11] tests: introduce 'test_atexit' SZEDER Gábor
2019-03-13 12:24 ` [PATCH 01/11] test-lib: fix interrupt handling with 'dash' and '--verbose-log -x' SZEDER Gábor
2019-03-14 2:18 ` Junio C Hamano
2019-03-14 14:50 ` SZEDER Gábor
2019-03-13 12:24 ` [PATCH 02/11] t/lib-git-daemon: make sure to kill the 'git-daemon' process SZEDER Gábor
2019-03-14 2:35 ` Junio C Hamano
2019-03-13 12:24 ` [PATCH 03/11] test-lib: introduce 'test_atexit' SZEDER Gábor
2019-03-14 3:21 ` Junio C Hamano
2019-03-14 17:40 ` SZEDER Gábor [this message]
2019-03-18 1:50 ` Junio C Hamano
2019-03-13 12:24 ` [PATCH 04/11] git-daemon: use 'test_atexit` to stop 'git-daemon' SZEDER Gábor
2019-03-13 12:24 ` [PATCH 05/11] tests: use 'test_atexit' to stop httpd SZEDER Gábor
2019-03-14 3:28 ` Junio C Hamano
2019-03-14 4:34 ` Junio C Hamano
2019-03-14 15:19 ` SZEDER Gábor
2019-03-13 12:24 ` [PATCH 06/11] t0301-credential-cache: use 'test_atexit' to stop the credentials helper SZEDER Gábor
2019-03-13 12:24 ` [PATCH 07/11] git p4 test: use 'test_atexit' to kill p4d and the watchdog process SZEDER Gábor
2019-03-13 12:24 ` [PATCH 08/11] git p4 test: clean up the p4d cleanup functions SZEDER Gábor
2019-03-13 12:24 ` [PATCH 09/11] git p4 test: simplify timeout handling SZEDER Gábor
2019-03-13 12:24 ` [PATCH 10/11] git p4 test: disable '-x' tracing in the p4d watchdog loop SZEDER Gábor
2019-03-13 12:24 ` [PATCH 11/11] t9811-git-p4-label-import: fix pipeline negation SZEDER Gábor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190314174024.GH28939@szeder.dev \
--to=szeder.dev@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=larsxschneider@gmail.com \
--cc=luke@diamand.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).