summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-10-19 09:31:33 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-10-19 09:31:33 -0400
commitc78e5ddfc8aa0a6373b063dd787f416e9de8002e (patch)
treee0f9ce4ee5ef0a7a39aaf7388eda5290ebc95a06 /tests
parentb1c6199ad82de2081f18eba931a787a25eb27ea7 (diff)
downloadbfs-c78e5ddfc8aa0a6373b063dd787f416e9de8002e.tar.xz
tests: New defer function
Diffstat (limited to 'tests')
-rw-r--r--tests/bsd/type_w.sh20
-rw-r--r--tests/common/L_mount.sh2
-rw-r--r--tests/common/inum_bind_mount.sh2
-rw-r--r--tests/common/inum_mount.sh2
-rw-r--r--tests/common/mount.sh2
-rw-r--r--tests/gnu/fstype_stacked.sh4
-rw-r--r--tests/gnu/fstype_umount.sh2
-rw-r--r--tests/gnu/inum_automount.sh2
-rw-r--r--tests/gnu/printf_Y_error.sh2
-rw-r--r--tests/gnu/xtype_bind_mount.sh2
-rw-r--r--tests/posix/L_xdev.sh2
-rw-r--r--tests/posix/depth_error.sh2
-rw-r--r--tests/posix/overlayfs.sh6
-rw-r--r--tests/posix/readdir_error.sh2
-rw-r--r--tests/posix/type_bind_mount.sh2
-rw-r--r--tests/posix/unionfs.sh2
-rw-r--r--tests/posix/xdev.sh2
-rwxr-xr-xtests/tests.sh30
18 files changed, 54 insertions, 34 deletions
diff --git a/tests/bsd/type_w.sh b/tests/bsd/type_w.sh
index d0cb58c..15b4e68 100644
--- a/tests/bsd/type_w.sh
+++ b/tests/bsd/type_w.sh
@@ -2,23 +2,12 @@
command -v mdconfig &>/dev/null || skip
command -v newfs &>/dev/null || skip
-cleanup=()
-do_cleanup() {
- # Run cleanup hooks in reverse order
- while ((${#cleanup[@]} > 0)); do
- cmd="${cleanup[-1]}"
- unset 'cleanup[-1]'
- eval "bfs_sudo $cmd"
- done
-}
-trap do_cleanup EXIT
-
clean_scratch
# Create a ramdisk
truncate -s1M scratch/img
md=$(bfs_sudo mdconfig scratch/img) || skip
-cleanup+=("mdconfig -du $md")
+defer bfs_sudo mdconfig -du "$md"
# Make an ffs filesystem
bfs_sudo newfs -n "/dev/$md" >&2 || skip
@@ -26,7 +15,7 @@ mkdir scratch/mnt
# Mount it
bfs_sudo mount "/dev/$md" scratch/mnt || skip
-cleanup+=("umount scratch/mnt")
+defer bfs_sudo umount scratch/mnt
# Make it owned by us
bfs_sudo chown "$(id -u):$(id -g)" scratch/mnt
@@ -34,7 +23,7 @@ bfs_sudo chown "$(id -u):$(id -g)" scratch/mnt
# Mount a union filesystem within it
bfs_sudo mount -t unionfs -o below scratch/mnt/{lower,upper}
-cleanup+=("umount scratch/mnt/upper")
+defer bfs_sudo umount scratch/mnt/upper
# Create a whiteout
rm scratch/mnt/upper/bar
@@ -53,8 +42,7 @@ munge_ls() {
# So this is not the same as test 1
invoke_bfs scratch/mnt \( -type w -or -not -type w \) -ls | munge_ls 3
# Unmount the unionfs
- bfs_sudo umount scratch/mnt/upper
- unset 'cleanup[-1]'
+ pop_defer
# Now repeat the same tests
invoke_bfs scratch/mnt -ls | munge_ls 4
invoke_bfs scratch/mnt -type w -ls | munge_ls 5
diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh
index b04acd0..aaf9069 100644
--- a/tests/common/L_mount.sh
+++ b/tests/common/L_mount.sh
@@ -4,7 +4,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
ln -s ../mnt scratch/foo/bar
"$XTOUCH" scratch/mnt/baz
diff --git a/tests/common/inum_bind_mount.sh b/tests/common/inum_bind_mount.sh
index ecb4ec3..47f7c36 100644
--- a/tests/common/inum_bind_mount.sh
+++ b/tests/common/inum_bind_mount.sh
@@ -4,6 +4,6 @@ clean_scratch
"$XTOUCH" scratch/{foo,bar}
bfs_sudo mount --bind scratch/{foo,bar} || skip
-trap "bfs_sudo umount scratch/bar" EXIT
+defer bfs_sudo umount scratch/bar
bfs_diff scratch -inum "$(inum scratch/bar)"
diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh
index a4832e4..1bf2d86 100644
--- a/tests/common/inum_mount.sh
+++ b/tests/common/inum_mount.sh
@@ -4,6 +4,6 @@ clean_scratch
mkdir scratch/{foo,mnt}
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
bfs_diff scratch -inum "$(inum scratch/mnt)"
diff --git a/tests/common/mount.sh b/tests/common/mount.sh
index b13b43c..db8b801 100644
--- a/tests/common/mount.sh
+++ b/tests/common/mount.sh
@@ -4,7 +4,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
"$XTOUCH" scratch/foo/bar scratch/mnt/baz
diff --git a/tests/gnu/fstype_stacked.sh b/tests/gnu/fstype_stacked.sh
index 16f428f..a4b067a 100644
--- a/tests/gnu/fstype_stacked.sh
+++ b/tests/gnu/fstype_stacked.sh
@@ -4,9 +4,9 @@ clean_scratch
mkdir scratch/mnt
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
bfs_sudo mount -t ramfs ramfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt; bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
bfs_diff scratch/mnt -fstype ramfs -print -o -printf '%p: %F\n'
diff --git a/tests/gnu/fstype_umount.sh b/tests/gnu/fstype_umount.sh
index e817831..b6da7a3 100644
--- a/tests/gnu/fstype_umount.sh
+++ b/tests/gnu/fstype_umount.sh
@@ -4,7 +4,7 @@ clean_scratch
mkdir scratch/tmp
bfs_sudo mount -t tmpfs tmpfs scratch/tmp || skip
-trap "bfs_sudo umount -R scratch/tmp" EXIT
+defer bfs_sudo umount -R scratch/tmp
mkdir scratch/tmp/ram
bfs_sudo mount -t ramfs ramfs scratch/tmp/ram || skip
diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh
index c4450ef..261a4be 100644
--- a/tests/gnu/inum_automount.sh
+++ b/tests/gnu/inum_automount.sh
@@ -6,7 +6,7 @@ clean_scratch
mkdir scratch/{foo,automnt}
bfs_sudo systemd-mount -A -o bind basic scratch/automnt || skip
-trap "bfs_sudo systemd-umount scratch/automnt" EXIT
+defer bfs_sudo systemd-umount scratch/automnt
before=$(inum scratch/automnt)
bfs_diff scratch -inum "$before" -prune
diff --git a/tests/gnu/printf_Y_error.sh b/tests/gnu/printf_Y_error.sh
index 3aa816e..13d52e3 100644
--- a/tests/gnu/printf_Y_error.sh
+++ b/tests/gnu/printf_Y_error.sh
@@ -3,6 +3,6 @@ mkdir scratch/foo
ln -s foo/bar scratch/bar
chmod -x scratch/foo
-trap "chmod +x scratch/foo" EXIT
+defer chmod +x scratch/foo
! bfs_diff scratch -printf '(%p) (%l) %y %Y\n'
diff --git a/tests/gnu/xtype_bind_mount.sh b/tests/gnu/xtype_bind_mount.sh
index 99a11ab..1cc20ec 100644
--- a/tests/gnu/xtype_bind_mount.sh
+++ b/tests/gnu/xtype_bind_mount.sh
@@ -5,6 +5,6 @@ clean_scratch
ln -s /dev/null scratch/link
bfs_sudo mount --bind /dev/null scratch/null || skip
-trap "bfs_sudo umount scratch/null" EXIT
+defer bfs_sudo umount scratch/null
bfs_diff -L scratch -type c
diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh
index 172ea23..d16c211 100644
--- a/tests/posix/L_xdev.sh
+++ b/tests/posix/L_xdev.sh
@@ -4,7 +4,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
ln -s ../mnt scratch/foo/bar
"$XTOUCH" scratch/mnt/baz
diff --git a/tests/posix/depth_error.sh b/tests/posix/depth_error.sh
index e91fbf6..a6429d1 100644
--- a/tests/posix/depth_error.sh
+++ b/tests/posix/depth_error.sh
@@ -2,6 +2,6 @@ clean_scratch
"$XTOUCH" -p scratch/foo/bar
chmod a-r scratch/foo
-trap "chmod +r scratch/foo" EXIT
+defer chmod +r scratch/foo
! bfs_diff scratch -depth
diff --git a/tests/posix/overlayfs.sh b/tests/posix/overlayfs.sh
index a56b5b3..4cccebf 100644
--- a/tests/posix/overlayfs.sh
+++ b/tests/posix/overlayfs.sh
@@ -1,7 +1,11 @@
test "$UNAME" = "Linux" || skip
+
clean_scratch
"$XTOUCH" -p scratch/{lower/{foo,bar,baz},upper/{bar,baz/qux}}
+
mkdir -p scratch/{work,merged}
bfs_sudo mount -t overlay overlay -olowerdir=scratch/lower,upperdir=scratch/upper,workdir=scratch/work scratch/merged || skip
-trap "bfs_sudo umount scratch/merged; bfs_sudo rm -rf scratch/work" EXIT
+defer bfs_sudo rm -rf scratch/work
+defer bfs_sudo umount scratch/merged
+
bfs_diff scratch/merged
diff --git a/tests/posix/readdir_error.sh b/tests/posix/readdir_error.sh
index 9a002a1..fc48eb1 100644
--- a/tests/posix/readdir_error.sh
+++ b/tests/posix/readdir_error.sh
@@ -13,7 +13,7 @@ mkfifo scratch/{fever,pid,wait,running}
) &
# Kill the parent cat on exit
-trap "kill -9 %1" EXIT
+defer kill -9 %1
# Read the child PID
read -r pid <scratch/pid
diff --git a/tests/posix/type_bind_mount.sh b/tests/posix/type_bind_mount.sh
index c9a161d..7852167 100644
--- a/tests/posix/type_bind_mount.sh
+++ b/tests/posix/type_bind_mount.sh
@@ -4,6 +4,6 @@ clean_scratch
"$XTOUCH" scratch/{file,null}
bfs_sudo mount --bind /dev/null scratch/null || skip
-trap "bfs_sudo umount scratch/null" EXIT
+defer bfs_sudo umount scratch/null
bfs_diff scratch -type c
diff --git a/tests/posix/unionfs.sh b/tests/posix/unionfs.sh
index 88a549f..0f68a43 100644
--- a/tests/posix/unionfs.sh
+++ b/tests/posix/unionfs.sh
@@ -2,5 +2,5 @@
clean_scratch
"$XTOUCH" -p scratch/{lower/{foo,bar,baz},upper/{bar,baz/qux}}
bfs_sudo mount -t unionfs -o below scratch/{lower,upper} || skip
-trap "bfs_sudo umount scratch/upper" EXIT
+defer bfs_sudo umount scratch/upper
bfs_diff scratch
diff --git a/tests/posix/xdev.sh b/tests/posix/xdev.sh
index 33412bf..461a1e9 100644
--- a/tests/posix/xdev.sh
+++ b/tests/posix/xdev.sh
@@ -4,7 +4,7 @@ clean_scratch
mkdir scratch/{foo,mnt}
bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip
-trap "bfs_sudo umount scratch/mnt" EXIT
+defer bfs_sudo umount scratch/mnt
"$XTOUCH" scratch/foo/bar scratch/mnt/baz
diff --git a/tests/tests.sh b/tests/tests.sh
index 282a428..b159ea2 100755
--- a/tests/tests.sh
+++ b/tests/tests.sh
@@ -260,6 +260,34 @@ if (( ${#TEST_CASES[@]} == 0 )); then
exit 1
fi
+DEFER=()
+
+# Run a command when this (sub)shell exits
+function defer() {
+ trap -- KILL
+ if ! trap -p EXIT | grep -q pop_defers; then
+ DEFER=()
+ trap pop_defers EXIT
+ fi
+ DEFER+=("$(printf '%q ' "$@")")
+}
+
+function pop_defer() {
+ local cmd="${DEFER[-1]}"
+ unset "DEFER[-1]"
+ eval "$cmd"
+}
+
+function pop_defers() {
+ local ret=0
+
+ while ((${#DEFER[@]} > 0)); do
+ pop_defer || ret=$?
+ done
+
+ return $ret
+}
+
function bfs_sudo() {
if ((${#SUDO[@]})); then
"${SUDO[@]}" "$@"
@@ -304,7 +332,7 @@ function cleanup() {
}
if [ "$CLEAN" ]; then
- trap cleanup EXIT
+ defer cleanup
else
echo "Test files saved to $TMP"
fi