Skip to content

Commit

Permalink
Merge pull request felipec#12 from seveas/cleanup-function
Browse files Browse the repository at this point in the history
Add a 'cleanup' api to register cleanup actions
  • Loading branch information
mlafeldt committed Oct 1, 2014
2 parents 4d23fc5 + a4d6a99 commit 50229a7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@

Returns 0 if all tests passed or 1 if there was a failure.

### cleanup()

Public: Schedule cleanup commands to be run unconditionally when all tests
have run.

This can be used to clean up things like test databases. It is not needed to
clean up temporary files, as test_done already does that.

Examples:

cleanup mysql -e "DROP DATABASE mytest"

Returns the exit code of the last cleanup command executed.

### SHARNESS_TEST_DIRECTORY

Public: Root directory containing tests. Tests can override this variable,
Expand Down
19 changes: 19 additions & 0 deletions sharness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,23 @@ test_when_finished() {
} && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
}

# Public: Schedule cleanup commands to be run unconditionally when all tests
# have run.
#
# This can be used to clean up things like test databases. It is not needed to
# clean up temporary files, as test_done already does that.
#
# Examples:
#
# cleanup mysql -e "DROP DATABASE mytest"
#
# Returns the exit code of the last cleanup command executed.
final_cleanup=
cleanup() {
final_cleanup="{ $*
} && (exit \"\$eval_ret\"); eval_ret=\$?; $final_cleanup"
}

# Public: Summarize test results and exit with an appropriate error code.
#
# Must be called at the end of each test script.
Expand Down Expand Up @@ -641,6 +658,8 @@ test_done() {
fi
say "1..$test_count$skip_all"

test_eval_ "$final_cleanup"

test -d "$remove_trash" &&
cd "$(dirname "$remove_trash")" &&
rm -rf "$(basename "$remove_trash")"
Expand Down
11 changes: 11 additions & 0 deletions test/sharness.t
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,17 @@ test_expect_success 'tests clean up even on failures' "
EOF
"

test_expect_success 'cleanup functions tun at the end of the test' "
run_sub_test_lib_test cleanup-function 'Empty test with cleanup function' <<-\\EOF &&
cleanup 'echo cleanup-function-called >&5'
test_done
EOF
check_sub_test_lib_test cleanup-function <<-\\EOF
1..0
cleanup-function-called
EOF
"

test_done

# vi: set ft=sh :

0 comments on commit 50229a7

Please sign in to comment.