[Libguestfs] [PATCH v2 nbdkit 4/5] tests: Use a generic cleanup mechanism instead of explicit trap.
Eric Blake
eblake at redhat.com
Thu Sep 13 18:00:40 UTC 2018
On 9/13/18 11:09 AM, Richard W.M. Jones wrote:
> Thanks: Eric Blake for the suggestion here:
> https://www.redhat.com/archives/libguestfs/2018-September/msg00069.html
> ---
I'm glad the idea worked!
> +# cleanup_fn f [args]
> +#
> +# A generic trap handling function. This runs the function f + args
Not just functions, but any command name...
> +# when the script exits for any reason.
> +declare -a _cleanup_hook
> +cleanup_fn ()
> +{
> + _cleanup_hook[${#_cleanup_hook[@]}]="$@"
> +}
> +++ b/tests/test-zero.sh
> @@ -31,6 +31,7 @@
> # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
> # SUCH DAMAGE.
>
> +source ./functions.sh
> set -x
> set -e
>
> @@ -42,18 +43,7 @@ fi
>
> files="test-zero.out"
> rm -f $files
> -
> -cleanup ()
> -{
> - status=$?
> - trap '' INT QUIT TERM EXIT ERR
> - echo $0: cleanup: exit code $status
> -
> - rm $files
> -
> - exit $status
> -}
> -trap cleanup INT QUIT TERM EXIT ERR
> +cleanup_fn rm $files
...as evidenced by this conversion. Pre-existing, but you could fix it
while here: why is this not using 'rm -f' like all the other cleanups?
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
More information about the Libguestfs
mailing list