From 900721e15c43742955878dbbdb24c5a2ce262630 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 13 Mar 2019 13:24:11 +0100 Subject: [PATCH] test-lib: introduce 'test_atexit' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running Apache, 'git daemon', or p4d, we want to kill them at the end of the test script, otherwise a leftover daemon process will keep its port open indefinitely, and thus will interfere with subsequent executions of the same test script. So far, we stop these daemon processes "manually", i.e.: - by registering functions or commands in the trap on EXIT to stop the daemon while preserving the last seen exit code before the trap (to deal with a failure when run with '--immediate' or with interrupts by ctrl-C), - and by invoking these functions/commands last thing before 'test_done' (and sometimes restoring the test framework's default trap on EXIT, to prevent the daemons from being killed twice). On one hand, we do this inconsistently, e.g. 'git p4' tests invoke different functions in the trap on EXIT and in the last test before 'test_done', and they neither restore the test framework's default trap on EXIT nor preserve the last seen exit code. On the other hand, this is error prone, because, as shown in a previous patch in this series, any output from the cleanup commands in the trap on EXIT can prevent a proper cleanup when a test script run with '--verbose-log' and certain shells, notably 'dash', is interrupted. Let's introduce 'test_atexit', which is loosely modeled after 'test_when_finished', but has a broader scope: rather than running the commands after the current test case, run them when the test script finishes, and also run them when the test is interrupted, or exits early in case of a failure while the '--immediate' option is in effect. When running the cleanup commands at the end of a successful test, then they will be run in 'test_done' before it removes the trash directory, i.e. the cleanup commands will still be able to access any pidfiles or socket files in there. When running the cleanup commands after an interrupt or failure with '--immediate', then they will be run in the trap on EXIT. In both cases they will be run in 'test_eval_', i.e. both standard error and output of all cleanup commands will go where they should according to the '-v' or '--verbose-log' options, and thus won't cause any troubles when interrupting a test script run with '--verbose-log'. Signed-off-by: Johannes Schindelin Signed-off-by: SZEDER Gábor Signed-off-by: Junio C Hamano --- t/README | 20 ++++++++++++++++++++ t/t0000-basic.sh | 18 ++++++++++++++++++ t/test-lib-functions.sh | 28 ++++++++++++++++++++++++++++ t/test-lib.sh | 23 +++++++++++++++++++++++ 4 files changed, 89 insertions(+) diff --git a/t/README b/t/README index 7a3d582267..0b03175c7d 100644 --- a/t/README +++ b/t/README @@ -862,6 +862,26 @@ library for your script to use. ... ' + - test_atexit