Since trap handlers don't nest tests that install their own EXIT (0) trap handler need to cleanup themselves. Add an exit_cleanup helper function to test-subr.sh that can be used by run-native-test.sh to exit cleanly.
Signed-off-by: Mark Wielaard mjw@redhat.com --- tests/ChangeLog | 7 +++++++ tests/run-native-test.sh | 11 +++++++++-- tests/test-subr.sh | 10 ++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/tests/ChangeLog b/tests/ChangeLog index dfee832..1e80d7a 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,5 +1,12 @@ 2013-04-27 Mark Wielaard mjw@redhat.com
+ * test-subr.sh (exit_cleanup): New function. + (trap): Use exit_cleanup as argument. + * run-native-test.sh (native_exit): New function. + (trap): For EXIT (0) use native_exit as argument. + +2013-04-27 Mark Wielaard mjw@redhat.com + * run-alldts.sh: Add testfile-alldts to tempfiles. * run-elf_cntl_gelf_getshdr.sh: Add test_shdr.out to tempfiles. * run-macro-test.sh: Add readelf.macros.out to tempfiles. diff --git a/tests/run-native-test.sh b/tests/run-native-test.sh index b543922..d19007f 100755 --- a/tests/run-native-test.sh +++ b/tests/run-native-test.sh @@ -1,5 +1,5 @@ #! /bin/sh -# Copyright (C) 2005, 2006 Red Hat, Inc. +# Copyright (C) 2005, 2006, 2013 Red Hat, Inc. # This file is part of elfutils. # # This file is free software; you can redistribute it and/or modify @@ -45,7 +45,14 @@ native_cleanup() test_cleanup }
-trap native_cleanup 0 1 2 15 +native_exit() +{ + native_cleanup + exit_cleanup +} + +trap native_cleanup 1 2 15 +trap native_exit 0
for cc in "$HOSTCC" "$HOST_CC" cc gcc "$CC"; do test "x$cc" != x || continue diff --git a/tests/test-subr.sh b/tests/test-subr.sh index 7bf1f71..3ef218c 100644 --- a/tests/test-subr.sh +++ b/tests/test-subr.sh @@ -1,5 +1,5 @@ #! /bin/sh -# Copyright (C) 2005-2012 Red Hat, Inc. +# Copyright (C) 2005-2013 Red Hat, Inc. # This file is part of elfutils. # # This file is free software; you can redistribute it and/or modify @@ -31,7 +31,13 @@ pushd "$test_dir" > /dev/null #export LC_ALL
remove_files= -trap 'rm -f $remove_files; popd > /dev/null; rmdir $test_dir' 0 + +# Tests that trap EXIT (0) themselves should call this explicitly. +exit_cleanup() +{ + rm -f $remove_files; popd > /dev/null; rmdir $test_dir +} +trap exit_cleanup 0
tempfiles() {
Looks fine. But separately, what the hell is the popd about? Scripts should never use pushd/popd.
On Mon, 2013-04-29 at 09:35 -0700, Roland McGrath wrote:
Looks fine.
Thanks, pushed.
But separately, what the hell is the popd about? Scripts should never use pushd/popd.
They shouldn't? It comes from commit 86be79 "Enable automake parallel-tests for make check."
[...] To enable this all tests are ran in their own temporary directory so they don't stomp on each others input or output files. [...]
I dunno why I thought pushd and popd would be a good idea. A simple cd to change the working directory instead works fine if that is better:
diff --git a/tests/test-subr.sh b/tests/test-subr.sh index 3ef218c..b5ab105 100644 --- a/tests/test-subr.sh +++ b/tests/test-subr.sh @@ -25,7 +25,7 @@ set -e # Each test runs in its own directory to make sure they can run in parallel. test_dir="test-$$" mkdir -p "$test_dir" -pushd "$test_dir" > /dev/null +cd "$test_dir"
#LC_ALL=C #export LC_ALL @@ -35,7 +35,7 @@ remove_files= # Tests that trap EXIT (0) themselves should call this explicitly. exit_cleanup() { - rm -f $remove_files; popd > /dev/null; rmdir $test_dir + rm -f $remove_files; cd ..; rmdir $test_dir } trap exit_cleanup 0
They shouldn't? It comes from commit 86be79 "Enable automake parallel-tests for make check."
Sorry I let that slip by. Those are bash extensions and so they should not be used in a script with #!/bin/sh. But moreover, they are commands that exist solely for interactive use and in scripts you can use more straightforward methods (like var=`pwd` and cd "$var").
I dunno why I thought pushd and popd would be a good idea. A simple cd to change the working directory instead works fine if that is better:
That's fine.
elfutils-devel@lists.fedorahosted.org