From 4480010d4a46498097c8936bcbe46e99b21d3ed2 Mon Sep 17 00:00:00 2001 From: Magnus Malm Date: Mon, 22 Feb 2021 23:43:15 +0100 Subject: [PATCH] test: Add support for teardown in tests If a test defines a function 'test_teardown(), that function will be called before the test env teardown is run. Signed-off-by: Magnus Malm --- test/lib.sh | 5 +++++ test/test_start_stop_service/test.sh | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/lib.sh b/test/lib.sh index afa2d6f1..953525a5 100755 --- a/test/lib.sh +++ b/test/lib.sh @@ -69,6 +69,11 @@ say() { teardown() { test_status="$?" + + if type test_teardown > /dev/null 2>&1 ; then + test_teardown + fi + log "$color_reset" '--' '' if [ "$test_status" -eq 0 ]; then log "$fg_green" 'TEST PASS' '' diff --git a/test/test_start_stop_service/test.sh b/test/test_start_stop_service/test.sh index d4146e4a..ae5fe8ff 100755 --- a/test/test_start_stop_service/test.sh +++ b/test/test_start_stop_service/test.sh @@ -10,6 +10,14 @@ assert_num_children() { assert "$1 services are running" "$(texec pgrep -P 1 "$2" | wc -l)" -eq "$1" } +test_teardown() { + say "Test done $(date)" + say "Running test teardown..." + + # Teardown + rm -f test_root/bin/service.sh +} + # Test say 'Set up a service' cp "$TEST_DIR"/test_start_stop_service/service.sh "$TEST_DIR"/test_root/test_assets/ @@ -27,6 +35,3 @@ say 'Reload Finit' texec kill -SIGHUP 1 retry 'assert_num_children 0 service.sh' - -# Teardown -rm -f test_root/bin/service.sh