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/bfs/L_capable.sh | 2 +- tests/bfs/capable.sh | 2 +- tests/bfs/color_ls.sh | 2 +- tests/bfs/exec_flush_fprint.sh | 2 +- tests/bsd/L_acl.sh | 2 +- tests/bsd/acl.sh | 2 +- tests/bsd/flags.sh | 2 +- tests/bsd/rm.sh | 2 +- tests/common/L_mount.sh | 2 +- tests/common/delete.sh | 2 +- tests/common/delete_many.sh | 2 +- tests/common/execdir_ulimit.sh | 2 +- tests/common/inum_bind_mount.sh | 2 +- tests/common/inum_mount.sh | 2 +- tests/common/mount.sh | 2 +- tests/gnu/L_delete.sh | 2 +- tests/gnu/ignore_readdir_race.sh | 2 +- tests/gnu/ignore_readdir_race_notdir.sh | 2 +- tests/gnu/inum_automount.sh | 2 +- tests/gnu/printf_Y_error.sh | 4 ++-- tests/gnu/regex_invalid_utf8.sh | 2 +- tests/gnu/xtype_bind_mount.sh | 2 +- tests/posix/L_xdev.sh | 2 +- tests/posix/depth_error.sh | 4 ++-- tests/posix/type_bind_mount.sh | 2 +- tests/posix/xdev.sh | 2 +- tests/tests.sh | 32 ++++++++++++++++++++++---------- 27 files changed, 50 insertions(+), 38 deletions(-) diff --git a/tests/bfs/L_capable.sh b/tests/bfs/L_capable.sh index e1806b7..f37e1f2 100644 --- a/tests/bfs/L_capable.sh +++ b/tests/bfs/L_capable.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_unless test "$UNAME" = "Linux" -rm -rf scratch/* +clean_scratch skip_unless invoke_bfs scratch -quit -capable diff --git a/tests/bfs/capable.sh b/tests/bfs/capable.sh index 964f37f..6dc9bf2 100644 --- a/tests/bfs/capable.sh +++ b/tests/bfs/capable.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_unless test "$UNAME" = "Linux" -rm -rf scratch/* +clean_scratch skip_unless invoke_bfs scratch -quit -capable diff --git a/tests/bfs/color_ls.sh b/tests/bfs/color_ls.sh index 79e1db2..33922a8 100644 --- a/tests/bfs/color_ls.sh +++ b/tests/bfs/color_ls.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch touchp scratch/foo/bar/baz ln -s foo/bar/baz scratch/link ln -s foo/bar/nowhere scratch/broken diff --git a/tests/bfs/exec_flush_fprint.sh b/tests/bfs/exec_flush_fprint.sh index 9756b6a..bf6b62f 100644 --- a/tests/bfs/exec_flush_fprint.sh +++ b/tests/bfs/exec_flush_fprint.sh @@ -1,3 +1,3 @@ # Even non-stdstreams should be flushed -rm -rf scratch/* +clean_scratch bfs_diff basic/a -fprint scratch/foo -exec cat scratch/foo \; diff --git a/tests/bsd/L_acl.sh b/tests/bsd/L_acl.sh index d8f5a7d..4f990bb 100644 --- a/tests/bsd/L_acl.sh +++ b/tests/bsd/L_acl.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch skip_unless invoke_bfs scratch -quit -acl diff --git a/tests/bsd/acl.sh b/tests/bsd/acl.sh index 5bbfccc..c669005 100644 --- a/tests/bsd/acl.sh +++ b/tests/bsd/acl.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch skip_unless invoke_bfs scratch -quit -acl diff --git a/tests/bsd/flags.sh b/tests/bsd/flags.sh index 99d0fdb..fd25150 100644 --- a/tests/bsd/flags.sh +++ b/tests/bsd/flags.sh @@ -1,6 +1,6 @@ skip_unless invoke_bfs scratch -quit -flags offline -rm -rf scratch/* +clean_scratch $TOUCH scratch/{foo,bar} skip_unless chflags offline scratch/bar diff --git a/tests/bsd/rm.sh b/tests/bsd/rm.sh index 2e8b613..e661a37 100644 --- a/tests/bsd/rm.sh +++ b/tests/bsd/rm.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch touchp scratch/foo/bar/baz (cd scratch && invoke_bfs . -rm) diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh index d43f95a..d9e1dec 100644 --- a/tests/common/L_mount.sh +++ b/tests/common/L_mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_if test "$UNAME" = "Darwin" -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} sudo mount -t tmpfs tmpfs scratch/mnt ln -s ../mnt scratch/foo/bar diff --git a/tests/common/delete.sh b/tests/common/delete.sh index 4855d60..3709897 100644 --- a/tests/common/delete.sh +++ b/tests/common/delete.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch touchp scratch/foo/bar/baz # Don't try to delete '.' diff --git a/tests/common/delete_many.sh b/tests/common/delete_many.sh index 6446f93..6f54825 100644 --- a/tests/common/delete_many.sh +++ b/tests/common/delete_many.sh @@ -1,6 +1,6 @@ # Test for https://github.com/tavianator/bfs/issues/67 -rm -rf scratch/* +clean_scratch mkdir scratch/foo $TOUCH scratch/foo/{1..256} diff --git a/tests/common/execdir_ulimit.sh b/tests/common/execdir_ulimit.sh index 6da8783..8bd9edd 100644 --- a/tests/common/execdir_ulimit.sh +++ b/tests/common/execdir_ulimit.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch mkdir -p scratch/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z mkdir -p scratch/a/b/c/d/e/f/g/h/i/j/k/l/m/0/1/2/3/4/5/6/7/8/9/A/B/C diff --git a/tests/common/inum_bind_mount.sh b/tests/common/inum_bind_mount.sh index 654e762..8b1ac0e 100644 --- a/tests/common/inum_bind_mount.sh +++ b/tests/common/inum_bind_mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_unless test "$UNAME" = "Linux" -rm -rf scratch/* +clean_scratch $TOUCH scratch/{foo,bar} sudo mount --bind scratch/{foo,bar} diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh index 8f5c317..f9f4e2b 100644 --- a/tests/common/inum_mount.sh +++ b/tests/common/inum_mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_if test "$UNAME" = "Darwin" -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} sudo mount -t tmpfs tmpfs scratch/mnt diff --git a/tests/common/mount.sh b/tests/common/mount.sh index 6e20d90..6131ad4 100644 --- a/tests/common/mount.sh +++ b/tests/common/mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_if test "$UNAME" = "Darwin" -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} sudo mount -t tmpfs tmpfs scratch/mnt $TOUCH scratch/foo/bar scratch/mnt/baz diff --git a/tests/gnu/L_delete.sh b/tests/gnu/L_delete.sh index 08f39af..6ec167c 100644 --- a/tests/gnu/L_delete.sh +++ b/tests/gnu/L_delete.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch mkdir scratch/foo mkdir scratch/bar ln -s ../foo scratch/bar/baz diff --git a/tests/gnu/ignore_readdir_race.sh b/tests/gnu/ignore_readdir_race.sh index 429933c..547253b 100644 --- a/tests/gnu/ignore_readdir_race.sh +++ b/tests/gnu/ignore_readdir_race.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch $TOUCH scratch/{foo,bar} # -links 1 forces a stat() call, which will fail for the second file diff --git a/tests/gnu/ignore_readdir_race_notdir.sh b/tests/gnu/ignore_readdir_race_notdir.sh index 0c3ff5d..91a383b 100644 --- a/tests/gnu/ignore_readdir_race_notdir.sh +++ b/tests/gnu/ignore_readdir_race_notdir.sh @@ -1,5 +1,5 @@ # Check -ignore_readdir_race handling when a directory is replaced with a file -rm -rf scratch/* +clean_scratch touchp scratch/foo/bar invoke_bfs scratch -mindepth 1 -ignore_readdir_race -execdir rm -r {} \; -execdir $TOUCH {} \; diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh index 0149043..82f85d2 100644 --- a/tests/gnu/inum_automount.sh +++ b/tests/gnu/inum_automount.sh @@ -3,7 +3,7 @@ skip_unless test "$SUDO" skip_unless command -v systemd-mount &>/dev/null -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} skip_unless sudo systemd-mount -A -o bind basic scratch/mnt diff --git a/tests/gnu/printf_Y_error.sh b/tests/gnu/printf_Y_error.sh index 933c734..6487711 100644 --- a/tests/gnu/printf_Y_error.sh +++ b/tests/gnu/printf_Y_error.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch mkdir scratch/foo chmod -x scratch/foo ln -s foo/bar scratch/bar @@ -7,6 +7,6 @@ bfs_diff scratch -printf '(%p) (%l) %y %Y\n' ret=$? chmod +x scratch/foo -rm -rf scratch/* +clean_scratch [ $ret -eq $EX_BFS ] diff --git a/tests/gnu/regex_invalid_utf8.sh b/tests/gnu/regex_invalid_utf8.sh index 85f1897..edb4b1e 100644 --- a/tests/gnu/regex_invalid_utf8.sh +++ b/tests/gnu/regex_invalid_utf8.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch # Incomplete UTF-8 sequences skip_unless touch scratch/$'\xC3' diff --git a/tests/gnu/xtype_bind_mount.sh b/tests/gnu/xtype_bind_mount.sh index 56f9c5c..d371cd5 100644 --- a/tests/gnu/xtype_bind_mount.sh +++ b/tests/gnu/xtype_bind_mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_unless test "$UNAME" = "Linux" -rm -rf scratch/* +clean_scratch $TOUCH scratch/{file,null} sudo mount --bind /dev/null scratch/null ln -s /dev/null scratch/link diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh index f0ba120..c18a39e 100644 --- a/tests/posix/L_xdev.sh +++ b/tests/posix/L_xdev.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_if test "$UNAME" = "Darwin" -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} sudo mount -t tmpfs tmpfs scratch/mnt ln -s ../mnt scratch/foo/bar diff --git a/tests/posix/depth_error.sh b/tests/posix/depth_error.sh index 9acf976..e1267f4 100644 --- a/tests/posix/depth_error.sh +++ b/tests/posix/depth_error.sh @@ -1,4 +1,4 @@ -rm -rf scratch/* +clean_scratch touchp scratch/foo/bar chmod a-r scratch/foo @@ -6,6 +6,6 @@ bfs_diff scratch -depth ret=$? chmod +r scratch/foo -rm -rf scratch/* +clean_scratch [ $ret -eq $EX_BFS ] diff --git a/tests/posix/type_bind_mount.sh b/tests/posix/type_bind_mount.sh index 87b1549..e05baa3 100644 --- a/tests/posix/type_bind_mount.sh +++ b/tests/posix/type_bind_mount.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_unless test "$UNAME" = "Linux" -rm -rf scratch/* +clean_scratch $TOUCH scratch/{file,null} sudo mount --bind /dev/null scratch/null diff --git a/tests/posix/xdev.sh b/tests/posix/xdev.sh index cbb3347..7c26c3f 100644 --- a/tests/posix/xdev.sh +++ b/tests/posix/xdev.sh @@ -1,7 +1,7 @@ skip_unless test "$SUDO" skip_if test "$UNAME" = "Darwin" -rm -rf scratch/* +clean_scratch mkdir scratch/{foo,mnt} sudo mount -t tmpfs tmpfs scratch/mnt $TOUCH scratch/foo/bar scratch/mnt/baz 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