]> Sergey Matveev's repositories - bfs.git/commitdiff
tests: New defer function
authorTavian Barnes <tavianator@tavianator.com>
Thu, 19 Oct 2023 13:31:33 +0000 (09:31 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Thu, 19 Oct 2023 13:31:33 +0000 (09:31 -0400)
18 files changed:
tests/bsd/type_w.sh
tests/common/L_mount.sh
tests/common/inum_bind_mount.sh
tests/common/inum_mount.sh
tests/common/mount.sh
tests/gnu/fstype_stacked.sh
tests/gnu/fstype_umount.sh
tests/gnu/inum_automount.sh
tests/gnu/printf_Y_error.sh
tests/gnu/xtype_bind_mount.sh
tests/posix/L_xdev.sh
tests/posix/depth_error.sh
tests/posix/overlayfs.sh
tests/posix/readdir_error.sh
tests/posix/type_bind_mount.sh
tests/posix/unionfs.sh
tests/posix/xdev.sh
tests/tests.sh

index d0cb58cb70b265109d0cd332a0348c2f88744f33..15b4e68ba8e003e9d68ff524b15a20228ee43f58 100644 (file)
@@ -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
index b04acd0154bd1f4c8865645f7da936a1fd6864f9..aaf9069eccbd7703362a09ca8e5503c54399b10b 100644 (file)
@@ -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
index ecb4ec3a7002485015332669820c4fb84ae5a9ef..47f7c363369769b6bc288cbec9fea3a3a947303b 100644 (file)
@@ -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)"
index a4832e4ff08b169a95b3946369fb5cbfc0177f6b..1bf2d8690501787af28c3cb768412b328ae05ba3 100644 (file)
@@ -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)"
index b13b43c3b7e71ee40c49d88c348f19435e05fe06..db8b801332c9a8484c0d437c6077116b7884e6a5 100644 (file)
@@ -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
 
index 16f428fdbc807485289de8531140746b5936ab14..a4b067ada7ab929d8c1a85595d9d84ccf5f04cc4 100644 (file)
@@ -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'
index e8178312795fb7cee5058c5da65fdf9da88b0065..b6da7a3e4de51c759042a49e70f589cc84abf3e2 100644 (file)
@@ -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
index c4450efe93e3f5fe47d47a18ebb36bcadecdfe3b..261a4beefe07cbc4e67702db0c9c1570ce1903af 100644 (file)
@@ -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
index 3aa816eef9e0b453e68605d5a18a6f2bba2b016e..13d52e38e1f5aa6364ab0f295108994dcd0b144d 100644 (file)
@@ -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'
index 99a11abd424f5d23df807d016b7edbe8fb87b6bd..1cc20eccc5f93c2133e4bdc6d7c14e89d5317627 100644 (file)
@@ -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
index 172ea2383009c866a434b320c878ec4d75e1ef80..d16c211b5f5f3e7ca95e442c43f69895bcf7d94e 100644 (file)
@@ -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
index e91fbf6c4baf08ef37b6d9a0ed568629c8390a7f..a6429d19ecb8883d30769213bdec86c37c22f137 100644 (file)
@@ -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
index a56b5b38c3769b94e78e0ffc1a9f8179918a6e1b..4cccebfee7a53f4008e9e562cb33e6247743f33f 100644 (file)
@@ -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
index 9a002a12ab2d7ab8bbda3fbaf8956c0498209412..fc48eb129541bc6387e8025424691c034ab4df87 100644 (file)
@@ -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
index c9a161d4e29fdf3b944fe4ef202ac1d1242dbc16..7852167a7f479f4b3fc9410c47fb307ed0029d88 100644 (file)
@@ -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
index 88a549fec77e970192e5c8ba665fc1ac1b434285..0f68a43a6fbe62c60e32f0d1435af21af506397f 100644 (file)
@@ -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
index 33412bfd5d8b14bf6b24cccc6ca7490b981ab494..461a1e96fdc7d9c6e543fa5c25adb5ed61d23121 100644 (file)
@@ -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
 
index 282a428e5f3e240b3d4aab71f6e6a9894f70f352..b159ea2dc8f5fd9bd80ec21f8e6e798a9cdbda8b 100755 (executable)
@@ -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