From 55a1ad42bd172aa235015b88c092a225ec7f4862 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 20 Oct 2022 13:21:41 -0400 Subject: tests: Add a helper for cleaning scratch And try to unmount things if the a test left them mounted. --- tests/tests.sh | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'tests/tests.sh') diff --git a/tests/tests.sh b/tests/tests.sh index f6fe506..3a2446b 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -270,6 +270,26 @@ if [ ! "$EXPLICIT" ]; then enabled_tests=("${enabled_tests[@]%.sh}") fi +function clean_scratch() { + if [ -e "$TMP/scratch" ]; then + # Try to unmount anything left behind + if [ "$SUDO" ] && command -v mountpoint &>/dev/null; then + for path in "$TMP"/scratch/*; do + if mountpoint -q "$path"; then + sudo umount "$path" + fi + done + fi + + # Reset any modified permissions + chmod -R +rX "$TMP/scratch" + + rm -rf "$TMP/scratch" + fi + + mkdir "$TMP/scratch" +} + # Clean up temporary directories on exit function cleanup() { # Don't force rm to deal with long paths @@ -280,9 +300,7 @@ function cleanup() { done # In case a test left anything weird in scratch/ - if [ -e "$TMP"/scratch ]; then - chmod -R +rX "$TMP"/scratch - fi + clean_scratch rm -rf "$TMP" } @@ -451,12 +469,6 @@ function make_rainbow() { } make_rainbow "$TMP/rainbow" -# Creates a scratch directory that tests can modify -function make_scratch() { - mkdir -p "$1" -} -make_scratch "$TMP/scratch" - # Close stdin so bfs doesn't think we're interactive exec