From 33a6a3027f0b1ec931824a4b2206bf6659df53a4 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 30 Nov 2022 13:57:12 -0500 Subject: parse: Pass the right argument when warning about -O9 --- tests/bfs/warn_O9.out | 19 +++++++++++++++++++ tests/bfs/warn_O9.sh | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 tests/bfs/warn_O9.out create mode 100644 tests/bfs/warn_O9.sh (limited to 'tests/bfs') diff --git a/tests/bfs/warn_O9.out b/tests/bfs/warn_O9.out new file mode 100644 index 0000000..336a6e8 --- /dev/null +++ b/tests/bfs/warn_O9.out @@ -0,0 +1,19 @@ +. +./a +./b +./c +./c/d +./e +./e/f +./g +./g/h +./i +./j +./j/foo +./k +./k/foo +./k/foo/bar +./l +./l/foo +./l/foo/bar +./l/foo/bar/baz diff --git a/tests/bfs/warn_O9.sh b/tests/bfs/warn_O9.sh new file mode 100644 index 0000000..821789f --- /dev/null +++ b/tests/bfs/warn_O9.sh @@ -0,0 +1,3 @@ +# Regression test: don't crash when warning if -O9 is the last argument +cd basic +bfs_diff -warn -O9 -- cgit v1.2.3 From c55e85580df10c5afdc6fc0710e756a456aa8e93 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 30 Nov 2022 14:16:51 -0500 Subject: parse: Fix crash on -xdev -mount --- src/parse.c | 2 +- tests/bfs/warn_xdev_mount.out | 19 +++++++++++++++++++ tests/bfs/warn_xdev_mount.sh | 2 ++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/bfs/warn_xdev_mount.out create mode 100644 tests/bfs/warn_xdev_mount.sh (limited to 'tests/bfs') diff --git a/src/parse.c b/src/parse.c index 5ef71f8..a858a4c 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1802,7 +1802,7 @@ static struct bfs_expr *parse_mount(struct parser_state *state, int arg1, int ar bfs_warning(state->ctx, "${blu}-xdev${rs}, due to http://austingroupbugs.net/view.php?id=1133.\n\n"); state->ctx->flags |= BFTW_PRUNE_MOUNTS; - state->mount_arg = state->argv; + state->mount_arg = expr->argv; return expr; } diff --git a/tests/bfs/warn_xdev_mount.out b/tests/bfs/warn_xdev_mount.out new file mode 100644 index 0000000..a7ccfe4 --- /dev/null +++ b/tests/bfs/warn_xdev_mount.out @@ -0,0 +1,19 @@ +basic +basic/a +basic/b +basic/c +basic/c/d +basic/e +basic/e/f +basic/g +basic/g/h +basic/i +basic/j +basic/j/foo +basic/k +basic/k/foo +basic/k/foo/bar +basic/l +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar/baz diff --git a/tests/bfs/warn_xdev_mount.sh b/tests/bfs/warn_xdev_mount.sh new file mode 100644 index 0000000..5d395f6 --- /dev/null +++ b/tests/bfs/warn_xdev_mount.sh @@ -0,0 +1,2 @@ +# Regression test: don't crash if -mount is the last option +bfs_diff basic -warn -xdev -mount -- cgit v1.2.3 From e01042b84abdfa224d47e6d11eb9798ce4c7d2f8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 14:17:07 -0500 Subject: tests: Replace skip_unless test with test || skip --- tests/bfs/L_capable.sh | 6 +++--- tests/bfs/capable.sh | 6 +++--- tests/bfs/exec_flush_fprint_fail.sh | 2 +- tests/bfs/fprint_error_stderr.sh | 2 +- tests/bfs/fprint_error_stdout.sh | 2 +- tests/bfs/stderr_fails_loudly.sh | 2 +- tests/bfs/stderr_fails_silently.sh | 2 +- tests/bsd/L_acl.sh | 4 ++-- tests/bsd/L_xattr.sh | 4 ++-- tests/bsd/L_xattrname.sh | 4 ++-- tests/bsd/acl.sh | 4 ++-- tests/bsd/flags.sh | 4 ++-- tests/bsd/xattr.sh | 4 ++-- tests/bsd/xattrname.sh | 4 ++-- tests/common/L_ilname.sh | 2 +- tests/common/L_mount.sh | 4 ++-- tests/common/ilname.sh | 2 +- tests/common/iname.sh | 2 +- tests/common/inum_bind_mount.sh | 4 ++-- tests/common/inum_mount.sh | 4 ++-- tests/common/ipath.sh | 2 +- tests/common/mount.sh | 4 ++-- tests/gnu/exec_flush_fail.sh | 2 +- tests/gnu/exec_plus_flush_fail.sh | 2 +- tests/gnu/fprint_error.sh | 2 +- tests/gnu/fprint_noerror.sh | 2 +- tests/gnu/fstype.sh | 3 +-- tests/gnu/gid_plus.sh | 2 +- tests/gnu/gid_plus_plus.sh | 2 +- tests/gnu/inum_automount.sh | 6 +++--- tests/gnu/iwholename.sh | 2 +- tests/gnu/print_error.sh | 2 +- tests/gnu/regex_invalid_utf8.sh | 6 +++--- tests/gnu/regextype_emacs.sh | 2 +- tests/gnu/regextype_grep.sh | 2 +- tests/gnu/uid_plus.sh | 2 +- tests/gnu/uid_plus_plus.sh | 2 +- tests/gnu/xtype_bind_mount.sh | 4 ++-- tests/posix/L_xdev.sh | 4 ++-- tests/posix/name_bracket.sh | 2 +- tests/posix/readdir_error.sh | 4 ++-- tests/posix/type_bind_mount.sh | 4 ++-- tests/posix/xdev.sh | 4 ++-- tests/tests.sh | 10 ---------- 44 files changed, 67 insertions(+), 78 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/L_capable.sh b/tests/bfs/L_capable.sh index 533ac2f..a349677 100644 --- a/tests/bfs/L_capable.sh +++ b/tests/bfs/L_capable.sh @@ -1,9 +1,9 @@ -skip_unless test "$SUDO" -skip_unless test "$UNAME" = "Linux" +test "$SUDO" || skip +test "$UNAME" = "Linux" || skip clean_scratch -skip_unless invoke_bfs scratch -quit -capable +invoke_bfs scratch -quit -capable || skip "$XTOUCH" scratch/{normal,capable} sudo setcap all+ep scratch/capable diff --git a/tests/bfs/capable.sh b/tests/bfs/capable.sh index 256b9bc..8b60ea6 100644 --- a/tests/bfs/capable.sh +++ b/tests/bfs/capable.sh @@ -1,9 +1,9 @@ -skip_unless test "$SUDO" -skip_unless test "$UNAME" = "Linux" +test "$SUDO" || skip +test "$UNAME" = "Linux" || skip clean_scratch -skip_unless invoke_bfs scratch -quit -capable +invoke_bfs scratch -quit -capable || skip "$XTOUCH" scratch/{normal,capable} sudo setcap all+ep scratch/capable diff --git a/tests/bfs/exec_flush_fprint_fail.sh b/tests/bfs/exec_flush_fprint_fail.sh index 5da944a..b58624a 100644 --- a/tests/bfs/exec_flush_fprint_fail.sh +++ b/tests/bfs/exec_flush_fprint_fail.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic/a -fprint /dev/full -exec true \; diff --git a/tests/bfs/fprint_error_stderr.sh b/tests/bfs/fprint_error_stderr.sh index 427808f..e51d026 100644 --- a/tests/bfs/fprint_error_stderr.sh +++ b/tests/bfs/fprint_error_stderr.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic -maxdepth 0 -fprint /dev/full 2>/dev/full diff --git a/tests/bfs/fprint_error_stdout.sh b/tests/bfs/fprint_error_stdout.sh index fbdc1d0..6aa4b11 100644 --- a/tests/bfs/fprint_error_stdout.sh +++ b/tests/bfs/fprint_error_stdout.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic -maxdepth 0 -fprint /dev/full >/dev/full diff --git a/tests/bfs/stderr_fails_loudly.sh b/tests/bfs/stderr_fails_loudly.sh index d8b3861..c423d9e 100644 --- a/tests/bfs/stderr_fails_loudly.sh +++ b/tests/bfs/stderr_fails_loudly.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs -D all basic -false -fprint /dev/full 2>/dev/full diff --git a/tests/bfs/stderr_fails_silently.sh b/tests/bfs/stderr_fails_silently.sh index 731cb02..a37393d 100644 --- a/tests/bfs/stderr_fails_silently.sh +++ b/tests/bfs/stderr_fails_silently.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip bfs_diff -D all basic 2>/dev/full diff --git a/tests/bsd/L_acl.sh b/tests/bsd/L_acl.sh index cf573e4..db97013 100644 --- a/tests/bsd/L_acl.sh +++ b/tests/bsd/L_acl.sh @@ -1,9 +1,9 @@ clean_scratch -skip_unless invoke_bfs scratch -quit -acl +invoke_bfs scratch -quit -acl || skip "$XTOUCH" scratch/{normal,acl} -skip_unless set_acl scratch/acl +set_acl scratch/acl || skip ln -s acl scratch/link bfs_diff -L scratch -acl diff --git a/tests/bsd/L_xattr.sh b/tests/bsd/L_xattr.sh index 7c27e0d..1f61c78 100644 --- a/tests/bsd/L_xattr.sh +++ b/tests/bsd/L_xattr.sh @@ -1,3 +1,3 @@ -skip_unless invoke_bfs scratch -quit -xattr -skip_unless make_xattrs +invoke_bfs scratch -quit -xattr || skip +make_xattrs || skip bfs_diff -L scratch -xattr diff --git a/tests/bsd/L_xattrname.sh b/tests/bsd/L_xattrname.sh index 39d6a77..3b2006b 100644 --- a/tests/bsd/L_xattrname.sh +++ b/tests/bsd/L_xattrname.sh @@ -1,5 +1,5 @@ -skip_unless invoke_bfs scratch -quit -xattr -skip_unless make_xattrs +invoke_bfs scratch -quit -xattr || skip +make_xattrs || skip case "$UNAME" in Darwin|FreeBSD) diff --git a/tests/bsd/acl.sh b/tests/bsd/acl.sh index 1665684..c044398 100644 --- a/tests/bsd/acl.sh +++ b/tests/bsd/acl.sh @@ -1,9 +1,9 @@ clean_scratch -skip_unless invoke_bfs scratch -quit -acl +invoke_bfs scratch -quit -acl || skip "$XTOUCH" scratch/{normal,acl} -skip_unless set_acl scratch/acl +set_acl scratch/acl || skip ln -s acl scratch/link bfs_diff scratch -acl diff --git a/tests/bsd/flags.sh b/tests/bsd/flags.sh index ffb1cc2..949a7d3 100644 --- a/tests/bsd/flags.sh +++ b/tests/bsd/flags.sh @@ -1,8 +1,8 @@ -skip_unless invoke_bfs scratch -quit -flags offline +invoke_bfs scratch -quit -flags offline || skip clean_scratch "$XTOUCH" scratch/{foo,bar} -skip_unless chflags offline scratch/bar +chflags offline scratch/bar || skip bfs_diff scratch -flags -offline,nohidden diff --git a/tests/bsd/xattr.sh b/tests/bsd/xattr.sh index 727c220..4a4658c 100644 --- a/tests/bsd/xattr.sh +++ b/tests/bsd/xattr.sh @@ -1,3 +1,3 @@ -skip_unless invoke_bfs scratch -quit -xattr -skip_unless make_xattrs +invoke_bfs scratch -quit -xattr || skip +make_xattrs || skip bfs_diff scratch -xattr diff --git a/tests/bsd/xattrname.sh b/tests/bsd/xattrname.sh index 6a0fe7e..655bd74 100644 --- a/tests/bsd/xattrname.sh +++ b/tests/bsd/xattrname.sh @@ -1,5 +1,5 @@ -skip_unless invoke_bfs scratch -quit -xattr -skip_unless make_xattrs +invoke_bfs scratch -quit -xattr || skip +make_xattrs || skip case "$UNAME" in Darwin|FreeBSD) diff --git a/tests/common/L_ilname.sh b/tests/common/L_ilname.sh index cfb15a8..e0495ed 100644 --- a/tests/common/L_ilname.sh +++ b/tests/common/L_ilname.sh @@ -1,2 +1,2 @@ -skip_unless invoke_bfs -quit -ilname PATTERN +invoke_bfs -quit -ilname PATTERN || skip bfs_diff -L links -ilname '[AQ]' diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh index dad7e00..5b56762 100644 --- a/tests/common/L_mount.sh +++ b/tests/common/L_mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_if test "$UNAME" = "Darwin" +test "$SUDO" || skip +test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} diff --git a/tests/common/ilname.sh b/tests/common/ilname.sh index 7ab0793..fc7e9e4 100644 --- a/tests/common/ilname.sh +++ b/tests/common/ilname.sh @@ -1,2 +1,2 @@ -skip_unless invoke_bfs -quit -ilname PATTERN +invoke_bfs -quit -ilname PATTERN || skip bfs_diff links -ilname '[AQ]' diff --git a/tests/common/iname.sh b/tests/common/iname.sh index 8fcc443..c25a646 100644 --- a/tests/common/iname.sh +++ b/tests/common/iname.sh @@ -1,2 +1,2 @@ -skip_unless invoke_bfs -quit -iname PATTERN +invoke_bfs -quit -iname PATTERN || skip bfs_diff basic -iname '*F*' diff --git a/tests/common/inum_bind_mount.sh b/tests/common/inum_bind_mount.sh index e35ed4e..a9e01bf 100644 --- a/tests/common/inum_bind_mount.sh +++ b/tests/common/inum_bind_mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_unless test "$UNAME" = "Linux" +test "$SUDO" || skip +test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{foo,bar} diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh index f9f4e2b..7cc5e40 100644 --- a/tests/common/inum_mount.sh +++ b/tests/common/inum_mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_if test "$UNAME" = "Darwin" +test "$SUDO" || skip +test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} diff --git a/tests/common/ipath.sh b/tests/common/ipath.sh index 9e6f8c5..7d05f31 100644 --- a/tests/common/ipath.sh +++ b/tests/common/ipath.sh @@ -1,2 +1,2 @@ -skip_unless invoke_bfs -quit -ipath PATTERN +invoke_bfs -quit -ipath PATTERN || skip bfs_diff basic -ipath 'basic/*F*' diff --git a/tests/common/mount.sh b/tests/common/mount.sh index 2732a68..f077ea2 100644 --- a/tests/common/mount.sh +++ b/tests/common/mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_if test "$UNAME" = "Darwin" +test "$SUDO" || skip +test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} diff --git a/tests/gnu/exec_flush_fail.sh b/tests/gnu/exec_flush_fail.sh index 4772a14..ce796b2 100644 --- a/tests/gnu/exec_flush_fail.sh +++ b/tests/gnu/exec_flush_fail.sh @@ -1,3 +1,3 @@ # Failure to flush streams before exec should be caught -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic -print0 -exec true \; >/dev/full diff --git a/tests/gnu/exec_plus_flush_fail.sh b/tests/gnu/exec_plus_flush_fail.sh index 5c74fd8..8beee09 100644 --- a/tests/gnu/exec_plus_flush_fail.sh +++ b/tests/gnu/exec_plus_flush_fail.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic/a -print0 -exec echo found {} + >/dev/full diff --git a/tests/gnu/fprint_error.sh b/tests/gnu/fprint_error.sh index e7f2394..0e75b0e 100644 --- a/tests/gnu/fprint_error.sh +++ b/tests/gnu/fprint_error.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic -maxdepth 0 -fprint /dev/full diff --git a/tests/gnu/fprint_noerror.sh b/tests/gnu/fprint_noerror.sh index 142e935..f13a62b 100644 --- a/tests/gnu/fprint_noerror.sh +++ b/tests/gnu/fprint_noerror.sh @@ -1,3 +1,3 @@ # Regression test: /dev/full should not fail until actually written to -skip_unless test -e /dev/full +test -e /dev/full || skip invoke_bfs basic -false -fprint /dev/full diff --git a/tests/gnu/fstype.sh b/tests/gnu/fstype.sh index 939438e..05645c3 100644 --- a/tests/gnu/fstype.sh +++ b/tests/gnu/fstype.sh @@ -1,3 +1,2 @@ -fstype=$(invoke_bfs basic -maxdepth 0 -printf '%F\n') -skip_if test $? -ne 0 +fstype=$(invoke_bfs basic -maxdepth 0 -printf '%F\n') || skip bfs_diff basic -fstype "$fstype" diff --git a/tests/gnu/gid_plus.sh b/tests/gnu/gid_plus.sh index 8ad493b..ccba0e6 100644 --- a/tests/gnu/gid_plus.sh +++ b/tests/gnu/gid_plus.sh @@ -1,2 +1,2 @@ -skip_if test "$(id -g)" -eq 0 +test "$(id -g)" -eq 0 && skip bfs_diff basic -gid +0 diff --git a/tests/gnu/gid_plus_plus.sh b/tests/gnu/gid_plus_plus.sh index 7982633..ec7ae86 100644 --- a/tests/gnu/gid_plus_plus.sh +++ b/tests/gnu/gid_plus_plus.sh @@ -1,2 +1,2 @@ -skip_if test "$(id -g)" -eq 0 +test "$(id -g)" -eq 0 && skip bfs_diff basic -gid ++0 diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh index 648ea05..6bf2977 100644 --- a/tests/gnu/inum_automount.sh +++ b/tests/gnu/inum_automount.sh @@ -1,11 +1,11 @@ # bfs shouldn't trigger automounts unless it descends into them -skip_unless test "$SUDO" -skip_unless command -v systemd-mount &>/dev/null +test "$SUDO" || skip +command -v systemd-mount &>/dev/null || skip clean_scratch mkdir scratch/{foo,automnt} -skip_unless sudo systemd-mount -A -o bind basic scratch/automnt +sudo systemd-mount -A -o bind basic scratch/automnt || skip before=$(inum scratch/automnt) bfs_diff scratch -inum "$before" -prune diff --git a/tests/gnu/iwholename.sh b/tests/gnu/iwholename.sh index 67e9630..0b2d038 100644 --- a/tests/gnu/iwholename.sh +++ b/tests/gnu/iwholename.sh @@ -1,2 +1,2 @@ -skip_unless invoke_bfs -quit -iwholename PATTERN +invoke_bfs -quit -iwholename PATTERN || skip bfs_diff basic -iwholename 'basic/*F*' diff --git a/tests/gnu/print_error.sh b/tests/gnu/print_error.sh index 9fd5af5..62a32b4 100644 --- a/tests/gnu/print_error.sh +++ b/tests/gnu/print_error.sh @@ -1,2 +1,2 @@ -skip_unless test -e /dev/full +test -e /dev/full || skip fail invoke_bfs basic -maxdepth 0 >/dev/full diff --git a/tests/gnu/regex_invalid_utf8.sh b/tests/gnu/regex_invalid_utf8.sh index edb4b1e..603d688 100644 --- a/tests/gnu/regex_invalid_utf8.sh +++ b/tests/gnu/regex_invalid_utf8.sh @@ -1,8 +1,8 @@ clean_scratch # Incomplete UTF-8 sequences -skip_unless touch scratch/$'\xC3' -skip_unless touch scratch/$'\xE2\x84' -skip_unless touch scratch/$'\xF0\x9F\x92' +touch scratch/$'\xC3' || skip +touch scratch/$'\xE2\x84' || skip +touch scratch/$'\xF0\x9F\x92' || skip bfs_diff scratch -regex 'scratch/..' diff --git a/tests/gnu/regextype_emacs.sh b/tests/gnu/regextype_emacs.sh index d0f68cc..3cc388c 100644 --- a/tests/gnu/regextype_emacs.sh +++ b/tests/gnu/regextype_emacs.sh @@ -1,3 +1,3 @@ -skip_unless invoke_bfs -regextype emacs -quit +invoke_bfs -regextype emacs -quit || skip bfs_diff basic -regextype emacs -regex '.*/\(f+o?o?\|bar\)' diff --git a/tests/gnu/regextype_grep.sh b/tests/gnu/regextype_grep.sh index 0136700..0830667 100644 --- a/tests/gnu/regextype_grep.sh +++ b/tests/gnu/regextype_grep.sh @@ -1,3 +1,3 @@ -skip_unless invoke_bfs -regextype grep -quit +invoke_bfs -regextype grep -quit || skip bfs_diff basic -regextype grep -regex '.*/f\+o\?o\?' diff --git a/tests/gnu/uid_plus.sh b/tests/gnu/uid_plus.sh index fc4bce3..22b2c8e 100644 --- a/tests/gnu/uid_plus.sh +++ b/tests/gnu/uid_plus.sh @@ -1,2 +1,2 @@ -skip_if test "$(id -u)" -eq 0 +test "$(id -u)" -eq 0 && skip bfs_diff basic -uid +0 diff --git a/tests/gnu/uid_plus_plus.sh b/tests/gnu/uid_plus_plus.sh index 5d5e086..e021888 100644 --- a/tests/gnu/uid_plus_plus.sh +++ b/tests/gnu/uid_plus_plus.sh @@ -1,2 +1,2 @@ -skip_if test "$(id -u)" -eq 0 +test "$(id -u)" -eq 0 && skip bfs_diff basic -uid ++0 diff --git a/tests/gnu/xtype_bind_mount.sh b/tests/gnu/xtype_bind_mount.sh index 264b6f8..a6dbed3 100644 --- a/tests/gnu/xtype_bind_mount.sh +++ b/tests/gnu/xtype_bind_mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_unless test "$UNAME" = "Linux" +test "$SUDO" || skip +test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh index ddbadd8..2fc99dd 100644 --- a/tests/posix/L_xdev.sh +++ b/tests/posix/L_xdev.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_if test "$UNAME" = "Darwin" +test "$SUDO" || skip +test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} diff --git a/tests/posix/name_bracket.sh b/tests/posix/name_bracket.sh index 84a417f..80ca186 100644 --- a/tests/posix/name_bracket.sh +++ b/tests/posix/name_bracket.sh @@ -1,5 +1,5 @@ # fnmatch() is broken on macOS -skip_if test "$UNAME" = "Darwin" +test "$UNAME" = "Darwin" && skip # An unclosed [ should be matched literally bfs_diff weirdnames -name '[' diff --git a/tests/posix/readdir_error.sh b/tests/posix/readdir_error.sh index ce06723..483f543 100644 --- a/tests/posix/readdir_error.sh +++ b/tests/posix/readdir_error.sh @@ -1,4 +1,4 @@ -skip_unless test "$UNAME" = "Linux" +test "$UNAME" = "Linux" || skip clean_scratch mkfifo scratch/{fever,pid,wait,running} @@ -33,5 +33,5 @@ while [ "$state" != "Z" ]; do done # On Linux, open(/proc/$pid/net) will succeed but readdir() will fail -skip_unless test -r "/proc/$pid/net" +test -r "/proc/$pid/net" || skip fail invoke_bfs "/proc/$pid/net" >/dev/null diff --git a/tests/posix/type_bind_mount.sh b/tests/posix/type_bind_mount.sh index 445f6ef..fe32875 100644 --- a/tests/posix/type_bind_mount.sh +++ b/tests/posix/type_bind_mount.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_unless test "$UNAME" = "Linux" +test "$SUDO" || skip +test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} diff --git a/tests/posix/xdev.sh b/tests/posix/xdev.sh index 4591940..44e04dd 100644 --- a/tests/posix/xdev.sh +++ b/tests/posix/xdev.sh @@ -1,5 +1,5 @@ -skip_unless test "$SUDO" -skip_if test "$UNAME" = "Darwin" +test "$SUDO" || skip +test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} diff --git a/tests/tests.sh b/tests/tests.sh index 3fdf49b..3047bf4 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -543,16 +543,6 @@ function skip() { exit $EX_SKIP } -function skip_if() { - if "$@"; then - skip - fi -} - -function skip_unless() { - skip_if fail "$@" -} - function closefrom() { if [ -d /proc/self/fd ]; then local fds=/proc/self/fd -- cgit v1.2.3 From 7d87b96b421b76e387cee903b7b7c1bc16c54310 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 14:39:18 -0500 Subject: tests: Move crash detection into invoke_bfs, use ! instead of fail --- tests/bfs/D_incomplete.sh | 2 +- tests/bfs/and_incomplete.sh | 2 +- tests/bfs/comma_incomplete.sh | 2 +- tests/bfs/exclude_exclude.sh | 2 +- tests/bfs/exclude_print.sh | 2 +- tests/bfs/exec_flush_fprint_fail.sh | 2 +- tests/bfs/fprint_error_stderr.sh | 2 +- tests/bfs/fprint_error_stdout.sh | 2 +- tests/bfs/high_byte.sh | 2 +- tests/bfs/links_empty.sh | 2 +- tests/bfs/links_invalid.sh | 2 +- tests/bfs/links_negative.sh | 2 +- tests/bfs/links_noarg.sh | 2 +- tests/bfs/newerma_nonexistent.sh | 2 +- tests/bfs/newermq.sh | 2 +- tests/bfs/newermt_invalid.sh | 2 +- tests/bfs/newerqm.sh | 2 +- tests/bfs/or_incomplete.sh | 2 +- tests/bfs/perm_symbolic_double_comma.sh | 2 +- tests/bfs/perm_symbolic_missing_action.sh | 2 +- tests/bfs/perm_symbolic_trailing_comma.sh | 2 +- tests/bfs/printf_duplicate_flag.sh | 2 +- tests/bfs/printf_everything.sh | 4 ++-- tests/bfs/printf_incomplete_escape.sh | 2 +- tests/bfs/printf_incomplete_format.sh | 2 +- tests/bfs/printf_invalid_escape.sh | 2 +- tests/bfs/printf_invalid_format.sh | 2 +- tests/bfs/printf_must_be_numeric.sh | 2 +- tests/bfs/stderr_fails_loudly.sh | 2 +- tests/bfs/unexpected_operator.sh | 2 +- tests/bfs/xtype_depth.sh | 2 +- tests/bsd/f_incomplete.sh | 2 +- tests/bsd/mtime_bad_unit.sh | 2 +- tests/bsd/mtime_missing_unit.sh | 2 +- tests/common/maxdepth_incomplete.sh | 2 +- tests/common/mindepth_incomplete.sh | 2 +- tests/gnu/exec_flush_fail.sh | 2 +- tests/gnu/exec_nothing.sh | 2 +- tests/gnu/exec_plus_flush_fail.sh | 2 +- tests/gnu/files0_from_empty.sh | 2 +- tests/gnu/files0_from_error.sh | 2 +- tests/gnu/files0_from_none.sh | 2 +- tests/gnu/files0_from_nothing.sh | 2 +- tests/gnu/files0_from_nowhere.sh | 2 +- tests/gnu/files0_from_ok.sh | 2 +- tests/gnu/fls_nonexistent.sh | 2 +- tests/gnu/fprint0_nonexistent.sh | 2 +- tests/gnu/fprint_error.sh | 2 +- tests/gnu/fprint_noarg.sh | 2 +- tests/gnu/fprint_nonexistent.sh | 2 +- tests/gnu/fprintf_nofile.sh | 2 +- tests/gnu/fprintf_noformat.sh | 2 +- tests/gnu/fprintf_nonexistent.sh | 2 +- tests/gnu/ignore_readdir_race_root.sh | 2 +- tests/gnu/ok_nothing.sh | 2 +- tests/gnu/print_error.sh | 2 +- tests/gnu/regex_error.sh | 2 +- tests/posix/closed_stderr.sh | 2 +- tests/posix/closed_stdout.sh | 2 +- tests/posix/exec_plus_nothing.sh | 2 +- tests/posix/extra_paren.sh | 2 +- tests/posix/incomplete.sh | 2 +- tests/posix/missing_paren.sh | 2 +- tests/posix/newer_nonexistent.sh | 2 +- tests/posix/ok_plus_nothing.sh | 2 +- tests/posix/readdir_error.sh | 2 +- tests/tests.sh | 16 +++++----------- 67 files changed, 72 insertions(+), 78 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/D_incomplete.sh b/tests/bfs/D_incomplete.sh index 396d365..30c522a 100644 --- a/tests/bfs/D_incomplete.sh +++ b/tests/bfs/D_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs -D +! invoke_bfs -D diff --git a/tests/bfs/and_incomplete.sh b/tests/bfs/and_incomplete.sh index f7bc2c3..05abc2d 100644 --- a/tests/bfs/and_incomplete.sh +++ b/tests/bfs/and_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs -print -a +! invoke_bfs -print -a diff --git a/tests/bfs/comma_incomplete.sh b/tests/bfs/comma_incomplete.sh index 07cf505..bd60168 100644 --- a/tests/bfs/comma_incomplete.sh +++ b/tests/bfs/comma_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs -print , +! invoke_bfs -print , diff --git a/tests/bfs/exclude_exclude.sh b/tests/bfs/exclude_exclude.sh index c687623..739342f 100644 --- a/tests/bfs/exclude_exclude.sh +++ b/tests/bfs/exclude_exclude.sh @@ -1 +1 @@ -fail invoke_bfs basic -exclude -exclude -name foo +! invoke_bfs basic -exclude -exclude -name foo diff --git a/tests/bfs/exclude_print.sh b/tests/bfs/exclude_print.sh index 52ff0fd..dc89e1d 100644 --- a/tests/bfs/exclude_print.sh +++ b/tests/bfs/exclude_print.sh @@ -1 +1 @@ -fail invoke_bfs basic -exclude -print +! invoke_bfs basic -exclude -print diff --git a/tests/bfs/exec_flush_fprint_fail.sh b/tests/bfs/exec_flush_fprint_fail.sh index b58624a..cd38e41 100644 --- a/tests/bfs/exec_flush_fprint_fail.sh +++ b/tests/bfs/exec_flush_fprint_fail.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic/a -fprint /dev/full -exec true \; +! invoke_bfs basic/a -fprint /dev/full -exec true \; diff --git a/tests/bfs/fprint_error_stderr.sh b/tests/bfs/fprint_error_stderr.sh index e51d026..2cc4037 100644 --- a/tests/bfs/fprint_error_stderr.sh +++ b/tests/bfs/fprint_error_stderr.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic -maxdepth 0 -fprint /dev/full 2>/dev/full +! invoke_bfs basic -maxdepth 0 -fprint /dev/full 2>/dev/full diff --git a/tests/bfs/fprint_error_stdout.sh b/tests/bfs/fprint_error_stdout.sh index 6aa4b11..42a7b36 100644 --- a/tests/bfs/fprint_error_stdout.sh +++ b/tests/bfs/fprint_error_stdout.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic -maxdepth 0 -fprint /dev/full >/dev/full +! invoke_bfs basic -maxdepth 0 -fprint /dev/full >/dev/full diff --git a/tests/bfs/high_byte.sh b/tests/bfs/high_byte.sh index 222f24b..c76199f 100644 --- a/tests/bfs/high_byte.sh +++ b/tests/bfs/high_byte.sh @@ -1 +1 @@ -fail invoke_bfs -$'\xFF' +! invoke_bfs -$'\xFF' diff --git a/tests/bfs/links_empty.sh b/tests/bfs/links_empty.sh index 34c7c25..42cf6e5 100644 --- a/tests/bfs/links_empty.sh +++ b/tests/bfs/links_empty.sh @@ -1 +1 @@ -fail invoke_bfs links -links '' +! invoke_bfs links -links '' diff --git a/tests/bfs/links_invalid.sh b/tests/bfs/links_invalid.sh index ff69fa6..4d139c9 100644 --- a/tests/bfs/links_invalid.sh +++ b/tests/bfs/links_invalid.sh @@ -1 +1 @@ -fail invoke_bfs links -links ASDF +! invoke_bfs links -links ASDF diff --git a/tests/bfs/links_negative.sh b/tests/bfs/links_negative.sh index b5d9c58..e664b99 100644 --- a/tests/bfs/links_negative.sh +++ b/tests/bfs/links_negative.sh @@ -1 +1 @@ -fail invoke_bfs links -links +-1 +! invoke_bfs links -links +-1 diff --git a/tests/bfs/links_noarg.sh b/tests/bfs/links_noarg.sh index 5dede5f..5c948dc 100644 --- a/tests/bfs/links_noarg.sh +++ b/tests/bfs/links_noarg.sh @@ -1 +1 @@ -fail invoke_bfs links -links +! invoke_bfs links -links diff --git a/tests/bfs/newerma_nonexistent.sh b/tests/bfs/newerma_nonexistent.sh index 7f3695f..cdedb4a 100644 --- a/tests/bfs/newerma_nonexistent.sh +++ b/tests/bfs/newerma_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs times -newerma basic/nonexistent +! invoke_bfs times -newerma basic/nonexistent diff --git a/tests/bfs/newermq.sh b/tests/bfs/newermq.sh index 2a22586..2f705dc 100644 --- a/tests/bfs/newermq.sh +++ b/tests/bfs/newermq.sh @@ -1 +1 @@ -fail invoke_bfs times -newermq times/a +! invoke_bfs times -newermq times/a diff --git a/tests/bfs/newermt_invalid.sh b/tests/bfs/newermt_invalid.sh index 61d2485..98efece 100644 --- a/tests/bfs/newermt_invalid.sh +++ b/tests/bfs/newermt_invalid.sh @@ -1 +1 @@ -fail invoke_bfs times -newermt not_a_date_time +! invoke_bfs times -newermt not_a_date_time diff --git a/tests/bfs/newerqm.sh b/tests/bfs/newerqm.sh index da84350..c0cff98 100644 --- a/tests/bfs/newerqm.sh +++ b/tests/bfs/newerqm.sh @@ -1 +1 @@ -fail invoke_bfs times -newerqm times/a +! invoke_bfs times -newerqm times/a diff --git a/tests/bfs/or_incomplete.sh b/tests/bfs/or_incomplete.sh index c941b95..4af31b6 100644 --- a/tests/bfs/or_incomplete.sh +++ b/tests/bfs/or_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs -print -o +! invoke_bfs -print -o diff --git a/tests/bfs/perm_symbolic_double_comma.sh b/tests/bfs/perm_symbolic_double_comma.sh index 66db0ac..48f9d4b 100644 --- a/tests/bfs/perm_symbolic_double_comma.sh +++ b/tests/bfs/perm_symbolic_double_comma.sh @@ -1 +1 @@ -fail invoke_bfs perms -perm a+r,,u+w +! invoke_bfs perms -perm a+r,,u+w diff --git a/tests/bfs/perm_symbolic_missing_action.sh b/tests/bfs/perm_symbolic_missing_action.sh index 3b18721..28446ab 100644 --- a/tests/bfs/perm_symbolic_missing_action.sh +++ b/tests/bfs/perm_symbolic_missing_action.sh @@ -1 +1 @@ -fail invoke_bfs perms -perm a +! invoke_bfs perms -perm a diff --git a/tests/bfs/perm_symbolic_trailing_comma.sh b/tests/bfs/perm_symbolic_trailing_comma.sh index c52ebe6..01bbc16 100644 --- a/tests/bfs/perm_symbolic_trailing_comma.sh +++ b/tests/bfs/perm_symbolic_trailing_comma.sh @@ -1 +1 @@ -fail invoke_bfs perms -perm a+r, +! invoke_bfs perms -perm a+r, diff --git a/tests/bfs/printf_duplicate_flag.sh b/tests/bfs/printf_duplicate_flag.sh index 77650d0..5ff29f1 100644 --- a/tests/bfs/printf_duplicate_flag.sh +++ b/tests/bfs/printf_duplicate_flag.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '%--p' +! invoke_bfs basic -printf '%--p' diff --git a/tests/bfs/printf_everything.sh b/tests/bfs/printf_everything.sh index 5f20718..5e95830 100644 --- a/tests/bfs/printf_everything.sh +++ b/tests/bfs/printf_everything.sh @@ -1,14 +1,14 @@ everything=(%{a,b,c,d,D,f,g,G,h,H,i,k,l,m,M,n,p,P,s,S,t,u,U,y,Y}) # Check if we have fstypes -if ! fail invoke_bfs basic -printf '%F' -quit >/dev/null; then +if ! ! invoke_bfs basic -printf '%F' -quit >/dev/null; then everything+=(%F) fi everything+=(%{A,C,T}{%,+,@,a,A,b,B,c,C,d,D,e,F,g,G,h,H,I,j,k,l,m,M,n,p,r,R,s,S,t,T,u,U,V,w,W,x,X,y,Y,z,Z}) # Check if we have birth times -if ! fail invoke_bfs basic -printf '%w' -quit >/dev/null; then +if ! ! invoke_bfs basic -printf '%w' -quit >/dev/null; then everything+=(%w %{B,W}{%,+,@,a,A,b,B,c,C,d,D,e,F,g,G,h,H,I,j,k,l,m,M,n,p,r,R,s,S,t,T,u,U,V,w,W,x,X,y,Y,z,Z}) fi diff --git a/tests/bfs/printf_incomplete_escape.sh b/tests/bfs/printf_incomplete_escape.sh index 144add5..f560d28 100644 --- a/tests/bfs/printf_incomplete_escape.sh +++ b/tests/bfs/printf_incomplete_escape.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '\' +! invoke_bfs basic -printf '\' diff --git a/tests/bfs/printf_incomplete_format.sh b/tests/bfs/printf_incomplete_format.sh index 347a0f4..92c6afc 100644 --- a/tests/bfs/printf_incomplete_format.sh +++ b/tests/bfs/printf_incomplete_format.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '%' +! invoke_bfs basic -printf '%' diff --git a/tests/bfs/printf_invalid_escape.sh b/tests/bfs/printf_invalid_escape.sh index ce12233..4338f9b 100644 --- a/tests/bfs/printf_invalid_escape.sh +++ b/tests/bfs/printf_invalid_escape.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '\!' +! invoke_bfs basic -printf '\!' diff --git a/tests/bfs/printf_invalid_format.sh b/tests/bfs/printf_invalid_format.sh index 1717615..59d63a7 100644 --- a/tests/bfs/printf_invalid_format.sh +++ b/tests/bfs/printf_invalid_format.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '%!' +! invoke_bfs basic -printf '%!' diff --git a/tests/bfs/printf_must_be_numeric.sh b/tests/bfs/printf_must_be_numeric.sh index eabb3d6..7c7c3fa 100644 --- a/tests/bfs/printf_must_be_numeric.sh +++ b/tests/bfs/printf_must_be_numeric.sh @@ -1 +1 @@ -fail invoke_bfs basic -printf '%+p' +! invoke_bfs basic -printf '%+p' diff --git a/tests/bfs/stderr_fails_loudly.sh b/tests/bfs/stderr_fails_loudly.sh index c423d9e..8572d5a 100644 --- a/tests/bfs/stderr_fails_loudly.sh +++ b/tests/bfs/stderr_fails_loudly.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs -D all basic -false -fprint /dev/full 2>/dev/full +! invoke_bfs -D all basic -false -fprint /dev/full 2>/dev/full diff --git a/tests/bfs/unexpected_operator.sh b/tests/bfs/unexpected_operator.sh index b3658f6..2eb0e71 100644 --- a/tests/bfs/unexpected_operator.sh +++ b/tests/bfs/unexpected_operator.sh @@ -1 +1 @@ -fail invoke_bfs \! -o -print +! invoke_bfs \! -o -print diff --git a/tests/bfs/xtype_depth.sh b/tests/bfs/xtype_depth.sh index cd478af..02c8173 100644 --- a/tests/bfs/xtype_depth.sh +++ b/tests/bfs/xtype_depth.sh @@ -1,2 +1,2 @@ # Make sure -xtype is considered side-effecting for facts_when_impure -fail invoke_bfs loops -xtype l -depth 100 +! invoke_bfs loops -xtype l -depth 100 diff --git a/tests/bsd/f_incomplete.sh b/tests/bsd/f_incomplete.sh index acb63af..50afe42 100644 --- a/tests/bsd/f_incomplete.sh +++ b/tests/bsd/f_incomplete.sh @@ -1,2 +1,2 @@ -fail invoke_bfs -f +! invoke_bfs -f diff --git a/tests/bsd/mtime_bad_unit.sh b/tests/bsd/mtime_bad_unit.sh index 3921f80..6e2caf1 100644 --- a/tests/bsd/mtime_bad_unit.sh +++ b/tests/bsd/mtime_bad_unit.sh @@ -1 +1 @@ -fail invoke_bfs times -mtime +1q +! invoke_bfs times -mtime +1q diff --git a/tests/bsd/mtime_missing_unit.sh b/tests/bsd/mtime_missing_unit.sh index 3ac4c97..f6b1f93 100644 --- a/tests/bsd/mtime_missing_unit.sh +++ b/tests/bsd/mtime_missing_unit.sh @@ -1 +1 @@ -fail invoke_bfs times -mtime +1w2 +! invoke_bfs times -mtime +1w2 diff --git a/tests/common/maxdepth_incomplete.sh b/tests/common/maxdepth_incomplete.sh index 536dcf5..0bcb461 100644 --- a/tests/common/maxdepth_incomplete.sh +++ b/tests/common/maxdepth_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs basic -maxdepth +! invoke_bfs basic -maxdepth diff --git a/tests/common/mindepth_incomplete.sh b/tests/common/mindepth_incomplete.sh index 19a3b21..6f55a42 100644 --- a/tests/common/mindepth_incomplete.sh +++ b/tests/common/mindepth_incomplete.sh @@ -1 +1 @@ -fail invoke_bfs basic -mindepth +! invoke_bfs basic -mindepth diff --git a/tests/gnu/exec_flush_fail.sh b/tests/gnu/exec_flush_fail.sh index ce796b2..5505f7a 100644 --- a/tests/gnu/exec_flush_fail.sh +++ b/tests/gnu/exec_flush_fail.sh @@ -1,3 +1,3 @@ # Failure to flush streams before exec should be caught test -e /dev/full || skip -fail invoke_bfs basic -print0 -exec true \; >/dev/full +! invoke_bfs basic -print0 -exec true \; >/dev/full diff --git a/tests/gnu/exec_nothing.sh b/tests/gnu/exec_nothing.sh index 9d613e8..443aa0d 100644 --- a/tests/gnu/exec_nothing.sh +++ b/tests/gnu/exec_nothing.sh @@ -1,2 +1,2 @@ # Regression test: don't segfault on missing command -fail invoke_bfs basic -exec \; +! invoke_bfs basic -exec \; diff --git a/tests/gnu/exec_plus_flush_fail.sh b/tests/gnu/exec_plus_flush_fail.sh index 8beee09..53a50e5 100644 --- a/tests/gnu/exec_plus_flush_fail.sh +++ b/tests/gnu/exec_plus_flush_fail.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic/a -print0 -exec echo found {} + >/dev/full +! invoke_bfs basic/a -print0 -exec echo found {} + >/dev/full diff --git a/tests/gnu/files0_from_empty.sh b/tests/gnu/files0_from_empty.sh index bd4fbf4..85eee8f 100644 --- a/tests/gnu/files0_from_empty.sh +++ b/tests/gnu/files0_from_empty.sh @@ -1 +1 @@ -printf "\0" | fail invoke_bfs -files0-from - +! printf "\0" | invoke_bfs -files0-from - diff --git a/tests/gnu/files0_from_error.sh b/tests/gnu/files0_from_error.sh index ab27ea2..1515d0b 100644 --- a/tests/gnu/files0_from_error.sh +++ b/tests/gnu/files0_from_error.sh @@ -1 +1 @@ -fail invoke_bfs -files0-from basic +! invoke_bfs -files0-from basic diff --git a/tests/gnu/files0_from_none.sh b/tests/gnu/files0_from_none.sh index c6e5b97..090fce0 100644 --- a/tests/gnu/files0_from_none.sh +++ b/tests/gnu/files0_from_none.sh @@ -1 +1 @@ -printf "" | fail invoke_bfs -files0-from - +! printf "" | invoke_bfs -files0-from - diff --git a/tests/gnu/files0_from_nothing.sh b/tests/gnu/files0_from_nothing.sh index 5fdae60..fee50a8 100644 --- a/tests/gnu/files0_from_nothing.sh +++ b/tests/gnu/files0_from_nothing.sh @@ -1 +1 @@ -fail invoke_bfs -files0-from basic/nonexistent +! invoke_bfs -files0-from basic/nonexistent diff --git a/tests/gnu/files0_from_nowhere.sh b/tests/gnu/files0_from_nowhere.sh index 2337613..68eea4b 100644 --- a/tests/gnu/files0_from_nowhere.sh +++ b/tests/gnu/files0_from_nowhere.sh @@ -1 +1 @@ -fail invoke_bfs -files0-from +! invoke_bfs -files0-from diff --git a/tests/gnu/files0_from_ok.sh b/tests/gnu/files0_from_ok.sh index 5387e5c..8e145ce 100644 --- a/tests/gnu/files0_from_ok.sh +++ b/tests/gnu/files0_from_ok.sh @@ -1 +1 @@ -printf "basic\0" | fail invoke_bfs -files0-from - -ok echo {} \; +! printf "basic\0" | invoke_bfs -files0-from - -ok echo {} \; diff --git a/tests/gnu/fls_nonexistent.sh b/tests/gnu/fls_nonexistent.sh index 4756834..ff86763 100644 --- a/tests/gnu/fls_nonexistent.sh +++ b/tests/gnu/fls_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs rainbow -fls scratch/nonexistent/path +! invoke_bfs rainbow -fls scratch/nonexistent/path diff --git a/tests/gnu/fprint0_nonexistent.sh b/tests/gnu/fprint0_nonexistent.sh index d8e0f30..ec14c2d 100644 --- a/tests/gnu/fprint0_nonexistent.sh +++ b/tests/gnu/fprint0_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprint0 scratch/nonexistent/path +! invoke_bfs basic -fprint0 scratch/nonexistent/path diff --git a/tests/gnu/fprint_error.sh b/tests/gnu/fprint_error.sh index 0e75b0e..7617034 100644 --- a/tests/gnu/fprint_error.sh +++ b/tests/gnu/fprint_error.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic -maxdepth 0 -fprint /dev/full +! invoke_bfs basic -maxdepth 0 -fprint /dev/full diff --git a/tests/gnu/fprint_noarg.sh b/tests/gnu/fprint_noarg.sh index bf772f3..8511649 100644 --- a/tests/gnu/fprint_noarg.sh +++ b/tests/gnu/fprint_noarg.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprint +! invoke_bfs basic -fprint diff --git a/tests/gnu/fprint_nonexistent.sh b/tests/gnu/fprint_nonexistent.sh index b6dac8a..4409162 100644 --- a/tests/gnu/fprint_nonexistent.sh +++ b/tests/gnu/fprint_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprint scratch/nonexistent/path +! invoke_bfs basic -fprint scratch/nonexistent/path diff --git a/tests/gnu/fprintf_nofile.sh b/tests/gnu/fprintf_nofile.sh index c2c48a5..4e79002 100644 --- a/tests/gnu/fprintf_nofile.sh +++ b/tests/gnu/fprintf_nofile.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprintf +! invoke_bfs basic -fprintf diff --git a/tests/gnu/fprintf_noformat.sh b/tests/gnu/fprintf_noformat.sh index 0d285c3..fd97f4c 100644 --- a/tests/gnu/fprintf_noformat.sh +++ b/tests/gnu/fprintf_noformat.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprintf /dev/null +! invoke_bfs basic -fprintf /dev/null diff --git a/tests/gnu/fprintf_nonexistent.sh b/tests/gnu/fprintf_nonexistent.sh index 6ed141a..160e739 100644 --- a/tests/gnu/fprintf_nonexistent.sh +++ b/tests/gnu/fprintf_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs basic -fprintf scratch/nonexistent/path '%p\n' +! invoke_bfs basic -fprintf scratch/nonexistent/path '%p\n' diff --git a/tests/gnu/ignore_readdir_race_root.sh b/tests/gnu/ignore_readdir_race_root.sh index bad7391..dc41e7f 100644 --- a/tests/gnu/ignore_readdir_race_root.sh +++ b/tests/gnu/ignore_readdir_race_root.sh @@ -1,2 +1,2 @@ # Make sure -ignore_readdir_race doesn't suppress ENOENT at the root -fail invoke_bfs basic/nonexistent -ignore_readdir_race +! invoke_bfs basic/nonexistent -ignore_readdir_race diff --git a/tests/gnu/ok_nothing.sh b/tests/gnu/ok_nothing.sh index 439687b..52c3547 100644 --- a/tests/gnu/ok_nothing.sh +++ b/tests/gnu/ok_nothing.sh @@ -1,2 +1,2 @@ # Regression test: don't segfault on missing command -fail invoke_bfs basic -ok \; +! invoke_bfs basic -ok \; diff --git a/tests/gnu/print_error.sh b/tests/gnu/print_error.sh index 62a32b4..bc79637 100644 --- a/tests/gnu/print_error.sh +++ b/tests/gnu/print_error.sh @@ -1,2 +1,2 @@ test -e /dev/full || skip -fail invoke_bfs basic -maxdepth 0 >/dev/full +! invoke_bfs basic -maxdepth 0 >/dev/full diff --git a/tests/gnu/regex_error.sh b/tests/gnu/regex_error.sh index 9bd4c8d..4af933f 100644 --- a/tests/gnu/regex_error.sh +++ b/tests/gnu/regex_error.sh @@ -1 +1 @@ -fail invoke_bfs basic -regex '[' +! invoke_bfs basic -regex '[' diff --git a/tests/posix/closed_stderr.sh b/tests/posix/closed_stderr.sh index cc746ef..570d5bb 100644 --- a/tests/posix/closed_stderr.sh +++ b/tests/posix/closed_stderr.sh @@ -1 +1 @@ -fail invoke_bfs basic >&- 2>&- +! invoke_bfs basic >&- 2>&- diff --git a/tests/posix/closed_stdout.sh b/tests/posix/closed_stdout.sh index 446bf56..25c060d 100644 --- a/tests/posix/closed_stdout.sh +++ b/tests/posix/closed_stdout.sh @@ -1 +1 @@ -fail invoke_bfs basic >&- +! invoke_bfs basic >&- diff --git a/tests/posix/exec_plus_nothing.sh b/tests/posix/exec_plus_nothing.sh index ef01968..347722d 100644 --- a/tests/posix/exec_plus_nothing.sh +++ b/tests/posix/exec_plus_nothing.sh @@ -1,2 +1,2 @@ # Regression test: don't look OOB for {} + -fail invoke_bfs basic -exec + +! invoke_bfs basic -exec + diff --git a/tests/posix/extra_paren.sh b/tests/posix/extra_paren.sh index cd8e8f8..d15022f 100644 --- a/tests/posix/extra_paren.sh +++ b/tests/posix/extra_paren.sh @@ -1 +1 @@ -fail invoke_bfs basic -print \) +! invoke_bfs basic -print \) diff --git a/tests/posix/incomplete.sh b/tests/posix/incomplete.sh index 07b1b61..bca5a13 100644 --- a/tests/posix/incomplete.sh +++ b/tests/posix/incomplete.sh @@ -1 +1 @@ -fail invoke_bfs basic \( +! invoke_bfs basic \( diff --git a/tests/posix/missing_paren.sh b/tests/posix/missing_paren.sh index ac8dd60..d906fbe 100644 --- a/tests/posix/missing_paren.sh +++ b/tests/posix/missing_paren.sh @@ -1 +1 @@ -fail invoke_bfs basic \( -print +! invoke_bfs basic \( -print diff --git a/tests/posix/newer_nonexistent.sh b/tests/posix/newer_nonexistent.sh index 789cadf..5f2da4b 100644 --- a/tests/posix/newer_nonexistent.sh +++ b/tests/posix/newer_nonexistent.sh @@ -1 +1 @@ -fail invoke_bfs times -newer times/nonexistent +! invoke_bfs times -newer times/nonexistent diff --git a/tests/posix/ok_plus_nothing.sh b/tests/posix/ok_plus_nothing.sh index 7cb7de5..77c7644 100644 --- a/tests/posix/ok_plus_nothing.sh +++ b/tests/posix/ok_plus_nothing.sh @@ -1,2 +1,2 @@ # Regression test: don't look OOB for {} + -fail invoke_bfs basic -ok + +! invoke_bfs basic -ok + diff --git a/tests/posix/readdir_error.sh b/tests/posix/readdir_error.sh index 483f543..e45ec5c 100644 --- a/tests/posix/readdir_error.sh +++ b/tests/posix/readdir_error.sh @@ -34,4 +34,4 @@ done # On Linux, open(/proc/$pid/net) will succeed but readdir() will fail test -r "/proc/$pid/net" || skip -fail invoke_bfs "/proc/$pid/net" >/dev/null +! invoke_bfs "/proc/$pid/net" >/dev/null diff --git a/tests/tests.sh b/tests/tests.sh index 3047bf4..844de5c 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -476,19 +476,13 @@ function bfs_verbose() { function invoke_bfs() { bfs_verbose "$@" "${BFS[@]}" "$@" -} - -# Expect a command to fail, but not crash -function fail() { - "$@" - local STATUS="$?" + local status=$? - if ((STATUS > 125)); then - exit "$STATUS" - elif ((STATUS > 0)); then - return 0 + # Allow bfs to fail, but not crash + if ((status > 125)); then + exit $status else - return 1 + return $status fi } -- cgit v1.2.3 From e50c19f284dad6b4b7b79f91cc8576a97626be8a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 15:32:04 -0500 Subject: tests: Turn on set -e --- tests/bfs/D_unknown.sh | 2 +- tests/bfs/O9.sh | 2 +- tests/bfs/execdir_plus_nonexistent.sh | 7 +++---- tests/bfs/help.sh | 10 ++++------ tests/bsd/X.sh | 3 +-- tests/bsd/exit.sh | 10 ++-------- tests/common/L_mount.sh | 7 +++---- tests/common/execdir_nonexistent.sh | 7 +++---- tests/common/inum_bind_mount.sh | 6 ++---- tests/common/inum_mount.sh | 6 ++---- tests/common/mount.sh | 7 +++---- tests/gnu/L_delete.sh | 2 +- tests/gnu/L_loops_continue.sh | 3 +-- tests/gnu/inum_automount.sh | 8 +++----- tests/gnu/printf_Y_error.sh | 10 +++------- tests/gnu/xtype_bind_mount.sh | 8 +++----- tests/posix/L_loops.sh | 2 +- tests/posix/L_xdev.sh | 7 +++---- tests/posix/depth_error.sh | 10 +++------- tests/posix/exec_nonexistent.sh | 7 +++---- tests/posix/exec_plus_nonexistent.sh | 7 +++---- tests/posix/exec_plus_status.sh | 3 +-- tests/posix/readdir_error.sh | 2 +- tests/posix/type_bind_mount.sh | 6 ++---- tests/posix/xdev.sh | 7 +++---- tests/tests.sh | 18 +++++++++++++----- 26 files changed, 69 insertions(+), 98 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/D_unknown.sh b/tests/bfs/D_unknown.sh index e3614ba..cac9bd9 100644 --- a/tests/bfs/D_unknown.sh +++ b/tests/bfs/D_unknown.sh @@ -1,4 +1,4 @@ stderr=$(invoke_bfs -warn -D unknown basic 2>&1 >"$OUT") -[ -n "$stderr" ] || return 1 +[ -n "$stderr" ] sort_output diff_output diff --git a/tests/bfs/O9.sh b/tests/bfs/O9.sh index 12f6c2d..c12a7a3 100644 --- a/tests/bfs/O9.sh +++ b/tests/bfs/O9.sh @@ -1,4 +1,4 @@ stderr=$(invoke_bfs -warn -O9 basic 2>&1 >"$OUT") -[ -n "$stderr" ] || return 1 +[ -n "$stderr" ] sort_output diff_output diff --git a/tests/bfs/execdir_plus_nonexistent.sh b/tests/bfs/execdir_plus_nonexistent.sh index 8436953..88f3e90 100644 --- a/tests/bfs/execdir_plus_nonexistent.sh +++ b/tests/bfs/execdir_plus_nonexistent.sh @@ -1,5 +1,4 @@ -stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} + 2>&1 >/dev/null) -[ -n "$stderr" ] || return 1 +! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} + 2>&1 >/dev/null) +[ -n "$stderr" ] -bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print -(($? == EX_BFS)) +check_exit $EX_BFS bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print diff --git a/tests/bfs/help.sh b/tests/bfs/help.sh index 5e5c684..2c0b28a 100644 --- a/tests/bfs/help.sh +++ b/tests/bfs/help.sh @@ -1,6 +1,4 @@ -invoke_bfs -help | grep -E '\{...?\}' && return 1 -invoke_bfs -D help | grep -E '\{...?\}' && return 1 -invoke_bfs -S help | grep -E '\{...?\}' && return 1 -invoke_bfs -regextype help | grep -E '\{...?\}' && return 1 - -return 0 +! invoke_bfs -help | grep -E '\{...?\}' +! invoke_bfs -D help | grep -E '\{...?\}' +! invoke_bfs -S help | grep -E '\{...?\}' +! invoke_bfs -regextype help | grep -E '\{...?\}' diff --git a/tests/bsd/X.sh b/tests/bsd/X.sh index 03d9eee..df9a261 100644 --- a/tests/bsd/X.sh +++ b/tests/bsd/X.sh @@ -1,2 +1 @@ -bfs_diff -X weirdnames -[ $? -eq $EX_BFS ] +check_exit $EX_BFS bfs_diff -X weirdnames diff --git a/tests/bsd/exit.sh b/tests/bsd/exit.sh index 524a75f..248349c 100644 --- a/tests/bsd/exit.sh +++ b/tests/bsd/exit.sh @@ -1,11 +1,5 @@ -invoke_bfs basic -name foo -exit 42 -if [ $? -ne 42 ]; then - return 1 -fi +check_exit 42 invoke_bfs basic -name foo -exit 42 -invoke_bfs basic -name qux -exit 42 -if [ $? -ne 0 ]; then - return 1 -fi +check_exit 0 invoke_bfs basic -name qux -exit 42 bfs_diff basic/g -print -name g -exit diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh index 5b56762..9ab785e 100644 --- a/tests/common/L_mount.sh +++ b/tests/common/L_mount.sh @@ -3,13 +3,12 @@ test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} + sudo mount -t tmpfs tmpfs scratch/mnt +trap "sudo umount scratch/mnt" EXIT + ln -s ../mnt scratch/foo/bar "$XTOUCH" scratch/mnt/baz ln -s ../mnt/baz scratch/foo/qux bfs_diff -L scratch -mount -ret=$? - -sudo umount scratch/mnt -return $ret diff --git a/tests/common/execdir_nonexistent.sh b/tests/common/execdir_nonexistent.sh index 5d116e5..af17fe5 100644 --- a/tests/common/execdir_nonexistent.sh +++ b/tests/common/execdir_nonexistent.sh @@ -1,5 +1,4 @@ -stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) -[ -n "$stderr" ] || return 1 +! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) +[ -n "$stderr" ] -bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print -(($? == EX_BFS)) +check_exit $EX_BFS bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print diff --git a/tests/common/inum_bind_mount.sh b/tests/common/inum_bind_mount.sh index a9e01bf..625ee3d 100644 --- a/tests/common/inum_bind_mount.sh +++ b/tests/common/inum_bind_mount.sh @@ -3,10 +3,8 @@ test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{foo,bar} + sudo mount --bind scratch/{foo,bar} +trap "sudo umount scratch/bar" EXIT bfs_diff scratch -inum "$(inum scratch/bar)" -ret=$? - -sudo umount scratch/bar -return $ret diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh index 7cc5e40..91d06e2 100644 --- a/tests/common/inum_mount.sh +++ b/tests/common/inum_mount.sh @@ -3,10 +3,8 @@ test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} + sudo mount -t tmpfs tmpfs scratch/mnt +trap "sudo umount scratch/mnt" EXIT bfs_diff scratch -inum "$(inum scratch/mnt)" -ret=$? - -sudo umount scratch/mnt -return $ret diff --git a/tests/common/mount.sh b/tests/common/mount.sh index f077ea2..3f3ed38 100644 --- a/tests/common/mount.sh +++ b/tests/common/mount.sh @@ -3,11 +3,10 @@ test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} + sudo mount -t tmpfs tmpfs scratch/mnt +trap "sudo umount scratch/mnt" EXIT + "$XTOUCH" scratch/foo/bar scratch/mnt/baz bfs_diff scratch -mount -ret=$? - -sudo umount scratch/mnt -return $ret diff --git a/tests/gnu/L_delete.sh b/tests/gnu/L_delete.sh index 6ec167c..8fdb12a 100644 --- a/tests/gnu/L_delete.sh +++ b/tests/gnu/L_delete.sh @@ -4,6 +4,6 @@ mkdir scratch/bar ln -s ../foo scratch/bar/baz # Don't try to rmdir() a symlink -invoke_bfs -L scratch/bar -delete || return 1 +invoke_bfs -L scratch/bar -delete bfs_diff scratch diff --git a/tests/gnu/L_loops_continue.sh b/tests/gnu/L_loops_continue.sh index 0244137..d4c95f1 100644 --- a/tests/gnu/L_loops_continue.sh +++ b/tests/gnu/L_loops_continue.sh @@ -1,2 +1 @@ -bfs_diff -L loops -[ $? -eq $EX_BFS ] +check_exit $EX_BFS bfs_diff -L loops diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh index 6bf2977..da2e3b0 100644 --- a/tests/gnu/inum_automount.sh +++ b/tests/gnu/inum_automount.sh @@ -5,13 +5,11 @@ command -v systemd-mount &>/dev/null || skip clean_scratch mkdir scratch/{foo,automnt} + sudo systemd-mount -A -o bind basic scratch/automnt || skip +trap "sudo systemd-umount scratch/automnt" EXIT before=$(inum scratch/automnt) bfs_diff scratch -inum "$before" -prune -ret=$? after=$(inum scratch/automnt) - -sudo systemd-umount scratch/automnt - -((ret == 0 && before == after)) +((before == after)) diff --git a/tests/gnu/printf_Y_error.sh b/tests/gnu/printf_Y_error.sh index 6487711..e9a2083 100644 --- a/tests/gnu/printf_Y_error.sh +++ b/tests/gnu/printf_Y_error.sh @@ -1,12 +1,8 @@ clean_scratch mkdir scratch/foo -chmod -x scratch/foo ln -s foo/bar scratch/bar -bfs_diff scratch -printf '(%p) (%l) %y %Y\n' -ret=$? - -chmod +x scratch/foo -clean_scratch +chmod -x scratch/foo +trap "chmod +x scratch/foo" EXIT -[ $ret -eq $EX_BFS ] +check_exit $EX_BFS 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 a6dbed3..9babd9d 100644 --- a/tests/gnu/xtype_bind_mount.sh +++ b/tests/gnu/xtype_bind_mount.sh @@ -3,11 +3,9 @@ test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} -sudo mount --bind /dev/null scratch/null ln -s /dev/null scratch/link -bfs_diff -L scratch -type c -ret=$? +sudo mount --bind /dev/null scratch/null +trap "sudo umount scratch/null" EXIT -sudo umount scratch/null -return $ret +bfs_diff -L scratch -type c diff --git a/tests/posix/L_loops.sh b/tests/posix/L_loops.sh index f737cea..1314401 100644 --- a/tests/posix/L_loops.sh +++ b/tests/posix/L_loops.sh @@ -1,4 +1,4 @@ # POSIX says it's okay to either stop or keep going on seeing a filesystem # loop, as long as a diagnostic is printed -errors=$(invoke_bfs -L loops 2>&1 >/dev/null) +! errors=$(invoke_bfs -L loops 2>&1 >/dev/null) [ -n "$errors" ] diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh index 2fc99dd..587c8bb 100644 --- a/tests/posix/L_xdev.sh +++ b/tests/posix/L_xdev.sh @@ -3,13 +3,12 @@ test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} + sudo mount -t tmpfs tmpfs scratch/mnt +trap "sudo umount scratch/mnt" EXIT + ln -s ../mnt scratch/foo/bar "$XTOUCH" scratch/mnt/baz ln -s ../mnt/baz scratch/foo/qux bfs_diff -L scratch -xdev -ret=$? - -sudo umount scratch/mnt -return $ret diff --git a/tests/posix/depth_error.sh b/tests/posix/depth_error.sh index f770210..15cc82d 100644 --- a/tests/posix/depth_error.sh +++ b/tests/posix/depth_error.sh @@ -1,11 +1,7 @@ clean_scratch "$XTOUCH" -p scratch/foo/bar -chmod a-r scratch/foo - -bfs_diff scratch -depth -ret=$? -chmod +r scratch/foo -clean_scratch +chmod a-r scratch/foo +trap "chmod +r scratch/foo" EXIT -[ $ret -eq $EX_BFS ] +check_exit $EX_BFS bfs_diff scratch -depth diff --git a/tests/posix/exec_nonexistent.sh b/tests/posix/exec_nonexistent.sh index b4e08e0..d4ad92a 100644 --- a/tests/posix/exec_nonexistent.sh +++ b/tests/posix/exec_nonexistent.sh @@ -1,8 +1,7 @@ # Failure to execute the command should lead to an error message and # non-zero exit status. See https://unix.stackexchange.com/q/704522/56202 -stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) -[ -n "$stderr" ] || return 1 +! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) +[ -n "$stderr" ] -bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print -(($? == EX_BFS)) +check_exit $EX_BFS bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print diff --git a/tests/posix/exec_plus_nonexistent.sh b/tests/posix/exec_plus_nonexistent.sh index f96099e..6c9cb8c 100644 --- a/tests/posix/exec_plus_nonexistent.sh +++ b/tests/posix/exec_plus_nonexistent.sh @@ -1,5 +1,4 @@ -stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} + 2>&1 >/dev/null) -[ -n "$stderr" ] || return 1 +! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} + 2>&1 >/dev/null) +[ -n "$stderr" ] -bfs_diff basic -exec "$TESTS/nonexistent" {} + -print -(($? == EX_BFS)) +check_exit $EX_BFS bfs_diff basic -exec "$TESTS/nonexistent" {} + -print diff --git a/tests/posix/exec_plus_status.sh b/tests/posix/exec_plus_status.sh index ea9e5ef..f44062e 100644 --- a/tests/posix/exec_plus_status.sh +++ b/tests/posix/exec_plus_status.sh @@ -1,4 +1,3 @@ # -exec ... {} + should always return true, but if the command fails, bfs # should exit with a non-zero status -bfs_diff basic -exec false {} + -print -(($? == EX_BFS)) +check_exit $EX_BFS bfs_diff basic -exec false {} + -print diff --git a/tests/posix/readdir_error.sh b/tests/posix/readdir_error.sh index e45ec5c..9a002a1 100644 --- a/tests/posix/readdir_error.sh +++ b/tests/posix/readdir_error.sh @@ -29,7 +29,7 @@ kill -9 "$pid" # Wait until it's really a zombie state=R while [ "$state" != "Z" ]; do - read -r _ _ state _ <"/proc/$pid/stat" || exit 1 + read -r _ _ state _ <"/proc/$pid/stat" done # On Linux, open(/proc/$pid/net) will succeed but readdir() will fail diff --git a/tests/posix/type_bind_mount.sh b/tests/posix/type_bind_mount.sh index fe32875..2d913db 100644 --- a/tests/posix/type_bind_mount.sh +++ b/tests/posix/type_bind_mount.sh @@ -3,10 +3,8 @@ test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} + sudo mount --bind /dev/null scratch/null +trap "sudo umount scratch/null" EXIT bfs_diff scratch -type c -ret=$? - -sudo umount scratch/null -return $ret diff --git a/tests/posix/xdev.sh b/tests/posix/xdev.sh index 44e04dd..9d21b14 100644 --- a/tests/posix/xdev.sh +++ b/tests/posix/xdev.sh @@ -3,11 +3,10 @@ test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} + sudo mount -t tmpfs tmpfs scratch/mnt +trap "sudo umount scratch/mnt" EXIT + "$XTOUCH" scratch/foo/bar scratch/mnt/baz bfs_diff scratch -xdev -ret=$? - -sudo umount scratch/mnt -return $ret diff --git a/tests/tests.sh b/tests/tests.sh index 844de5c..7ccd9b5 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -476,16 +476,24 @@ function bfs_verbose() { function invoke_bfs() { bfs_verbose "$@" "${BFS[@]}" "$@" - local status=$? + local status="$?" # Allow bfs to fail, but not crash if ((status > 125)); then - exit $status + exit "$status" else - return $status + return "$status" fi } +function check_exit() { + local expected="$1" + local actual="0" + shift + "$@" || actual="$?" + ((actual == expected)) +} + # Detect colored diff support if [ -t 2 ] && diff --color=always /dev/null /dev/null 2>/dev/null; then DIFF="diff --color=always" @@ -655,9 +663,9 @@ for TEST in "${TEST_CASES[@]}"; do mkdir -p "${OUT%/*}" if [ "$VERBOSE_ERRORS" ]; then - (. "$TESTS/$TEST.sh") + (set -e; . "$TESTS/$TEST.sh") else - (. "$TESTS/$TEST.sh") 2>"$TMP/stderr" + (set -e; . "$TESTS/$TEST.sh") 2>"$TMP/stderr" fi status=$? -- cgit v1.2.3 From e82a7f51d4b4e90df868e5410a0445b009b54ff2 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 15:51:59 -0500 Subject: tests: Print the skip reason for --verbose=skipped --- tests/bfs/execdir_plus.sh | 5 +---- tests/tests.sh | 13 +++++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/execdir_plus.sh b/tests/bfs/execdir_plus.sh index 9ae7764..f66b898 100644 --- a/tests/bfs/execdir_plus.sh +++ b/tests/bfs/execdir_plus.sh @@ -1,7 +1,4 @@ tree=$(invoke_bfs -D tree 2>&1 -quit) - -if [[ "$tree" == *"-S dfs"* ]]; then - skip -fi +[[ "$tree" == *"-S dfs"* ]] && skip bfs_diff basic -execdir "$TESTS/sort-args.sh" {} + diff --git a/tests/tests.sh b/tests/tests.sh index 7ccd9b5..4a95fca 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -191,7 +191,6 @@ for arg; do VERBOSE_SKIPPED=yes ;; --verbose=tests) - VERBOSE_SKIPPED=yes VERBOSE_TESTS=yes ;; --verbose) @@ -542,6 +541,15 @@ function bfs_diff() ( ) function skip() { + if [ "$VERBOSE_SKIPPED" ]; then + caller | { + read -r line file + printf "${BOL}${CYN}%s skipped!${RST} (%s)\n" "$TEST" "$(awk "NR == $line" "$file")" + } + elif [ "$VERBOSE_TESTS" ]; then + printf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST" + fi + exit $EX_SKIP } @@ -673,9 +681,6 @@ for TEST in "${TEST_CASES[@]}"; do ((++passed)) elif ((status == EX_SKIP)); then ((++skipped)) - if [ "$VERBOSE_SKIPPED" ]; then - printf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST" - fi else ((++failed)) [ "$VERBOSE_ERRORS" ] || cat "$TMP/stderr" >&2 -- cgit v1.2.3 From e2b540c9e2a52500b17fa1005b26b2dd5a652c09 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 16:43:25 -0500 Subject: tests: Fail early in bfs_diff if the diff fails Otherwise, propagate the exit code from bfs --- tests/bfs/execdir_plus_nonexistent.sh | 2 +- tests/bsd/X.sh | 2 +- tests/common/execdir_nonexistent.sh | 2 +- tests/gnu/L_loops_continue.sh | 2 +- tests/gnu/printf_Y_error.sh | 2 +- tests/posix/depth_error.sh | 2 +- tests/posix/exec_nonexistent.sh | 2 +- tests/posix/exec_plus_nonexistent.sh | 2 +- tests/posix/exec_plus_status.sh | 2 +- tests/tests.sh | 13 +++---------- 10 files changed, 12 insertions(+), 19 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/execdir_plus_nonexistent.sh b/tests/bfs/execdir_plus_nonexistent.sh index 88f3e90..e3b4d2d 100644 --- a/tests/bfs/execdir_plus_nonexistent.sh +++ b/tests/bfs/execdir_plus_nonexistent.sh @@ -1,4 +1,4 @@ ! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} + 2>&1 >/dev/null) [ -n "$stderr" ] -check_exit $EX_BFS bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print +! bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print diff --git a/tests/bsd/X.sh b/tests/bsd/X.sh index df9a261..54000cf 100644 --- a/tests/bsd/X.sh +++ b/tests/bsd/X.sh @@ -1 +1 @@ -check_exit $EX_BFS bfs_diff -X weirdnames +! bfs_diff -X weirdnames diff --git a/tests/common/execdir_nonexistent.sh b/tests/common/execdir_nonexistent.sh index af17fe5..4bb4fdb 100644 --- a/tests/common/execdir_nonexistent.sh +++ b/tests/common/execdir_nonexistent.sh @@ -1,4 +1,4 @@ ! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) [ -n "$stderr" ] -check_exit $EX_BFS bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print +! bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print diff --git a/tests/gnu/L_loops_continue.sh b/tests/gnu/L_loops_continue.sh index d4c95f1..55aeb33 100644 --- a/tests/gnu/L_loops_continue.sh +++ b/tests/gnu/L_loops_continue.sh @@ -1 +1 @@ -check_exit $EX_BFS bfs_diff -L loops +! bfs_diff -L loops diff --git a/tests/gnu/printf_Y_error.sh b/tests/gnu/printf_Y_error.sh index e9a2083..3aa816e 100644 --- a/tests/gnu/printf_Y_error.sh +++ b/tests/gnu/printf_Y_error.sh @@ -5,4 +5,4 @@ ln -s foo/bar scratch/bar chmod -x scratch/foo trap "chmod +x scratch/foo" EXIT -check_exit $EX_BFS bfs_diff scratch -printf '(%p) (%l) %y %Y\n' +! bfs_diff scratch -printf '(%p) (%l) %y %Y\n' diff --git a/tests/posix/depth_error.sh b/tests/posix/depth_error.sh index 15cc82d..e91fbf6 100644 --- a/tests/posix/depth_error.sh +++ b/tests/posix/depth_error.sh @@ -4,4 +4,4 @@ clean_scratch chmod a-r scratch/foo trap "chmod +r scratch/foo" EXIT -check_exit $EX_BFS bfs_diff scratch -depth +! bfs_diff scratch -depth diff --git a/tests/posix/exec_nonexistent.sh b/tests/posix/exec_nonexistent.sh index d4ad92a..901be86 100644 --- a/tests/posix/exec_nonexistent.sh +++ b/tests/posix/exec_nonexistent.sh @@ -4,4 +4,4 @@ ! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) [ -n "$stderr" ] -check_exit $EX_BFS bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print +! bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print diff --git a/tests/posix/exec_plus_nonexistent.sh b/tests/posix/exec_plus_nonexistent.sh index 6c9cb8c..6bddc67 100644 --- a/tests/posix/exec_plus_nonexistent.sh +++ b/tests/posix/exec_plus_nonexistent.sh @@ -1,4 +1,4 @@ ! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} + 2>&1 >/dev/null) [ -n "$stderr" ] -check_exit $EX_BFS bfs_diff basic -exec "$TESTS/nonexistent" {} + -print +! bfs_diff basic -exec "$TESTS/nonexistent" {} + -print diff --git a/tests/posix/exec_plus_status.sh b/tests/posix/exec_plus_status.sh index f44062e..a814c4e 100644 --- a/tests/posix/exec_plus_status.sh +++ b/tests/posix/exec_plus_status.sh @@ -1,3 +1,3 @@ # -exec ... {} + should always return true, but if the command fails, bfs # should exit with a non-zero status -check_exit $EX_BFS bfs_diff basic -exec false {} + -print +! bfs_diff basic -exec false {} + -print diff --git a/tests/tests.sh b/tests/tests.sh index 4a95fca..9bf9b8b 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -500,8 +500,6 @@ else DIFF="diff" fi -# Return value when bfs fails -EX_BFS=10 # Return value when a difference is detected EX_DIFF=20 # Return value when a test is skipped @@ -529,15 +527,10 @@ function bfs_diff() ( exec 3>&- "${BFS[@]}" "$@" | sort >"$OUT" - local STATUS="${PIPESTATUS[0]}" + local status="${PIPESTATUS[0]}" - diff_output || return $EX_DIFF - - if [ "$STATUS" -eq 0 ]; then - return 0 - else - return $EX_BFS - fi + diff_output || exit $EX_DIFF + return "$status" ) function skip() { -- cgit v1.2.3 From 0040a91a6b3a192acfeec0ae1e24516b54ba872a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 16 Dec 2022 18:03:44 -0500 Subject: tests/bfs/printf_everything: Simplify double negation --- tests/bfs/printf_everything.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/printf_everything.sh b/tests/bfs/printf_everything.sh index 5e95830..07d574a 100644 --- a/tests/bfs/printf_everything.sh +++ b/tests/bfs/printf_everything.sh @@ -1,14 +1,14 @@ everything=(%{a,b,c,d,D,f,g,G,h,H,i,k,l,m,M,n,p,P,s,S,t,u,U,y,Y}) # Check if we have fstypes -if ! ! invoke_bfs basic -printf '%F' -quit >/dev/null; then +if invoke_bfs basic -printf '%F' -quit >/dev/null; then everything+=(%F) fi everything+=(%{A,C,T}{%,+,@,a,A,b,B,c,C,d,D,e,F,g,G,h,H,I,j,k,l,m,M,n,p,r,R,s,S,t,T,u,U,V,w,W,x,X,y,Y,z,Z}) # Check if we have birth times -if ! ! invoke_bfs basic -printf '%w' -quit >/dev/null; then +if invoke_bfs basic -printf '%w' -quit >/dev/null; then everything+=(%w %{B,W}{%,+,@,a,A,b,B,c,C,d,D,e,F,g,G,h,H,I,j,k,l,m,M,n,p,r,R,s,S,t,T,u,U,V,w,W,x,X,y,Y,z,Z}) fi -- cgit v1.2.3 From b6859d7a6f7e0b3a3cb70fa75e7e46998e8f0f03 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 29 Dec 2022 13:05:06 -0500 Subject: tests: Use bfs_sudo wrapper instead of testing $SUDO --- tests/bfs/L_capable.sh | 3 +-- tests/bfs/capable.sh | 3 +-- tests/common/L_mount.sh | 5 ++--- tests/common/inum_bind_mount.sh | 5 ++--- tests/common/inum_mount.sh | 5 ++--- tests/common/mount.sh | 5 ++--- tests/gnu/fstype_stacked.sh | 9 ++++----- tests/gnu/inum_automount.sh | 5 ++--- tests/gnu/xtype_bind_mount.sh | 5 ++--- tests/posix/L_xdev.sh | 5 ++--- tests/posix/type_bind_mount.sh | 5 ++--- tests/posix/xdev.sh | 5 ++--- tests/tests.sh | 40 +++++++++++++++++++++++++--------------- 13 files changed, 49 insertions(+), 51 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/L_capable.sh b/tests/bfs/L_capable.sh index a349677..232d6ac 100644 --- a/tests/bfs/L_capable.sh +++ b/tests/bfs/L_capable.sh @@ -1,4 +1,3 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch @@ -6,7 +5,7 @@ clean_scratch invoke_bfs scratch -quit -capable || skip "$XTOUCH" scratch/{normal,capable} -sudo setcap all+ep scratch/capable +bfs_sudo setcap all+ep scratch/capable || skip ln -s capable scratch/link bfs_diff -L scratch -capable diff --git a/tests/bfs/capable.sh b/tests/bfs/capable.sh index 8b60ea6..e5cad63 100644 --- a/tests/bfs/capable.sh +++ b/tests/bfs/capable.sh @@ -1,4 +1,3 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch @@ -6,7 +5,7 @@ clean_scratch invoke_bfs scratch -quit -capable || skip "$XTOUCH" scratch/{normal,capable} -sudo setcap all+ep scratch/capable +bfs_sudo setcap all+ep scratch/capable || skip ln -s capable scratch/link bfs_diff scratch -capable diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh index 9ab785e..b04acd0 100644 --- a/tests/common/L_mount.sh +++ b/tests/common/L_mount.sh @@ -1,11 +1,10 @@ -test "$SUDO" || skip test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT 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 625ee3d..ecb4ec3 100644 --- a/tests/common/inum_bind_mount.sh +++ b/tests/common/inum_bind_mount.sh @@ -1,10 +1,9 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{foo,bar} -sudo mount --bind scratch/{foo,bar} -trap "sudo umount scratch/bar" EXIT +bfs_sudo mount --bind scratch/{foo,bar} || skip +trap "bfs_sudo umount scratch/bar" EXIT bfs_diff scratch -inum "$(inum scratch/bar)" diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh index 91d06e2..a4832e4 100644 --- a/tests/common/inum_mount.sh +++ b/tests/common/inum_mount.sh @@ -1,10 +1,9 @@ -test "$SUDO" || skip test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT bfs_diff scratch -inum "$(inum scratch/mnt)" diff --git a/tests/common/mount.sh b/tests/common/mount.sh index 3f3ed38..b13b43c 100644 --- a/tests/common/mount.sh +++ b/tests/common/mount.sh @@ -1,11 +1,10 @@ -test "$SUDO" || skip test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT "$XTOUCH" scratch/foo/bar scratch/mnt/baz diff --git a/tests/gnu/fstype_stacked.sh b/tests/gnu/fstype_stacked.sh index 7110402..16f428f 100644 --- a/tests/gnu/fstype_stacked.sh +++ b/tests/gnu/fstype_stacked.sh @@ -1,13 +1,12 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch mkdir scratch/mnt -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT -sudo mount -t ramfs ramfs scratch/mnt -trap "sudo umount scratch/mnt; sudo umount scratch/mnt" EXIT +bfs_sudo mount -t ramfs ramfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt; bfs_sudo umount scratch/mnt" EXIT bfs_diff scratch/mnt -fstype ramfs -print -o -printf '%p: %F\n' diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh index da2e3b0..c4450ef 100644 --- a/tests/gnu/inum_automount.sh +++ b/tests/gnu/inum_automount.sh @@ -1,13 +1,12 @@ # bfs shouldn't trigger automounts unless it descends into them -test "$SUDO" || skip command -v systemd-mount &>/dev/null || skip clean_scratch mkdir scratch/{foo,automnt} -sudo systemd-mount -A -o bind basic scratch/automnt || skip -trap "sudo systemd-umount scratch/automnt" EXIT +bfs_sudo systemd-mount -A -o bind basic scratch/automnt || skip +trap "bfs_sudo systemd-umount scratch/automnt" EXIT before=$(inum scratch/automnt) bfs_diff scratch -inum "$before" -prune diff --git a/tests/gnu/xtype_bind_mount.sh b/tests/gnu/xtype_bind_mount.sh index 9babd9d..99a11ab 100644 --- a/tests/gnu/xtype_bind_mount.sh +++ b/tests/gnu/xtype_bind_mount.sh @@ -1,11 +1,10 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} ln -s /dev/null scratch/link -sudo mount --bind /dev/null scratch/null -trap "sudo umount scratch/null" EXIT +bfs_sudo mount --bind /dev/null scratch/null || skip +trap "bfs_sudo umount scratch/null" EXIT bfs_diff -L scratch -type c diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh index 587c8bb..172ea23 100644 --- a/tests/posix/L_xdev.sh +++ b/tests/posix/L_xdev.sh @@ -1,11 +1,10 @@ -test "$SUDO" || skip test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT ln -s ../mnt scratch/foo/bar "$XTOUCH" scratch/mnt/baz diff --git a/tests/posix/type_bind_mount.sh b/tests/posix/type_bind_mount.sh index 2d913db..c9a161d 100644 --- a/tests/posix/type_bind_mount.sh +++ b/tests/posix/type_bind_mount.sh @@ -1,10 +1,9 @@ -test "$SUDO" || skip test "$UNAME" = "Linux" || skip clean_scratch "$XTOUCH" scratch/{file,null} -sudo mount --bind /dev/null scratch/null -trap "sudo umount scratch/null" EXIT +bfs_sudo mount --bind /dev/null scratch/null || skip +trap "bfs_sudo umount scratch/null" EXIT bfs_diff scratch -type c diff --git a/tests/posix/xdev.sh b/tests/posix/xdev.sh index 9d21b14..33412bf 100644 --- a/tests/posix/xdev.sh +++ b/tests/posix/xdev.sh @@ -1,11 +1,10 @@ -test "$SUDO" || skip test "$UNAME" = "Darwin" && skip clean_scratch mkdir scratch/{foo,mnt} -sudo mount -t tmpfs tmpfs scratch/mnt -trap "sudo umount scratch/mnt" EXIT +bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip +trap "bfs_sudo umount scratch/mnt" EXIT "$XTOUCH" scratch/foo/bar scratch/mnt/baz diff --git a/tests/tests.sh b/tests/tests.sh index 962a2cf..e57db4e 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -101,18 +101,15 @@ fi function usage() { local pad=$(printf "%*s" ${#0} "") cat </dev/null; then + if ((${#SUDO[@]})) && command -v mountpoint &>/dev/null; then for path in "$TMP"/scratch/*; do if mountpoint -q "$path"; then sudo umount "$path" @@ -608,10 +619,9 @@ function make_xattrs() { *) # Linux tmpfs doesn't support the user.* namespace, so we use the security.* # namespace, which is writable by root and readable by others - [ "$SUDO" ] \ - && sudo setfattr -n security.bfs_test scratch/xattr \ - && sudo setfattr -n security.bfs_test_2 scratch/xattr_2 \ - && sudo setfattr -h -n security.bfs_test scratch/xattr_link + bfs_sudo setfattr -n security.bfs_test scratch/xattr \ + && bfs_sudo setfattr -n security.bfs_test_2 scratch/xattr_2 \ + && bfs_sudo setfattr -h -n security.bfs_test scratch/xattr_link ;; esac } -- cgit v1.2.3 From 1d1443193878ae72e54560bb21de79668cd954b9 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 8 Jun 2023 13:19:57 -0400 Subject: color: `fi=0` should not fall back to `no` --- src/color.c | 8 ++++---- tests/bfs/color_cd0_no.out | 20 ++++++++++++++++++++ tests/bfs/color_cd0_no.sh | 1 + tests/bfs/color_fi0_no.out | 20 ++++++++++++++++++++ tests/bfs/color_fi0_no.sh | 1 + tests/bfs/color_fi_no.out | 20 ++++++++++++++++++++ tests/bfs/color_fi_no.sh | 1 + tests/bfs/color_no.out | 20 ++++++++++++++++++++ tests/bfs/color_no.sh | 1 + 9 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 tests/bfs/color_cd0_no.out create mode 100644 tests/bfs/color_cd0_no.sh create mode 100644 tests/bfs/color_fi0_no.out create mode 100644 tests/bfs/color_fi0_no.sh create mode 100644 tests/bfs/color_fi_no.out create mode 100644 tests/bfs/color_fi_no.sh create mode 100644 tests/bfs/color_no.out create mode 100644 tests/bfs/color_no.sh (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index a723084..87575c9 100644 --- a/src/color.c +++ b/src/color.c @@ -436,10 +436,11 @@ struct colors *parse_colors(void) { ret |= init_color(colors, "err", "01;31", &colors->error); // Defaults from man dir_colors + // "" means fall back to ->normal ret |= init_color(colors, "no", NULL, &colors->normal); - ret |= init_color(colors, "fi", NULL, &colors->file); + ret |= init_color(colors, "fi", "", &colors->file); ret |= init_color(colors, "mh", NULL, &colors->multi_hard); ret |= init_color(colors, "ex", "01;32", &colors->executable); ret |= init_color(colors, "ca", NULL, &colors->capable); @@ -472,8 +473,7 @@ struct colors *parse_colors(void) { if (colors->link && strcmp(colors->link, "target") == 0) { colors->link_as_target = true; - dstrfree(colors->link); - colors->link = NULL; + dstresize(&colors->link, 0); } return colors; @@ -634,7 +634,7 @@ static const char *file_color(const struct colors *colors, const char *filename, break; } - if (!color) { + if (color && !color[0]) { color = colors->normal; } diff --git a/tests/bfs/color_cd0_no.out b/tests/bfs/color_cd0_no.out new file mode 100644 index 0000000..d5c98a9 --- /dev/null +++ b/tests/bfs/color_cd0_no.out @@ -0,0 +1,20 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/file.dat +rainbow/file.txt +rainbow/link.txt +rainbow/mh1 +rainbow/mh2 +rainbow/star.gz +rainbow/star.tar +rainbow/star.tar.gz +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/chardev_link diff --git a/tests/bfs/color_cd0_no.sh b/tests/bfs/color_cd0_no.sh new file mode 100644 index 0000000..325a782 --- /dev/null +++ b/tests/bfs/color_cd0_no.sh @@ -0,0 +1 @@ +LS_COLORS="ln=target:cd=0:no=01;92:" bfs_diff rainbow -color diff --git a/tests/bfs/color_fi0_no.out b/tests/bfs/color_fi0_no.out new file mode 100644 index 0000000..77fc8a8 --- /dev/null +++ b/tests/bfs/color_fi0_no.out @@ -0,0 +1,20 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/star.gz +rainbow/star.tar +rainbow/star.tar.gz diff --git a/tests/bfs/color_fi0_no.sh b/tests/bfs/color_fi0_no.sh new file mode 100644 index 0000000..f947d64 --- /dev/null +++ b/tests/bfs/color_fi0_no.sh @@ -0,0 +1 @@ +LS_COLORS="fi=0:no=01;92:" bfs_diff rainbow -color diff --git a/tests/bfs/color_fi_no.out b/tests/bfs/color_fi_no.out new file mode 100644 index 0000000..7162ded --- /dev/null +++ b/tests/bfs/color_fi_no.out @@ -0,0 +1,20 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/star.gz +rainbow/star.tar +rainbow/star.tar.gz +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky diff --git a/tests/bfs/color_fi_no.sh b/tests/bfs/color_fi_no.sh new file mode 100644 index 0000000..c2b4ec7 --- /dev/null +++ b/tests/bfs/color_fi_no.sh @@ -0,0 +1 @@ +LS_COLORS="fi=01;91:no=01;92:" bfs_diff rainbow -color diff --git a/tests/bfs/color_no.out b/tests/bfs/color_no.out new file mode 100644 index 0000000..9f0dd66 --- /dev/null +++ b/tests/bfs/color_no.out @@ -0,0 +1,20 @@ +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 +rainbow/star.gz +rainbow/star.tar +rainbow/star.tar.gz +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky diff --git a/tests/bfs/color_no.sh b/tests/bfs/color_no.sh new file mode 100644 index 0000000..b7527cb --- /dev/null +++ b/tests/bfs/color_no.sh @@ -0,0 +1 @@ +LS_COLORS="no=01;92:" bfs_diff rainbow -color -- cgit v1.2.3 From ec50b98d5584b0bb291a463a0c39905ce05acfe7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 27 Jun 2023 12:07:14 -0400 Subject: tests/bfs/color: Add mixed-case extensions --- tests/bfs/color.out | 11 ++++++++--- tests/bfs/color_L.out | 11 ++++++++--- tests/bfs/color_L_ln_target.out | 11 ++++++++--- tests/bfs/color_L_no_stat.out | 11 ++++++++--- tests/bfs/color_cd0_no.out | 11 ++++++++--- tests/bfs/color_escapes.out | 11 ++++++++--- tests/bfs/color_ext.out | 11 ++++++++--- tests/bfs/color_ext0.out | 11 ++++++++--- tests/bfs/color_ext_override.out | 11 ++++++++--- tests/bfs/color_ext_underride.out | 11 ++++++++--- tests/bfs/color_fi0_no.out | 11 ++++++++--- tests/bfs/color_fi_no.out | 11 ++++++++--- tests/bfs/color_ln_target.out | 11 ++++++++--- tests/bfs/color_mh.out | 11 ++++++++--- tests/bfs/color_mh0.out | 11 ++++++++--- tests/bfs/color_mi.out | 11 ++++++++--- tests/bfs/color_missing_colon.out | 11 ++++++++--- tests/bfs/color_no.out | 11 ++++++++--- tests/bfs/color_no_stat.out | 11 ++++++++--- tests/bfs/color_or.out | 11 ++++++++--- tests/bfs/color_or0_mi.out | 11 ++++++++--- tests/bfs/color_or0_mi0.out | 11 ++++++++--- tests/bfs/color_or_mi.out | 11 ++++++++--- tests/bfs/color_or_mi0.out | 11 ++++++++--- tests/bfs/color_rs_lc_rc_ec.out | 11 ++++++++--- tests/bfs/color_st0_tw0_ow.out | 11 ++++++++--- tests/bfs/color_st0_tw0_ow0.out | 11 ++++++++--- tests/bfs/color_st0_tw_ow.out | 11 ++++++++--- tests/bfs/color_st0_tw_ow0.out | 11 ++++++++--- tests/bfs/color_st_tw0_ow.out | 11 ++++++++--- tests/bfs/color_st_tw0_ow0.out | 11 ++++++++--- tests/bfs/color_st_tw_ow0.out | 11 ++++++++--- tests/bfs/color_star.out | 11 ++++++++--- tests/bfs/color_su0_sg.out | 11 ++++++++--- tests/bfs/color_su0_sg0.out | 11 ++++++++--- tests/bfs/color_su_sg0.out | 11 ++++++++--- tests/bfs/nocolor.out | 11 ++++++++--- tests/bfs/printf_color.out | 11 ++++++++--- tests/gnu/empty_special.out | 11 ++++++++--- tests/tests.sh | 4 +++- 40 files changed, 315 insertions(+), 118 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/color.out b/tests/bfs/color.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color.out +++ b/tests/bfs/color.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_L.out b/tests/bfs/color_L.out index b60dd4a..89f9410 100644 --- a/tests/bfs/color_L.out +++ b/tests/bfs/color_L.out @@ -13,8 +13,13 @@ rainbow/file.dat rainbow/file.txt rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_L_ln_target.out b/tests/bfs/color_L_ln_target.out index cd4ec5e..2562c98 100644 --- a/tests/bfs/color_L_ln_target.out +++ b/tests/bfs/color_L_ln_target.out @@ -13,8 +13,13 @@ rainbow/file.dat rainbow/file.txt rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_L_no_stat.out b/tests/bfs/color_L_no_stat.out index c0bb1be..4fe99c0 100644 --- a/tests/bfs/color_L_no_stat.out +++ b/tests/bfs/color_L_no_stat.out @@ -10,11 +10,16 @@ rainbow/pipe rainbow/exec.sh rainbow/file.dat +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/sgid -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/sugid rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_cd0_no.out b/tests/bfs/color_cd0_no.out index d5c98a9..30ad97f 100644 --- a/tests/bfs/color_cd0_no.out +++ b/tests/bfs/color_cd0_no.out @@ -5,11 +5,16 @@ rainbow/file.dat rainbow/file.txt rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ rainbow/sticky_ow rainbow/sgid rainbow/pipe diff --git a/tests/bfs/color_escapes.out b/tests/bfs/color_escapes.out index b71e138..808585e 100644 --- a/tests/bfs/color_escapes.out +++ b/tests/bfs/color_escapes.out @@ -13,8 +13,13 @@ :rainbow/:sticky :rainbow/file.dat :rainbow/file.txt +:rainbow/lower.gz +:rainbow/lower.tar +:rainbow/lower.tar.gz +:rainbow/lu.tar.GZ :rainbow/mh1 :rainbow/mh2 -:rainbow/star.gz -:rainbow/star.tar -:rainbow/star.tar.gz +:rainbow/ul.TAR.gz +:rainbow/upper.GZ +:rainbow/upper.TAR +:rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_ext.out b/tests/bfs/color_ext.out index cf26e73..be22b82 100644 --- a/tests/bfs/color_ext.out +++ b/tests/bfs/color_ext.out @@ -13,8 +13,13 @@ rainbow/suid rainbow/sticky rainbow/file.dat +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_ext0.out b/tests/bfs/color_ext0.out index e764a6b..d151319 100644 --- a/tests/bfs/color_ext0.out +++ b/tests/bfs/color_ext0.out @@ -13,8 +13,13 @@ rainbow/suid rainbow/sticky rainbow/file.dat +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_ext_override.out b/tests/bfs/color_ext_override.out index 1377b65..3a09cd2 100644 --- a/tests/bfs/color_ext_override.out +++ b/tests/bfs/color_ext_override.out @@ -1,8 +1,13 @@ rainbow rainbow/exec.sh -rainbow/star.tar -rainbow/star.gz -rainbow/star.tar.gz +rainbow/lower.tar +rainbow/upper.TAR +rainbow/lower.gz +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR.GZ rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext_underride.out b/tests/bfs/color_ext_underride.out index 787248a..073d176 100644 --- a/tests/bfs/color_ext_underride.out +++ b/tests/bfs/color_ext_underride.out @@ -1,8 +1,13 @@ rainbow -rainbow/star.tar.gz +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/ul.TAR.gz +rainbow/upper.TAR.GZ rainbow/exec.sh -rainbow/star.tar -rainbow/star.gz +rainbow/lower.tar +rainbow/upper.TAR +rainbow/lower.gz +rainbow/upper.GZ rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_fi0_no.out b/tests/bfs/color_fi0_no.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_fi0_no.out +++ b/tests/bfs/color_fi0_no.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_fi_no.out b/tests/bfs/color_fi_no.out index 7162ded..8df9355 100644 --- a/tests/bfs/color_fi_no.out +++ b/tests/bfs/color_fi_no.out @@ -6,11 +6,16 @@ rainbow/link.txt rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ rainbow/sticky_ow rainbow/sgid rainbow/pipe diff --git a/tests/bfs/color_ln_target.out b/tests/bfs/color_ln_target.out index cd4ec5e..2562c98 100644 --- a/tests/bfs/color_ln_target.out +++ b/tests/bfs/color_ln_target.out @@ -13,8 +13,13 @@ rainbow/file.dat rainbow/file.txt rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_mh.out b/tests/bfs/color_mh.out index 757a6a1..7521b31 100644 --- a/tests/bfs/color_mh.out +++ b/tests/bfs/color_mh.out @@ -15,6 +15,11 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_mh0.out b/tests/bfs/color_mh0.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_mh0.out +++ b/tests/bfs/color_mh0.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_mi.out b/tests/bfs/color_mi.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_mi.out +++ b/tests/bfs/color_mi.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_missing_colon.out b/tests/bfs/color_missing_colon.out index cf26e73..be22b82 100644 --- a/tests/bfs/color_missing_colon.out +++ b/tests/bfs/color_missing_colon.out @@ -13,8 +13,13 @@ rainbow/suid rainbow/sticky rainbow/file.dat +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_no.out b/tests/bfs/color_no.out index 9f0dd66..b70e47d 100644 --- a/tests/bfs/color_no.out +++ b/tests/bfs/color_no.out @@ -6,11 +6,16 @@ rainbow/link.txt rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ rainbow/sticky_ow rainbow/sgid rainbow/pipe diff --git a/tests/bfs/color_no_stat.out b/tests/bfs/color_no_stat.out index 1fc5324..7d7d767 100644 --- a/tests/bfs/color_no_stat.out +++ b/tests/bfs/color_no_stat.out @@ -10,11 +10,16 @@ rainbow/pipe rainbow/exec.sh rainbow/file.dat +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/sgid -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/sugid rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_or.out b/tests/bfs/color_or.out index 9e1fe5c..98efb63 100644 --- a/tests/bfs/color_or.out +++ b/tests/bfs/color_or.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_or0_mi.out b/tests/bfs/color_or0_mi.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_or0_mi.out +++ b/tests/bfs/color_or0_mi.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_or0_mi0.out b/tests/bfs/color_or0_mi0.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_or0_mi0.out +++ b/tests/bfs/color_or0_mi0.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_or_mi.out b/tests/bfs/color_or_mi.out index 5667f56..a9dc229 100644 --- a/tests/bfs/color_or_mi.out +++ b/tests/bfs/color_or_mi.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_or_mi0.out b/tests/bfs/color_or_mi0.out index 5667f56..a9dc229 100644 --- a/tests/bfs/color_or_mi0.out +++ b/tests/bfs/color_or_mi0.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_rs_lc_rc_ec.out b/tests/bfs/color_rs_lc_rc_ec.out index d39bbe7..82d94ec 100644 --- a/tests/bfs/color_rs_lc_rc_ec.out +++ b/tests/bfs/color_rs_lc_rc_ec.out @@ -12,9 +12,14 @@ LC01;34RCrainbow/ECLC37;41RCsuidEC LC01;34RCrainbow/ECLC37;44RCstickyEC LC01;34RCrainbow/ECfile.dat LC01;34RCrainbow/ECfile.txt +LC01;34RCrainbow/EClower.gz +LC01;34RCrainbow/EClower.tar +LC01;34RCrainbow/EClower.tar.gz +LC01;34RCrainbow/EClu.tar.GZ LC01;34RCrainbow/ECmh1 LC01;34RCrainbow/ECmh2 -LC01;34RCrainbow/ECstar.gz -LC01;34RCrainbow/ECstar.tar -LC01;34RCrainbow/ECstar.tar.gz +LC01;34RCrainbow/ECul.TAR.gz +LC01;34RCrainbow/ECupper.GZ +LC01;34RCrainbow/ECupper.TAR +LC01;34RCrainbow/ECupper.TAR.GZ LC01;34RCrainbowEC diff --git a/tests/bfs/color_st0_tw0_ow.out b/tests/bfs/color_st0_tw0_ow.out index 9a47ef2..bdc5942 100644 --- a/tests/bfs/color_st0_tw0_ow.out +++ b/tests/bfs/color_st0_tw0_ow.out @@ -13,8 +13,13 @@ rainbow/suid rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st0_tw0_ow0.out b/tests/bfs/color_st0_tw0_ow0.out index 2b86fe4..f13b7f3 100644 --- a/tests/bfs/color_st0_tw0_ow0.out +++ b/tests/bfs/color_st0_tw0_ow0.out @@ -13,8 +13,13 @@ rainbow/suid rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st0_tw_ow.out b/tests/bfs/color_st0_tw_ow.out index 42549a1..2d7f682 100644 --- a/tests/bfs/color_st0_tw_ow.out +++ b/tests/bfs/color_st0_tw_ow.out @@ -13,8 +13,13 @@ rainbow/sticky_ow rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st0_tw_ow0.out b/tests/bfs/color_st0_tw_ow0.out index 535b8ae..7e343b8 100644 --- a/tests/bfs/color_st0_tw_ow0.out +++ b/tests/bfs/color_st0_tw_ow0.out @@ -13,8 +13,13 @@ rainbow/sticky_ow rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st_tw0_ow.out b/tests/bfs/color_st_tw0_ow.out index c9a86f4..c61a327 100644 --- a/tests/bfs/color_st_tw0_ow.out +++ b/tests/bfs/color_st_tw0_ow.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st_tw0_ow0.out b/tests/bfs/color_st_tw0_ow0.out index 2d94f3a..929a993 100644 --- a/tests/bfs/color_st_tw0_ow0.out +++ b/tests/bfs/color_st_tw0_ow0.out @@ -13,8 +13,13 @@ rainbow/sticky_ow rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_st_tw_ow0.out b/tests/bfs/color_st_tw_ow0.out index 317ef90..7092f5a 100644 --- a/tests/bfs/color_st_tw_ow0.out +++ b/tests/bfs/color_st_tw_ow0.out @@ -13,8 +13,13 @@ rainbow/sticky_ow rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_star.out b/tests/bfs/color_star.out index 77fc8a8..34c7153 100644 --- a/tests/bfs/color_star.out +++ b/tests/bfs/color_star.out @@ -13,8 +13,13 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_su0_sg.out b/tests/bfs/color_su0_sg.out index 8b8c8b8..2bce534 100644 --- a/tests/bfs/color_su0_sg.out +++ b/tests/bfs/color_su0_sg.out @@ -12,9 +12,14 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_su0_sg0.out b/tests/bfs/color_su0_sg0.out index 0cd5f9a..0c3d757 100644 --- a/tests/bfs/color_su0_sg0.out +++ b/tests/bfs/color_su0_sg0.out @@ -10,11 +10,16 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/sgid -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/sugid rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_su_sg0.out b/tests/bfs/color_su_sg0.out index a9e8c5d..7a70598 100644 --- a/tests/bfs/color_su_sg0.out +++ b/tests/bfs/color_su_sg0.out @@ -12,9 +12,14 @@ rainbow/sticky rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/sgid -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/nocolor.out b/tests/bfs/nocolor.out index b53fe03..29e7de8 100644 --- a/tests/bfs/nocolor.out +++ b/tests/bfs/nocolor.out @@ -5,16 +5,21 @@ rainbow/exec.sh rainbow/file.dat rainbow/file.txt rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/ow rainbow/pipe rainbow/sgid rainbow/socket -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/sticky rainbow/sticky_ow rainbow/sugid rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/printf_color.out b/tests/bfs/printf_color.out index d9cd1a4..0468f7d 100644 --- a/tests/bfs/printf_color.out +++ b/tests/bfs/printf_color.out @@ -13,8 +13,13 @@ . ./rainbow sticky ./rainbow/sticky rainbow/sticky . ./rainbow file.dat ./rainbow/file.dat rainbow/file.dat . ./rainbow file.txt ./rainbow/file.txt rainbow/file.txt +. ./rainbow lower.gz ./rainbow/lower.gz rainbow/lower.gz +. ./rainbow lower.tar ./rainbow/lower.tar rainbow/lower.tar +. ./rainbow lower.tar.gz ./rainbow/lower.tar.gz rainbow/lower.tar.gz +. ./rainbow lu.tar.GZ ./rainbow/lu.tar.GZ rainbow/lu.tar.GZ . ./rainbow mh1 ./rainbow/mh1 rainbow/mh1 . ./rainbow mh2 ./rainbow/mh2 rainbow/mh2 -. ./rainbow star.gz ./rainbow/star.gz rainbow/star.gz -. ./rainbow star.tar ./rainbow/star.tar rainbow/star.tar -. ./rainbow star.tar.gz ./rainbow/star.tar.gz rainbow/star.tar.gz +. ./rainbow ul.TAR.gz ./rainbow/ul.TAR.gz rainbow/ul.TAR.gz +. ./rainbow upper.GZ ./rainbow/upper.GZ rainbow/upper.GZ +. ./rainbow upper.TAR ./rainbow/upper.TAR rainbow/upper.TAR +. ./rainbow upper.TAR.GZ ./rainbow/upper.TAR.GZ rainbow/upper.TAR.GZ diff --git a/tests/gnu/empty_special.out b/tests/gnu/empty_special.out index 3927f2b..3aa57d2 100644 --- a/tests/gnu/empty_special.out +++ b/tests/gnu/empty_special.out @@ -1,14 +1,19 @@ rainbow/exec.sh rainbow/file.dat rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ rainbow/mh1 rainbow/mh2 rainbow/ow rainbow/sgid -rainbow/star.gz -rainbow/star.tar -rainbow/star.tar.gz rainbow/sticky rainbow/sticky_ow rainbow/sugid rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/tests.sh b/tests/tests.sh index 9265481..46e3e33 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -413,7 +413,9 @@ make_deep "$TMP/deep" function make_rainbow() { "$XTOUCH" -p "$1/file.txt" "$XTOUCH" -p "$1/file.dat" - "$XTOUCH" -p "$1/star".{gz,tar,tar.gz} + "$XTOUCH" -p "$1/lower".{gz,tar,tar.gz} + "$XTOUCH" -p "$1/upper".{GZ,TAR,TAR.GZ} + "$XTOUCH" -p "$1/lu.tar.GZ" "$1/ul.TAR.gz" ln -s file.txt "$1/link.txt" "$XTOUCH" -p "$1/mh1" ln "$1/mh1" "$1/mh2" -- cgit v1.2.3 From 27dc7a126d6c00b7a41e0559254928555200ee42 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 29 Jun 2023 15:17:27 -0400 Subject: color: Implement smart casing Since coreutils 9.2, ls does case-sensitive extension matching if the same extension is capitalized differently in $LS_COLORS. Implement the same logic. Link: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=33123 Link: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=9086 --- src/color.c | 591 +++++++++++++++++++++++++++---------------- tests/bfs/color_ext_case.out | 25 ++ tests/bfs/color_ext_case.sh | 1 + tests/bfs/color_nul.out | Bin 20 -> 908 bytes tests/bfs/color_nul.sh | 3 +- tests/bfs/color_star.out | 25 -- tests/bfs/color_star.sh | 2 +- 7 files changed, 401 insertions(+), 246 deletions(-) create mode 100644 tests/bfs/color_ext_case.out create mode 100644 tests/bfs/color_ext_case.sh delete mode 100644 tests/bfs/color_star.out (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index b54ad53..82ff763 100644 --- a/src/color.c +++ b/src/color.c @@ -22,69 +22,122 @@ #include #include +/** + * An escape sequence, which may contain embedded NUL bytes. + */ +struct esc_seq { + /** The length of the escape sequence. */ + size_t len; + /** The escape sequence iteself, without a terminating NUL. */ + char seq[]; +}; + +/** + * A colored file extension, like `*.tar=01;31`. + */ +struct ext_color { + /** Priority, to disambiguate case-sensitive and insensitive matches. */ + size_t priority; + /** The escape sequence associated with this extension. */ + struct esc_seq *esc; + /** The length of the extension to match. */ + size_t len; + /** Whether the comparison should be case-insensitive. */ + bool icase; + /** The extension to match (NUL-terminated). */ + char ext[]; +}; + struct colors { - char *reset; - char *leftcode; - char *rightcode; - char *endcode; - char *clear_to_eol; - - char *bold; - char *gray; - char *red; - char *green; - char *yellow; - char *blue; - char *magenta; - char *cyan; - char *white; - - char *warning; - char *error; - - char *normal; - - char *file; - char *multi_hard; - char *executable; - char *capable; - char *setgid; - char *setuid; - - char *directory; - char *sticky; - char *other_writable; - char *sticky_other_writable; - - char *link; - char *orphan; - char *missing; + /** esc_seq allocator. */ + struct varena esc_arena; + /** ext_color allocator. */ + struct varena ext_arena; + + // Known dircolors keys + + struct esc_seq *reset; + struct esc_seq *leftcode; + struct esc_seq *rightcode; + struct esc_seq *endcode; + struct esc_seq *clear_to_eol; + + struct esc_seq *bold; + struct esc_seq *gray; + struct esc_seq *red; + struct esc_seq *green; + struct esc_seq *yellow; + struct esc_seq *blue; + struct esc_seq *magenta; + struct esc_seq *cyan; + struct esc_seq *white; + + struct esc_seq *warning; + struct esc_seq *error; + + struct esc_seq *normal; + + struct esc_seq *file; + struct esc_seq *multi_hard; + struct esc_seq *executable; + struct esc_seq *capable; + struct esc_seq *setgid; + struct esc_seq *setuid; + + struct esc_seq *directory; + struct esc_seq *sticky; + struct esc_seq *other_writable; + struct esc_seq *sticky_other_writable; + + struct esc_seq *link; + struct esc_seq *orphan; + struct esc_seq *missing; bool link_as_target; - char *blockdev; - char *chardev; - char *door; - char *pipe; - char *socket; + struct esc_seq *blockdev; + struct esc_seq *chardev; + struct esc_seq *door; + struct esc_seq *pipe; + struct esc_seq *socket; /** A mapping from color names (fi, di, ln, etc.) to struct fields. */ struct trie names; - /** A mapping from file extensions to colors. */ - struct trie ext_colors; + /** Number of extensions. */ + size_t ext_count; + /** Case-sensitive extension trie. */ + struct trie ext_trie; + /** Case-insensitive extension trie. */ + struct trie iext_trie; }; +/** Allocate an escape sequence. */ +static struct esc_seq *new_esc(struct colors *colors, const char *seq, size_t len) { + struct esc_seq *esc = varena_alloc(&colors->esc_arena, len); + if (esc) { + esc->len = len; + memcpy(esc->seq, seq, len); + } + return esc; +} + +/** Free an escape sequence. */ +static void free_esc(struct colors *colors, struct esc_seq *seq) { + varena_free(&colors->esc_arena, seq, seq->len); +} + /** Initialize a color in the table. */ -static int init_color(struct colors *colors, const char *name, const char *value, char **field) { +static int init_esc(struct colors *colors, const char *name, const char *value, struct esc_seq **field) { + struct esc_seq *esc = NULL; if (value) { - *field = dstrdup(value); - if (!*field) { + esc = new_esc(colors, value, strlen(value)); + if (!esc) { return -1; } - } else { - *field = NULL; } + *field = esc; + struct trie_leaf *leaf = trie_insert_str(&colors->names, name); if (leaf) { leaf->value = field; @@ -94,100 +147,182 @@ static int init_color(struct colors *colors, const char *name, const char *value } } -/** Get a color from the table. */ -static char **get_color(const struct colors *colors, const char *name) { +/** Get an escape sequence from the table. */ +static struct esc_seq **get_esc(const struct colors *colors, const char *name) { const struct trie_leaf *leaf = trie_find_str(&colors->names, name); - if (leaf) { - return (char **)leaf->value; - } else { - return NULL; - } + return leaf ? leaf->value : NULL; } -/** Set the value of a color. */ -static int set_color(struct colors *colors, const char *name, char *value) { - char **color = get_color(colors, name); - if (color) { - dstrfree(*color); - *color = value; +/** Set a named escape sequence. */ +static int set_esc(struct colors *colors, const char *name, char *value) { + struct esc_seq **field = get_esc(colors, name); + if (!field) { return 0; - } else { - return -1; } + + if (*field) { + free_esc(colors, *field); + *field = NULL; + } + + if (value) { + *field = new_esc(colors, value, dstrlen(value)); + if (!*field) { + return -1; + } + } + + return 0; } -/** - * Transform a file extension for fast lookups, by reversing and lowercasing it. - */ -static void extxfrm(char *ext, size_t len) { - for (size_t i = 0; i < len - i; ++i) { - char a = ext[i]; - char b = ext[len - i - 1]; +/** Reverse a string, to turn suffix matches into prefix matches. */ +static void ext_reverse(char *ext, size_t len) { + for (size_t i = 0, j = len - 1; len && i < j; ++i, --j) { + char c = ext[i]; + ext[i] = ext[j]; + ext[j] = c; + } +} + +/** Convert a string to lowercase for case-insensitive matching. */ +static void ext_tolower(char *ext, size_t len) { + for (size_t i = 0; i < len; ++i) { + char c = ext[i]; // What's internationalization? Doesn't matter, this is what // GNU ls does. Luckily, since there's no standard C way to // casefold. Not using tolower() here since it respects the // current locale, which GNU ls doesn't do. - if (a >= 'A' && a <= 'Z') { - a += 'a' - 'A'; - } - if (b >= 'A' && b <= 'Z') { - b += 'a' - 'A'; + if (c >= 'A' && c <= 'Z') { + c += 'a' - 'A'; } - ext[i] = b; - ext[len - i - 1] = a; + ext[i] = c; } } /** Maximum supported extension length. */ #define EXT_MAX 255 -/** - * Set the color for an extension. - */ -static int set_ext_color(struct colors *colors, char *key, char *value) { +/** Set the color for an extension. */ +static int set_ext(struct colors *colors, char *key, char *value) { size_t len = dstrlen(key); - if (len > EXT_MAX) { + struct ext_color *ext = varena_alloc(&colors->ext_arena, len + 1); + if (!ext) { return -1; } - extxfrm(key, len); + ext->priority = colors->ext_count++; + ext->len = len; + ext->icase = true; + ext->esc = new_esc(colors, value, dstrlen(value)); + if (!ext->esc) { + goto fail; + } + + key = memcpy(ext->ext, key, len + 1); + + // Reverse the extension (`*.y.x` -> `x.y.*`) so we can use trie_find_prefix() + ext_reverse(key, len); + + // Find any pre-existing exact match + struct ext_color *prev = NULL; + struct trie_leaf *leaf = trie_find_str(&colors->ext_trie, key); + if (leaf) { + prev = leaf->value; + trie_remove(&colors->ext_trie, leaf); + } // A later *.x should override any earlier *.x, *.y.x, etc. - struct trie_leaf *match; - while ((match = trie_find_postfix(&colors->ext_colors, key))) { - dstrfree(match->value); - trie_remove(&colors->ext_colors, match); + while ((leaf = trie_find_postfix(&colors->ext_trie, key))) { + trie_remove(&colors->ext_trie, leaf); } - struct trie_leaf *leaf = trie_insert_str(&colors->ext_colors, key); - if (leaf) { - leaf->value = value; - return 0; - } else { - return -1; + // Insert the extension into the case-sensitive trie + leaf = trie_insert_str(&colors->ext_trie, key); + if (!leaf) { + goto fail; } + leaf->value = ext; + + // "Smart case": if the same extension is given with two different + // capitalizations (e.g. `*.y.x=31:*.Y.Z=32:`), make it case-sensitive + ext_tolower(key, len); + leaf = trie_insert_str(&colors->iext_trie, key); + if (!leaf) { + goto fail; + } + + // If a match for the lowercased extension exists and is different from + // the exact match, or is already case-sensitive, mark this one too + struct ext_color *iprev = leaf->value; + if (iprev && (iprev != prev || !iprev->icase)) { + iprev->icase = false; + ext->icase = false; + } + leaf->value = ext; + + return 0; + +fail: + if (ext->esc) { + free_esc(colors, ext->esc); + } + varena_free(&colors->ext_arena, ext, len + 1); + return -1; +} + +/** Rebuild the case-insensitive trie after all extensions have been parsed. */ +static int build_iext_trie(struct colors *colors) { + trie_destroy(&colors->iext_trie); + trie_init(&colors->iext_trie); + + TRIE_FOR_EACH(&colors->ext_trie, leaf) { + struct ext_color *ext = leaf->value; + if (!ext->icase) { + continue; + } + + // set_ext() already reversed and lowercased the extension + struct trie_leaf *ileaf; + while ((ileaf = trie_find_postfix(&colors->iext_trie, ext->ext))) { + trie_remove(&colors->ext_trie, ileaf); + } + + ileaf = trie_insert_str(&colors->iext_trie, ext->ext); + if (!ileaf) { + return -1; + } + ileaf->value = ext; + } + + return 0; } /** * Find a color by an extension. */ -static const char *get_ext_color(const struct colors *colors, const char *filename) { +static const struct esc_seq *get_ext(const struct colors *colors, const char *filename) { size_t name_len = strlen(filename); size_t ext_len = name_len < EXT_MAX ? name_len : EXT_MAX; - const char *ext = filename + name_len - ext_len; + const char *suffix = filename + name_len - ext_len; char xfrm[ext_len + 1]; - memcpy(xfrm, ext, sizeof(xfrm)); - extxfrm(xfrm, ext_len); + memcpy(xfrm, suffix, sizeof(xfrm)); - const struct trie_leaf *leaf = trie_find_prefix(&colors->ext_colors, xfrm); - if (leaf) { - return leaf->value; - } else { - return NULL; + ext_reverse(xfrm, ext_len); + const struct trie_leaf *leaf = trie_find_prefix(&colors->ext_trie, xfrm); + const struct ext_color *ext = leaf ? leaf->value : NULL; + + ext_tolower(xfrm, ext_len); + const struct trie_leaf *ileaf = trie_find_prefix(&colors->iext_trie, xfrm); + const struct ext_color *iext = ileaf ? ileaf->value : NULL; + + if (iext && (!ext || ext->priority < iext->priority)) { + ext = iext; } + + return ext ? ext->esc : NULL; } /** @@ -211,6 +346,8 @@ static const char *get_ext_color(const struct colors *colors, const char *filena * * See man dir_colors. * + * @param str + * A dstring to fill with the unescaped chunk. * @param value * The value to parse. * @param end @@ -218,16 +355,18 @@ static const char *get_ext_color(const struct colors *colors, const char *filena * @param[out] next * Will be set to the next chunk. * @return - * The parsed chunk as a dstring. + * 0 on success, -1 on failure. */ -static char *unescape(const char *value, char end, const char **next) { +static int unescape(char **str, const char *value, char end, const char **next) { + *next = NULL; + if (!value) { - goto fail; + errno = EINVAL; + return -1; } - char *str = dstralloc(0); - if (!str) { - goto fail_str; + if (dstresize(str, 0) != 0) { + return -1; } const char *i; @@ -304,7 +443,8 @@ static char *unescape(const char *value, char end, const char **next) { break; case '\0': - goto fail_str; + errno = EINVAL; + return -1; default: c = *i; @@ -318,7 +458,8 @@ static char *unescape(const char *value, char end, const char **next) { c = '\177'; break; case '\0': - goto fail_str; + errno = EINVAL; + return -1; default: // CTRL masks bits 6 and 7 c = *i & 0x1F; @@ -331,77 +472,70 @@ static char *unescape(const char *value, char end, const char **next) { break; } - if (dstrapp(&str, c) != 0) { - goto fail_str; + if (dstrapp(str, c) != 0) { + return -1; } } if (*i) { *next = i + 1; - } else { - *next = NULL; } - return str; - -fail_str: - dstrfree(str); -fail: - *next = NULL; - return NULL; + return 0; } /** Parse the GNU $LS_COLORS format. */ -static void parse_gnu_ls_colors(struct colors *colors, const char *ls_colors) { +static int parse_gnu_ls_colors(struct colors *colors, const char *ls_colors) { + int ret = -1; + char *key = NULL; + char *value = NULL; + for (const char *chunk = ls_colors, *next; chunk; chunk = next) { if (chunk[0] == '*') { - char *key = unescape(chunk + 1, '=', &next); - if (!key) { - continue; + if (unescape(&key, chunk + 1, '=', &next) != 0) { + goto fail; } - - char *value = unescape(next, ':', &next); - if (value) { - if (set_ext_color(colors, key, value) != 0) { - dstrfree(value); - } + if (unescape(&value, next, ':', &next) != 0) { + goto fail; + } + if (set_ext(colors, key, value) != 0) { + goto fail; } - - dstrfree(key); } else { const char *equals = strchr(chunk, '='); if (!equals) { break; } - char *value = unescape(equals + 1, ':', &next); - if (!value) { - continue; + if (dstrncpy(&key, chunk, equals - chunk) != 0) { + goto fail; } - - char *key = strndup(chunk, equals - chunk); - if (!key) { - dstrfree(value); - continue; + if (unescape(&value, equals + 1, ':', &next) != 0) { + goto fail; } // All-zero values should be treated like NULL, to fall // back on any other relevant coloring for that file + char *esc = value; if (strspn(value, "0") == strlen(value) && strcmp(key, "rs") != 0 && strcmp(key, "lc") != 0 && strcmp(key, "rc") != 0 && strcmp(key, "ec") != 0) { - dstrfree(value); - value = NULL; + esc = NULL; } - if (set_color(colors, key, value) != 0) { - dstrfree(value); + if (set_esc(colors, key, esc) != 0) { + goto fail; } - free(key); } } + + ret = 0; +fail: + dstrfree(value); + dstrfree(key); + return ret; } struct colors *parse_colors(void) { @@ -410,91 +544,105 @@ struct colors *parse_colors(void) { return NULL; } + VARENA_INIT(&colors->esc_arena, struct esc_seq, seq); + VARENA_INIT(&colors->ext_arena, struct ext_color, ext); trie_init(&colors->names); - trie_init(&colors->ext_colors); + colors->ext_count = 0; + trie_init(&colors->ext_trie); + trie_init(&colors->iext_trie); int ret = 0; // From man console_codes - ret |= init_color(colors, "rs", "0", &colors->reset); - ret |= init_color(colors, "lc", "\033[", &colors->leftcode); - ret |= init_color(colors, "rc", "m", &colors->rightcode); - ret |= init_color(colors, "ec", NULL, &colors->endcode); - ret |= init_color(colors, "cl", "\033[K", &colors->clear_to_eol); - - ret |= init_color(colors, "bld", "01;39", &colors->bold); - ret |= init_color(colors, "gry", "01;30", &colors->gray); - ret |= init_color(colors, "red", "01;31", &colors->red); - ret |= init_color(colors, "grn", "01;32", &colors->green); - ret |= init_color(colors, "ylw", "01;33", &colors->yellow); - ret |= init_color(colors, "blu", "01;34", &colors->blue); - ret |= init_color(colors, "mag", "01;35", &colors->magenta); - ret |= init_color(colors, "cyn", "01;36", &colors->cyan); - ret |= init_color(colors, "wht", "01;37", &colors->white); - - ret |= init_color(colors, "wrn", "01;33", &colors->warning); - ret |= init_color(colors, "err", "01;31", &colors->error); + ret |= init_esc(colors, "rs", "0", &colors->reset); + ret |= init_esc(colors, "lc", "\033[", &colors->leftcode); + ret |= init_esc(colors, "rc", "m", &colors->rightcode); + ret |= init_esc(colors, "ec", NULL, &colors->endcode); + ret |= init_esc(colors, "cl", "\033[K", &colors->clear_to_eol); + + ret |= init_esc(colors, "bld", "01;39", &colors->bold); + ret |= init_esc(colors, "gry", "01;30", &colors->gray); + ret |= init_esc(colors, "red", "01;31", &colors->red); + ret |= init_esc(colors, "grn", "01;32", &colors->green); + ret |= init_esc(colors, "ylw", "01;33", &colors->yellow); + ret |= init_esc(colors, "blu", "01;34", &colors->blue); + ret |= init_esc(colors, "mag", "01;35", &colors->magenta); + ret |= init_esc(colors, "cyn", "01;36", &colors->cyan); + ret |= init_esc(colors, "wht", "01;37", &colors->white); + + ret |= init_esc(colors, "wrn", "01;33", &colors->warning); + ret |= init_esc(colors, "err", "01;31", &colors->error); // Defaults from man dir_colors // "" means fall back to ->normal - ret |= init_color(colors, "no", NULL, &colors->normal); + ret |= init_esc(colors, "no", NULL, &colors->normal); - ret |= init_color(colors, "fi", "", &colors->file); - ret |= init_color(colors, "mh", NULL, &colors->multi_hard); - ret |= init_color(colors, "ex", "01;32", &colors->executable); - ret |= init_color(colors, "ca", NULL, &colors->capable); - ret |= init_color(colors, "sg", "30;43", &colors->setgid); - ret |= init_color(colors, "su", "37;41", &colors->setuid); + ret |= init_esc(colors, "fi", "", &colors->file); + ret |= init_esc(colors, "mh", NULL, &colors->multi_hard); + ret |= init_esc(colors, "ex", "01;32", &colors->executable); + ret |= init_esc(colors, "ca", NULL, &colors->capable); + ret |= init_esc(colors, "sg", "30;43", &colors->setgid); + ret |= init_esc(colors, "su", "37;41", &colors->setuid); - ret |= init_color(colors, "di", "01;34", &colors->directory); - ret |= init_color(colors, "st", "37;44", &colors->sticky); - ret |= init_color(colors, "ow", "34;42", &colors->other_writable); - ret |= init_color(colors, "tw", "30;42", &colors->sticky_other_writable); + ret |= init_esc(colors, "di", "01;34", &colors->directory); + ret |= init_esc(colors, "st", "37;44", &colors->sticky); + ret |= init_esc(colors, "ow", "34;42", &colors->other_writable); + ret |= init_esc(colors, "tw", "30;42", &colors->sticky_other_writable); - ret |= init_color(colors, "ln", "01;36", &colors->link); - ret |= init_color(colors, "or", NULL, &colors->orphan); - ret |= init_color(colors, "mi", NULL, &colors->missing); + ret |= init_esc(colors, "ln", "01;36", &colors->link); + ret |= init_esc(colors, "or", NULL, &colors->orphan); + ret |= init_esc(colors, "mi", NULL, &colors->missing); colors->link_as_target = false; - ret |= init_color(colors, "bd", "01;33", &colors->blockdev); - ret |= init_color(colors, "cd", "01;33", &colors->chardev); - ret |= init_color(colors, "do", "01;35", &colors->door); - ret |= init_color(colors, "pi", "33", &colors->pipe); - ret |= init_color(colors, "so", "01;35", &colors->socket); + ret |= init_esc(colors, "bd", "01;33", &colors->blockdev); + ret |= init_esc(colors, "cd", "01;33", &colors->chardev); + ret |= init_esc(colors, "do", "01;35", &colors->door); + ret |= init_esc(colors, "pi", "33", &colors->pipe); + ret |= init_esc(colors, "so", "01;35", &colors->socket); - if (ret) { - free_colors(colors); - return NULL; + if (ret != 0) { + goto fail; } - parse_gnu_ls_colors(colors, getenv("LS_COLORS")); - parse_gnu_ls_colors(colors, getenv("BFS_COLORS")); + if (parse_gnu_ls_colors(colors, getenv("LS_COLORS")) != 0) { + goto fail; + } + if (parse_gnu_ls_colors(colors, getenv("BFS_COLORS")) != 0) { + goto fail; + } + if (build_iext_trie(colors) != 0) { + goto fail; + } - if (colors->link && strcmp(colors->link, "target") == 0) { - colors->link_as_target = true; - dstresize(&colors->link, 0); + if (colors->link) { + size_t len = strlen("target"); + if (colors->link->len == len && memcmp(colors->link->seq, "target", len) == 0) { + colors->link_as_target = true; + colors->link->len = 0; + } } return colors; + +fail: + free_colors(colors); + return NULL; } void free_colors(struct colors *colors) { - if (colors) { - TRIE_FOR_EACH(&colors->ext_colors, leaf) { - dstrfree(leaf->value); - } - trie_destroy(&colors->ext_colors); + if (!colors) { + return; + } - TRIE_FOR_EACH(&colors->names, leaf) { - char **field = leaf->value; - dstrfree(*field); - } - trie_destroy(&colors->names); + trie_destroy(&colors->iext_trie); + trie_destroy(&colors->ext_trie); + trie_destroy(&colors->names); + varena_destroy(&colors->ext_arena); + varena_destroy(&colors->esc_arena); - free(colors); - } + free(colors); } CFILE *cfwrap(FILE *file, const struct colors *colors, bool close) { @@ -547,14 +695,14 @@ static bool is_link_broken(const struct BFTW *ftwbuf) { } /** Get the color for a file. */ -static const char *file_color(const struct colors *colors, const char *filename, const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { +static const struct esc_seq *file_color(const struct colors *colors, const char *filename, const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { enum bfs_type type = bftw_type(ftwbuf, flags); if (type == BFS_ERROR) { goto error; } const struct bfs_stat *statbuf = NULL; - const char *color = NULL; + const struct esc_seq *color = NULL; switch (type) { case BFS_REG: @@ -578,7 +726,7 @@ static const char *file_color(const struct colors *colors, const char *filename, } if (!color) { - color = get_ext_color(colors, filename); + color = get_ext(colors, filename); } if (!color) { @@ -635,7 +783,7 @@ static const char *file_color(const struct colors *colors, const char *filename, break; } - if (color && !color[0]) { + if (color && color->len == 0) { color = colors->normal; } @@ -649,17 +797,22 @@ error: } } +/** Print an escape sequence chunk. */ +static int print_esc_chunk(CFILE *cfile, const struct esc_seq *esc) { + return dstrxcat(&cfile->buffer, esc->seq, esc->len); +} + /** Print an ANSI escape sequence. */ -static int print_esc(CFILE *cfile, const char *esc) { +static int print_esc(CFILE *cfile, const struct esc_seq *esc) { const struct colors *colors = cfile->colors; - if (dstrdcat(&cfile->buffer, colors->leftcode) != 0) { + if (print_esc_chunk(cfile, colors->leftcode) != 0) { return -1; } - if (dstrdcat(&cfile->buffer, esc) != 0) { + if (print_esc_chunk(cfile, esc) != 0) { return -1; } - if (dstrdcat(&cfile->buffer, colors->rightcode) != 0) { + if (print_esc_chunk(cfile, colors->rightcode) != 0) { return -1; } @@ -671,20 +824,20 @@ static int print_reset(CFILE *cfile) { const struct colors *colors = cfile->colors; if (colors->endcode) { - return dstrdcat(&cfile->buffer, colors->endcode); + return dstrxcat(&cfile->buffer, colors->endcode->seq, colors->endcode->len); } else { return print_esc(cfile, colors->reset); } } /** Print a string with an optional color. */ -static int print_colored(CFILE *cfile, const char *esc, const char *str, size_t len) { +static int print_colored(CFILE *cfile, const struct esc_seq *esc, const char *str, size_t len) { if (esc) { if (print_esc(cfile, esc) != 0) { return -1; } } - if (dstrncat(&cfile->buffer, str, len) != 0) { + if (dstrxcat(&cfile->buffer, str, len) != 0) { return -1; } if (esc) { @@ -772,7 +925,7 @@ static int print_dirs_colored(CFILE *cfile, const char *path, const struct BFTW } if ((size_t)broken < nameoff) { - const char *color = colors->missing; + const struct esc_seq *color = colors->missing; if (!color) { color = colors->orphan; } @@ -786,8 +939,8 @@ static int print_dirs_colored(CFILE *cfile, const char *path, const struct BFTW /** Print a file name with colors. */ static int print_name_colored(CFILE *cfile, const char *name, const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { - const char *color = file_color(cfile->colors, name, ftwbuf, flags); - return print_colored(cfile, color, name, strlen(name)); + const struct esc_seq *esc = file_color(cfile->colors, name, ftwbuf, flags); + return print_colored(cfile, esc, name, strlen(name)); } /** Print a path with colors. */ @@ -1077,7 +1230,7 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { memcpy(name, i, len); name[len] = '\0'; - char **esc = get_color(colors, name); + struct esc_seq **esc = get_esc(colors, name); if (!esc) { goto invalid; } diff --git a/tests/bfs/color_ext_case.out b/tests/bfs/color_ext_case.out new file mode 100644 index 0000000..c3ded3a --- /dev/null +++ b/tests/bfs/color_ext_case.out @@ -0,0 +1,25 @@ +rainbow +rainbow/lower.gz +rainbow/lower.tar.gz +rainbow/exec.sh +rainbow/upper.GZ +rainbow/upper.TAR.GZ +rainbow/lower.tar +rainbow/upper.TAR +rainbow/ul.TAR.gz +rainbow/lu.tar.GZ +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/mh1 +rainbow/mh2 diff --git a/tests/bfs/color_ext_case.sh b/tests/bfs/color_ext_case.sh new file mode 100644 index 0000000..91f1f59 --- /dev/null +++ b/tests/bfs/color_ext_case.sh @@ -0,0 +1 @@ +LS_COLORS="*.gz=01;31:*.GZ=01;32:*.tAr=01;33:*.TAR.gz=01;34:*.tar.GZ=01;35:" bfs_diff rainbow -color diff --git a/tests/bfs/color_nul.out b/tests/bfs/color_nul.out index c328f82..4ea4d85 100644 Binary files a/tests/bfs/color_nul.out and b/tests/bfs/color_nul.out differ diff --git a/tests/bfs/color_nul.sh b/tests/bfs/color_nul.sh index 4979569..cb662d6 100644 --- a/tests/bfs/color_nul.sh +++ b/tests/bfs/color_nul.sh @@ -1,2 +1,3 @@ -LS_COLORS="ec=\33[m\0:" invoke_bfs rainbow -color -maxdepth 0 >"$OUT" +LS_COLORS="ec=\33[\0m:*.gz=\0\61;31:" invoke_bfs rainbow -color | tr '\0' '0' >"$OUT" +sort_output diff_output diff --git a/tests/bfs/color_star.out b/tests/bfs/color_star.out deleted file mode 100644 index 34c7153..0000000 --- a/tests/bfs/color_star.out +++ /dev/null @@ -1,25 +0,0 @@ -rainbow -rainbow/exec.sh -rainbow/socket -rainbow/broken -rainbow/chardev_link -rainbow/link.txt -rainbow/sticky_ow -rainbow/sgid -rainbow/pipe -rainbow/ow -rainbow/sugid -rainbow/suid -rainbow/sticky -rainbow/file.dat -rainbow/file.txt -rainbow/lower.gz -rainbow/lower.tar -rainbow/lower.tar.gz -rainbow/lu.tar.GZ -rainbow/mh1 -rainbow/mh2 -rainbow/ul.TAR.gz -rainbow/upper.GZ -rainbow/upper.TAR -rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_star.sh b/tests/bfs/color_star.sh index 3ada4fd..6d5312e 100644 --- a/tests/bfs/color_star.sh +++ b/tests/bfs/color_star.sh @@ -1,2 +1,2 @@ # Regression test: don't segfault on LS_COLORS="*" -LS_COLORS="*" bfs_diff rainbow -color +! LS_COLORS="*" invoke_bfs rainbow -color -- cgit v1.2.3 From 2ea6a1d7b99dbb389ec81054d136e5030717d52a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 30 Jun 2023 10:25:42 -0400 Subject: color: Compare values too when deciding to smart-case --- src/color.c | 81 +++++++++++++++++++++++++++++++++++--------- tests/bfs/color_ext_case.out | 2 +- tests/bfs/color_ext_case.sh | 7 +++- 3 files changed, 72 insertions(+), 18 deletions(-) (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index 82ff763..ec24d98 100644 --- a/src/color.c +++ b/src/color.c @@ -42,8 +42,8 @@ struct ext_color { struct esc_seq *esc; /** The length of the extension to match. */ size_t len; - /** Whether the comparison should be case-insensitive. */ - bool icase; + /** Whether the comparison should be case-sensitive. */ + bool case_sensitive; /** The extension to match (NUL-terminated). */ char ext[]; }; @@ -147,6 +147,11 @@ static int init_esc(struct colors *colors, const char *name, const char *value, } } +/** Check if an escape sequence is equal to a string. */ +static bool esc_eq(const struct esc_seq *esc, const char *str, size_t len) { + return esc->len == len && memcmp(esc->seq, str, len) == 0; +} + /** Get an escape sequence from the table. */ static struct esc_seq **get_esc(const struct colors *colors, const char *name) { const struct trie_leaf *leaf = trie_find_str(&colors->names, name); @@ -204,6 +209,55 @@ static void ext_tolower(char *ext, size_t len) { /** Maximum supported extension length. */ #define EXT_MAX 255 +/** + * The "smart case" algorithm. + * + * @param ext + * The current extension being added. + * @param prev + * The previous case-sensitive match, if any, for the same extension. + * @param iprev + * The previous case-insensitive match, if any, for the same extension. + * @return + * Whether this extension should become case-sensitive. + */ +static bool ext_case_sensitive(struct ext_color *ext, struct ext_color *prev, struct ext_color *iprev) { + // This is the first case-insensitive occurrence of this extension, e.g. + // + // *.gz=01;31:*.tar.gz=01;33 + if (!iprev) { + bfs_assert(!prev); + return false; + } + + // If the last version of this extension is already case-sensitive, + // this one should be too, e.g. + // + // *.tar.gz=01;31:*.TAR.GZ=01;32:*.TAR.GZ=01;33 + if (iprev->case_sensitive) { + return true; + } + + // The case matches the last occurrence exactly, e.g. + // + // *.tar.gz=01;31:*.tar.gz=01;33 + if (iprev == prev) { + return false; + } + + // Different case, but same value, e.g. + // + // *.tar.gz=01;31:*.TAR.GZ=01;31 + if (esc_eq(iprev->esc, ext->esc->seq, ext->esc->len)) { + return false; + } + + // Different case, different value, e.g. + // + // *.tar.gz=01;31:*.TAR.GZ=01;33 + return true; +} + /** Set the color for an extension. */ static int set_ext(struct colors *colors, char *key, char *value) { size_t len = dstrlen(key); @@ -214,7 +268,7 @@ static int set_ext(struct colors *colors, char *key, char *value) { ext->priority = colors->ext_count++; ext->len = len; - ext->icase = true; + ext->case_sensitive = false; ext->esc = new_esc(colors, value, dstrlen(value)); if (!ext->esc) { goto fail; @@ -253,12 +307,10 @@ static int set_ext(struct colors *colors, char *key, char *value) { goto fail; } - // If a match for the lowercased extension exists and is different from - // the exact match, or is already case-sensitive, mark this one too struct ext_color *iprev = leaf->value; - if (iprev && (iprev != prev || !iprev->icase)) { - iprev->icase = false; - ext->icase = false; + if (ext_case_sensitive(ext, prev, iprev)) { + iprev->case_sensitive = true; + ext->case_sensitive = true; } leaf->value = ext; @@ -279,14 +331,14 @@ static int build_iext_trie(struct colors *colors) { TRIE_FOR_EACH(&colors->ext_trie, leaf) { struct ext_color *ext = leaf->value; - if (!ext->icase) { + if (ext->case_sensitive) { continue; } // set_ext() already reversed and lowercased the extension struct trie_leaf *ileaf; while ((ileaf = trie_find_postfix(&colors->iext_trie, ext->ext))) { - trie_remove(&colors->ext_trie, ileaf); + trie_remove(&colors->iext_trie, ileaf); } ileaf = trie_insert_str(&colors->iext_trie, ext->ext); @@ -616,12 +668,9 @@ struct colors *parse_colors(void) { goto fail; } - if (colors->link) { - size_t len = strlen("target"); - if (colors->link->len == len && memcmp(colors->link->seq, "target", len) == 0) { - colors->link_as_target = true; - colors->link->len = 0; - } + if (colors->link && esc_eq(colors->link, "target", strlen("target"))) { + colors->link_as_target = true; + colors->link->len = 0; } return colors; diff --git a/tests/bfs/color_ext_case.out b/tests/bfs/color_ext_case.out index c3ded3a..4e7258d 100644 --- a/tests/bfs/color_ext_case.out +++ b/tests/bfs/color_ext_case.out @@ -16,10 +16,10 @@ rainbow/sgid rainbow/pipe rainbow/ow +rainbow/file.txt rainbow/sugid rainbow/suid rainbow/sticky rainbow/file.dat -rainbow/file.txt rainbow/mh1 rainbow/mh2 diff --git a/tests/bfs/color_ext_case.sh b/tests/bfs/color_ext_case.sh index 91f1f59..4adba69 100644 --- a/tests/bfs/color_ext_case.sh +++ b/tests/bfs/color_ext_case.sh @@ -1 +1,6 @@ -LS_COLORS="*.gz=01;31:*.GZ=01;32:*.tAr=01;33:*.TAR.gz=01;34:*.tar.GZ=01;35:" bfs_diff rainbow -color +# *.gz=01;31:*.GZ=01;32 -- case sensitive +# *.tAr=01;33:*.TaR=01;33 -- case-insensitive +# *.TAR.gz=01;34:*.tar.GZ=01;35 -- case-sensitive +# *.txt=35:*TXT=36 -- case-insensitive +export LS_COLORS="*.gz=01;31:*.GZ=01;32:*.tAr=01;33:*.TaR=01;33:*.TAR.gz=01;34:*.tar.GZ=01;35:*.txt=35:*TXT=36" +bfs_diff rainbow -color -- cgit v1.2.3 From afdc5dca7ca378cb75fe2a2c9df3881a89ba17dd Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 12 Jul 2023 14:10:29 -0400 Subject: tests/bfs: Add tests for -j --- tests/bfs/j1.out | 19 +++++++++++++++++++ tests/bfs/j1.sh | 1 + tests/bfs/j64.out | 19 +++++++++++++++++++ tests/bfs/j64.sh | 1 + tests/bfs/j_negative.sh | 1 + 5 files changed, 41 insertions(+) create mode 100644 tests/bfs/j1.out create mode 100644 tests/bfs/j1.sh create mode 100644 tests/bfs/j64.out create mode 100644 tests/bfs/j64.sh create mode 100644 tests/bfs/j_negative.sh (limited to 'tests/bfs') diff --git a/tests/bfs/j1.out b/tests/bfs/j1.out new file mode 100644 index 0000000..a7ccfe4 --- /dev/null +++ b/tests/bfs/j1.out @@ -0,0 +1,19 @@ +basic +basic/a +basic/b +basic/c +basic/c/d +basic/e +basic/e/f +basic/g +basic/g/h +basic/i +basic/j +basic/j/foo +basic/k +basic/k/foo +basic/k/foo/bar +basic/l +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar/baz diff --git a/tests/bfs/j1.sh b/tests/bfs/j1.sh new file mode 100644 index 0000000..972ac1b --- /dev/null +++ b/tests/bfs/j1.sh @@ -0,0 +1 @@ +bfs_diff -j1 basic diff --git a/tests/bfs/j64.out b/tests/bfs/j64.out new file mode 100644 index 0000000..a7ccfe4 --- /dev/null +++ b/tests/bfs/j64.out @@ -0,0 +1,19 @@ +basic +basic/a +basic/b +basic/c +basic/c/d +basic/e +basic/e/f +basic/g +basic/g/h +basic/i +basic/j +basic/j/foo +basic/k +basic/k/foo +basic/k/foo/bar +basic/l +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar/baz diff --git a/tests/bfs/j64.sh b/tests/bfs/j64.sh new file mode 100644 index 0000000..c56788f --- /dev/null +++ b/tests/bfs/j64.sh @@ -0,0 +1 @@ +bfs_diff -j64 basic diff --git a/tests/bfs/j_negative.sh b/tests/bfs/j_negative.sh new file mode 100644 index 0000000..809c98c --- /dev/null +++ b/tests/bfs/j_negative.sh @@ -0,0 +1 @@ +! invoke_bfs -j-1 basic -- cgit v1.2.3 From 663a2b938707458de4e87be68cffb6a970e771c6 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 12 Jul 2023 14:12:27 -0400 Subject: parse: Reject -j0 --- src/parse.c | 10 +++++++++- tests/bfs/j0.sh | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/bfs/j0.sh (limited to 'tests/bfs') diff --git a/src/parse.c b/src/parse.c index c225a5b..6b1eaa0 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1623,11 +1623,19 @@ static struct bfs_expr *parse_jobs(struct parser_state *state, int arg1, int arg return NULL; } - if (!parse_int(state, expr->argv, expr->argv[0] + 2, &state->ctx->threads, IF_INT | IF_UNSIGNED)) { + unsigned int n; + if (!parse_int(state, expr->argv, expr->argv[0] + 2, &n, IF_INT | IF_UNSIGNED)) { bfs_expr_free(expr); return NULL; } + if (n == 0) { + parse_expr_error(state, expr, "${bld}0${rs} is not enough threads.\n"); + bfs_expr_free(expr); + return NULL; + } + + state->ctx->threads = n; return expr; } diff --git a/tests/bfs/j0.sh b/tests/bfs/j0.sh new file mode 100644 index 0000000..97a7c5c --- /dev/null +++ b/tests/bfs/j0.sh @@ -0,0 +1 @@ +! invoke_bfs -j0 basic -- cgit v1.2.3 From b577bedfd37d93cb802c36beb93cf6581af51930 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Jul 2023 13:40:46 -0400 Subject: color: Get rid of EXT_MAX --- src/color.c | 40 ++++++++++++++++++++++++++++++---------- tests/bfs/color_deep.out | 16 ++++++++++++++++ tests/bfs/color_deep.sh | 7 +++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 tests/bfs/color_deep.out create mode 100644 tests/bfs/color_deep.sh (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index 0f5829f..00d7920 100644 --- a/src/color.c +++ b/src/color.c @@ -106,6 +106,8 @@ struct colors { /** Number of extensions. */ size_t ext_count; + /** Longest extension. */ + size_t ext_len; /** Case-sensitive extension trie. */ struct trie ext_trie; /** Case-insensitive extension trie. */ @@ -207,9 +209,6 @@ static void ext_tolower(char *ext, size_t len) { } } -/** Maximum supported extension length. */ -#define EXT_MAX 255 - /** * The "smart case" algorithm. * @@ -331,6 +330,11 @@ static int build_iext_trie(struct colors *colors) { trie_init(&colors->iext_trie); TRIE_FOR_EACH(&colors->ext_trie, leaf) { + size_t len = leaf->length - 1; + if (colors->ext_len < len) { + colors->ext_len = len; + } + struct ext_color *ext = leaf->value; if (ext->case_sensitive) { continue; @@ -356,25 +360,40 @@ static int build_iext_trie(struct colors *colors) { * Find a color by an extension. */ static const struct esc_seq *get_ext(const struct colors *colors, const char *filename) { + size_t ext_len = colors->ext_len; size_t name_len = strlen(filename); - size_t ext_len = name_len < EXT_MAX ? name_len : EXT_MAX; + if (name_len < ext_len) { + ext_len = name_len; + } const char *suffix = filename + name_len - ext_len; - char xfrm[ext_len + 1]; - memcpy(xfrm, suffix, sizeof(xfrm)); + char buf[256]; + char *copy; + if (ext_len < sizeof(buf)) { + copy = memcpy(buf, suffix, ext_len + 1); + } else { + copy = strndup(suffix, ext_len); + if (!copy) { + return NULL; + } + } - ext_reverse(xfrm, ext_len); - const struct trie_leaf *leaf = trie_find_prefix(&colors->ext_trie, xfrm); + ext_reverse(copy, ext_len); + const struct trie_leaf *leaf = trie_find_prefix(&colors->ext_trie, copy); const struct ext_color *ext = leaf ? leaf->value : NULL; - ext_tolower(xfrm, ext_len); - const struct trie_leaf *ileaf = trie_find_prefix(&colors->iext_trie, xfrm); + ext_tolower(copy, ext_len); + const struct trie_leaf *ileaf = trie_find_prefix(&colors->iext_trie, copy); const struct ext_color *iext = ileaf ? ileaf->value : NULL; if (iext && (!ext || ext->priority < iext->priority)) { ext = iext; } + if (copy != buf) { + free(copy); + } + return ext ? ext->esc : NULL; } @@ -601,6 +620,7 @@ struct colors *parse_colors(void) { VARENA_INIT(&colors->ext_arena, struct ext_color, ext); trie_init(&colors->names); colors->ext_count = 0; + colors->ext_len = 0; trie_init(&colors->ext_trie); trie_init(&colors->iext_trie); diff --git a/tests/bfs/color_deep.out b/tests/bfs/color_deep.out new file mode 100644 index 0000000..fb990d5 --- /dev/null +++ b/tests/bfs/color_deep.out @@ -0,0 +1,16 @@ +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE diff --git a/tests/bfs/color_deep.sh b/tests/bfs/color_deep.sh new file mode 100644 index 0000000..a83ee0e --- /dev/null +++ b/tests/bfs/color_deep.sh @@ -0,0 +1,7 @@ +name="0123456789ABCDEF" +name="${name}${name}${name}${name}" +name="${name}${name}${name}${name}" +name="${name:0:255}" +export LS_COLORS="*${name}=01:" + +bfs_diff deep -color -type f -printf '%f\n' -- cgit v1.2.3 From c5a2bfd924445dfccd943ea34374a5d1266f5cb9 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Jul 2023 15:22:42 -0400 Subject: color: TTY-escape filenames --- src/color.c | 10 +++++++++- src/color.h | 1 + src/printf.c | 4 ++-- tests/bfs/color.out | 2 ++ tests/bfs/color_L.out | 2 ++ tests/bfs/color_L_ln_target.out | 2 ++ tests/bfs/color_L_no_stat.out | 2 ++ tests/bfs/color_cd0_no.out | 2 ++ tests/bfs/color_escapes.out | 2 ++ tests/bfs/color_ext.out | 2 ++ tests/bfs/color_ext0.out | 2 ++ tests/bfs/color_ext_case.out | 2 ++ tests/bfs/color_ext_override.out | 2 ++ tests/bfs/color_ext_underride.out | 2 ++ tests/bfs/color_fi0_no.out | 2 ++ tests/bfs/color_fi_no.out | 2 ++ tests/bfs/color_ln_target.out | 2 ++ tests/bfs/color_mh.out | 2 ++ tests/bfs/color_mh0.out | 2 ++ tests/bfs/color_mi.out | 2 ++ tests/bfs/color_missing_colon.out | 2 ++ tests/bfs/color_no.out | 2 ++ tests/bfs/color_no_stat.out | 2 ++ tests/bfs/color_nul.out | 2 ++ tests/bfs/color_or.out | 2 ++ tests/bfs/color_or0_mi.out | 2 ++ tests/bfs/color_or0_mi0.out | 2 ++ tests/bfs/color_or_mi.out | 2 ++ tests/bfs/color_or_mi0.out | 2 ++ tests/bfs/color_rs_lc_rc_ec.out | 2 ++ tests/bfs/color_st0_tw0_ow.out | 2 ++ tests/bfs/color_st0_tw0_ow0.out | 2 ++ tests/bfs/color_st0_tw_ow.out | 2 ++ tests/bfs/color_st0_tw_ow0.out | 2 ++ tests/bfs/color_st_tw0_ow.out | 2 ++ tests/bfs/color_st_tw0_ow0.out | 2 ++ tests/bfs/color_st_tw_ow0.out | 2 ++ tests/bfs/color_su0_sg.out | 2 ++ tests/bfs/color_su0_sg0.out | 2 ++ tests/bfs/color_su_sg0.out | 2 ++ tests/bfs/nocolor.out | 2 ++ tests/gnu/empty_special.out | 1 + tests/tests.sh | 1 + 43 files changed, 90 insertions(+), 3 deletions(-) (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index 00d7920..6593f5b 100644 --- a/src/color.c +++ b/src/color.c @@ -922,9 +922,12 @@ static int print_colored(CFILE *cfile, const struct esc_seq *esc, const char *st if (print_esc(cfile, esc) != 0) { return -1; } - if (dstrxcat(&cfile->buffer, str, len) != 0) { + + // Don't let the string itself interfere with the colors + if (print_wordesc(cfile, str, len, WESC_TTY) != 0) { return -1; } + if (print_reset(cfile) != 0) { return -1; } @@ -1242,6 +1245,11 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { return -1; } break; + case 'Q': + if (print_wordesc(cfile, va_arg(args, const char *), SIZE_MAX, WESC_TTY) != 0) { + return -1; + } + break; case 'F': if (print_name(cfile, va_arg(args, const struct BFTW *)) != 0) { diff --git a/src/color.h b/src/color.h index 932d551..0d46c33 100644 --- a/src/color.h +++ b/src/color.h @@ -87,6 +87,7 @@ int cfclose(CFILE *cfile); * %zu: A size_t * %m: strerror(errno) * %pq: A shell-escaped string, like bash's printf %q + * %pQ: A TTY-escaped string. * %pF: A colored file name, from a const struct BFTW * argument * %pP: A colored file path, from a const struct BFTW * argument * %pL: A colored link target, from a const struct BFTW * argument diff --git a/src/printf.c b/src/printf.c index 6520d2d..f0910fa 100644 --- a/src/printf.c +++ b/src/printf.c @@ -298,7 +298,7 @@ static int bfs_printf_h(CFILE *cfile, const struct bfs_printf *directive, const int ret; if (should_color(cfile, directive)) { - ret = cfprintf(cfile, "${di}%s${rs}", buf); + ret = cfprintf(cfile, "${di}%pQ${rs}", buf); } else { ret = dyn_fprintf(cfile->file, directive, buf); } @@ -313,7 +313,7 @@ static int bfs_printf_H(CFILE *cfile, const struct bfs_printf *directive, const if (ftwbuf->depth == 0) { return cfprintf(cfile, "%pP", ftwbuf); } else { - return cfprintf(cfile, "${di}%s${rs}", ftwbuf->root); + return cfprintf(cfile, "${di}%pQ${rs}", ftwbuf->root); } } else { return dyn_fprintf(cfile->file, directive, ftwbuf->root); diff --git a/tests/bfs/color.out b/tests/bfs/color.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color.out +++ b/tests/bfs/color.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_L.out b/tests/bfs/color_L.out index 89f9410..6904e39 100644 --- a/tests/bfs/color_L.out +++ b/tests/bfs/color_L.out @@ -1,6 +1,8 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh rainbow/chardev_link +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/sticky_ow diff --git a/tests/bfs/color_L_ln_target.out b/tests/bfs/color_L_ln_target.out index 2562c98..50105c3 100644 --- a/tests/bfs/color_L_ln_target.out +++ b/tests/bfs/color_L_ln_target.out @@ -1,7 +1,9 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh rainbow/chardev_link +rainbow/$'\e[1m' rainbow/socket rainbow/sticky_ow rainbow/sgid diff --git a/tests/bfs/color_L_no_stat.out b/tests/bfs/color_L_no_stat.out index 4fe99c0..beb538c 100644 --- a/tests/bfs/color_L_no_stat.out +++ b/tests/bfs/color_L_no_stat.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/chardev_link +rainbow/$'\e[1m' rainbow/ow rainbow/sticky rainbow/sticky_ow diff --git a/tests/bfs/color_cd0_no.out b/tests/bfs/color_cd0_no.out index 30ad97f..4d710fd 100644 --- a/tests/bfs/color_cd0_no.out +++ b/tests/bfs/color_cd0_no.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/file.dat diff --git a/tests/bfs/color_escapes.out b/tests/bfs/color_escapes.out index 808585e..746a9ea 100644 --- a/tests/bfs/color_escapes.out +++ b/tests/bfs/color_escapes.out @@ -1,5 +1,7 @@ +:$'rainbow/\e[1m/'$'\e[0m' :rainbow :rainbow/:exec.sh +:rainbow/:$'\e[1m' :rainbow/:socket :rainbow/:broken :rainbow/:chardev_link diff --git a/tests/bfs/color_ext.out b/tests/bfs/color_ext.out index be22b82..a35ca0b 100644 --- a/tests/bfs/color_ext.out +++ b/tests/bfs/color_ext.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext0.out b/tests/bfs/color_ext0.out index d151319..37301cc 100644 --- a/tests/bfs/color_ext0.out +++ b/tests/bfs/color_ext0.out @@ -1,6 +1,8 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/file.txt rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext_case.out b/tests/bfs/color_ext_case.out index 4e7258d..9388343 100644 --- a/tests/bfs/color_ext_case.out +++ b/tests/bfs/color_ext_case.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.gz rainbow/lower.tar.gz @@ -6,6 +7,7 @@ rainbow/upper.TAR.GZ rainbow/lower.tar rainbow/upper.TAR +rainbow/$'\e[1m' rainbow/ul.TAR.gz rainbow/lu.tar.GZ rainbow/socket diff --git a/tests/bfs/color_ext_override.out b/tests/bfs/color_ext_override.out index 3a09cd2..1e0146c 100644 --- a/tests/bfs/color_ext_override.out +++ b/tests/bfs/color_ext_override.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh rainbow/lower.tar @@ -8,6 +9,7 @@ rainbow/ul.TAR.gz rainbow/upper.GZ rainbow/upper.TAR.GZ +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext_underride.out b/tests/bfs/color_ext_underride.out index 073d176..1ed66da 100644 --- a/tests/bfs/color_ext_underride.out +++ b/tests/bfs/color_ext_underride.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.tar.gz rainbow/lu.tar.GZ @@ -8,6 +9,7 @@ rainbow/upper.TAR rainbow/lower.gz rainbow/upper.GZ +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_fi0_no.out b/tests/bfs/color_fi0_no.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color_fi0_no.out +++ b/tests/bfs/color_fi0_no.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_fi_no.out b/tests/bfs/color_fi_no.out index 8df9355..e64684d 100644 --- a/tests/bfs/color_fi_no.out +++ b/tests/bfs/color_fi_no.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ln_target.out b/tests/bfs/color_ln_target.out index 2562c98..50105c3 100644 --- a/tests/bfs/color_ln_target.out +++ b/tests/bfs/color_ln_target.out @@ -1,7 +1,9 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh rainbow/chardev_link +rainbow/$'\e[1m' rainbow/socket rainbow/sticky_ow rainbow/sgid diff --git a/tests/bfs/color_mh.out b/tests/bfs/color_mh.out index 7521b31..93bfde1 100644 --- a/tests/bfs/color_mh.out +++ b/tests/bfs/color_mh.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_mh0.out b/tests/bfs/color_mh0.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color_mh0.out +++ b/tests/bfs/color_mh0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_mi.out b/tests/bfs/color_mi.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color_mi.out +++ b/tests/bfs/color_mi.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_missing_colon.out b/tests/bfs/color_missing_colon.out index be22b82..a35ca0b 100644 --- a/tests/bfs/color_missing_colon.out +++ b/tests/bfs/color_missing_colon.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_no.out b/tests/bfs/color_no.out index b70e47d..c61d246 100644 --- a/tests/bfs/color_no.out +++ b/tests/bfs/color_no.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_no_stat.out b/tests/bfs/color_no_stat.out index 7d7d767..e57e11a 100644 --- a/tests/bfs/color_no_stat.out +++ b/tests/bfs/color_no_stat.out @@ -1,4 +1,6 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow +rainbow/$'\e[1m' rainbow/ow rainbow/sticky rainbow/sticky_ow diff --git a/tests/bfs/color_nul.out b/tests/bfs/color_nul.out index 4ea4d85..7140e32 100644 --- a/tests/bfs/color_nul.out +++ b/tests/bfs/color_nul.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.gz rainbow/lower.tar.gz @@ -6,6 +7,7 @@ rainbow/upper.GZ rainbow/upper.TAR.GZ rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or.out b/tests/bfs/color_or.out index 98efb63..07916da 100644 --- a/tests/bfs/color_or.out +++ b/tests/bfs/color_or.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_or0_mi.out b/tests/bfs/color_or0_mi.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color_or0_mi.out +++ b/tests/bfs/color_or0_mi.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or0_mi0.out b/tests/bfs/color_or0_mi0.out index 34c7153..5c6b43e 100644 --- a/tests/bfs/color_or0_mi0.out +++ b/tests/bfs/color_or0_mi0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or_mi.out b/tests/bfs/color_or_mi.out index a9dc229..20bc61d 100644 --- a/tests/bfs/color_or_mi.out +++ b/tests/bfs/color_or_mi.out @@ -1,6 +1,8 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_or_mi0.out b/tests/bfs/color_or_mi0.out index a9dc229..20bc61d 100644 --- a/tests/bfs/color_or_mi0.out +++ b/tests/bfs/color_or_mi0.out @@ -1,6 +1,8 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_rs_lc_rc_ec.out b/tests/bfs/color_rs_lc_rc_ec.out index 82d94ec..f78b41f 100644 --- a/tests/bfs/color_rs_lc_rc_ec.out +++ b/tests/bfs/color_rs_lc_rc_ec.out @@ -1,4 +1,6 @@ +LC01;34RC$'rainbow/\e[1m/'EC$'\e[0m' LC01;34RCrainbow/ECLC01;32RCexec.shEC +LC01;34RCrainbow/ECLC01;34RC$'\e[1m'EC LC01;34RCrainbow/ECLC01;35RCsocketEC LC01;34RCrainbow/ECLC01;36RCbrokenEC LC01;34RCrainbow/ECLC01;36RCchardev_linkEC diff --git a/tests/bfs/color_st0_tw0_ow.out b/tests/bfs/color_st0_tw0_ow.out index bdc5942..d1fec74 100644 --- a/tests/bfs/color_st0_tw0_ow.out +++ b/tests/bfs/color_st0_tw0_ow.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/sticky rainbow/socket rainbow/broken diff --git a/tests/bfs/color_st0_tw0_ow0.out b/tests/bfs/color_st0_tw0_ow0.out index f13b7f3..cb80cb8 100644 --- a/tests/bfs/color_st0_tw0_ow0.out +++ b/tests/bfs/color_st0_tw0_ow0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/ow rainbow/sticky rainbow/sticky_ow diff --git a/tests/bfs/color_st0_tw_ow.out b/tests/bfs/color_st0_tw_ow.out index 2d7f682..a183932 100644 --- a/tests/bfs/color_st0_tw_ow.out +++ b/tests/bfs/color_st0_tw_ow.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/sticky rainbow/socket rainbow/broken diff --git a/tests/bfs/color_st0_tw_ow0.out b/tests/bfs/color_st0_tw_ow0.out index 7e343b8..cdc5cdd 100644 --- a/tests/bfs/color_st0_tw_ow0.out +++ b/tests/bfs/color_st0_tw_ow0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/ow rainbow/sticky rainbow/socket diff --git a/tests/bfs/color_st_tw0_ow.out b/tests/bfs/color_st_tw0_ow.out index c61a327..f45c75c 100644 --- a/tests/bfs/color_st_tw0_ow.out +++ b/tests/bfs/color_st_tw0_ow.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_st_tw0_ow0.out b/tests/bfs/color_st_tw0_ow0.out index 929a993..d95d12a 100644 --- a/tests/bfs/color_st_tw0_ow0.out +++ b/tests/bfs/color_st_tw0_ow0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/ow rainbow/socket rainbow/broken diff --git a/tests/bfs/color_st_tw_ow0.out b/tests/bfs/color_st_tw_ow0.out index 7092f5a..f1059dd 100644 --- a/tests/bfs/color_st_tw_ow0.out +++ b/tests/bfs/color_st_tw_ow0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/ow rainbow/socket rainbow/broken diff --git a/tests/bfs/color_su0_sg.out b/tests/bfs/color_su0_sg.out index 2bce534..31d5108 100644 --- a/tests/bfs/color_su0_sg.out +++ b/tests/bfs/color_su0_sg.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_su0_sg0.out b/tests/bfs/color_su0_sg0.out index 0c3d757..4804030 100644 --- a/tests/bfs/color_su0_sg0.out +++ b/tests/bfs/color_su0_sg0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_su_sg0.out b/tests/bfs/color_su_sg0.out index 7a70598..3dbc7fe 100644 --- a/tests/bfs/color_su_sg0.out +++ b/tests/bfs/color_su_sg0.out @@ -1,5 +1,7 @@ +$'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh +rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/nocolor.out b/tests/bfs/nocolor.out index 29e7de8..d51d24d 100644 --- a/tests/bfs/nocolor.out +++ b/tests/bfs/nocolor.out @@ -1,4 +1,6 @@ rainbow +rainbow/ +rainbow// rainbow/broken rainbow/chardev_link rainbow/exec.sh diff --git a/tests/gnu/empty_special.out b/tests/gnu/empty_special.out index 3aa57d2..fa35478 100644 --- a/tests/gnu/empty_special.out +++ b/tests/gnu/empty_special.out @@ -1,3 +1,4 @@ +rainbow// rainbow/exec.sh rainbow/file.dat rainbow/file.txt diff --git a/tests/tests.sh b/tests/tests.sh index 46e3e33..66a79a8 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -432,6 +432,7 @@ function make_rainbow() { chmod +t "$1"/sticky* "$XTOUCH" -p "$1"/exec.sh chmod +x "$1"/exec.sh + "$XTOUCH" -p "$1/"$'\e[1m/\e[0m' } make_rainbow "$TMP/rainbow" -- cgit v1.2.3 From 0e4dffeb46f3d8bfe30d60a0cfe1e1fa5a082b13 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Jul 2023 15:45:08 -0400 Subject: color: Don't break up leading and trailing dirs --- src/color.c | 59 +++++++++++++++++++++------------------ tests/bfs/color.out | 2 +- tests/bfs/color_L.out | 2 +- tests/bfs/color_L_ln_target.out | 2 +- tests/bfs/color_L_no_stat.out | 8 +++--- tests/bfs/color_cd0_no.out | 2 +- tests/bfs/color_escapes.out | 2 +- tests/bfs/color_ext.out | 2 +- tests/bfs/color_ext0.out | 2 +- tests/bfs/color_ext_case.out | 2 +- tests/bfs/color_ext_override.out | 2 +- tests/bfs/color_ext_underride.out | 2 +- tests/bfs/color_fi0_no.out | 2 +- tests/bfs/color_fi_no.out | 2 +- tests/bfs/color_ln_target.out | 2 +- tests/bfs/color_ls.out | 16 +++++------ tests/bfs/color_mh.out | 2 +- tests/bfs/color_mh0.out | 2 +- tests/bfs/color_mi.out | 2 +- tests/bfs/color_missing_colon.out | 2 +- tests/bfs/color_no.out | 2 +- tests/bfs/color_no_stat.out | 8 +++--- tests/bfs/color_nul.out | 2 +- tests/bfs/color_or.out | 2 +- tests/bfs/color_or0_mi.out | 2 +- tests/bfs/color_or0_mi0.out | 2 +- tests/bfs/color_or_mi.out | 2 +- tests/bfs/color_or_mi0.out | 2 +- tests/bfs/color_rs_lc_rc_ec.out | 2 +- tests/bfs/color_st0_tw0_ow.out | 4 +-- tests/bfs/color_st0_tw0_ow0.out | 8 +++--- tests/bfs/color_st0_tw_ow.out | 4 +-- tests/bfs/color_st0_tw_ow0.out | 6 ++-- tests/bfs/color_st_tw0_ow.out | 2 +- tests/bfs/color_st_tw0_ow0.out | 4 +-- tests/bfs/color_st_tw_ow0.out | 4 +-- tests/bfs/color_su0_sg.out | 2 +- tests/bfs/color_su0_sg0.out | 2 +- tests/bfs/color_su_sg0.out | 2 +- tests/bfs/printf_color.out | 4 ++- 40 files changed, 95 insertions(+), 88 deletions(-) (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index 6593f5b..7d98978 100644 --- a/src/color.c +++ b/src/color.c @@ -995,27 +995,48 @@ out: return ret; } -/** Print the directories leading up to a file. */ -static int print_dirs_colored(CFILE *cfile, const char *path, const struct BFTW *ftwbuf, enum bfs_stat_flags flags, size_t nameoff) { - const struct colors *colors = cfile->colors; +/** Print a path with colors. */ +static int print_path_colored(CFILE *cfile, const char *path, const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { + size_t nameoff; + if (path == ftwbuf->path) { + nameoff = ftwbuf->nameoff; + } else { + nameoff = xbaseoff(path); + } + + const char *name = path + nameoff; + size_t pathlen = nameoff + strlen(name); ssize_t broken = first_broken_offset(path, ftwbuf, flags, nameoff); if (broken < 0) { return -1; } + size_t split = broken; - if (broken > 0) { - if (print_colored(cfile, colors->directory, path, broken) != 0) { - return -1; + const struct colors *colors = cfile->colors; + const struct esc_seq *dirs_color = colors->directory; + const struct esc_seq *name_color; + + if (split < nameoff) { + name_color = colors->missing; + if (!name_color) { + name_color = colors->orphan; + } + } else { + name_color = file_color(cfile->colors, path + nameoff, ftwbuf, flags); + if (name_color == dirs_color) { + split = pathlen; } } - if ((size_t)broken < nameoff) { - const struct esc_seq *color = colors->missing; - if (!color) { - color = colors->orphan; + if (split > 0) { + if (print_colored(cfile, dirs_color, path, split) != 0) { + return -1; } - if (print_colored(cfile, color, path + broken, nameoff - broken) != 0) { + } + + if (split < pathlen) { + if (print_colored(cfile, name_color, path + split, pathlen - split) != 0) { return -1; } } @@ -1029,22 +1050,6 @@ static int print_name_colored(CFILE *cfile, const char *name, const struct BFTW return print_colored(cfile, esc, name, strlen(name)); } -/** Print a path with colors. */ -static int print_path_colored(CFILE *cfile, const char *path, const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { - size_t nameoff; - if (path == ftwbuf->path) { - nameoff = ftwbuf->nameoff; - } else { - nameoff = xbaseoff(path); - } - - if (print_dirs_colored(cfile, path, ftwbuf, flags, nameoff) != 0) { - return -1; - } - - return print_name_colored(cfile, path + nameoff, ftwbuf, flags); -} - /** Print the name of a file with the appropriate colors. */ static int print_name(CFILE *cfile, const struct BFTW *ftwbuf) { const char *name = ftwbuf->path + ftwbuf->nameoff; diff --git a/tests/bfs/color.out b/tests/bfs/color.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color.out +++ b/tests/bfs/color.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_L.out b/tests/bfs/color_L.out index 6904e39..85923db 100644 --- a/tests/bfs/color_L.out +++ b/tests/bfs/color_L.out @@ -1,8 +1,8 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh rainbow/chardev_link -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/sticky_ow diff --git a/tests/bfs/color_L_ln_target.out b/tests/bfs/color_L_ln_target.out index 50105c3..23fe8d7 100644 --- a/tests/bfs/color_L_ln_target.out +++ b/tests/bfs/color_L_ln_target.out @@ -1,9 +1,9 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh rainbow/chardev_link -rainbow/$'\e[1m' rainbow/socket rainbow/sticky_ow rainbow/sgid diff --git a/tests/bfs/color_L_no_stat.out b/tests/bfs/color_L_no_stat.out index beb538c..72e0319 100644 --- a/tests/bfs/color_L_no_stat.out +++ b/tests/bfs/color_L_no_stat.out @@ -1,10 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/chardev_link -rainbow/$'\e[1m' -rainbow/ow -rainbow/sticky -rainbow/sticky_ow rainbow/socket rainbow/broken rainbow/file.txt @@ -25,3 +22,6 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow +rainbow/sticky +rainbow/sticky_ow diff --git a/tests/bfs/color_cd0_no.out b/tests/bfs/color_cd0_no.out index 4d710fd..37b3fbc 100644 --- a/tests/bfs/color_cd0_no.out +++ b/tests/bfs/color_cd0_no.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/file.dat diff --git a/tests/bfs/color_escapes.out b/tests/bfs/color_escapes.out index 746a9ea..0bf9fbb 100644 --- a/tests/bfs/color_escapes.out +++ b/tests/bfs/color_escapes.out @@ -1,7 +1,7 @@ +:$'rainbow/\e[1m' :$'rainbow/\e[1m/'$'\e[0m' :rainbow :rainbow/:exec.sh -:rainbow/:$'\e[1m' :rainbow/:socket :rainbow/:broken :rainbow/:chardev_link diff --git a/tests/bfs/color_ext.out b/tests/bfs/color_ext.out index a35ca0b..218100f 100644 --- a/tests/bfs/color_ext.out +++ b/tests/bfs/color_ext.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext0.out b/tests/bfs/color_ext0.out index 37301cc..d2a7fd5 100644 --- a/tests/bfs/color_ext0.out +++ b/tests/bfs/color_ext0.out @@ -1,8 +1,8 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/file.txt rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext_case.out b/tests/bfs/color_ext_case.out index 9388343..93dc8f6 100644 --- a/tests/bfs/color_ext_case.out +++ b/tests/bfs/color_ext_case.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.gz @@ -7,7 +8,6 @@ rainbow/upper.TAR.GZ rainbow/lower.tar rainbow/upper.TAR -rainbow/$'\e[1m' rainbow/ul.TAR.gz rainbow/lu.tar.GZ rainbow/socket diff --git a/tests/bfs/color_ext_override.out b/tests/bfs/color_ext_override.out index 1e0146c..0acfcbc 100644 --- a/tests/bfs/color_ext_override.out +++ b/tests/bfs/color_ext_override.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh @@ -9,7 +10,6 @@ rainbow/ul.TAR.gz rainbow/upper.GZ rainbow/upper.TAR.GZ -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ext_underride.out b/tests/bfs/color_ext_underride.out index 1ed66da..5c98341 100644 --- a/tests/bfs/color_ext_underride.out +++ b/tests/bfs/color_ext_underride.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.tar.gz @@ -9,7 +10,6 @@ rainbow/upper.TAR rainbow/lower.gz rainbow/upper.GZ -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_fi0_no.out b/tests/bfs/color_fi0_no.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color_fi0_no.out +++ b/tests/bfs/color_fi0_no.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_fi_no.out b/tests/bfs/color_fi_no.out index e64684d..1c1ad8e 100644 --- a/tests/bfs/color_fi_no.out +++ b/tests/bfs/color_fi_no.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_ln_target.out b/tests/bfs/color_ln_target.out index 50105c3..23fe8d7 100644 --- a/tests/bfs/color_ln_target.out +++ b/tests/bfs/color_ln_target.out @@ -1,9 +1,9 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh rainbow/chardev_link -rainbow/$'\e[1m' rainbow/socket rainbow/sticky_ow rainbow/sgid diff --git a/tests/bfs/color_ls.out b/tests/bfs/color_ls.out index b08d894..b5216c4 100644 --- a/tests/bfs/color_ls.out +++ b/tests/bfs/color_ls.out @@ -1,12 +1,12 @@ -scratch/foo/bar -scratch/foo/bar -/__bfs__/nowhere -/__bfs__/nowhere -foo/bar/baz/qux -foo/bar/baz/qux +scratch/foo/bar +scratch/foo/bar +/__bfs__/nowhere +/__bfs__/nowhere +foo/bar/baz/qux +foo/bar/baz/qux foo/bar/nowhere foo/bar/nowhere -foo/bar/nowhere/nothing -foo/bar/nowhere/nothing +foo/bar/nowhere/nothing +foo/bar/nowhere/nothing foo/bar/baz foo/bar/baz diff --git a/tests/bfs/color_mh.out b/tests/bfs/color_mh.out index 93bfde1..c658082 100644 --- a/tests/bfs/color_mh.out +++ b/tests/bfs/color_mh.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_mh0.out b/tests/bfs/color_mh0.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color_mh0.out +++ b/tests/bfs/color_mh0.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_mi.out b/tests/bfs/color_mi.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color_mi.out +++ b/tests/bfs/color_mi.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_missing_colon.out b/tests/bfs/color_missing_colon.out index a35ca0b..218100f 100644 --- a/tests/bfs/color_missing_colon.out +++ b/tests/bfs/color_missing_colon.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_no.out b/tests/bfs/color_no.out index c61d246..67e1eee 100644 --- a/tests/bfs/color_no.out +++ b/tests/bfs/color_no.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_no_stat.out b/tests/bfs/color_no_stat.out index e57e11a..e3031b2 100644 --- a/tests/bfs/color_no_stat.out +++ b/tests/bfs/color_no_stat.out @@ -1,9 +1,6 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow -rainbow/$'\e[1m' -rainbow/ow -rainbow/sticky -rainbow/sticky_ow rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +22,6 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow +rainbow/sticky +rainbow/sticky_ow diff --git a/tests/bfs/color_nul.out b/tests/bfs/color_nul.out index 7140e32..8ccd9a7 100644 --- a/tests/bfs/color_nul.out +++ b/tests/bfs/color_nul.out @@ -1,3 +1,4 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/lower.gz @@ -7,7 +8,6 @@ rainbow/upper.GZ rainbow/upper.TAR.GZ rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or.out b/tests/bfs/color_or.out index 07916da..0bd2570 100644 --- a/tests/bfs/color_or.out +++ b/tests/bfs/color_or.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_or0_mi.out b/tests/bfs/color_or0_mi.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color_or0_mi.out +++ b/tests/bfs/color_or0_mi.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or0_mi0.out b/tests/bfs/color_or0_mi0.out index 5c6b43e..a439814 100644 --- a/tests/bfs/color_or0_mi0.out +++ b/tests/bfs/color_or0_mi0.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_or_mi.out b/tests/bfs/color_or_mi.out index 20bc61d..fb67e58 100644 --- a/tests/bfs/color_or_mi.out +++ b/tests/bfs/color_or_mi.out @@ -1,8 +1,8 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_or_mi0.out b/tests/bfs/color_or_mi0.out index 20bc61d..fb67e58 100644 --- a/tests/bfs/color_or_mi0.out +++ b/tests/bfs/color_or_mi0.out @@ -1,8 +1,8 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/broken rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/chardev_link rainbow/link.txt diff --git a/tests/bfs/color_rs_lc_rc_ec.out b/tests/bfs/color_rs_lc_rc_ec.out index f78b41f..077ef8d 100644 --- a/tests/bfs/color_rs_lc_rc_ec.out +++ b/tests/bfs/color_rs_lc_rc_ec.out @@ -1,6 +1,6 @@ +LC01;34RC$'rainbow/\e[1m'EC LC01;34RC$'rainbow/\e[1m/'EC$'\e[0m' LC01;34RCrainbow/ECLC01;32RCexec.shEC -LC01;34RCrainbow/ECLC01;34RC$'\e[1m'EC LC01;34RCrainbow/ECLC01;35RCsocketEC LC01;34RCrainbow/ECLC01;36RCbrokenEC LC01;34RCrainbow/ECLC01;36RCchardev_linkEC diff --git a/tests/bfs/color_st0_tw0_ow.out b/tests/bfs/color_st0_tw0_ow.out index d1fec74..a82762b 100644 --- a/tests/bfs/color_st0_tw0_ow.out +++ b/tests/bfs/color_st0_tw0_ow.out @@ -1,8 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/sticky rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +24,4 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/sticky diff --git a/tests/bfs/color_st0_tw0_ow0.out b/tests/bfs/color_st0_tw0_ow0.out index cb80cb8..041f1d4 100644 --- a/tests/bfs/color_st0_tw0_ow0.out +++ b/tests/bfs/color_st0_tw0_ow0.out @@ -1,10 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/ow -rainbow/sticky -rainbow/sticky_ow rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +22,6 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow +rainbow/sticky +rainbow/sticky_ow diff --git a/tests/bfs/color_st0_tw_ow.out b/tests/bfs/color_st0_tw_ow.out index a183932..4dcb2f2 100644 --- a/tests/bfs/color_st0_tw_ow.out +++ b/tests/bfs/color_st0_tw_ow.out @@ -1,8 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/sticky rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +24,4 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/sticky diff --git a/tests/bfs/color_st0_tw_ow0.out b/tests/bfs/color_st0_tw_ow0.out index cdc5cdd..954ce9c 100644 --- a/tests/bfs/color_st0_tw_ow0.out +++ b/tests/bfs/color_st0_tw_ow0.out @@ -1,9 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/ow -rainbow/sticky rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +23,5 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow +rainbow/sticky diff --git a/tests/bfs/color_st_tw0_ow.out b/tests/bfs/color_st_tw0_ow.out index f45c75c..a6e9a16 100644 --- a/tests/bfs/color_st_tw0_ow.out +++ b/tests/bfs/color_st_tw0_ow.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_st_tw0_ow0.out b/tests/bfs/color_st_tw0_ow0.out index d95d12a..756dafb 100644 --- a/tests/bfs/color_st_tw0_ow0.out +++ b/tests/bfs/color_st_tw0_ow0.out @@ -1,8 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/ow rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +24,4 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow diff --git a/tests/bfs/color_st_tw_ow0.out b/tests/bfs/color_st_tw_ow0.out index f1059dd..6e4a260 100644 --- a/tests/bfs/color_st_tw_ow0.out +++ b/tests/bfs/color_st_tw_ow0.out @@ -1,8 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' -rainbow/ow rainbow/socket rainbow/broken rainbow/chardev_link @@ -25,3 +24,4 @@ rainbow/upper.GZ rainbow/upper.TAR rainbow/upper.TAR.GZ +rainbow/ow diff --git a/tests/bfs/color_su0_sg.out b/tests/bfs/color_su0_sg.out index 31d5108..d13b6b6 100644 --- a/tests/bfs/color_su0_sg.out +++ b/tests/bfs/color_su0_sg.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_su0_sg0.out b/tests/bfs/color_su0_sg0.out index 4804030..77fba58 100644 --- a/tests/bfs/color_su0_sg0.out +++ b/tests/bfs/color_su0_sg0.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/color_su_sg0.out b/tests/bfs/color_su_sg0.out index 3dbc7fe..8fab046 100644 --- a/tests/bfs/color_su_sg0.out +++ b/tests/bfs/color_su_sg0.out @@ -1,7 +1,7 @@ +$'rainbow/\e[1m' $'rainbow/\e[1m/'$'\e[0m' rainbow rainbow/exec.sh -rainbow/$'\e[1m' rainbow/socket rainbow/broken rainbow/chardev_link diff --git a/tests/bfs/printf_color.out b/tests/bfs/printf_color.out index 0468f7d..6641e9a 100644 --- a/tests/bfs/printf_color.out +++ b/tests/bfs/printf_color.out @@ -1,5 +1,7 @@ -. . rainbow ./rainbow rainbow +. $'./rainbow/\e[1m' $'\e[0m' $'./rainbow/\e[1m/'$'\e[0m' $'rainbow/\e[1m/'$'\e[0m' +. . rainbow ./rainbow rainbow . ./rainbow exec.sh ./rainbow/exec.sh rainbow/exec.sh +. ./rainbow $'\e[1m' $'./rainbow/\e[1m' $'rainbow/\e[1m' . ./rainbow socket ./rainbow/socket rainbow/socket . ./rainbow broken ./rainbow/broken rainbow/broken nowhere . ./rainbow chardev_link ./rainbow/chardev_link rainbow/chardev_link /dev/null -- cgit v1.2.3 From b4c3201ccceb9c73dd7751d7f9937b4afe78966f Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Jul 2023 16:00:02 -0400 Subject: color: Only highlight the trailing slash on ENOTDIR --- src/color.c | 6 ++++-- tests/bfs/color_ls.out | 4 ++-- tests/bfs/color_ls.sh | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) (limited to 'tests/bfs') diff --git a/src/color.c b/src/color.c index 7d98978..f7a5d86 100644 --- a/src/color.c +++ b/src/color.c @@ -982,8 +982,10 @@ static ssize_t first_broken_offset(const char *path, const struct BFTW *ftwbuf, while (ret && at_path[len - 1] == '/') { --len, --ret; } - while (ret && at_path[len - 1] != '/') { - --len, --ret; + if (errno != ENOTDIR) { + while (ret && at_path[len - 1] != '/') { + --len, --ret; + } } dstresize(&at_path, len); diff --git a/tests/bfs/color_ls.out b/tests/bfs/color_ls.out index b5216c4..f69eb9c 100644 --- a/tests/bfs/color_ls.out +++ b/tests/bfs/color_ls.out @@ -2,11 +2,11 @@ scratch/foo/bar /__bfs__/nowhere /__bfs__/nowhere -foo/bar/baz/qux -foo/bar/baz/qux foo/bar/nowhere foo/bar/nowhere foo/bar/nowhere/nothing foo/bar/nowhere/nothing foo/bar/baz foo/bar/baz +foo/bar/baz//qux +foo/bar/baz//qux diff --git a/tests/bfs/color_ls.sh b/tests/bfs/color_ls.sh index 37d088f..f2d3c72 100644 --- a/tests/bfs/color_ls.sh +++ b/tests/bfs/color_ls.sh @@ -3,13 +3,13 @@ clean_scratch ln -s foo/bar/baz scratch/link ln -s foo/bar/nowhere scratch/broken ln -s foo/bar/nowhere/nothing scratch/nested -ln -s foo/bar/baz/qux scratch/notdir +ln -s foo/bar/baz//qux scratch/notdir ln -s scratch/foo/bar scratch/relative mkdir scratch/__bfs__ ln -s /__bfs__/nowhere scratch/absolute -LS_COLORS="or=01;31:" invoke_bfs scratch/{,link,broken,nested,notdir,relative,absolute} -color -type l -ls \ +export LS_COLORS="or=01;31:" +invoke_bfs scratch/{,link,broken,nested,notdir,relative,absolute} -color -type l -ls \ | sed 's/.* -> //' \ | sort >"$OUT" - diff_output -- cgit v1.2.3 From 5a16a1e2b33733f0e6dff4ddfaed2662c3ba7ccb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 5 Oct 2023 16:25:56 -0400 Subject: tests/bfs: Add tests for color autodetection --- tests/bfs/color_auto.out | 27 +++++++++++++++++++++++++++ tests/bfs/color_auto.sh | 6 ++++++ tests/bfs/nocolor_env.out | 27 +++++++++++++++++++++++++++ tests/bfs/nocolor_env.sh | 5 +++++ 4 files changed, 65 insertions(+) create mode 100644 tests/bfs/color_auto.out create mode 100644 tests/bfs/color_auto.sh create mode 100644 tests/bfs/nocolor_env.out create mode 100644 tests/bfs/nocolor_env.sh (limited to 'tests/bfs') diff --git a/tests/bfs/color_auto.out b/tests/bfs/color_auto.out new file mode 100644 index 0000000..a439814 --- /dev/null +++ b/tests/bfs/color_auto.out @@ -0,0 +1,27 @@ +$'rainbow/\e[1m' +$'rainbow/\e[1m/'$'\e[0m' +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/mh1 +rainbow/mh2 +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/color_auto.sh b/tests/bfs/color_auto.sh new file mode 100644 index 0000000..aa2eb02 --- /dev/null +++ b/tests/bfs/color_auto.sh @@ -0,0 +1,6 @@ +command -v unbuffer &>/dev/null || skip + +unset NO_COLOR +unbuffer "${BFS[@]}" rainbow >"$OUT" +sort_output +diff_output diff --git a/tests/bfs/nocolor_env.out b/tests/bfs/nocolor_env.out new file mode 100644 index 0000000..d51d24d --- /dev/null +++ b/tests/bfs/nocolor_env.out @@ -0,0 +1,27 @@ +rainbow +rainbow/ +rainbow// +rainbow/broken +rainbow/chardev_link +rainbow/exec.sh +rainbow/file.dat +rainbow/file.txt +rainbow/link.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/mh1 +rainbow/mh2 +rainbow/ow +rainbow/pipe +rainbow/sgid +rainbow/socket +rainbow/sticky +rainbow/sticky_ow +rainbow/sugid +rainbow/suid +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/nocolor_env.sh b/tests/bfs/nocolor_env.sh new file mode 100644 index 0000000..399bdb0 --- /dev/null +++ b/tests/bfs/nocolor_env.sh @@ -0,0 +1,5 @@ +command -v unbuffer &>/dev/null || skip + +NO_COLOR=1 unbuffer "${BFS[@]}" rainbow >"$OUT" +sort_output +diff_output -- cgit v1.2.3 From 6e12ff030a380d0f13c505b5285ceb589761b68d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 5 Oct 2023 17:18:40 -0400 Subject: tests/bfs: Add a test that runs -status --- tests/bfs/status.sh | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/bfs/status.sh (limited to 'tests/bfs') diff --git a/tests/bfs/status.sh b/tests/bfs/status.sh new file mode 100644 index 0000000..30bdfa7 --- /dev/null +++ b/tests/bfs/status.sh @@ -0,0 +1,3 @@ +command -v unbuffer &>/dev/null || skip + +unbuffer "${BFS[@]}" basic -status >"$OUT" -- cgit v1.2.3 From 5e7b5eeb59f9f46ce916aaf968dd42570570580a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 6 Oct 2023 11:42:06 -0400 Subject: tests: New bfs_pty wrapper for unbuffer --- tests/bfs/color_auto.sh | 4 +--- tests/bfs/nocolor_env.sh | 2 +- tests/bfs/status.sh | 4 +--- tests/tests.sh | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/color_auto.sh b/tests/bfs/color_auto.sh index aa2eb02..7e875cc 100644 --- a/tests/bfs/color_auto.sh +++ b/tests/bfs/color_auto.sh @@ -1,6 +1,4 @@ -command -v unbuffer &>/dev/null || skip - unset NO_COLOR -unbuffer "${BFS[@]}" rainbow >"$OUT" +bfs_pty rainbow >"$OUT" sort_output diff_output diff --git a/tests/bfs/nocolor_env.sh b/tests/bfs/nocolor_env.sh index 399bdb0..0a17fb2 100644 --- a/tests/bfs/nocolor_env.sh +++ b/tests/bfs/nocolor_env.sh @@ -1,5 +1,5 @@ command -v unbuffer &>/dev/null || skip -NO_COLOR=1 unbuffer "${BFS[@]}" rainbow >"$OUT" +NO_COLOR=1 bfs_pty rainbow >"$OUT" sort_output diff_output diff --git a/tests/bfs/status.sh b/tests/bfs/status.sh index 30bdfa7..d3859e2 100644 --- a/tests/bfs/status.sh +++ b/tests/bfs/status.sh @@ -1,3 +1 @@ -command -v unbuffer &>/dev/null || skip - -unbuffer "${BFS[@]}" basic -status >"$OUT" +bfs_pty basic -status >"$OUT" diff --git a/tests/tests.sh b/tests/tests.sh index 61f17b4..5297446 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -488,6 +488,20 @@ function invoke_bfs() { fi } +function bfs_pty() { + command -v unbuffer &>/dev/null || skip + + bfs_verbose "$@" + unbuffer bash -c 'stty cols 80 rows 24 && "$@"' bash "${BFS[@]}" "$@" + local status="$?" + + if ((status > 125)); then + exit "$status" + else + return "$status" + fi +} + function check_exit() { local expected="$1" local actual="0" -- cgit v1.2.3 From bcfe9c4e846bc97f97967c7df95e6b0a08a9a0ad Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 6 Oct 2023 12:00:02 -0400 Subject: tests/bfs/status: Try to test SIGWINCH --- tests/bfs/status.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/bfs') diff --git a/tests/bfs/status.sh b/tests/bfs/status.sh index d3859e2..83e12d3 100644 --- a/tests/bfs/status.sh +++ b/tests/bfs/status.sh @@ -1 +1 @@ -bfs_pty basic -status >"$OUT" +bfs_pty basic -status -print -depth 0 -exec stty cols 123 rows 14 \; >"$OUT" -- cgit v1.2.3 From ddb476bcd224f8c262d24981af48da2fc649f43d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 14 Oct 2023 08:55:57 -0400 Subject: tests: Move closed_std* tests out of the POSIX group POSIX actually says > If the utility would be executed with file descriptor 0, 1, or 2 > closed, implementations may execute the utility with the file > descriptor open to an unspecified file. So we're not guaranteed to be able to detect the situation in the first place. Add a best-effort check for these platforms and skip the test. Link: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01 --- tests/bfs/closed_stderr.sh | 4 ++++ tests/bfs/closed_stdin.out | 19 +++++++++++++++++++ tests/bfs/closed_stdin.sh | 1 + tests/bfs/closed_stdout.sh | 4 ++++ tests/posix/closed_stderr.sh | 1 - tests/posix/closed_stdin.out | 19 ------------------- tests/posix/closed_stdin.sh | 1 - tests/posix/closed_stdout.sh | 1 - 8 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 tests/bfs/closed_stderr.sh create mode 100644 tests/bfs/closed_stdin.out create mode 100644 tests/bfs/closed_stdin.sh create mode 100644 tests/bfs/closed_stdout.sh delete mode 100644 tests/posix/closed_stderr.sh delete mode 100644 tests/posix/closed_stdin.out delete mode 100644 tests/posix/closed_stdin.sh delete mode 100644 tests/posix/closed_stdout.sh (limited to 'tests/bfs') diff --git a/tests/bfs/closed_stderr.sh b/tests/bfs/closed_stderr.sh new file mode 100644 index 0000000..26abd85 --- /dev/null +++ b/tests/bfs/closed_stderr.sh @@ -0,0 +1,4 @@ +# Check if the platform automatically re-opens stderr before we can +(bash -c 'echo >&2' 2>&-) && skip + +! invoke_bfs basic >&- 2>&- diff --git a/tests/bfs/closed_stdin.out b/tests/bfs/closed_stdin.out new file mode 100644 index 0000000..a7ccfe4 --- /dev/null +++ b/tests/bfs/closed_stdin.out @@ -0,0 +1,19 @@ +basic +basic/a +basic/b +basic/c +basic/c/d +basic/e +basic/e/f +basic/g +basic/g/h +basic/i +basic/j +basic/j/foo +basic/k +basic/k/foo +basic/k/foo/bar +basic/l +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar/baz diff --git a/tests/bfs/closed_stdin.sh b/tests/bfs/closed_stdin.sh new file mode 100644 index 0000000..6932be8 --- /dev/null +++ b/tests/bfs/closed_stdin.sh @@ -0,0 +1 @@ +bfs_diff basic <&- diff --git a/tests/bfs/closed_stdout.sh b/tests/bfs/closed_stdout.sh new file mode 100644 index 0000000..5b6f7c3 --- /dev/null +++ b/tests/bfs/closed_stdout.sh @@ -0,0 +1,4 @@ +# Check if the platform automatically re-opens stdout before we can +(bash -c echo >&-) && skip + +! invoke_bfs basic >&- diff --git a/tests/posix/closed_stderr.sh b/tests/posix/closed_stderr.sh deleted file mode 100644 index 570d5bb..0000000 --- a/tests/posix/closed_stderr.sh +++ /dev/null @@ -1 +0,0 @@ -! invoke_bfs basic >&- 2>&- diff --git a/tests/posix/closed_stdin.out b/tests/posix/closed_stdin.out deleted file mode 100644 index a7ccfe4..0000000 --- a/tests/posix/closed_stdin.out +++ /dev/null @@ -1,19 +0,0 @@ -basic -basic/a -basic/b -basic/c -basic/c/d -basic/e -basic/e/f -basic/g -basic/g/h -basic/i -basic/j -basic/j/foo -basic/k -basic/k/foo -basic/k/foo/bar -basic/l -basic/l/foo -basic/l/foo/bar -basic/l/foo/bar/baz diff --git a/tests/posix/closed_stdin.sh b/tests/posix/closed_stdin.sh deleted file mode 100644 index 6932be8..0000000 --- a/tests/posix/closed_stdin.sh +++ /dev/null @@ -1 +0,0 @@ -bfs_diff basic <&- diff --git a/tests/posix/closed_stdout.sh b/tests/posix/closed_stdout.sh deleted file mode 100644 index 25c060d..0000000 --- a/tests/posix/closed_stdout.sh +++ /dev/null @@ -1 +0,0 @@ -! invoke_bfs basic >&- -- cgit v1.2.3 From 785a3f2d777627f39bed44f4ae7a0180d5184109 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 19 Oct 2023 16:37:47 -0400 Subject: tests: Refactor implementation into separate files --- tests/bfs/deep_strict.sh | 2 - tests/color.sh | 43 +++ tests/common/execdir_ulimit.sh | 1 - tests/getopts.sh | 158 ++++++++ tests/gnu/printf_u_g_ulimit.sh | 1 - tests/ls-color.sh | 6 +- tests/posix/deep.sh | 2 - tests/posix/nogroup_ulimit.sh | 1 - tests/posix/nouser_ulimit.sh | 1 - tests/run.sh | 316 ++++++++++++++++ tests/stddirs.sh | 185 +++++++++ tests/tests.sh | 824 +---------------------------------------- tests/util.sh | 189 ++++++++++ 13 files changed, 906 insertions(+), 823 deletions(-) create mode 100644 tests/color.sh create mode 100644 tests/getopts.sh create mode 100644 tests/run.sh create mode 100644 tests/stddirs.sh create mode 100644 tests/util.sh (limited to 'tests/bfs') diff --git a/tests/bfs/deep_strict.sh b/tests/bfs/deep_strict.sh index e057310..50c8f05 100644 --- a/tests/bfs/deep_strict.sh +++ b/tests/bfs/deep_strict.sh @@ -1,5 +1,3 @@ -closefrom 4 - # Not even enough fds to keep the root open ulimit -n 7 bfs_diff deep -type f -exec bash -c 'echo "${1:0:6}/.../${1##*/} (${#1})"' bash {} \; diff --git a/tests/color.sh b/tests/color.sh new file mode 100644 index 0000000..0d6ef68 --- /dev/null +++ b/tests/color.sh @@ -0,0 +1,43 @@ +#!/hint/bash + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +## Colored output + +# Common escape sequences +BLD=$'\e[01m' +RED=$'\e[01;31m' +GRN=$'\e[01;32m' +YLW=$'\e[01;33m' +BLU=$'\e[01;34m' +MAG=$'\e[01;35m' +CYN=$'\e[01;36m' +RST=$'\e[0m' + +# Check if we should color output to the given fd +color_fd() { + [ -z "${NO_COLOR:-}" ] && [ -t "$1" ] +} + +# Cache the color status for std{out,err} +color_fd 1 && COLOR_STDOUT=1 || COLOR_STDOUT=0 +color_fd 2 && COLOR_STDERR=1 || COLOR_STDERR=0 + +# Save these in case the tests unset PATH +CAT=$(command -v cat) +SED=$(command -v sed) + +# Filter out escape sequences if necessary +color() { + if color_fd 1; then + "$CAT" + else + "$SED" $'s/\e\\[[^m]*m//g' + fi +} + +# printf with auto-detected color support +cprintf() { + printf "$@" | color +} diff --git a/tests/common/execdir_ulimit.sh b/tests/common/execdir_ulimit.sh index 8bd9edd..f7fc467 100644 --- a/tests/common/execdir_ulimit.sh +++ b/tests/common/execdir_ulimit.sh @@ -2,6 +2,5 @@ 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 -closefrom 4 ulimit -n 13 bfs_diff scratch -execdir echo {} \; diff --git a/tests/getopts.sh b/tests/getopts.sh new file mode 100644 index 0000000..6616a4a --- /dev/null +++ b/tests/getopts.sh @@ -0,0 +1,158 @@ +#!/hint/bash + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +## Argument parsing + +# Print usage information +usage() { + local pad=$(printf "%*s" ${#0} "") + color <&2 + usage >&2 + exit 1 + ;; + *) + PATTERNS+=("$arg") + ;; + esac + done + + # Try to resolve the path to $BFS before we cd, while also supporting + # --bfs="./bin/bfs -S ids" + read -a BFS <<<"${BFS:-$BIN/bfs}" + BFS[0]=$(_realpath "$(command -v "${BFS[0]}")") + + if ((${#PATTERNS[@]} == 0)); then + PATTERNS=("*") + fi + + TEST_CASES=() + ALL_TESTS=($(cd "$TESTS" && quote {posix,common,bsd,gnu,bfs}/*.sh)) + for TEST in "${ALL_TESTS[@]}"; do + TEST="${TEST%.sh}" + for PATTERN in "${PATTERNS[@]}"; do + if [[ $TEST == $PATTERN ]]; then + TEST_CASES+=("$TEST") + break + fi + done + done + + if ((${#TEST_CASES[@]} == 0)); then + cprintf "${RED}error:${RST} No tests matched" >&2 + cprintf " ${BLD}%s${RST}" "${PATTERNS[@]}" >&2 + cprintf ".\n\n" >&2 + usage >&2 + exit 1 + fi +} diff --git a/tests/gnu/printf_u_g_ulimit.sh b/tests/gnu/printf_u_g_ulimit.sh index a84ee29..390ad48 100644 --- a/tests/gnu/printf_u_g_ulimit.sh +++ b/tests/gnu/printf_u_g_ulimit.sh @@ -1,3 +1,2 @@ -closefrom 4 ulimit -n 16 [ "$(invoke_bfs deep -printf '%u %g\n' | uniq)" = "$(id -un) $(id -gn)" ] diff --git a/tests/ls-color.sh b/tests/ls-color.sh index 9fdd59c..b9a0402 100755 --- a/tests/ls-color.sh +++ b/tests/ls-color.sh @@ -7,7 +7,7 @@ set -e -function parse_ls_colors() { +parse_ls_colors() { for key; do local -n var="$key" if [[ "$LS_COLORS" =~ (^|:)$key=(([^:]|\\:)*) ]]; then @@ -18,7 +18,7 @@ function parse_ls_colors() { done } -function re_escape() { +re_escape() { # https://stackoverflow.com/a/29613573/502399 sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$1" } @@ -34,7 +34,7 @@ parse_ls_colors rs lc rc ec no strip="(($(re_escape "$lc$no$rc"))?($(re_escape "$ec")|$(re_escape "$lc$rc")))+" -function ls_color() { +ls_color() { # Strip the leading reset sequence from the ls output ls -1d --color "$@" | sed -E "s/^$strip([a-z].*)$strip/\4/; s/^$strip//" } diff --git a/tests/posix/deep.sh b/tests/posix/deep.sh index 3d1cd60..431705e 100644 --- a/tests/posix/deep.sh +++ b/tests/posix/deep.sh @@ -1,4 +1,2 @@ -closefrom 4 - ulimit -n 16 bfs_diff deep -type f -exec bash -c 'echo "${1:0:6}/.../${1##*/} (${#1})"' bash {} \; diff --git a/tests/posix/nogroup_ulimit.sh b/tests/posix/nogroup_ulimit.sh index 8f758c4..2186321 100644 --- a/tests/posix/nogroup_ulimit.sh +++ b/tests/posix/nogroup_ulimit.sh @@ -1,4 +1,3 @@ -closefrom 4 ulimit -n 16 # -mindepth 18, but POSIX diff --git a/tests/posix/nouser_ulimit.sh b/tests/posix/nouser_ulimit.sh index 2777589..be0a65f 100644 --- a/tests/posix/nouser_ulimit.sh +++ b/tests/posix/nouser_ulimit.sh @@ -1,4 +1,3 @@ -closefrom 4 ulimit -n 16 # -mindepth 18, but POSIX diff --git a/tests/run.sh b/tests/run.sh new file mode 100644 index 0000000..70c9cc2 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,316 @@ +#!/hint/bash + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +## Running test cases + +# Beginning/end of line escape sequences +BOL=$'\n' +EOL=$'\n' + +# Update $EOL for the terminal size +update_eol() { + # Bash gets $COLUMNS from stderr, so if it's redirected use tput instead + local cols="${COLUMNS-}" + if [ -z "$cols" ]; then + cols=$(tput cols) + fi + + # Put the cursor at the last column, then write a space so the next + # character will wrap + EOL=$'\e['"${cols}G " +} + +# ERR trap for tests +debug_err() { + local ret=$? line func file + callers | while read -r line func file; do + if [ "$func" = source ]; then + local cmd="$(awk "NR == $line" "$file" 2>/dev/null)" || : + debug "$file" $line "${RED}error $ret${RST}" "$cmd" >&4 + break + fi + done +} + +# Run a single test +run_test() ( + set -eE + trap debug_err ERR + cd "$TMP" + source "$@" +) + +# Run all the tests +run_tests() { + if ((VERBOSE_TESTS)); then + BOL='' + elif ((COLOR_STDOUT)); then + # Carriage return + clear line + BOL=$'\r\e[K' + + # Workaround for bash 4: checkwinsize is off by default. We can turn it + # on, but we also have to explicitly trigger a foreground job to finish + # so that it will update the window size before we use $COLUMNS + shopt -s checkwinsize + (:) + + update_eol + trap update_eol WINCH + fi + + passed=0 + failed=0 + skipped=0 + + if ((COLOR_STDOUT || VERBOSE_TESTS)); then + TEST_FMT="${BOL}${YLW}%s${RST}${EOL}" + else + TEST_FMT="." + fi + + # Turn off set -e (but turn it back on in run_test) + set +e + + for TEST in "${TEST_CASES[@]}"; do + printf "$TEST_FMT" "$TEST" + + OUT="$TMP/$TEST.out" + mkdir -p "${OUT%/*}" + + if ((VERBOSE_ERRORS)); then + run_test "$TESTS/$TEST.sh" + else + run_test "$TESTS/$TEST.sh" 2>"$TMP/$TEST.err" + fi + status=$? + + if ((status == 0)); then + ((++passed)) + elif ((status == EX_SKIP)); then + ((++skipped)) + else + ((++failed)) + ((VERBOSE_ERRORS)) || cat "$TMP/$TEST.err" >&2 + cprintf "${BOL}${RED}%s failed!${RST}\n" "$TEST" + ((STOP)) && break + fi + done + + printf "${BOL}" + + if ((passed > 0)); then + cprintf "${GRN}tests passed: %d${RST}\n" "$passed" + fi + if ((skipped > 0)); then + cprintf "${CYN}tests skipped: %s${RST}\n" "$skipped" + fi + if ((failed > 0)); then + cprintf "${RED}tests failed: %s${RST}\n" "$failed" + exit 1 + fi +} + +## Utilities for the tests themselves + +# Return value when a test is skipped +EX_SKIP=77 + +# Skip the current test +skip() { + if ((VERBOSE_SKIPPED)); then + caller | { + read -r line file + printf "${BOL}" + debug "$file" $line "${CYN}$TEST skipped!${RST}" "$(awk "NR == $line" "$file")" >&3 + } + elif ((VERBOSE_TESTS)); then + cprintf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST" + fi + + exit $EX_SKIP +} + +# Run a command and check its exit status +check_exit() { + local expected="$1" + local actual="0" + shift + "$@" || actual="$?" + ((actual == expected)) +} + +# Run a command with sudo +bfs_sudo() { + if ((${#SUDO[@]})); then + "${SUDO[@]}" "$@" + else + return 1 + fi +} + +# Get the inode number of a file +inum() { + ls -id "$@" | awk '{ print $1 }' +} + +# Set an ACL on a file +set_acl() { + case "$UNAME" in + Darwin) + chmod +a "$(id -un) allow read,write" "$1" + ;; + FreeBSD) + if (($(getconf ACL_NFS4 "$1") > 0)); then + setfacl -m "u:$(id -un):rw::allow" "$1" + else + setfacl -m "u:$(id -un):rw" "$1" + fi + ;; + *) + setfacl -m "u:$(id -un):rw" "$1" + ;; + esac +} + +# Print a bfs invocation for --verbose=commands +bfs_verbose() ( + if ((!VERBOSE_COMMANDS)); then + return + fi + + # Free up an fd for the pipe + exec 4>&- + + { + printf "${GRN}%q${RST} " "${BFS[@]}" + + local expr_started= + for arg; do + if [[ $arg == -[A-Z]* ]]; then + printf "${CYN}%q${RST} " "$arg" + elif [[ $arg == [\(!] || $arg == -[ao] || $arg == -and || $arg == -or || $arg == -not ]]; then + expr_started=yes + printf "${RED}%q${RST} " "$arg" + elif [[ $expr_started && $arg == [\),] ]]; then + printf "${RED}%q${RST} " "$arg" + elif [[ $arg == -?* ]]; then + expr_started=yes + printf "${BLU}%q${RST} " "$arg" + elif [ "$expr_started" ]; then + printf "${BLD}%q${RST} " "$arg" + else + printf "${MAG}%q${RST} " "$arg" + fi + done + + printf '\n' + } | color >&3 +) + +# Run the bfs we're testing +invoke_bfs() { + bfs_verbose "$@" + + local ret=0 + # Close the logging fds + "${BFS[@]}" "$@" 3>&- 4>&- || ret=$? + + # Allow bfs to fail, but not crash + if ((ret > 125)); then + exit "$ret" + else + return "$ret" + fi +} + +if command -v unbuffer &>/dev/null; then + UNBUFFER=unbuffer +elif command -v expect_unbuffer &>/dev/null; then + UNBUFFER=expect_unbuffer +fi + +# Run bfs with a pseudo-terminal attached +bfs_pty() { + test -n "${UNBUFFER:-}" || skip + + bfs_verbose "$@" + + local ret=0 + "$UNBUFFER" bash -c 'stty cols 80 rows 24 && "$@"' bash "${BFS[@]}" "$@" || ret=$? + + if ((ret > 125)); then + exit "$ret" + else + return "$ret" + fi +} + +# Create a directory tree with xattrs in scratch +make_xattrs() { + clean_scratch + + "$XTOUCH" scratch/{normal,xattr,xattr_2} + ln -s xattr scratch/link + ln -s normal scratch/xattr_link + + case "$UNAME" in + Darwin) + xattr -w bfs_test true scratch/xattr \ + && xattr -w bfs_test_2 true scratch/xattr_2 \ + && xattr -s -w bfs_test true scratch/xattr_link + ;; + FreeBSD) + setextattr user bfs_test true scratch/xattr \ + && setextattr user bfs_test_2 true scratch/xattr_2 \ + && setextattr -h user bfs_test true scratch/xattr_link + ;; + *) + # Linux tmpfs doesn't support the user.* namespace, so we use the security.* + # namespace, which is writable by root and readable by others + bfs_sudo setfattr -n security.bfs_test scratch/xattr \ + && bfs_sudo setfattr -n security.bfs_test_2 scratch/xattr_2 \ + && bfs_sudo setfattr -h -n security.bfs_test scratch/xattr_link + ;; + esac +} + +## Snapshot testing + +# Return value when a difference is detected +EX_DIFF=20 + +# Detect colored diff support +if ((COLOR_STDERR)) && diff --color=always /dev/null /dev/null 2>/dev/null; then + DIFF="diff --color=always" +else + DIFF="diff" +fi + +# Sort the output file +sort_output() { + sort -o "$OUT" "$OUT" +} + +# Diff against the expected output +diff_output() { + local GOLD="$TESTS/$TEST.out" + + if ((UPDATE)); then + cp "$OUT" "$GOLD" + else + $DIFF -u "$GOLD" "$OUT" >&2 + fi +} + +# Run bfs, and diff it against the expected output +bfs_diff() { + local ret=0 + invoke_bfs "$@" >"$OUT" || ret=$? + + sort_output + diff_output || exit $EX_DIFF + + return $ret +} diff --git a/tests/stddirs.sh b/tests/stddirs.sh new file mode 100644 index 0000000..e7f7246 --- /dev/null +++ b/tests/stddirs.sh @@ -0,0 +1,185 @@ +#!/hint/bash + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +## Standard directory trees for tests + +# Creates a simple file+directory structure for tests +make_basic() { + "$XTOUCH" -p "$1"/{a,b,c/d,e/f,g/h/,i/} + "$XTOUCH" -p "$1"/{j/foo,k/foo/bar,l/foo/bar/baz} + echo baz >"$1/l/foo/bar/baz" +} + +# Creates a file+directory structure with various permissions for tests +make_perms() { + "$XTOUCH" -p -M000 "$1/0" + "$XTOUCH" -p -M444 "$1/r" + "$XTOUCH" -p -M222 "$1/w" + "$XTOUCH" -p -M644 "$1/rw" + "$XTOUCH" -p -M555 "$1/rx" + "$XTOUCH" -p -M311 "$1/wx" + "$XTOUCH" -p -M755 "$1/rwx" +} + +# Creates a file+directory structure with various symbolic and hard links +make_links() { + "$XTOUCH" -p "$1/file" + ln -s file "$1/symlink" + ln "$1/file" "$1/hardlink" + ln -s nowhere "$1/broken" + ln -s symlink/file "$1/notdir" + "$XTOUCH" -p "$1/deeply/nested"/{dir/,file} + ln -s file "$1/deeply/nested/link" + ln -s nowhere "$1/deeply/nested/broken" + ln -s deeply/nested "$1/skip" +} + +# Creates a file+directory structure with symbolic link loops +make_loops() { + "$XTOUCH" -p "$1/file" + ln -s file "$1/symlink" + ln -s nowhere "$1/broken" + ln -s symlink/file "$1/notdir" + ln -s loop "$1/loop" + mkdir -p "$1/deeply/nested/dir" + ln -s ../../deeply "$1/deeply/nested/loop" + ln -s deeply/nested/loop/nested "$1/skip" +} + +# Creates a file+directory structure with varying timestamps +make_times() { + "$XTOUCH" -p -t "1991-12-14 00:00" "$1/a" + "$XTOUCH" -p -t "1991-12-14 00:01" "$1/b" + "$XTOUCH" -p -t "1991-12-14 00:02" "$1/c" + ln -s a "$1/l" + "$XTOUCH" -p -h -t "1991-12-14 00:03" "$1/l" + "$XTOUCH" -p -t "1991-12-14 00:04" "$1" +} + +# Creates a file+directory structure with various weird file/directory names +make_weirdnames() { + "$XTOUCH" -p "$1/-/a" + "$XTOUCH" -p "$1/(/b" + "$XTOUCH" -p "$1/(-/c" + "$XTOUCH" -p "$1/!/d" + "$XTOUCH" -p "$1/!-/e" + "$XTOUCH" -p "$1/,/f" + "$XTOUCH" -p "$1/)/g" + "$XTOUCH" -p "$1/.../h" + "$XTOUCH" -p "$1/\\/i" + "$XTOUCH" -p "$1/ /j" + "$XTOUCH" -p "$1/[/k" +} + +# Creates a very deep directory structure for testing PATH_MAX handling +make_deep() { + mkdir -p "$1" + + # $name will be 255 characters, aka _XOPEN_NAME_MAX + local name="0123456789ABCDEF" + name="${name}${name}${name}${name}" + name="${name}${name}${name}${name}" + name="${name:0:255}" + + for i in {0..9} A B C D E F; do + "$XTOUCH" -p "$1/$i/$name" + + ( + cd "$1/$i" + + # 8 * 512 == 4096 >= PATH_MAX + for _ in {1..8}; do + mv "$name" .. + mkdir -p "$name/$name" + mv "../$name" "$name/$name/" + done + ) + done +} + +# Creates a directory structure with many different types, and therefore colors +make_rainbow() { + "$XTOUCH" -p "$1/file.txt" + "$XTOUCH" -p "$1/file.dat" + "$XTOUCH" -p "$1/lower".{gz,tar,tar.gz} + "$XTOUCH" -p "$1/upper".{GZ,TAR,TAR.GZ} + "$XTOUCH" -p "$1/lu.tar.GZ" "$1/ul.TAR.gz" + ln -s file.txt "$1/link.txt" + "$XTOUCH" -p "$1/mh1" + ln "$1/mh1" "$1/mh2" + mkfifo "$1/pipe" + # TODO: block + ln -s /dev/null "$1/chardev_link" + ln -s nowhere "$1/broken" + "$MKSOCK" "$1/socket" + "$XTOUCH" -p "$1"/s{u,g,ug}id + chmod u+s "$1"/su{,g}id + chmod g+s "$1"/s{u,}gid + mkdir "$1/ow" "$1"/sticky{,_ow} + chmod o+w "$1"/*ow + chmod +t "$1"/sticky* + "$XTOUCH" -p "$1"/exec.sh + chmod +x "$1"/exec.sh + "$XTOUCH" -p "$1/"$'\e[1m/\e[0m' +} + +# Create all standard directory trees +make_stddirs() { + TMP=$(mktemp -d "${TMPDIR:-/tmp}"/bfs.XXXXXXXXXX) + + if ((CLEAN)); then + defer clean_stddirs + else + printf "Test files saved to ${BLD}%s${RST}\n" "$TMP" + fi + + chown "$(id -u):$(id -g)" "$TMP" + + make_basic "$TMP/basic" + make_perms "$TMP/perms" + make_links "$TMP/links" + make_loops "$TMP/loops" + make_times "$TMP/times" + make_weirdnames "$TMP/weirdnames" + make_deep "$TMP/deep" + make_rainbow "$TMP/rainbow" + mkdir "$TMP/scratch" +} + +# Clean whatever was left in the scratch directory +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 +clean_stddirs() { + # Don't force rm to deal with long paths + for dir in "$TMP"/deep/*/*; do + if [ -d "$dir" ]; then + (cd "$dir" && rm -rf *) + fi + done + + # In case a test left anything weird in scratch/ + clean_scratch + + rm -rf "$TMP" +} diff --git a/tests/tests.sh b/tests/tests.sh index dcef28e..3890243 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -6,815 +6,15 @@ set -euP umask 022 -export LC_ALL=C -export TZ=UTC0 - -SAN_OPTIONS="halt_on_error=1:log_to_syslog=0" -export ASAN_OPTIONS="$SAN_OPTIONS" -export LSAN_OPTIONS="$SAN_OPTIONS" -export MSAN_OPTIONS="$SAN_OPTIONS" -export TSAN_OPTIONS="$SAN_OPTIONS" -export UBSAN_OPTIONS="$SAN_OPTIONS" - -export LS_COLORS="" -unset BFS_COLORS - -BLD=$'\e[01m' -RED=$'\e[01;31m' -GRN=$'\e[01;32m' -YLW=$'\e[01;33m' -BLU=$'\e[01;34m' -MAG=$'\e[01;35m' -CYN=$'\e[01;36m' -RST=$'\e[0m' - -function color_fd() { - [ -z "${NO_COLOR:-}" ] && [ -t "$1" ] -} - -color_fd 1 && COLOR_STDOUT=1 || COLOR_STDOUT=0 -color_fd 2 && COLOR_STDERR=1 || COLOR_STDERR=0 - -# Filter out escape sequences if necessary -function color() { - if color_fd 1; then - cat - else - sed $'s/\e\\[[^m]*m//g' - fi -} - -# printf with auto-detected color support -function cprintf() { - printf "$@" | color -} - -UNAME=$(uname) - -if [ "$UNAME" = Darwin ]; then - # ASan on macOS likes to report - # - # malloc: nano zone abandoned due to inability to preallocate reserved vm space. - # - # to syslog, which as a side effect opens a socket which might take the - # place of one of the standard streams if the process is launched with it - # closed. This environment variable avoids the message. - export MallocNanoZone=0 -fi - -if command -v capsh &>/dev/null; then - if capsh --has-p=cap_dac_override &>/dev/null || capsh --has-p=cap_dac_read_search &>/dev/null; then - if [ -n "${BFS_TRIED_DROP:-}" ]; then - color >&2 <&2 <&2 <&2 - usage >&2 - exit 1 - ;; - *) - PATTERNS+=("$arg") - ;; - esac -done - -function _realpath() { - ( - cd "$(dirname -- "$1")" - echo "$PWD/$(basename -- "$1")" - ) -} - -TESTS=$(_realpath "$(dirname -- "${BASH_SOURCE[0]}")") - -if [ "${BUILDDIR-}" ]; then - BIN=$(_realpath "$BUILDDIR/bin") -else - BIN=$(_realpath "$TESTS/../bin") -fi -MKSOCK="$BIN/tests/mksock" -XTOUCH="$BIN/tests/xtouch" - -# Try to resolve the path to $BFS before we cd, while also supporting -# --bfs="./bin/bfs -S ids" -read -a BFS <<<"${BFS:-$BIN/bfs}" -BFS[0]=$(_realpath "$(command -v "${BFS[0]}")") - -# The temporary directory that will hold our test data -TMP=$(mktemp -d "${TMPDIR:-/tmp}"/bfs.XXXXXXXXXX) -chown "$(id -u):$(id -g)" "$TMP" - -cd "$TESTS" - -if ((${#PATTERNS[@]} == 0)); then - PATTERNS=("*") -fi - -TEST_CASES=() -for TEST in {posix,common,bsd,gnu,bfs}/*.sh; do - TEST="${TEST%.sh}" - for PATTERN in "${PATTERNS[@]}"; do - if [[ $TEST == $PATTERN ]]; then - TEST_CASES+=("$TEST") - break - fi - done -done - -if ((${#TEST_CASES[@]} == 0)); then - cprintf "${RED}error:${RST} No tests matched" >&2 - cprintf " ${BLD}%s${RST}" "${PATTERNS[@]}" >&2 - cprintf ".\n\n" >&2 - usage >&2 - exit 1 -fi - -function quote() { - printf '%q' "$1" - shift - if (($# > 0)); then - printf ' %q' "$@" - fi -} - -# Run a command when this (sub)shell exits -function defer() { - trap -- KILL - if ! trap -p EXIT | grep -q pop_defers; then - DEFER_CMDS=() - DEFER_LINES=() - DEFER_FILES=() - trap pop_defers EXIT - fi - - DEFER_CMDS+=("$(quote "$@")") - - local line file - read -r line file < <(caller) - DEFER_LINES+=("$line") - DEFER_FILES+=("$file") -} - -function report_err() { - local file="${1/#*\/tests\//tests\/}" - set -- "$file" "${@:2}" - - if ((COLOR_STDERR)); then - printf "${BLD}%s:%d:${RST} ${RED}error %d:${RST}\n %s\n" "$@" >&2 - else - printf "%s:%d: error %d:\n %s\n" "$@" >&2 - fi -} - -function pop_defer() { - local cmd="${DEFER_CMDS[-1]}" - local file="${DEFER_FILES[-1]}" - local line="${DEFER_LINES[-1]}" - unset "DEFER_CMDS[-1]" - unset "DEFER_FILES[-1]" - unset "DEFER_LINES[-1]" - - local ret=0 - eval "$cmd" || ret=$? - - if ((ret != 0)); then - report_err "$file" $line $ret "defer $cmd" - fi - - return $ret -} - -function pop_defers() { - local ret=0 - - while ((${#DEFER_CMDS[@]} > 0)); do - pop_defer || ret=$? - done - - return $ret -} - -function bfs_sudo() { - if ((${#SUDO[@]})); then - "${SUDO[@]}" "$@" - else - return 1 - 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 - for dir in "$TMP"/deep/*/*; do - if [ -d "$dir" ]; then - (cd "$dir" && rm -rf *) - fi - done - - # In case a test left anything weird in scratch/ - clean_scratch - - rm -rf "$TMP" -} - -if ((CLEAN)); then - defer cleanup -else - echo "Test files saved to $TMP" -fi - -# Creates a simple file+directory structure for tests -function make_basic() { - "$XTOUCH" -p "$1"/{a,b,c/d,e/f,g/h/,i/} - "$XTOUCH" -p "$1"/{j/foo,k/foo/bar,l/foo/bar/baz} - echo baz >"$1/l/foo/bar/baz" -} -make_basic "$TMP/basic" - -# Creates a file+directory structure with various permissions for tests -function make_perms() { - "$XTOUCH" -p -M000 "$1/0" - "$XTOUCH" -p -M444 "$1/r" - "$XTOUCH" -p -M222 "$1/w" - "$XTOUCH" -p -M644 "$1/rw" - "$XTOUCH" -p -M555 "$1/rx" - "$XTOUCH" -p -M311 "$1/wx" - "$XTOUCH" -p -M755 "$1/rwx" -} -make_perms "$TMP/perms" - -# Creates a file+directory structure with various symbolic and hard links -function make_links() { - "$XTOUCH" -p "$1/file" - ln -s file "$1/symlink" - ln "$1/file" "$1/hardlink" - ln -s nowhere "$1/broken" - ln -s symlink/file "$1/notdir" - "$XTOUCH" -p "$1/deeply/nested"/{dir/,file} - ln -s file "$1/deeply/nested/link" - ln -s nowhere "$1/deeply/nested/broken" - ln -s deeply/nested "$1/skip" -} -make_links "$TMP/links" - -# Creates a file+directory structure with symbolic link loops -function make_loops() { - "$XTOUCH" -p "$1/file" - ln -s file "$1/symlink" - ln -s nowhere "$1/broken" - ln -s symlink/file "$1/notdir" - ln -s loop "$1/loop" - mkdir -p "$1/deeply/nested/dir" - ln -s ../../deeply "$1/deeply/nested/loop" - ln -s deeply/nested/loop/nested "$1/skip" -} -make_loops "$TMP/loops" - -# Creates a file+directory structure with varying timestamps -function make_times() { - "$XTOUCH" -p -t "1991-12-14 00:00" "$1/a" - "$XTOUCH" -p -t "1991-12-14 00:01" "$1/b" - "$XTOUCH" -p -t "1991-12-14 00:02" "$1/c" - ln -s a "$1/l" - "$XTOUCH" -p -h -t "1991-12-14 00:03" "$1/l" - "$XTOUCH" -p -t "1991-12-14 00:04" "$1" -} -make_times "$TMP/times" - -# Creates a file+directory structure with various weird file/directory names -function make_weirdnames() { - "$XTOUCH" -p "$1/-/a" - "$XTOUCH" -p "$1/(/b" - "$XTOUCH" -p "$1/(-/c" - "$XTOUCH" -p "$1/!/d" - "$XTOUCH" -p "$1/!-/e" - "$XTOUCH" -p "$1/,/f" - "$XTOUCH" -p "$1/)/g" - "$XTOUCH" -p "$1/.../h" - "$XTOUCH" -p "$1/\\/i" - "$XTOUCH" -p "$1/ /j" - "$XTOUCH" -p "$1/[/k" -} -make_weirdnames "$TMP/weirdnames" - -# Creates a very deep directory structure for testing PATH_MAX handling -function make_deep() { - mkdir -p "$1" - - # $name will be 255 characters, aka _XOPEN_NAME_MAX - local name="0123456789ABCDEF" - name="${name}${name}${name}${name}" - name="${name}${name}${name}${name}" - name="${name:0:255}" - - for i in {0..9} A B C D E F; do - "$XTOUCH" -p "$1/$i/$name" - - ( - cd "$1/$i" - - # 8 * 512 == 4096 >= PATH_MAX - for _ in {1..8}; do - mv "$name" .. - mkdir -p "$name/$name" - mv "../$name" "$name/$name/" - done - ) - done -} -make_deep "$TMP/deep" - -# Creates a directory structure with many different types, and therefore colors -function make_rainbow() { - "$XTOUCH" -p "$1/file.txt" - "$XTOUCH" -p "$1/file.dat" - "$XTOUCH" -p "$1/lower".{gz,tar,tar.gz} - "$XTOUCH" -p "$1/upper".{GZ,TAR,TAR.GZ} - "$XTOUCH" -p "$1/lu.tar.GZ" "$1/ul.TAR.gz" - ln -s file.txt "$1/link.txt" - "$XTOUCH" -p "$1/mh1" - ln "$1/mh1" "$1/mh2" - mkfifo "$1/pipe" - # TODO: block - ln -s /dev/null "$1/chardev_link" - ln -s nowhere "$1/broken" - "$MKSOCK" "$1/socket" - "$XTOUCH" -p "$1"/s{u,g,ug}id - chmod u+s "$1"/su{,g}id - chmod g+s "$1"/s{u,}gid - mkdir "$1/ow" "$1"/sticky{,_ow} - chmod o+w "$1"/*ow - chmod +t "$1"/sticky* - "$XTOUCH" -p "$1"/exec.sh - chmod +x "$1"/exec.sh - "$XTOUCH" -p "$1/"$'\e[1m/\e[0m' -} -make_rainbow "$TMP/rainbow" - -mkdir "$TMP/scratch" - -# Close stdin so bfs doesn't think we're interactive -exec &1 -fi - -function bfs_verbose() { - if ((!VERBOSE_COMMANDS)); then - return - fi - - { - printf "${GRN}%q${RST} " "${BFS[@]}" - - local expr_started= - for arg; do - if [[ $arg == -[A-Z]* ]]; then - printf "${CYN}%q${RST} " "$arg" - elif [[ $arg == [\(!] || $arg == -[ao] || $arg == -and || $arg == -or || $arg == -not ]]; then - expr_started=yes - printf "${RED}%q${RST} " "$arg" - elif [[ $expr_started && $arg == [\),] ]]; then - printf "${RED}%q${RST} " "$arg" - elif [[ $arg == -?* ]]; then - expr_started=yes - printf "${BLU}%q${RST} " "$arg" - elif [ "$expr_started" ]; then - printf "${BLD}%q${RST} " "$arg" - else - printf "${MAG}%q${RST} " "$arg" - fi - done - - printf '\n' - } | color >&3 -} - -function invoke_bfs() { - bfs_verbose "$@" - - local ret=0 - "${BFS[@]}" "$@" || ret=$? - - # Allow bfs to fail, but not crash - if ((ret > 125)); then - exit "$ret" - else - return "$ret" - fi -} - -if command -v unbuffer &>/dev/null; then - UNBUFFER=unbuffer -elif command -v expect_unbuffer &>/dev/null; then - UNBUFFER=expect_unbuffer -fi - -function bfs_pty() { - test -n "${UNBUFFER:-}" || skip - - bfs_verbose "$@" - - local ret=0 - "$UNBUFFER" bash -c 'stty cols 80 rows 24 && "$@"' bash "${BFS[@]}" "$@" || ret=$? - - if ((ret > 125)); then - exit "$ret" - else - return "$ret" - fi -} - -function check_exit() { - local expected="$1" - local actual="0" - shift - "$@" || actual="$?" - ((actual == expected)) -} - -# Detect colored diff support -if ((COLOR_STDERR)) && diff --color=always /dev/null /dev/null 2>/dev/null; then - DIFF="diff --color=always" -else - DIFF="diff" -fi - -# Return value when a difference is detected -EX_DIFF=20 -# Return value when a test is skipped -EX_SKIP=77 - -function sort_output() { - sort -o "$OUT" "$OUT" -} - -function diff_output() { - local GOLD="$TESTS/$TEST.out" - - if ((UPDATE)); then - cp "$OUT" "$GOLD" - else - $DIFF -u "$GOLD" "$OUT" >&2 - fi -} - -function bfs_diff() ( - bfs_verbose "$@" - - # Close the dup()'d stdout to make sure we have enough fd's for the process - # substitution, even with low ulimit -n - exec 3>&- - - "${BFS[@]}" "$@" | sort >"$OUT" - local status="${PIPESTATUS[0]}" - - diff_output || exit $EX_DIFF - return "$status" -) - -function skip() { - if ((VERBOSE_SKIPPED)); then - caller | { - read -r line file - cprintf "${BOL}${CYN}%s skipped!${RST} (%s)\n" "$TEST" "$(awk "NR == $line" "$file")" - } - elif ((VERBOSE_TESTS)); then - cprintf "${BOL}${CYN}%s skipped!${RST}\n" "$TEST" - fi - - exit $EX_SKIP -} - -function closefrom() { - if [ -d /proc/self/fd ]; then - local fds=/proc/self/fd - else - local fds=/dev/fd - fi - - for fd in "$fds"/*; do - if [ ! -e "$fd" ]; then - continue - fi - - local fd="${fd##*/}" - if ((fd >= $1)); then - eval "exec ${fd}<&-" - fi - done -} - -function inum() { - ls -id "$@" | awk '{ print $1 }' -} - -function set_acl() { - case "$UNAME" in - Darwin) - chmod +a "$(id -un) allow read,write" "$1" - ;; - FreeBSD) - if (($(getconf ACL_NFS4 "$1") > 0)); then - setfacl -m "u:$(id -un):rw::allow" "$1" - else - setfacl -m "u:$(id -un):rw" "$1" - fi - ;; - *) - setfacl -m "u:$(id -un):rw" "$1" - ;; - esac -} - -function make_xattrs() { - clean_scratch - - "$XTOUCH" scratch/{normal,xattr,xattr_2} - ln -s xattr scratch/link - ln -s normal scratch/xattr_link - - case "$UNAME" in - Darwin) - xattr -w bfs_test true scratch/xattr \ - && xattr -w bfs_test_2 true scratch/xattr_2 \ - && xattr -s -w bfs_test true scratch/xattr_link - ;; - FreeBSD) - setextattr user bfs_test true scratch/xattr \ - && setextattr user bfs_test_2 true scratch/xattr_2 \ - && setextattr -h user bfs_test true scratch/xattr_link - ;; - *) - # Linux tmpfs doesn't support the user.* namespace, so we use the security.* - # namespace, which is writable by root and readable by others - bfs_sudo setfattr -n security.bfs_test scratch/xattr \ - && bfs_sudo setfattr -n security.bfs_test_2 scratch/xattr_2 \ - && bfs_sudo setfattr -h -n security.bfs_test scratch/xattr_link - ;; - esac -} - -cd "$TMP" -set +e - -BOL='\n' -EOL='\n' - -function update_eol() { - # Bash gets $COLUMNS from stderr, so if it's redirected use tput instead - local cols="${COLUMNS-}" - if [ -z "$cols" ]; then - cols=$(tput cols) - fi - - # Put the cursor at the last column, then write a space so the next - # character will wrap - EOL="\\033[${cols}G " -} - -if ((VERBOSE_TESTS)); then - BOL='' -elif ((COLOR_STDOUT)); then - BOL='\r\033[K' - - # Workaround for bash 4: checkwinsize is off by default. We can turn it on, - # but we also have to explicitly trigger a foreground job to finish so that - # it will update the window size before we use $COLUMNS - shopt -s checkwinsize - (:) - - update_eol - trap update_eol WINCH -fi - -function callers() { - local frame=0 - while caller $frame; do - ((++frame)) - done -} - -function debug_err() { - local ret=$? line func file - callers | while read -r line func file; do - if [ "$func" = source ]; then - local cmd="$(awk "NR == $line" "$file" 2>/dev/null)" || : - report_err "$file" $line $ret "$cmd" - break - fi - done -} - -function run_test() ( - set -eE - trap debug_err ERR - source "$@" -) - -passed=0 -failed=0 -skipped=0 - -if ((COLOR_STDOUT || VERBOSE_TESTS)); then - TEST_FMT="${BOL}${YLW}%s${RST}${EOL}" -else - TEST_FMT="." -fi - -for TEST in "${TEST_CASES[@]}"; do - printf "$TEST_FMT" "$TEST" - - OUT="$TMP/$TEST.out" - mkdir -p "${OUT%/*}" - - if ((VERBOSE_ERRORS)); then - run_test "$TESTS/$TEST.sh" - else - run_test "$TESTS/$TEST.sh" 2>"$TMP/$TEST.err" - fi - status=$? - - if ((status == 0)); then - ((++passed)) - elif ((status == EX_SKIP)); then - ((++skipped)) - else - ((++failed)) - ((VERBOSE_ERRORS)) || cat "$TMP/$TEST.err" >&2 - cprintf "${BOL}${RED}%s failed!${RST}\n" "$TEST" - ((STOP)) && break - fi -done - -printf "${BOL}" - -if ((passed > 0)); then - cprintf "${GRN}tests passed: %d${RST}\n" "$passed" -fi -if ((skipped > 0)); then - cprintf "${CYN}tests skipped: %s${RST}\n" "$skipped" -fi -if ((failed > 0)); then - cprintf "${RED}tests failed: %s${RST}\n" "$failed" - exit 1 -fi +TESTS="$(dirname -- "${BASH_SOURCE[0]}")" +. "$TESTS/util.sh" +. "$TESTS/color.sh" +. "$TESTS/stddirs.sh" +. "$TESTS/getopts.sh" +. "$TESTS/run.sh" + +stdenv +drop_root "$@" +parse_args "$@" +make_stddirs +run_tests diff --git a/tests/util.sh b/tests/util.sh new file mode 100644 index 0000000..5131522 --- /dev/null +++ b/tests/util.sh @@ -0,0 +1,189 @@ +#!/hint/bash + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +## Utility functions + +# Portable realpath(1) +_realpath() ( + cd "$(dirname -- "$1")" + echo "$PWD/$(basename -- "$1")" +) + +# Globals +TESTS=$(_realpath "$TESTS") +if [ "${BUILDDIR-}" ]; then + BIN=$(_realpath "$BUILDDIR/bin") +else + BIN=$(_realpath "$TESTS/../bin") +fi +MKSOCK="$BIN/tests/mksock" +XTOUCH="$BIN/tests/xtouch" +UNAME=$(uname) + +# Standardize the environment +stdenv() { + export LC_ALL=C + export TZ=UTC0 + + local SAN_OPTIONS="halt_on_error=1:log_to_syslog=0" + export ASAN_OPTIONS="$SAN_OPTIONS" + export LSAN_OPTIONS="$SAN_OPTIONS" + export MSAN_OPTIONS="$SAN_OPTIONS" + export TSAN_OPTIONS="$SAN_OPTIONS" + export UBSAN_OPTIONS="$SAN_OPTIONS" + + export LS_COLORS="" + unset BFS_COLORS + + if [ "$UNAME" = Darwin ]; then + # ASan on macOS likes to report + # + # malloc: nano zone abandoned due to inability to preallocate reserved vm space. + # + # to syslog, which as a side effect opens a socket which might take the + # place of one of the standard streams if the process is launched with + # it closed. This environment variable avoids the message. + export MallocNanoZone=0 + fi + + # Close non-standard inherited fds + if [ -d /proc/self/fd ]; then + local fds=/proc/self/fd + else + local fds=/dev/fd + fi + + for fd in "$fds"/*; do + if [ ! -e "$fd" ]; then + continue + fi + + local fd="${fd##*/}" + if ((fd > 2)); then + eval "exec ${fd}<&-" + fi + done + + # Close stdin so bfs doesn't think we're interactive + # dup() the standard fds for logging even when redirected + exec &1 4>&2 +} + +# Drop root priviliges or bail +drop_root() { + if command -v capsh &>/dev/null; then + if capsh --has-p=cap_dac_override &>/dev/null || capsh --has-p=cap_dac_read_search &>/dev/null; then + if [ -n "${BFS_TRIED_DROP:-}" ]; then + color >&2 <&2 <&2 < 0)); then + printf ' %q' "$@" + fi +} + +# Run a command when this (sub)shell exits +defer() { + # Refresh trap state before trap -p + # See https://unix.stackexchange.com/a/556888/56202 + trap -- KILL + + # Check if the EXIT trap is already set + if ! trap -p EXIT | grep -q pop_defers; then + DEFER_CMDS=() + DEFER_LINES=() + DEFER_FILES=() + trap pop_defers EXIT + fi + + DEFER_CMDS+=("$(quote "$@")") + + local line file + read -r line file < <(caller) + DEFER_LINES+=("$line") + DEFER_FILES+=("$file") +} + +# Pop a single command from the defer stack and run it +pop_defer() { + local cmd="${DEFER_CMDS[-1]}" + local file="${DEFER_FILES[-1]}" + local line="${DEFER_LINES[-1]}" + unset "DEFER_CMDS[-1]" + unset "DEFER_FILES[-1]" + unset "DEFER_LINES[-1]" + + local ret=0 + eval "$cmd" || ret=$? + + if ((ret != 0)); then + debug "$file" $line "${RED}error $ret${RST}" "defer $cmd" >&4 + fi + + return $ret +} + +# Run all deferred commands +pop_defers() { + local ret=0 + + while ((${#DEFER_CMDS[@]} > 0)); do + pop_defer || ret=$? + done + + return $ret +} -- cgit v1.2.3 From a2af2746c4686201e2e0796fbdfa115d08727b86 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 22 Oct 2023 16:41:41 -0400 Subject: tests: Use test-specific scratch directories --- tests/bfs/L_capable.out | 4 +- tests/bfs/L_capable.sh | 13 +++--- tests/bfs/capable.out | 2 +- tests/bfs/capable.sh | 13 +++--- tests/bfs/color_ls.sh | 2 +- tests/bfs/exec_flush_fprint.sh | 3 +- tests/bsd/L_acl.out | 4 +- tests/bsd/L_acl.sh | 12 +++--- tests/bsd/L_xattr.out | 6 +-- tests/bsd/L_xattr.sh | 4 +- tests/bsd/L_xattrname.out | 4 +- tests/bsd/L_xattrname.sh | 6 +-- tests/bsd/acl.out | 2 +- tests/bsd/acl.sh | 12 +++--- tests/bsd/flags.out | 2 +- tests/bsd/flags.sh | 10 ++--- tests/bsd/rm.out | 2 +- tests/bsd/rm.sh | 10 ++--- tests/bsd/type_w.out | 72 ++++++++++++++++----------------- tests/bsd/type_w.sh | 36 ++++++++--------- tests/bsd/xattr.out | 6 +-- tests/bsd/xattr.sh | 4 +- tests/bsd/xattrname.out | 4 +- tests/bsd/xattrname.sh | 6 +-- tests/common/L_ls.sh | 3 +- tests/common/L_mount.out | 10 ++--- tests/common/L_mount.sh | 16 ++++---- tests/common/delete.out | 2 +- tests/common/delete.sh | 11 ++--- tests/common/delete_many.out | 2 +- tests/common/delete_many.sh | 10 ++--- tests/common/execdir_ulimit.out | 2 +- tests/common/execdir_ulimit.sh | 8 ++-- tests/common/inum_bind_mount.out | 4 +- tests/common/inum_bind_mount.sh | 10 ++--- tests/common/inum_mount.out | 2 +- tests/common/inum_mount.sh | 10 ++--- tests/common/ls.sh | 3 +- tests/common/mount.out | 8 ++-- tests/common/mount.sh | 12 +++--- tests/gnu/L_delete.out | 4 +- tests/gnu/L_delete.sh | 11 +++-- tests/gnu/files0_from_file.sh | 6 +-- tests/gnu/fls.sh | 3 +- tests/gnu/fls_nonexistent.sh | 2 +- tests/gnu/fprint0_nonexistent.sh | 2 +- tests/gnu/fprint_duplicate.sh | 10 ++--- tests/gnu/fprint_nonexistent.sh | 2 +- tests/gnu/fprintf_nonexistent.sh | 2 +- tests/gnu/fstype_stacked.out | 2 +- tests/gnu/fstype_stacked.sh | 14 +++---- tests/gnu/fstype_umount.sh | 14 +++---- tests/gnu/ignore_readdir_race.sh | 6 +-- tests/gnu/ignore_readdir_race_notdir.sh | 6 +-- tests/gnu/inum_automount.out | 2 +- tests/gnu/inum_automount.sh | 14 +++---- tests/gnu/printf_Y_error.out | 6 +-- tests/gnu/printf_Y_error.sh | 12 +++--- tests/gnu/regex_invalid_utf8.out | 2 +- tests/gnu/regex_invalid_utf8.sh | 10 ++--- tests/gnu/xtype_bind_mount.out | 4 +- tests/gnu/xtype_bind_mount.sh | 12 +++--- tests/posix/L_xdev.out | 10 ++--- tests/posix/L_xdev.sh | 16 ++++---- tests/posix/depth_error.out | 4 +- tests/posix/depth_error.sh | 10 ++--- tests/posix/overlayfs.out | 10 ++--- tests/posix/overlayfs.sh | 14 +++---- tests/posix/readdir_error.sh | 18 ++++----- tests/posix/type_bind_mount.out | 2 +- tests/posix/type_bind_mount.sh | 10 ++--- tests/posix/unionfs.out | 20 ++++----- tests/posix/unionfs.sh | 13 +++--- tests/posix/xdev.out | 8 ++-- tests/posix/xdev.sh | 12 +++--- tests/run.sh | 28 ++++++------- tests/stddirs.sh | 25 ------------ 77 files changed, 330 insertions(+), 368 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/L_capable.out b/tests/bfs/L_capable.out index e5ba3c7..0810d4a 100644 --- a/tests/bfs/L_capable.out +++ b/tests/bfs/L_capable.out @@ -1,2 +1,2 @@ -scratch/capable -scratch/link +./capable +./link diff --git a/tests/bfs/L_capable.sh b/tests/bfs/L_capable.sh index 232d6ac..97c404f 100644 --- a/tests/bfs/L_capable.sh +++ b/tests/bfs/L_capable.sh @@ -1,11 +1,10 @@ test "$UNAME" = "Linux" || skip +invoke_bfs . -quit -capable || skip -clean_scratch +cd "$TEST" -invoke_bfs scratch -quit -capable || skip +"$XTOUCH" normal capable +bfs_sudo setcap all+ep capable || skip +ln -s capable link -"$XTOUCH" scratch/{normal,capable} -bfs_sudo setcap all+ep scratch/capable || skip -ln -s capable scratch/link - -bfs_diff -L scratch -capable +bfs_diff -L . -capable diff --git a/tests/bfs/capable.out b/tests/bfs/capable.out index 78b5bd9..ac7b5ce 100644 --- a/tests/bfs/capable.out +++ b/tests/bfs/capable.out @@ -1 +1 @@ -scratch/capable +./capable diff --git a/tests/bfs/capable.sh b/tests/bfs/capable.sh index e5cad63..35bb0b4 100644 --- a/tests/bfs/capable.sh +++ b/tests/bfs/capable.sh @@ -1,11 +1,10 @@ test "$UNAME" = "Linux" || skip +invoke_bfs . -quit -capable || skip -clean_scratch +cd "$TEST" -invoke_bfs scratch -quit -capable || skip +"$XTOUCH" normal capable +bfs_sudo setcap all+ep capable || skip +ln -s capable link -"$XTOUCH" scratch/{normal,capable} -bfs_sudo setcap all+ep scratch/capable || skip -ln -s capable scratch/link - -bfs_diff scratch -capable +bfs_diff . -capable diff --git a/tests/bfs/color_ls.sh b/tests/bfs/color_ls.sh index f2d3c72..f1cc216 100644 --- a/tests/bfs/color_ls.sh +++ b/tests/bfs/color_ls.sh @@ -1,4 +1,4 @@ -clean_scratch +cd "$TEST" "$XTOUCH" -p 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 bf6b62f..a862773 100644 --- a/tests/bfs/exec_flush_fprint.sh +++ b/tests/bfs/exec_flush_fprint.sh @@ -1,3 +1,2 @@ # Even non-stdstreams should be flushed -clean_scratch -bfs_diff basic/a -fprint scratch/foo -exec cat scratch/foo \; +bfs_diff basic/a -fprint "$OUT.f" -exec cat "$OUT.f" \; diff --git a/tests/bsd/L_acl.out b/tests/bsd/L_acl.out index 1dae00a..dd89800 100644 --- a/tests/bsd/L_acl.out +++ b/tests/bsd/L_acl.out @@ -1,2 +1,2 @@ -scratch/acl -scratch/link +./acl +./link diff --git a/tests/bsd/L_acl.sh b/tests/bsd/L_acl.sh index db97013..a3fcbc8 100644 --- a/tests/bsd/L_acl.sh +++ b/tests/bsd/L_acl.sh @@ -1,9 +1,9 @@ -clean_scratch +cd "$TEST" -invoke_bfs scratch -quit -acl || skip +invoke_bfs . -quit -acl || skip -"$XTOUCH" scratch/{normal,acl} -set_acl scratch/acl || skip -ln -s acl scratch/link +"$XTOUCH" normal acl +set_acl acl || skip +ln -s acl link -bfs_diff -L scratch -acl +bfs_diff -L . -acl diff --git a/tests/bsd/L_xattr.out b/tests/bsd/L_xattr.out index 12fac95..21eb50f 100644 --- a/tests/bsd/L_xattr.out +++ b/tests/bsd/L_xattr.out @@ -1,3 +1,3 @@ -scratch/link -scratch/xattr -scratch/xattr_2 +./link +./xattr +./xattr_2 diff --git a/tests/bsd/L_xattr.sh b/tests/bsd/L_xattr.sh index 1f61c78..f8b56d8 100644 --- a/tests/bsd/L_xattr.sh +++ b/tests/bsd/L_xattr.sh @@ -1,3 +1,3 @@ -invoke_bfs scratch -quit -xattr || skip +invoke_bfs . -quit -xattr || skip make_xattrs || skip -bfs_diff -L scratch -xattr +bfs_diff -L . -xattr diff --git a/tests/bsd/L_xattrname.out b/tests/bsd/L_xattrname.out index 4dc4836..9e4c172 100644 --- a/tests/bsd/L_xattrname.out +++ b/tests/bsd/L_xattrname.out @@ -1,2 +1,2 @@ -scratch/link -scratch/xattr +./link +./xattr diff --git a/tests/bsd/L_xattrname.sh b/tests/bsd/L_xattrname.sh index 3b2006b..8108d57 100644 --- a/tests/bsd/L_xattrname.sh +++ b/tests/bsd/L_xattrname.sh @@ -1,11 +1,11 @@ -invoke_bfs scratch -quit -xattr || skip +invoke_bfs . -quit -xattr || skip make_xattrs || skip case "$UNAME" in Darwin|FreeBSD) - bfs_diff -L scratch -xattrname bfs_test + bfs_diff -L . -xattrname bfs_test ;; *) - bfs_diff -L scratch -xattrname security.bfs_test + bfs_diff -L . -xattrname security.bfs_test ;; esac diff --git a/tests/bsd/acl.out b/tests/bsd/acl.out index ddf8446..92e2f67 100644 --- a/tests/bsd/acl.out +++ b/tests/bsd/acl.out @@ -1 +1 @@ -scratch/acl +./acl diff --git a/tests/bsd/acl.sh b/tests/bsd/acl.sh index c044398..a13c75f 100644 --- a/tests/bsd/acl.sh +++ b/tests/bsd/acl.sh @@ -1,9 +1,9 @@ -clean_scratch +cd "$TEST" -invoke_bfs scratch -quit -acl || skip +invoke_bfs . -quit -acl || skip -"$XTOUCH" scratch/{normal,acl} -set_acl scratch/acl || skip -ln -s acl scratch/link +"$XTOUCH" normal acl +set_acl acl || skip +ln -s acl link -bfs_diff scratch -acl +bfs_diff . -acl diff --git a/tests/bsd/flags.out b/tests/bsd/flags.out index 11998ed..3216ff5 100644 --- a/tests/bsd/flags.out +++ b/tests/bsd/flags.out @@ -1 +1 @@ -scratch/bar +./bar diff --git a/tests/bsd/flags.sh b/tests/bsd/flags.sh index 949a7d3..eb9bc22 100644 --- a/tests/bsd/flags.sh +++ b/tests/bsd/flags.sh @@ -1,8 +1,8 @@ -invoke_bfs scratch -quit -flags offline || skip +invoke_bfs . -quit -flags offline || skip -clean_scratch +cd "$TEST" -"$XTOUCH" scratch/{foo,bar} -chflags offline scratch/bar || skip +"$XTOUCH" foo bar +chflags offline bar || skip -bfs_diff scratch -flags -offline,nohidden +bfs_diff . -flags -offline,nohidden diff --git a/tests/bsd/rm.out b/tests/bsd/rm.out index fb188b9..9c558e3 100644 --- a/tests/bsd/rm.out +++ b/tests/bsd/rm.out @@ -1 +1 @@ -scratch +. diff --git a/tests/bsd/rm.sh b/tests/bsd/rm.sh index 9ee2b0a..595d514 100644 --- a/tests/bsd/rm.sh +++ b/tests/bsd/rm.sh @@ -1,6 +1,4 @@ -clean_scratch -"$XTOUCH" -p scratch/foo/bar/baz - -(cd scratch && invoke_bfs . -rm) - -bfs_diff scratch +cd "$TEST" +"$XTOUCH" -p foo/bar/baz +invoke_bfs . -rm +bfs_diff . diff --git a/tests/bsd/type_w.out b/tests/bsd/type_w.out index d383f69..a20a4f3 100644 --- a/tests/bsd/type_w.out +++ b/tests/bsd/type_w.out @@ -1,38 +1,34 @@ -1: -rw-r--r-- scratch/mnt/lower/bar -1: -rw-r--r-- scratch/mnt/lower/baz -1: -rw-r--r-- scratch/mnt/lower/foo -1: -rw-r--r-- scratch/mnt/upper/baz/qux -1: -rw-r--r-- scratch/mnt/upper/foo -1: drwxr-xr-x scratch/mnt -1: drwxr-xr-x scratch/mnt/lower -1: drwxr-xr-x scratch/mnt/upper -1: drwxr-xr-x scratch/mnt/upper/baz -2: w--------- scratch/mnt/upper/bar -3: -rw-r--r-- scratch/mnt/lower/bar -3: -rw-r--r-- scratch/mnt/lower/baz -3: -rw-r--r-- scratch/mnt/lower/foo -3: -rw-r--r-- scratch/mnt/upper/baz/qux -3: -rw-r--r-- scratch/mnt/upper/foo -3: drwxr-xr-x scratch/mnt -3: drwxr-xr-x scratch/mnt/lower -3: drwxr-xr-x scratch/mnt/upper -3: drwxr-xr-x scratch/mnt/upper/baz -3: w--------- scratch/mnt/upper/bar -4: -rw-r--r-- scratch/mnt/lower/bar -4: -rw-r--r-- scratch/mnt/lower/baz -4: -rw-r--r-- scratch/mnt/lower/foo -4: -rw-r--r-- scratch/mnt/upper/baz/qux -4: drwxr-xr-x scratch/mnt -4: drwxr-xr-x scratch/mnt/lower -4: drwxr-xr-x scratch/mnt/upper -4: drwxr-xr-x scratch/mnt/upper/baz -5: w--------- scratch/mnt/upper/bar -6: -rw-r--r-- scratch/mnt/lower/bar -6: -rw-r--r-- scratch/mnt/lower/baz -6: -rw-r--r-- scratch/mnt/lower/foo -6: -rw-r--r-- scratch/mnt/upper/baz/qux -6: drwxr-xr-x scratch/mnt -6: drwxr-xr-x scratch/mnt/lower -6: drwxr-xr-x scratch/mnt/upper -6: drwxr-xr-x scratch/mnt/upper/baz -6: w--------- scratch/mnt/upper/bar +1: -rw-r--r-- mnt/lower/bar +1: -rw-r--r-- mnt/lower/baz +1: -rw-r--r-- mnt/lower/foo +1: -rw-r--r-- mnt/upper/baz/qux +1: -rw-r--r-- mnt/upper/foo +1: drwxr-xr-x mnt/lower +1: drwxr-xr-x mnt/upper +1: drwxr-xr-x mnt/upper/baz +2: w--------- mnt/upper/bar +3: -rw-r--r-- mnt/lower/bar +3: -rw-r--r-- mnt/lower/baz +3: -rw-r--r-- mnt/lower/foo +3: -rw-r--r-- mnt/upper/baz/qux +3: -rw-r--r-- mnt/upper/foo +3: drwxr-xr-x mnt/lower +3: drwxr-xr-x mnt/upper +3: drwxr-xr-x mnt/upper/baz +3: w--------- mnt/upper/bar +4: -rw-r--r-- mnt/lower/bar +4: -rw-r--r-- mnt/lower/baz +4: -rw-r--r-- mnt/lower/foo +4: -rw-r--r-- mnt/upper/baz/qux +4: drwxr-xr-x mnt/lower +4: drwxr-xr-x mnt/upper +4: drwxr-xr-x mnt/upper/baz +5: w--------- mnt/upper/bar +6: -rw-r--r-- mnt/lower/bar +6: -rw-r--r-- mnt/lower/baz +6: -rw-r--r-- mnt/lower/foo +6: -rw-r--r-- mnt/upper/baz/qux +6: drwxr-xr-x mnt/lower +6: drwxr-xr-x mnt/upper +6: drwxr-xr-x mnt/upper/baz +6: w--------- mnt/upper/bar diff --git a/tests/bsd/type_w.sh b/tests/bsd/type_w.sh index 15b4e68..aec9cb1 100644 --- a/tests/bsd/type_w.sh +++ b/tests/bsd/type_w.sh @@ -2,51 +2,51 @@ command -v mdconfig &>/dev/null || skip command -v newfs &>/dev/null || skip -clean_scratch +cd "$TEST" # Create a ramdisk -truncate -s1M scratch/img -md=$(bfs_sudo mdconfig scratch/img) || skip +truncate -s1M img +md=$(bfs_sudo mdconfig img) || skip defer bfs_sudo mdconfig -du "$md" # Make an ffs filesystem bfs_sudo newfs -n "/dev/$md" >&2 || skip -mkdir scratch/mnt +mkdir mnt # Mount it -bfs_sudo mount "/dev/$md" scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount "/dev/$md" mnt || skip +defer bfs_sudo umount mnt # Make it owned by us -bfs_sudo chown "$(id -u):$(id -g)" scratch/mnt -"$XTOUCH" -p scratch/mnt/{lower/{foo,bar,baz},upper/{bar,baz/qux}} +bfs_sudo chown "$(id -u):$(id -g)" mnt +"$XTOUCH" -p mnt/{lower/{foo,bar,baz},upper/{bar,baz/qux}} # Mount a union filesystem within it -bfs_sudo mount -t unionfs -o below scratch/mnt/{lower,upper} -defer bfs_sudo umount scratch/mnt/upper +bfs_sudo mount -t unionfs -o below mnt/{lower,upper} +defer bfs_sudo umount mnt/upper # Create a whiteout -rm scratch/mnt/upper/bar +rm mnt/upper/bar # FreeBSD find doesn't have -printf, so munge -ls output munge_ls() { - sed -En 's|.*([-drwx]{10}).*(scratch/.*)|'"$1"': \1 \2|p' + sed -En 's|.*([-drwx]{10}).*(mnt/.*)|'"$1"': \1 \2|p' } # Do a few tests in one { # Normally, we shouldn't see the whiteouts - invoke_bfs scratch/mnt -ls | munge_ls 1 + invoke_bfs mnt -ls | munge_ls 1 # -type w adds whiteouts to the output - invoke_bfs scratch/mnt -type w -ls | munge_ls 2 + invoke_bfs mnt -type w -ls | munge_ls 2 # So this is not the same as test 1 - invoke_bfs scratch/mnt \( -type w -or -not -type w \) -ls | munge_ls 3 + invoke_bfs mnt \( -type w -or -not -type w \) -ls | munge_ls 3 # Unmount the unionfs 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 - invoke_bfs scratch/mnt \( -type w -or -not -type w \) -ls | munge_ls 6 + invoke_bfs mnt -ls | munge_ls 4 + invoke_bfs mnt -type w -ls | munge_ls 5 + invoke_bfs mnt \( -type w -or -not -type w \) -ls | munge_ls 6 } >"$OUT" sort_output diff_output diff --git a/tests/bsd/xattr.out b/tests/bsd/xattr.out index 109e7c9..0afed35 100644 --- a/tests/bsd/xattr.out +++ b/tests/bsd/xattr.out @@ -1,3 +1,3 @@ -scratch/xattr -scratch/xattr_2 -scratch/xattr_link +./xattr +./xattr_2 +./xattr_link diff --git a/tests/bsd/xattr.sh b/tests/bsd/xattr.sh index 4a4658c..68f729a 100644 --- a/tests/bsd/xattr.sh +++ b/tests/bsd/xattr.sh @@ -1,3 +1,3 @@ -invoke_bfs scratch -quit -xattr || skip +invoke_bfs . -quit -xattr || skip make_xattrs || skip -bfs_diff scratch -xattr +bfs_diff . -xattr diff --git a/tests/bsd/xattrname.out b/tests/bsd/xattrname.out index 0285ac1..ef732bd 100644 --- a/tests/bsd/xattrname.out +++ b/tests/bsd/xattrname.out @@ -1,2 +1,2 @@ -scratch/xattr -scratch/xattr_link +./xattr +./xattr_link diff --git a/tests/bsd/xattrname.sh b/tests/bsd/xattrname.sh index 655bd74..38b111a 100644 --- a/tests/bsd/xattrname.sh +++ b/tests/bsd/xattrname.sh @@ -1,11 +1,11 @@ -invoke_bfs scratch -quit -xattr || skip +invoke_bfs . -quit -xattr || skip make_xattrs || skip case "$UNAME" in Darwin|FreeBSD) - bfs_diff scratch -xattrname bfs_test + bfs_diff . -xattrname bfs_test ;; *) - bfs_diff scratch -xattrname security.bfs_test + bfs_diff . -xattrname security.bfs_test ;; esac diff --git a/tests/common/L_ls.sh b/tests/common/L_ls.sh index ced16c6..7ee2b44 100644 --- a/tests/common/L_ls.sh +++ b/tests/common/L_ls.sh @@ -1,2 +1 @@ -clean_scratch -invoke_bfs -L rainbow -ls >scratch/L_ls.out +invoke_bfs -L rainbow -ls >"$OUT" diff --git a/tests/common/L_mount.out b/tests/common/L_mount.out index 2e80082..788579d 100644 --- a/tests/common/L_mount.out +++ b/tests/common/L_mount.out @@ -1,5 +1,5 @@ -scratch -scratch/foo -scratch/foo/bar -scratch/foo/qux -scratch/mnt +. +./foo +./foo/bar +./foo/qux +./mnt diff --git a/tests/common/L_mount.sh b/tests/common/L_mount.sh index aaf9069..fd8042a 100644 --- a/tests/common/L_mount.sh +++ b/tests/common/L_mount.sh @@ -1,13 +1,13 @@ test "$UNAME" = "Darwin" && skip -clean_scratch -mkdir scratch/{foo,mnt} +cd "$TEST" +mkdir foo mnt -bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t tmpfs tmpfs mnt || skip +defer bfs_sudo umount mnt -ln -s ../mnt scratch/foo/bar -"$XTOUCH" scratch/mnt/baz -ln -s ../mnt/baz scratch/foo/qux +ln -s ../mnt foo/bar +"$XTOUCH" mnt/baz +ln -s ../mnt/baz foo/qux -bfs_diff -L scratch -mount +bfs_diff -L . -mount diff --git a/tests/common/delete.out b/tests/common/delete.out index fb188b9..9c558e3 100644 --- a/tests/common/delete.out +++ b/tests/common/delete.out @@ -1 +1 @@ -scratch +. diff --git a/tests/common/delete.sh b/tests/common/delete.sh index 89cf2a2..638f307 100644 --- a/tests/common/delete.sh +++ b/tests/common/delete.sh @@ -1,7 +1,4 @@ -clean_scratch -"$XTOUCH" -p scratch/foo/bar/baz - -# Don't try to delete '.' -(cd scratch && invoke_bfs . -delete) - -bfs_diff scratch +cd "$TEST" +"$XTOUCH" -p foo/bar/baz +invoke_bfs . -delete +bfs_diff . diff --git a/tests/common/delete_many.out b/tests/common/delete_many.out index fb188b9..9c558e3 100644 --- a/tests/common/delete_many.out +++ b/tests/common/delete_many.out @@ -1 +1 @@ -scratch +. diff --git a/tests/common/delete_many.sh b/tests/common/delete_many.sh index 6274319..48fe4c2 100644 --- a/tests/common/delete_many.sh +++ b/tests/common/delete_many.sh @@ -1,8 +1,8 @@ # Test for https://github.com/tavianator/bfs/issues/67 -clean_scratch -mkdir scratch/foo -"$XTOUCH" scratch/foo/{1..256} +cd "$TEST" +mkdir foo +"$XTOUCH" foo/{1..256} -invoke_bfs scratch/foo -delete -bfs_diff scratch +invoke_bfs foo -delete +bfs_diff . diff --git a/tests/common/execdir_ulimit.out b/tests/common/execdir_ulimit.out index 7f53982..bf52c09 100644 --- a/tests/common/execdir_ulimit.out +++ b/tests/common/execdir_ulimit.out @@ -1,3 +1,4 @@ +./. ./0 ./1 ./2 @@ -30,7 +31,6 @@ ./q ./r ./s -./scratch ./t ./u ./v diff --git a/tests/common/execdir_ulimit.sh b/tests/common/execdir_ulimit.sh index f7fc467..90c93c1 100644 --- a/tests/common/execdir_ulimit.sh +++ b/tests/common/execdir_ulimit.sh @@ -1,6 +1,6 @@ -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 +cd "$TEST" +mkdir -p 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 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 ulimit -n 13 -bfs_diff scratch -execdir echo {} \; +bfs_diff . -execdir echo {} \; diff --git a/tests/common/inum_bind_mount.out b/tests/common/inum_bind_mount.out index a520de3..ede8749 100644 --- a/tests/common/inum_bind_mount.out +++ b/tests/common/inum_bind_mount.out @@ -1,2 +1,2 @@ -scratch/bar -scratch/foo +./bar +./foo diff --git a/tests/common/inum_bind_mount.sh b/tests/common/inum_bind_mount.sh index 47f7c36..892713e 100644 --- a/tests/common/inum_bind_mount.sh +++ b/tests/common/inum_bind_mount.sh @@ -1,9 +1,9 @@ test "$UNAME" = "Linux" || skip -clean_scratch -"$XTOUCH" scratch/{foo,bar} +cd "$TEST" +"$XTOUCH" foo bar baz -bfs_sudo mount --bind scratch/{foo,bar} || skip -defer bfs_sudo umount scratch/bar +bfs_sudo mount --bind foo bar || skip +defer bfs_sudo umount bar -bfs_diff scratch -inum "$(inum scratch/bar)" +bfs_diff . -inum "$(inum bar)" diff --git a/tests/common/inum_mount.out b/tests/common/inum_mount.out index 99c7511..99fa01e 100644 --- a/tests/common/inum_mount.out +++ b/tests/common/inum_mount.out @@ -1 +1 @@ -scratch/mnt +./mnt diff --git a/tests/common/inum_mount.sh b/tests/common/inum_mount.sh index 1bf2d86..7facf57 100644 --- a/tests/common/inum_mount.sh +++ b/tests/common/inum_mount.sh @@ -1,9 +1,9 @@ test "$UNAME" = "Darwin" && skip -clean_scratch -mkdir scratch/{foo,mnt} +cd "$TEST" +mkdir foo mnt -bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t tmpfs tmpfs mnt || skip +defer bfs_sudo umount mnt -bfs_diff scratch -inum "$(inum scratch/mnt)" +bfs_diff . -inum "$(inum mnt)" diff --git a/tests/common/ls.sh b/tests/common/ls.sh index 85ca39c..bc50d90 100644 --- a/tests/common/ls.sh +++ b/tests/common/ls.sh @@ -1,2 +1 @@ -clean_scratch -invoke_bfs rainbow -ls >scratch/ls.out +invoke_bfs rainbow -ls >"$OUT" diff --git a/tests/common/mount.out b/tests/common/mount.out index f7839fb..6253434 100644 --- a/tests/common/mount.out +++ b/tests/common/mount.out @@ -1,4 +1,4 @@ -scratch -scratch/foo -scratch/foo/bar -scratch/mnt +. +./foo +./foo/bar +./mnt diff --git a/tests/common/mount.sh b/tests/common/mount.sh index db8b801..c9abde5 100644 --- a/tests/common/mount.sh +++ b/tests/common/mount.sh @@ -1,11 +1,11 @@ test "$UNAME" = "Darwin" && skip -clean_scratch -mkdir scratch/{foo,mnt} +cd "$TEST" +mkdir foo mnt -bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t tmpfs tmpfs mnt || skip +defer bfs_sudo umount mnt -"$XTOUCH" scratch/foo/bar scratch/mnt/baz +"$XTOUCH" foo/bar mnt/baz -bfs_diff scratch -mount +bfs_diff . -mount diff --git a/tests/gnu/L_delete.out b/tests/gnu/L_delete.out index ed0e9a1..7ed5f0d 100644 --- a/tests/gnu/L_delete.out +++ b/tests/gnu/L_delete.out @@ -1,2 +1,2 @@ -scratch -scratch/foo +. +./foo diff --git a/tests/gnu/L_delete.sh b/tests/gnu/L_delete.sh index 8fdb12a..0559c49 100644 --- a/tests/gnu/L_delete.sh +++ b/tests/gnu/L_delete.sh @@ -1,9 +1,8 @@ -clean_scratch -mkdir scratch/foo -mkdir scratch/bar -ln -s ../foo scratch/bar/baz +cd "$TEST" +mkdir foo bar +ln -s ../foo bar/baz # Don't try to rmdir() a symlink -invoke_bfs -L scratch/bar -delete +invoke_bfs -L bar -delete -bfs_diff scratch +bfs_diff . diff --git a/tests/gnu/files0_from_file.sh b/tests/gnu/files0_from_file.sh index 089a20e..81435a0 100644 --- a/tests/gnu/files0_from_file.sh +++ b/tests/gnu/files0_from_file.sh @@ -1,4 +1,4 @@ -clean_scratch +FILE="$TMP/$TEST.in" cd weirdnames -invoke_bfs -mindepth 1 -fprintf ../scratch/files0.in "%P\0" -bfs_diff -files0-from ../scratch/files0.in +invoke_bfs -mindepth 1 -fprintf "$FILE" "%P\0" +bfs_diff -files0-from "$FILE" diff --git a/tests/gnu/fls.sh b/tests/gnu/fls.sh index a86fa20..d2ff794 100644 --- a/tests/gnu/fls.sh +++ b/tests/gnu/fls.sh @@ -1,2 +1 @@ -clean_scratch -invoke_bfs rainbow -fls scratch/fls.out +invoke_bfs rainbow -fls "$OUT" diff --git a/tests/gnu/fls_nonexistent.sh b/tests/gnu/fls_nonexistent.sh index ff86763..2854569 100644 --- a/tests/gnu/fls_nonexistent.sh +++ b/tests/gnu/fls_nonexistent.sh @@ -1 +1 @@ -! invoke_bfs rainbow -fls scratch/nonexistent/path +! invoke_bfs rainbow -fls nonexistent/path diff --git a/tests/gnu/fprint0_nonexistent.sh b/tests/gnu/fprint0_nonexistent.sh index ec14c2d..4906081 100644 --- a/tests/gnu/fprint0_nonexistent.sh +++ b/tests/gnu/fprint0_nonexistent.sh @@ -1 +1 @@ -! invoke_bfs basic -fprint0 scratch/nonexistent/path +! invoke_bfs basic -fprint0 nonexistent/path diff --git a/tests/gnu/fprint_duplicate.sh b/tests/gnu/fprint_duplicate.sh index 5275502..8533b05 100644 --- a/tests/gnu/fprint_duplicate.sh +++ b/tests/gnu/fprint_duplicate.sh @@ -1,7 +1,7 @@ -"$XTOUCH" -p scratch/foo.out -ln scratch/foo.out scratch/foo.hard -ln -s foo.out scratch/foo.soft +"$XTOUCH" -p "$TEST/foo.out" +ln "$TEST/foo.out" "$TEST/foo.hard" +ln -s foo.out "$TEST/foo.soft" -invoke_bfs basic -fprint scratch/foo.out -fprint scratch/foo.hard -fprint scratch/foo.soft -sort scratch/foo.out >"$OUT" +invoke_bfs basic -fprint "$TEST/foo.out" -fprint "$TEST/foo.hard" -fprint "$TEST/foo.soft" +sort "$TEST/foo.out" >"$OUT" diff_output diff --git a/tests/gnu/fprint_nonexistent.sh b/tests/gnu/fprint_nonexistent.sh index 4409162..2a403a2 100644 --- a/tests/gnu/fprint_nonexistent.sh +++ b/tests/gnu/fprint_nonexistent.sh @@ -1 +1 @@ -! invoke_bfs basic -fprint scratch/nonexistent/path +! invoke_bfs basic -fprint nonexistent/path diff --git a/tests/gnu/fprintf_nonexistent.sh b/tests/gnu/fprintf_nonexistent.sh index 160e739..b1eea10 100644 --- a/tests/gnu/fprintf_nonexistent.sh +++ b/tests/gnu/fprintf_nonexistent.sh @@ -1 +1 @@ -! invoke_bfs basic -fprintf scratch/nonexistent/path '%p\n' +! invoke_bfs basic -fprintf nonexistent/path '%p\n' diff --git a/tests/gnu/fstype_stacked.out b/tests/gnu/fstype_stacked.out index 99c7511..c1e0e6c 100644 --- a/tests/gnu/fstype_stacked.out +++ b/tests/gnu/fstype_stacked.out @@ -1 +1 @@ -scratch/mnt +mnt diff --git a/tests/gnu/fstype_stacked.sh b/tests/gnu/fstype_stacked.sh index a4b067a..a9739bb 100644 --- a/tests/gnu/fstype_stacked.sh +++ b/tests/gnu/fstype_stacked.sh @@ -1,12 +1,12 @@ test "$UNAME" = "Linux" || skip -clean_scratch -mkdir scratch/mnt +cd "$TEST" +mkdir mnt -bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t tmpfs tmpfs mnt || skip +defer bfs_sudo umount mnt -bfs_sudo mount -t ramfs ramfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t ramfs ramfs mnt || skip +defer bfs_sudo umount mnt -bfs_diff scratch/mnt -fstype ramfs -print -o -printf '%p: %F\n' +bfs_diff mnt -fstype ramfs -print -o -printf '%p: %F\n' diff --git a/tests/gnu/fstype_umount.sh b/tests/gnu/fstype_umount.sh index b6da7a3..81c195f 100644 --- a/tests/gnu/fstype_umount.sh +++ b/tests/gnu/fstype_umount.sh @@ -1,12 +1,12 @@ test "$UNAME" = "Linux" || skip -clean_scratch +cd "$TEST" -mkdir scratch/tmp -bfs_sudo mount -t tmpfs tmpfs scratch/tmp || skip -defer bfs_sudo umount -R scratch/tmp +mkdir tmp +bfs_sudo mount -t tmpfs tmpfs tmp || skip +defer bfs_sudo umount -R tmp -mkdir scratch/tmp/ram -bfs_sudo mount -t ramfs ramfs scratch/tmp/ram || skip +mkdir tmp/ram +bfs_sudo mount -t ramfs ramfs tmp/ram || skip -bfs_diff scratch/tmp -path scratch/tmp -exec "${SUDO[@]}" umount scratch/tmp/ram \; , -fstype ramfs -print +bfs_diff tmp -path tmp -exec "${SUDO[@]}" umount tmp/ram \; , -fstype ramfs -print diff --git a/tests/gnu/ignore_readdir_race.sh b/tests/gnu/ignore_readdir_race.sh index 6586bcc..75165f6 100644 --- a/tests/gnu/ignore_readdir_race.sh +++ b/tests/gnu/ignore_readdir_race.sh @@ -1,5 +1,5 @@ -clean_scratch -"$XTOUCH" scratch/{foo,bar} +cd "$TEST" +"$XTOUCH" foo bar # -links 1 forces a stat() call, which will fail for the second file -invoke_bfs scratch -mindepth 1 -ignore_readdir_race -links 1 -exec "$TESTS/remove-sibling.sh" {} \; +invoke_bfs . -mindepth 1 -ignore_readdir_race -links 1 -exec "$TESTS/remove-sibling.sh" {} \; diff --git a/tests/gnu/ignore_readdir_race_notdir.sh b/tests/gnu/ignore_readdir_race_notdir.sh index 5b8b56d..8b03164 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 -clean_scratch -"$XTOUCH" -p scratch/foo/bar +cd "$TEST" +"$XTOUCH" -p foo/bar -invoke_bfs scratch -mindepth 1 -ignore_readdir_race -execdir rm -r {} \; -execdir "$XTOUCH" {} \; +invoke_bfs . -mindepth 1 -ignore_readdir_race -execdir rm -r {} \; -execdir "$XTOUCH" {} \; diff --git a/tests/gnu/inum_automount.out b/tests/gnu/inum_automount.out index 7b53ae3..3378e2d 100644 --- a/tests/gnu/inum_automount.out +++ b/tests/gnu/inum_automount.out @@ -1 +1 @@ -scratch/automnt +./automnt diff --git a/tests/gnu/inum_automount.sh b/tests/gnu/inum_automount.sh index 261a4be..86b23e1 100644 --- a/tests/gnu/inum_automount.sh +++ b/tests/gnu/inum_automount.sh @@ -2,13 +2,13 @@ command -v systemd-mount &>/dev/null || skip -clean_scratch -mkdir scratch/{foo,automnt} +cd "$TEST" +mkdir foo automnt -bfs_sudo systemd-mount -A -o bind basic scratch/automnt || skip -defer bfs_sudo systemd-umount scratch/automnt +bfs_sudo systemd-mount -A -o bind "$TMP/basic" automnt || skip +defer bfs_sudo systemd-umount automnt -before=$(inum scratch/automnt) -bfs_diff scratch -inum "$before" -prune -after=$(inum scratch/automnt) +before=$(inum automnt) +bfs_diff . -inum "$before" -prune +after=$(inum automnt) ((before == after)) diff --git a/tests/gnu/printf_Y_error.out b/tests/gnu/printf_Y_error.out index 410a9b5..1dd554e 100644 --- a/tests/gnu/printf_Y_error.out +++ b/tests/gnu/printf_Y_error.out @@ -1,3 +1,3 @@ -(scratch) () d d -(scratch/bar) (foo/bar) l ? -(scratch/foo) () d d +(.) () d d +(./bar) (foo/bar) l ? +(./foo) () d d diff --git a/tests/gnu/printf_Y_error.sh b/tests/gnu/printf_Y_error.sh index 13d52e3..d3130ce 100644 --- a/tests/gnu/printf_Y_error.sh +++ b/tests/gnu/printf_Y_error.sh @@ -1,8 +1,8 @@ -clean_scratch -mkdir scratch/foo -ln -s foo/bar scratch/bar +cd "$TEST" +mkdir foo +ln -s foo/bar bar -chmod -x scratch/foo -defer chmod +x scratch/foo +chmod -x foo +defer chmod +x foo -! bfs_diff scratch -printf '(%p) (%l) %y %Y\n' +! bfs_diff . -printf '(%p) (%l) %y %Y\n' diff --git a/tests/gnu/regex_invalid_utf8.out b/tests/gnu/regex_invalid_utf8.out index 03f3f58..a133b1a 100644 --- a/tests/gnu/regex_invalid_utf8.out +++ b/tests/gnu/regex_invalid_utf8.out @@ -1 +1 @@ -scratch/℠+./℠diff --git a/tests/gnu/regex_invalid_utf8.sh b/tests/gnu/regex_invalid_utf8.sh index 603d688..7006dcd 100644 --- a/tests/gnu/regex_invalid_utf8.sh +++ b/tests/gnu/regex_invalid_utf8.sh @@ -1,8 +1,8 @@ -clean_scratch +cd "$TEST" # Incomplete UTF-8 sequences -touch scratch/$'\xC3' || skip -touch scratch/$'\xE2\x84' || skip -touch scratch/$'\xF0\x9F\x92' || skip +touch $'\xC3' || skip +touch $'\xE2\x84' || skip +touch $'\xF0\x9F\x92' || skip -bfs_diff scratch -regex 'scratch/..' +bfs_diff . -regex '\./..' diff --git a/tests/gnu/xtype_bind_mount.out b/tests/gnu/xtype_bind_mount.out index 16804ea..d18d706 100644 --- a/tests/gnu/xtype_bind_mount.out +++ b/tests/gnu/xtype_bind_mount.out @@ -1,2 +1,2 @@ -scratch/link -scratch/null +./link +./null diff --git a/tests/gnu/xtype_bind_mount.sh b/tests/gnu/xtype_bind_mount.sh index 1cc20ec..35fb3f5 100644 --- a/tests/gnu/xtype_bind_mount.sh +++ b/tests/gnu/xtype_bind_mount.sh @@ -1,10 +1,10 @@ test "$UNAME" = "Linux" || skip -clean_scratch -"$XTOUCH" scratch/{file,null} -ln -s /dev/null scratch/link +cd "$TEST" +"$XTOUCH" file null +ln -s /dev/null link -bfs_sudo mount --bind /dev/null scratch/null || skip -defer bfs_sudo umount scratch/null +bfs_sudo mount --bind /dev/null null || skip +defer bfs_sudo umount null -bfs_diff -L scratch -type c +bfs_diff . -xtype c diff --git a/tests/posix/L_xdev.out b/tests/posix/L_xdev.out index 2e80082..788579d 100644 --- a/tests/posix/L_xdev.out +++ b/tests/posix/L_xdev.out @@ -1,5 +1,5 @@ -scratch -scratch/foo -scratch/foo/bar -scratch/foo/qux -scratch/mnt +. +./foo +./foo/bar +./foo/qux +./mnt diff --git a/tests/posix/L_xdev.sh b/tests/posix/L_xdev.sh index d16c211..82d8605 100644 --- a/tests/posix/L_xdev.sh +++ b/tests/posix/L_xdev.sh @@ -1,13 +1,13 @@ test "$UNAME" = "Darwin" && skip -clean_scratch -mkdir scratch/{foo,mnt} +cd "$TEST" +mkdir foo mnt -bfs_sudo mount -t tmpfs tmpfs scratch/mnt || skip -defer bfs_sudo umount scratch/mnt +bfs_sudo mount -t tmpfs tmpfs mnt || skip +defer bfs_sudo umount mnt -ln -s ../mnt scratch/foo/bar -"$XTOUCH" scratch/mnt/baz -ln -s ../mnt/baz scratch/foo/qux +ln -s ../mnt foo/bar +"$XTOUCH" mnt/baz +ln -s ../mnt/baz foo/qux -bfs_diff -L scratch -xdev +bfs_diff -L . -xdev diff --git a/tests/posix/depth_error.out b/tests/posix/depth_error.out index ed0e9a1..7ed5f0d 100644 --- a/tests/posix/depth_error.out +++ b/tests/posix/depth_error.out @@ -1,2 +1,2 @@ -scratch -scratch/foo +. +./foo diff --git a/tests/posix/depth_error.sh b/tests/posix/depth_error.sh index a6429d1..db414ba 100644 --- a/tests/posix/depth_error.sh +++ b/tests/posix/depth_error.sh @@ -1,7 +1,7 @@ -clean_scratch -"$XTOUCH" -p scratch/foo/bar +cd "$TEST" +"$XTOUCH" -p foo/bar -chmod a-r scratch/foo -defer chmod +r scratch/foo +chmod a-r foo +defer chmod +r foo -! bfs_diff scratch -depth +! bfs_diff . -depth diff --git a/tests/posix/overlayfs.out b/tests/posix/overlayfs.out index 754d01d..b472b56 100644 --- a/tests/posix/overlayfs.out +++ b/tests/posix/overlayfs.out @@ -1,5 +1,5 @@ -scratch/merged -scratch/merged/bar -scratch/merged/baz -scratch/merged/baz/qux -scratch/merged/foo +merged +merged/bar +merged/baz +merged/baz/qux +merged/foo diff --git a/tests/posix/overlayfs.sh b/tests/posix/overlayfs.sh index 4cccebf..21ef22f 100644 --- a/tests/posix/overlayfs.sh +++ b/tests/posix/overlayfs.sh @@ -1,11 +1,11 @@ test "$UNAME" = "Linux" || skip -clean_scratch -"$XTOUCH" -p scratch/{lower/{foo,bar,baz},upper/{bar,baz/qux}} +cd "$TEST" +"$XTOUCH" -p 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 -defer bfs_sudo rm -rf scratch/work -defer bfs_sudo umount scratch/merged +mkdir -p work merged +bfs_sudo mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work merged || skip +defer bfs_sudo rm -rf work +defer bfs_sudo umount merged -bfs_diff scratch/merged +bfs_diff merged diff --git a/tests/posix/readdir_error.sh b/tests/posix/readdir_error.sh index fc48eb1..82fcd17 100644 --- a/tests/posix/readdir_error.sh +++ b/tests/posix/readdir_error.sh @@ -1,27 +1,27 @@ test "$UNAME" = "Linux" || skip -clean_scratch -mkfifo scratch/{fever,pid,wait,running} +cd "$TEST" +mkfifo hang pid wait running ( # Create a zombie process - cat scratch/fever >/dev/null & - # Write the PID to scratch/pid - echo $! >scratch/pid + cat hang >/dev/null & + # Write the PID to pid + echo $! >pid # Don't wait on the zombie process - exec cat scratch/wait scratch/fever >scratch/running + exec cat wait hang >running ) & # Kill the parent cat on exit defer kill -9 %1 # Read the child PID -read -r pid scratch/wait & -read -r _ wait & +read -r _ /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 @@ -169,8 +147,5 @@ clean_stddirs() { fi done - # In case a test left anything weird in scratch/ - clean_scratch - rm -rf "$TMP" } -- cgit v1.2.3 From de8e0fbb8c1e59b3f1af72a07477b63a8f0bbc75 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 23 Oct 2023 11:42:33 -0400 Subject: tests: Fix Bash 3 compatibility --- tests/bfs/execdir_plus_nonexistent.sh | 6 ++--- tests/common/execdir_nonexistent.sh | 6 ++--- tests/posix/L_loops.sh | 4 ++-- tests/posix/exec_nonexistent.sh | 7 ++---- tests/posix/exec_plus_nonexistent.sh | 6 ++--- tests/run.sh | 44 +++++++++++++++++++++++++---------- tests/util.sh | 11 ++++----- 7 files changed, 47 insertions(+), 37 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/execdir_plus_nonexistent.sh b/tests/bfs/execdir_plus_nonexistent.sh index e3b4d2d..ed7ed56 100644 --- a/tests/bfs/execdir_plus_nonexistent.sh +++ b/tests/bfs/execdir_plus_nonexistent.sh @@ -1,4 +1,2 @@ -! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} + 2>&1 >/dev/null) -[ -n "$stderr" ] - -! bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print +bfs_diff basic -execdir "$TESTS/nonexistent" {} + -print 2>"$TEST/err" && fail +test -s "$TEST/err" diff --git a/tests/common/execdir_nonexistent.sh b/tests/common/execdir_nonexistent.sh index 4bb4fdb..0ec013c 100644 --- a/tests/common/execdir_nonexistent.sh +++ b/tests/common/execdir_nonexistent.sh @@ -1,4 +1,2 @@ -! stderr=$(invoke_bfs basic -execdir "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) -[ -n "$stderr" ] - -! bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print +bfs_diff basic -print -execdir "$TESTS/nonexistent" {} \; -print 2>"$TEST/err" && fail +test -s "$TEST/err" diff --git a/tests/posix/L_loops.sh b/tests/posix/L_loops.sh index 1314401..01b7efc 100644 --- a/tests/posix/L_loops.sh +++ b/tests/posix/L_loops.sh @@ -1,4 +1,4 @@ # POSIX says it's okay to either stop or keep going on seeing a filesystem # loop, as long as a diagnostic is printed -! errors=$(invoke_bfs -L loops 2>&1 >/dev/null) -[ -n "$errors" ] +invoke_bfs -L loops >/dev/null 2>"$OUT" && fail +test -s "$OUT" diff --git a/tests/posix/exec_nonexistent.sh b/tests/posix/exec_nonexistent.sh index 901be86..a9ff052 100644 --- a/tests/posix/exec_nonexistent.sh +++ b/tests/posix/exec_nonexistent.sh @@ -1,7 +1,4 @@ # Failure to execute the command should lead to an error message and # non-zero exit status. See https://unix.stackexchange.com/q/704522/56202 - -! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} \; 2>&1 >/dev/null) -[ -n "$stderr" ] - -! bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print +bfs_diff basic -print -exec "$TESTS/nonexistent" {} \; -print 2>"$TEST/err" && fail +test -s "$TEST/err" diff --git a/tests/posix/exec_plus_nonexistent.sh b/tests/posix/exec_plus_nonexistent.sh index 6bddc67..24582a3 100644 --- a/tests/posix/exec_plus_nonexistent.sh +++ b/tests/posix/exec_plus_nonexistent.sh @@ -1,4 +1,2 @@ -! stderr=$(invoke_bfs basic -exec "$TESTS/nonexistent" {} + 2>&1 >/dev/null) -[ -n "$stderr" ] - -! bfs_diff basic -exec "$TESTS/nonexistent" {} + -print +bfs_diff basic -exec "$TESTS/nonexistent" {} + -print 2>"$TEST/err" && fail +test -s "$TEST/err" diff --git a/tests/run.sh b/tests/run.sh index b46fde6..d581476 100644 --- a/tests/run.sh +++ b/tests/run.sh @@ -47,7 +47,7 @@ bg_test() { if ((VERBOSE_ERRORS)); then run_test "$1" else - run_test "$1" 2>"$TMP/TEST.err" + run_test "$1" 2>"$TMP/$TEST.err" fi ret=$? @@ -59,9 +59,21 @@ bg_test() { return $ret } -# Reap a background job -reap() { - wait -n +# Wait for any background job to complete +if ((BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 3))); then + wait_any() { + wait -n + } +else + wait_any() { + read -ra jobs < <(jobs -p) + wait ${jobs[0]} + } +fi + +# Wait for a background test to finish +wait_test() { + wait_any ret=$? ((BG--)) @@ -118,7 +130,7 @@ run_tests() { OUT="$TMP/$TEST.out" if ((BG >= JOBS)); then - reap + wait_test fi ((++BG)) @@ -126,7 +138,7 @@ run_tests() { done while ((BG > 0)); do - reap + wait_test done printf "${BOL}" @@ -145,6 +157,14 @@ run_tests() { ## Utilities for the tests themselves +# Default return value for failed tests +EX_FAIL=1 + +# Fail the current test +fail() { + exit $EX_FAIL +} + # Return value when a test is skipped EX_SKIP=77 @@ -166,9 +186,9 @@ skip() { # Run a command and check its exit status check_exit() { local expected="$1" - local actual="0" + local actual=0 shift - "$@" || actual="$?" + "$@" || actual=$? ((actual == expected)) } @@ -253,9 +273,9 @@ invoke_bfs() { # Allow bfs to fail, but not crash if ((ret > 125)); then - exit "$ret" + exit $ret else - return "$ret" + return $ret fi } @@ -275,9 +295,9 @@ bfs_pty() { "$UNBUFFER" bash -c 'stty cols 80 rows 24 && "$@"' bash "${BFS[@]}" "$@" || ret=$? if ((ret > 125)); then - exit "$ret" + exit $ret else - return "$ret" + return $ret fi } diff --git a/tests/util.sh b/tests/util.sh index 31a7b6c..bfa5d16 100644 --- a/tests/util.sh +++ b/tests/util.sh @@ -120,7 +120,7 @@ callers() { # Print a message including path, line number, and command debug() { - local file="${1/#*\/tests\//tests\/}" + local file="${1/#*\/tests\//tests/}" set -- "$file" "${@:2}" color printf "${BLD}%s:%d:${RST} %s\n %s\n" "$@" } @@ -136,14 +136,13 @@ quote() { fi } +DEFER_LEVEL=-1 + # Run a command when this (sub)shell exits defer() { - # Refresh trap state before trap -p - # See https://unix.stackexchange.com/a/556888/56202 - trap -- KILL - # Check if the EXIT trap is already set - if ! trap -p EXIT | grep -q pop_defers; then + if ((DEFER_LEVEL != BASH_SUBSHELL)); then + DEFER_LEVEL=$BASH_SUBSHELL DEFER_CMDS=() DEFER_LINES=() DEFER_FILES=() -- cgit v1.2.3 From d2d5e5ad2e4b61493d27bfe8c9341fd81f703ae9 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 23 Oct 2023 13:06:44 -0400 Subject: tests/bfs/printf_color: Fix race condition Invoking bfs in the top directory can lead it to explore other tests' scratch directories, so explicitly exclude them. --- tests/bfs/printf_color.out | 1 + tests/bfs/printf_color.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/bfs') diff --git a/tests/bfs/printf_color.out b/tests/bfs/printf_color.out index 6641e9a..77d21c3 100644 --- a/tests/bfs/printf_color.out +++ b/tests/bfs/printf_color.out @@ -1,4 +1,5 @@ . $'./rainbow/\e[1m' $'\e[0m' $'./rainbow/\e[1m/'$'\e[0m' $'rainbow/\e[1m/'$'\e[0m' +. . . . . . rainbow ./rainbow rainbow . ./rainbow exec.sh ./rainbow/exec.sh rainbow/exec.sh . ./rainbow $'\e[1m' $'./rainbow/\e[1m' $'rainbow/\e[1m' diff --git a/tests/bfs/printf_color.sh b/tests/bfs/printf_color.sh index 7bb38c2..3641ddb 100644 --- a/tests/bfs/printf_color.sh +++ b/tests/bfs/printf_color.sh @@ -1 +1 @@ -bfs_diff -color -path './rainbow*' -printf '%H %h %f %p %P %l\n' +bfs_diff -color -exclude \( -depth 1 -not -name rainbow \) -printf '%H %h %f %p %P %l\n' -- cgit v1.2.3 From 8c06e033483215e5148bc803b61207b99d458a48 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 23 Oct 2023 13:34:33 -0400 Subject: tests/bfs/help: Use ... && fail instead of ! ... ! false doesn't trigger an error with set -e. --- tests/bfs/help.sh | 8 ++++---- tests/bsd/f_incomplete.sh | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/help.sh b/tests/bfs/help.sh index 2c0b28a..ec53867 100644 --- a/tests/bfs/help.sh +++ b/tests/bfs/help.sh @@ -1,4 +1,4 @@ -! invoke_bfs -help | grep -E '\{...?\}' -! invoke_bfs -D help | grep -E '\{...?\}' -! invoke_bfs -S help | grep -E '\{...?\}' -! invoke_bfs -regextype help | grep -E '\{...?\}' +invoke_bfs -help | grep -E '\{...?\}' && fail +invoke_bfs -D help | grep -E '\{...?\}' && fail +invoke_bfs -S help | grep -E '\{...?\}' && fail +invoke_bfs -regextype help | grep -E '\{...?\}' && fail diff --git a/tests/bsd/f_incomplete.sh b/tests/bsd/f_incomplete.sh index 50afe42..0dfb19f 100644 --- a/tests/bsd/f_incomplete.sh +++ b/tests/bsd/f_incomplete.sh @@ -1,2 +1 @@ ! invoke_bfs -f - -- cgit v1.2.3 From e1390e21d6fcfa97838bdaff968d9cfb26261065 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 23 Oct 2023 13:46:52 -0400 Subject: tests/bfs/help: Fix exit status --- tests/bfs/help.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/help.sh b/tests/bfs/help.sh index ec53867..5029c7e 100644 --- a/tests/bfs/help.sh +++ b/tests/bfs/help.sh @@ -1,4 +1,4 @@ -invoke_bfs -help | grep -E '\{...?\}' && fail -invoke_bfs -D help | grep -E '\{...?\}' && fail -invoke_bfs -S help | grep -E '\{...?\}' && fail -invoke_bfs -regextype help | grep -E '\{...?\}' && fail +! invoke_bfs -help | grep -E '\{...?\}' || fail +! invoke_bfs -D help | grep -E '\{...?\}' || fail +! invoke_bfs -S help | grep -E '\{...?\}' || fail +! invoke_bfs -regextype help | grep -E '\{...?\}' || fail -- cgit v1.2.3 From 79aee58a4621d01c4b1e98c332775f3b87213ddb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 6 Nov 2023 13:08:56 -0500 Subject: Treat NO_COLOR="" the same as unset The docs say > Command-line software which adds ANSI color to its output by default > should check for a NO_COLOR environment variable that, when present > and not an empty string (regardless of its value), prevents the > addition of ANSI color. but we were not checking for the empty string. Link: https://no-color.org/ Link: https://github.com/sharkdp/fd/pull/1421 --- src/parse.c | 3 ++- tests/bfs/nocolor_env_empty.out | 27 +++++++++++++++++++++++++++ tests/bfs/nocolor_env_empty.sh | 5 +++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/bfs/nocolor_env_empty.out create mode 100644 tests/bfs/nocolor_env_empty.sh (limited to 'tests/bfs') diff --git a/src/parse.c b/src/parse.c index 3f32021..09cfdd3 100644 --- a/src/parse.c +++ b/src/parse.c @@ -3670,7 +3670,8 @@ struct bfs_ctx *bfs_parse_cmdline(int argc, char *argv[]) { } enum use_color use_color = COLOR_AUTO; - if (getenv("NO_COLOR")) { + const char *no_color = getenv("NO_COLOR"); + if (no_color && *no_color) { // https://no-color.org/ use_color = COLOR_NEVER; } diff --git a/tests/bfs/nocolor_env_empty.out b/tests/bfs/nocolor_env_empty.out new file mode 100644 index 0000000..a439814 --- /dev/null +++ b/tests/bfs/nocolor_env_empty.out @@ -0,0 +1,27 @@ +$'rainbow/\e[1m' +$'rainbow/\e[1m/'$'\e[0m' +rainbow +rainbow/exec.sh +rainbow/socket +rainbow/broken +rainbow/chardev_link +rainbow/link.txt +rainbow/sticky_ow +rainbow/sgid +rainbow/pipe +rainbow/ow +rainbow/sugid +rainbow/suid +rainbow/sticky +rainbow/file.dat +rainbow/file.txt +rainbow/lower.gz +rainbow/lower.tar +rainbow/lower.tar.gz +rainbow/lu.tar.GZ +rainbow/mh1 +rainbow/mh2 +rainbow/ul.TAR.gz +rainbow/upper.GZ +rainbow/upper.TAR +rainbow/upper.TAR.GZ diff --git a/tests/bfs/nocolor_env_empty.sh b/tests/bfs/nocolor_env_empty.sh new file mode 100644 index 0000000..0ffc046 --- /dev/null +++ b/tests/bfs/nocolor_env_empty.sh @@ -0,0 +1,5 @@ +command -v unbuffer &>/dev/null || skip + +NO_COLOR= bfs_pty rainbow >"$OUT" +sort_output +diff_output -- cgit v1.2.3 From e0d7dc5dfd7bdaa62b6bc18e9c1cce00bbe08577 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sat, 9 Dec 2023 17:02:57 -0500 Subject: parse: Reject integers that start with whitespace --- src/parse.c | 6 +++++- tests/bfs/links_leading_space.sh | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 tests/bfs/links_leading_space.sh (limited to 'tests/bfs') diff --git a/src/parse.c b/src/parse.c index 13e65bc..778fc68 100644 --- a/src/parse.c +++ b/src/parse.c @@ -565,13 +565,17 @@ enum int_flags { * Parse an integer. */ static const char *parse_int(const struct parser_state *state, char **arg, const char *str, void *result, enum int_flags flags) { - char *endptr; + // strtoll() skips leading spaces, but we want to reject them + if (xisspace(str[0])) { + goto bad; + } int base = flags & IF_BASE_MASK; if (base == 0) { base = 10; } + char *endptr; errno = 0; long long value = strtoll(str, &endptr, base); if (errno != 0) { diff --git a/tests/bfs/links_leading_space.sh b/tests/bfs/links_leading_space.sh new file mode 100644 index 0000000..15957af --- /dev/null +++ b/tests/bfs/links_leading_space.sh @@ -0,0 +1 @@ +! invoke_bfs links -links ' 1' -- cgit v1.2.3 From 9cf8ea46ecc274351db419c2ab8fdf68c2d223d8 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 1 Jan 2024 23:46:11 -0500 Subject: tests: Remove unneeded command -v unbuffer || skip --- tests/bfs/nocolor_env.sh | 2 -- tests/bfs/nocolor_env_empty.sh | 2 -- 2 files changed, 4 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/nocolor_env.sh b/tests/bfs/nocolor_env.sh index 0a17fb2..d1c2afb 100644 --- a/tests/bfs/nocolor_env.sh +++ b/tests/bfs/nocolor_env.sh @@ -1,5 +1,3 @@ -command -v unbuffer &>/dev/null || skip - NO_COLOR=1 bfs_pty rainbow >"$OUT" sort_output diff_output diff --git a/tests/bfs/nocolor_env_empty.sh b/tests/bfs/nocolor_env_empty.sh index 0ffc046..1edfb1d 100644 --- a/tests/bfs/nocolor_env_empty.sh +++ b/tests/bfs/nocolor_env_empty.sh @@ -1,5 +1,3 @@ -command -v unbuffer &>/dev/null || skip - NO_COLOR= bfs_pty rainbow >"$OUT" sort_output diff_output -- cgit v1.2.3 From 4010140cb748cc4f7f57b0a3d514485796c665ce Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 7 Jan 2024 12:42:03 -0500 Subject: tests/bfs/D_opt: New test for more -D opt coverage --- tests/bfs/D_opt.out | 18 ++++++++++++++++++ tests/bfs/D_opt.sh | 1 + 2 files changed, 19 insertions(+) create mode 100644 tests/bfs/D_opt.out create mode 100644 tests/bfs/D_opt.sh (limited to 'tests/bfs') diff --git a/tests/bfs/D_opt.out b/tests/bfs/D_opt.out new file mode 100644 index 0000000..3b461cf --- /dev/null +++ b/tests/bfs/D_opt.out @@ -0,0 +1,18 @@ +basic/a +basic/b +basic/c +basic/c/d +basic/e +basic/e/f +basic/g +basic/g/h +basic/i +basic/j +basic/j/foo +basic/k +basic/k/foo +basic/k/foo/bar +basic/l +basic/l/foo +basic/l/foo/bar +basic/l/foo/bar/baz diff --git a/tests/bfs/D_opt.sh b/tests/bfs/D_opt.sh new file mode 100644 index 0000000..d95cf86 --- /dev/null +++ b/tests/bfs/D_opt.sh @@ -0,0 +1 @@ +bfs_diff -D opt -nohidden -not \( -type b -o -type c \) -links -5 -links -10 -not -hidden basic -- cgit v1.2.3 From c4334184502c25a41b5cab060681229b2b570c0a Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 1 Feb 2024 16:14:19 -0500 Subject: tests: Don't clobber inherited FDs Rather than attempting to close any unexpected FDs, just count them and adjust our ulimit -n calls to account for them. --- .github/workflows/ci.yml | 1 + tests/bfs/deep_strict.sh | 2 +- tests/common/execdir_ulimit.sh | 2 +- tests/gnu/execdir_ulimit.out | 32 ++++++++++++++++---------------- tests/gnu/execdir_ulimit.sh | 2 +- tests/gnu/printf_u_g_ulimit.sh | 2 +- tests/posix/deep.sh | 2 +- tests/posix/exec_ulimit.out | 32 ++++++++++++++++---------------- tests/posix/exec_ulimit.sh | 2 +- tests/posix/nogroup_ulimit.sh | 2 +- tests/posix/nouser_ulimit.sh | 2 +- tests/util.sh | 17 +++++------------ 12 files changed, 46 insertions(+), 52 deletions(-) (limited to 'tests/bfs') diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff0ff6f..8e96526 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,6 +86,7 @@ jobs: tcl-wrapper pw useradd -n action -m -G wheel -s /usr/local/bin/bash echo "%wheel ALL=(ALL) NOPASSWD: ALL" >>/usr/local/etc/sudoers + mount -t fdescfs none /dev/fd run: | chown -R action:action . diff --git a/tests/bfs/deep_strict.sh b/tests/bfs/deep_strict.sh index 50c8f05..22453c0 100644 --- a/tests/bfs/deep_strict.sh +++ b/tests/bfs/deep_strict.sh @@ -1,3 +1,3 @@ # Not even enough fds to keep the root open -ulimit -n 7 +ulimit -n $((NOPENFD + 4)) bfs_diff deep -type f -exec bash -c 'echo "${1:0:6}/.../${1##*/} (${#1})"' bash {} \; diff --git a/tests/common/execdir_ulimit.sh b/tests/common/execdir_ulimit.sh index 90c93c1..122c282 100644 --- a/tests/common/execdir_ulimit.sh +++ b/tests/common/execdir_ulimit.sh @@ -2,5 +2,5 @@ cd "$TEST" mkdir -p 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 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 -ulimit -n 13 +ulimit -n $((NOPENFD + 10)) bfs_diff . -execdir echo {} \; diff --git a/tests/gnu/execdir_ulimit.out b/tests/gnu/execdir_ulimit.out index 7931f9a..6749f7d 100644 --- a/tests/gnu/execdir_ulimit.out +++ b/tests/gnu/execdir_ulimit.out @@ -1,16 +1,16 @@ -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 ./0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE diff --git a/tests/gnu/execdir_ulimit.sh b/tests/gnu/execdir_ulimit.sh index 3245dad..e14e716 100644 --- a/tests/gnu/execdir_ulimit.sh +++ b/tests/gnu/execdir_ulimit.sh @@ -1,2 +1,2 @@ -ulimit -Sn 16 +ulimit -Sn 64 bfs_diff deep -type f -execdir bash -c 'printf "%d %s\n" $(ulimit -Sn) "$1"' bash {} \; diff --git a/tests/gnu/printf_u_g_ulimit.sh b/tests/gnu/printf_u_g_ulimit.sh index 390ad48..c621b9b 100644 --- a/tests/gnu/printf_u_g_ulimit.sh +++ b/tests/gnu/printf_u_g_ulimit.sh @@ -1,2 +1,2 @@ -ulimit -n 16 +ulimit -n $((NOPENFD + 13)) [ "$(invoke_bfs deep -printf '%u %g\n' | uniq)" = "$(id -un) $(id -gn)" ] diff --git a/tests/posix/deep.sh b/tests/posix/deep.sh index 431705e..36a88c0 100644 --- a/tests/posix/deep.sh +++ b/tests/posix/deep.sh @@ -1,2 +1,2 @@ -ulimit -n 16 +ulimit -n $((NOPENFD + 13)) bfs_diff deep -type f -exec bash -c 'echo "${1:0:6}/.../${1##*/} (${#1})"' bash {} \; diff --git a/tests/posix/exec_ulimit.out b/tests/posix/exec_ulimit.out index 587ece2..144169e 100644 --- a/tests/posix/exec_ulimit.out +++ b/tests/posix/exec_ulimit.out @@ -1,16 +1,16 @@ -16 deep/0/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/1/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/2/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/3/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/4/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/5/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/6/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/7/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/8/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/9/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/A/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/B/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/C/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/D/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/E/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE -16 deep/F/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/0/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/1/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/2/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/3/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/4/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/5/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/6/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/7/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/8/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/9/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/A/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/B/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/C/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/D/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/E/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE +64 deep/F/.../0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDE diff --git a/tests/posix/exec_ulimit.sh b/tests/posix/exec_ulimit.sh index 8ac8ff1..655fbec 100644 --- a/tests/posix/exec_ulimit.sh +++ b/tests/posix/exec_ulimit.sh @@ -1,2 +1,2 @@ -ulimit -Sn 16 +ulimit -Sn 64 bfs_diff deep -type f -exec bash -c 'printf "%d %s\n" $(ulimit -Sn) "${1:0:6}/.../${1##*/}"' bash {} \; diff --git a/tests/posix/nogroup_ulimit.sh b/tests/posix/nogroup_ulimit.sh index 5b4c440..a39dd1f 100644 --- a/tests/posix/nogroup_ulimit.sh +++ b/tests/posix/nogroup_ulimit.sh @@ -1,2 +1,2 @@ -ulimit -n 16 +ulimit -n $((NOPENFD + 13)) bfs_diff deep -type f -nogroup diff --git a/tests/posix/nouser_ulimit.sh b/tests/posix/nouser_ulimit.sh index e1400f0..a94b8c5 100644 --- a/tests/posix/nouser_ulimit.sh +++ b/tests/posix/nouser_ulimit.sh @@ -1,2 +1,2 @@ -ulimit -n 16 +ulimit -n $((NOPENFD + 13)) bfs_diff deep -type f -nouser diff --git a/tests/util.sh b/tests/util.sh index 686cc77..ec24958 100644 --- a/tests/util.sh +++ b/tests/util.sh @@ -48,23 +48,16 @@ stdenv() { export MallocNanoZone=0 fi - # Close non-standard inherited fds + # Count the inherited FDs if [ -d /proc/self/fd ]; then local fds=/proc/self/fd else local fds=/dev/fd fi - - for fd in "$fds"/*; do - if [ ! -e "$fd" ]; then - continue - fi - - local fd="${fd##*/}" - if ((fd > 2)); then - eval "exec ${fd}<&-" - fi - done + # We use ls $fds on purpose, rather than e.g. ($fds/*), to avoid counting + # internal bash fds that are not exposed to spawned processes + NOPENFD=$(ls -1q "$fds/" 2>/dev/null | wc -l) + NOPENFD=$((NOPENFD > 3 ? NOPENFD - 1 : 3)) # Close stdin so bfs doesn't think we're interactive # dup() the standard fds for logging even when redirected -- cgit v1.2.3 From 89ecb2a08467cd8aa6ba70f8519df494652cac96 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 5 Feb 2024 14:02:55 -0500 Subject: bftw: stat() files asynchronously --- src/bftw.c | 677 +++++++++++++++++++++++++++++++++------------- src/bftw.h | 20 +- src/eval.c | 20 +- tests/bfs/execdir_plus.sh | 2 +- 4 files changed, 507 insertions(+), 212 deletions(-) (limited to 'tests/bfs') diff --git a/src/bftw.c b/src/bftw.c index d392aed..664651c 100644 --- a/src/bftw.c +++ b/src/bftw.c @@ -36,58 +36,130 @@ #include #include +/** Initialize a bftw_stat cache. */ +static void bftw_stat_init(struct bftw_stat *bufs, struct bfs_stat *stat_buf, struct bfs_stat *lstat_buf) { + bufs->stat_buf = stat_buf; + bufs->lstat_buf = lstat_buf; + bufs->stat_err = -1; + bufs->lstat_err = -1; +} + +/** Fill a bftw_stat cache from another one. */ +static void bftw_stat_fill(struct bftw_stat *dest, const struct bftw_stat *src) { + if (dest->stat_err < 0 && src->stat_err >= 0) { + dest->stat_buf = src->stat_buf; + dest->stat_err = src->stat_err; + } + + if (dest->lstat_err < 0 && src->lstat_err >= 0) { + dest->lstat_buf = src->lstat_buf; + dest->lstat_err = src->lstat_err; + } +} + +/** Cache a bfs_stat() result. */ +static void bftw_stat_cache(struct bftw_stat *bufs, enum bfs_stat_flags flags, const struct bfs_stat *buf, int err) { + if (flags & BFS_STAT_NOFOLLOW) { + bufs->lstat_buf = buf; + bufs->lstat_err = err; + if (err || !S_ISLNK(buf->mode)) { + // Non-link, so share stat info + bufs->stat_buf = buf; + bufs->stat_err = err; + } + } else if (flags & BFS_STAT_TRYFOLLOW) { + if (err) { + bufs->stat_err = err; + } else if (S_ISLNK(buf->mode)) { + bufs->lstat_buf = buf; + bufs->lstat_err = err; + bufs->stat_err = ENOENT; + } else { + bufs->stat_buf = buf; + bufs->stat_err = err; + } + } else { + bufs->stat_buf = buf; + bufs->stat_err = err; + } +} + /** Caching bfs_stat(). */ -static const struct bfs_stat *bftw_stat_impl(struct BFTW *ftwbuf, struct bftw_stat *cache, enum bfs_stat_flags flags) { - if (!cache->buf) { - if (cache->error) { - errno = cache->error; - } else if (bfs_stat(ftwbuf->at_fd, ftwbuf->at_path, flags, &cache->storage) == 0) { - cache->buf = &cache->storage; +static const struct bfs_stat *bftw_stat_impl(struct BFTW *ftwbuf, enum bfs_stat_flags flags) { + struct bftw_stat *bufs = &ftwbuf->stat_bufs; + struct bfs_stat *buf; + + if (flags & BFS_STAT_NOFOLLOW) { + buf = (struct bfs_stat *)bufs->lstat_buf; + if (bufs->lstat_err == 0) { + return buf; + } else if (bufs->lstat_err > 0) { + errno = bufs->lstat_err; + return NULL; + } + } else { + buf = (struct bfs_stat *)bufs->stat_buf; + if (bufs->stat_err == 0) { + return buf; + } else if (bufs->stat_err > 0) { + errno = bufs->stat_err; + return NULL; + } + } + + struct bfs_stat *ret; + int err; + if (bfs_stat(ftwbuf->at_fd, ftwbuf->at_path, flags, buf) == 0) { + ret = buf; + err = 0; #ifdef S_IFWHT - } else if (errno == ENOENT && ftwbuf->type == BFS_WHT) { - // This matches the behavior of FTS_WHITEOUT on BSD - memset(&cache->storage, 0, sizeof(cache->storage)); - cache->storage.mode = S_IFWHT; - cache->buf = &cache->storage; + } else if (errno == ENOENT && ftwbuf->type == BFS_WHT) { + // This matches the behavior of FTS_WHITEOUT on BSD + ret = memset(buf, 0, sizeof(*buf)); + ret->mode = S_IFWHT; + err = 0; #endif - } else { - cache->error = errno; - } + } else { + ret = NULL; + err = errno; } - return cache->buf; + bftw_stat_cache(bufs, flags, ret, err); + return ret; } const struct bfs_stat *bftw_stat(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { struct BFTW *mutbuf = (struct BFTW *)ftwbuf; const struct bfs_stat *ret; - if (flags & BFS_STAT_NOFOLLOW) { - ret = bftw_stat_impl(mutbuf, &mutbuf->lstat_cache, BFS_STAT_NOFOLLOW); - if (ret && !S_ISLNK(ret->mode) && !mutbuf->stat_cache.buf) { - // Non-link, so share stat info - mutbuf->stat_cache.buf = ret; + if (flags & BFS_STAT_TRYFOLLOW) { + ret = bftw_stat_impl(mutbuf, BFS_STAT_FOLLOW); + if (!ret && errno_is_like(ENOENT)) { + ret = bftw_stat_impl(mutbuf, BFS_STAT_NOFOLLOW); } } else { - ret = bftw_stat_impl(mutbuf, &mutbuf->stat_cache, BFS_STAT_FOLLOW); - if (!ret && (flags & BFS_STAT_TRYFOLLOW) && errno_is_like(ENOENT)) { - ret = bftw_stat_impl(mutbuf, &mutbuf->lstat_cache, BFS_STAT_NOFOLLOW); - } + ret = bftw_stat_impl(mutbuf, flags); } return ret; } const struct bfs_stat *bftw_cached_stat(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { + const struct bftw_stat *bufs = &ftwbuf->stat_bufs; + if (flags & BFS_STAT_NOFOLLOW) { - return ftwbuf->lstat_cache.buf; - } else if (ftwbuf->stat_cache.buf) { - return ftwbuf->stat_cache.buf; - } else if ((flags & BFS_STAT_TRYFOLLOW) && error_is_like(ftwbuf->stat_cache.error, ENOENT)) { - return ftwbuf->lstat_cache.buf; - } else { - return NULL; + if (bufs->lstat_err == 0) { + return bufs->lstat_buf; + } + } else if (bufs->stat_err == 0) { + return bufs->stat_buf; + } else if ((flags & BFS_STAT_TRYFOLLOW) && error_is_like(bufs->stat_err, ENOENT)) { + if (bufs->lstat_err == 0) { + return bufs->lstat_buf; + } } + + return NULL; } enum bfs_type bftw_type(const struct BFTW *ftwbuf, enum bfs_stat_flags flags) { @@ -156,6 +228,9 @@ struct bftw_file { /** The inode number, for cycle detection. */ ino_t ino; + /** Cached bfs_stat() info. */ + struct bftw_stat stat_bufs; + /** The offset of this file in the full path. */ size_t nameoff; /** The length of the file's name. */ @@ -276,6 +351,10 @@ struct bftw_queue { struct bftw_list waiting; /** A list of already-serviced files. */ struct bftw_list ready; + /** The current size of the queue. */ + size_t size; + /** The number of files currently in-service. */ + size_t ioqueued; /** Tracks the imbalance between synchronous and async service. */ unsigned long imbalance; }; @@ -286,6 +365,8 @@ static void bftw_queue_init(struct bftw_queue *queue, enum bftw_qflags flags) { SLIST_INIT(&queue->buffer); SLIST_INIT(&queue->waiting); SLIST_INIT(&queue->ready); + queue->size = 0; + queue->ioqueued = 0; queue->imbalance = 0; } @@ -304,6 +385,8 @@ static void bftw_queue_push(struct bftw_queue *queue, struct bftw_file *file) { SLIST_APPEND(&queue->ready, file, ready); } } + + ++queue->size; } /** Add any buffered files to the queue. */ @@ -339,8 +422,19 @@ static bool bftw_queue_balanced(const struct bftw_queue *queue) { } } -/** Detatch the next waiting file to service it asynchronously. */ -static void bftw_queue_detach(struct bftw_queue *queue, struct bftw_file *file) { +/** Update the queue balance for (a)sync service. */ +static void bftw_queue_rebalance(struct bftw_queue *queue, bool async) { + if (async) { + --queue->imbalance; + } else { + ++queue->imbalance; + } +} + +/** Detatch the next waiting file. */ +static void bftw_queue_detach(struct bftw_queue *queue, struct bftw_file *file, bool async) { + bfs_assert(!file->ioqueued); + if (file == SLIST_HEAD(&queue->buffer)) { // To maintain order, we can't detach any files until they're // added to the waiting/ready lists @@ -352,19 +446,34 @@ static void bftw_queue_detach(struct bftw_queue *queue, struct bftw_file *file) bfs_bug("Detached file was not buffered or waiting"); } - file->ioqueued = true; - --queue->imbalance; + if (async) { + file->ioqueued = true; + ++queue->ioqueued; + bftw_queue_rebalance(queue, true); + } } /** Reattach a serviced file to the queue. */ -static void bftw_queue_attach(struct bftw_queue *queue, struct bftw_file *file) { - file->ioqueued = false; +static void bftw_queue_attach(struct bftw_queue *queue, struct bftw_file *file, bool async) { + if (async) { + bfs_assert(file->ioqueued); + file->ioqueued = false; + --queue->ioqueued; + } else { + bfs_assert(!file->ioqueued); + } if (!(queue->flags & BFTW_QORDER)) { SLIST_APPEND(&queue->ready, file, ready); } } +/** Make a file ready immediately. */ +static void bftw_queue_skip(struct bftw_queue *queue, struct bftw_file *file) { + bftw_queue_detach(queue, file, false); + bftw_queue_attach(queue, file, false); +} + /** Get the next waiting file. */ static struct bftw_file *bftw_queue_waiting(const struct bftw_queue *queue) { if (!(queue->flags & BFTW_QBUFFER)) { @@ -395,13 +504,6 @@ static struct bftw_file *bftw_queue_ready(const struct bftw_queue *queue) { return SLIST_HEAD(&queue->ready); } -/** Check if a queue is empty. */ -static bool bftw_queue_empty(const struct bftw_queue *queue) { - return SLIST_EMPTY(&queue->buffer) - && SLIST_EMPTY(&queue->waiting) - && SLIST_EMPTY(&queue->ready); -} - /** Pop a file from the queue. */ static struct bftw_file *bftw_queue_pop(struct bftw_queue *queue) { // Don't pop until we've had a chance to sort the buffer @@ -413,10 +515,10 @@ static struct bftw_file *bftw_queue_pop(struct bftw_queue *queue) { // If no files are ready, try the waiting list. Or, if // BFTW_QORDER is set, we may need to pop from both lists. file = SLIST_POP(&queue->waiting); - if (file) { - // This file will be serviced synchronously - ++queue->imbalance; - } + } + + if (file) { + --queue->size; } return file; @@ -444,6 +546,9 @@ struct bftw_cache { size_t dir_limit; /** Excess force-allocated bfs_dirs. */ size_t dir_excess; + + /** bfs_stat arena. */ + struct arena stat_bufs; }; /** Initialize a cache. */ @@ -462,6 +567,8 @@ static void bftw_cache_init(struct bftw_cache *cache, size_t capacity) { } cache->dir_excess = 0; + + ARENA_INIT(&cache->stat_bufs, struct bfs_stat); } /** Allocate a directory. */ @@ -603,8 +710,9 @@ static void bftw_cache_destroy(struct bftw_cache *cache) { bfs_assert(LIST_EMPTY(cache)); bfs_assert(!cache->target); - varena_destroy(&cache->files); + arena_destroy(&cache->stat_bufs); arena_destroy(&cache->dirs); + varena_destroy(&cache->files); } /** Create a new bftw_file. */ @@ -642,6 +750,8 @@ static struct bftw_file *bftw_file_new(struct bftw_cache *cache, struct bftw_fil file->dev = -1; file->ino = -1; + bftw_stat_init(&file->stat_bufs, NULL, NULL); + file->namelen = namelen; memcpy(file->name, name, namelen + 1); @@ -661,6 +771,21 @@ static void bftw_file_set_dir(struct bftw_cache *cache, struct bftw_file *file, } } +/** Free a file's cached stat() buffers. */ +static void bftw_stat_recycle(struct bftw_cache *cache, struct bftw_file *file) { + struct bftw_stat *bufs = &file->stat_bufs; + + struct bfs_stat *stat_buf = (struct bfs_stat *)bufs->stat_buf; + struct bfs_stat *lstat_buf = (struct bfs_stat *)bufs->lstat_buf; + if (stat_buf) { + arena_free(&cache->stat_bufs, stat_buf); + } else if (lstat_buf) { + arena_free(&cache->stat_bufs, lstat_buf); + } + + bftw_stat_init(bufs, NULL, NULL); +} + /** Free a bftw_file. */ static void bftw_file_free(struct bftw_cache *cache, struct bftw_file *file) { bfs_assert(file->refcount == 0); @@ -669,6 +794,8 @@ static void bftw_file_free(struct bftw_cache *cache, struct bftw_file *file) { bftw_file_close(cache, file); } + bftw_stat_recycle(cache, file); + varena_free(&cache->files, file, file->namelen + 1); } @@ -729,6 +856,10 @@ struct bftw_state { /** Extra data about the current file. */ struct BFTW ftwbuf; + /** stat() buffer storage. */ + struct bfs_stat stat_buf; + /** lstat() buffer storage. */ + struct bfs_stat lstat_buf; }; /** Check if we have to buffer files before visiting them. */ @@ -755,6 +886,11 @@ static bool bftw_must_buffer(const struct bftw_state *state) { return true; } + if ((state->flags & BFTW_STAT) && state->nthreads > 1) { + // We will be buffering every file anyway for ioq_stat() + return true; + } + return false; } @@ -841,19 +977,32 @@ static int bftw_state_init(struct bftw_state *state, const struct bftw_args *arg return 0; } -/** Unpin a directory, and possibly queue it for unwrapping. */ -static void bftw_unpin_dir(struct bftw_state *state, struct bftw_file *file, bool force) { - bftw_cache_unpin(&state->cache, file); +/** Queue a directory for unwrapping. */ +static void bftw_delayed_unwrap(struct bftw_state *state, struct bftw_file *file) { + bfs_assert(file->dir); - if (file->dir && (force || file->pincount == 0)) { - if (!SLIST_ATTACHED(&state->to_close, file)) { - SLIST_APPEND(&state->to_close, file); - } + if (!SLIST_ATTACHED(&state->to_close, file, ready)) { + SLIST_APPEND(&state->to_close, file, ready); + } +} + +/** Unpin a file's parent. */ +static void bftw_unpin_parent(struct bftw_state *state, struct bftw_file *file, bool unwrap) { + struct bftw_file *parent = file->parent; + if (!parent) { + return; + } + + bftw_cache_unpin(&state->cache, parent); + + if (unwrap && parent->dir && parent->pincount == 0) { + bftw_delayed_unwrap(state, parent); } } /** Pop a response from the I/O queue. */ static int bftw_ioq_pop(struct bftw_state *state, bool block) { + struct bftw_cache *cache = &state->cache; struct ioq *ioq = state->ioq; if (!ioq) { return -1; @@ -864,10 +1013,10 @@ static int bftw_ioq_pop(struct bftw_state *state, bool block) { return -1; } - struct bftw_cache *cache = &state->cache; - struct bftw_file *file; - struct bftw_file *parent; - struct bfs_dir *dir; + struct bftw_file *file = ent->ptr; + if (file) { + bftw_unpin_parent(state, file, true); + } enum ioq_op op = ent->op; switch (op) { @@ -877,30 +1026,30 @@ static int bftw_ioq_pop(struct bftw_state *state, bool block) { case IOQ_CLOSEDIR: ++cache->capacity; - dir = ent->closedir.dir; - bftw_freedir(cache, dir); + bftw_freedir(cache, ent->closedir.dir); break; case IOQ_OPENDIR: - file = ent->ptr; - ++cache->capacity; - parent = file->parent; - if (parent) { - bftw_unpin_dir(state, parent, false); - } - dir = ent->opendir.dir; if (ent->result >= 0) { - bftw_file_set_dir(cache, file, dir); + bftw_file_set_dir(cache, file, ent->opendir.dir); } else { - bftw_freedir(cache, dir); + bftw_freedir(cache, ent->opendir.dir); } - bftw_queue_attach(&state->dirq, file); + bftw_queue_attach(&state->dirq, file, true); break; case IOQ_STAT: + if (ent->result >= 0) { + bftw_stat_cache(&file->stat_bufs, ent->stat.flags, ent->stat.buf, 0); + } else { + arena_free(&cache->stat_bufs, ent->stat.buf); + bftw_stat_cache(&file->stat_bufs, ent->stat.flags, NULL, -ent->result); + } + + bftw_queue_attach(&state->fileq, file, true); break; } @@ -1125,21 +1274,34 @@ static int bftw_unwrapdir(struct bftw_state *state, struct bftw_file *file) { return bftw_ioq_closedir(state, dir); } +/** Try to pin a file's parent. */ +static int bftw_pin_parent(struct bftw_state *state, struct bftw_file *file) { + struct bftw_file *parent = file->parent; + if (!parent) { + return AT_FDCWD; + } + + int fd = parent->fd; + if (fd < 0) { + bfs_static_assert(AT_FDCWD != -1); + return -1; + } + + bftw_cache_pin(&state->cache, parent); + return fd; +} + /** Open a directory asynchronously. */ static int bftw_ioq_opendir(struct bftw_state *state, struct bftw_file *file) { + struct bftw_cache *cache = &state->cache; + if (bftw_ioq_reserve(state) != 0) { goto fail; } - int dfd = AT_FDCWD; - struct bftw_cache *cache = &state->cache; - struct bftw_file *parent = file->parent; - if (parent) { - dfd = parent->fd; - if (dfd < 0) { - goto fail; - } - bftw_cache_pin(cache, parent); + int dfd = bftw_pin_parent(state, file); + if (dfd < 0 && dfd != AT_FDCWD) { + goto fail; } if (bftw_cache_reserve(state) != 0) { @@ -1161,9 +1323,7 @@ static int bftw_ioq_opendir(struct bftw_state *state, struct bftw_file *file) { free: bftw_freedir(cache, dir); unpin: - if (parent) { - bftw_cache_unpin(cache, parent); - } + bftw_unpin_parent(state, file, false); fail: return -1; } @@ -1177,7 +1337,7 @@ static void bftw_ioq_opendirs(struct bftw_state *state) { } if (bftw_ioq_opendir(state, dir) == 0) { - bftw_queue_detach(&state->dirq, dir); + bftw_queue_detach(&state->dirq, dir, true); } else { break; } @@ -1191,24 +1351,36 @@ static void bftw_push_dir(struct bftw_state *state, struct bftw_file *file) { bftw_ioq_opendirs(state); } -/** Check if we should block on the ioq when popping a directory. */ -static bool bftw_block_for_dir(const struct bftw_state *state) { - // Always block with more than one background thread - if (state->nthreads > 1) { - return true; +/** Pop a file from a queue, then activate it. */ +static bool bftw_pop(struct bftw_state *state, struct bftw_queue *queue) { + if (queue->size == 0) { + return false; } - // Block if the cache is full - if (state->cache.capacity == 0) { - return true; + while (!bftw_queue_ready(queue) && queue->ioqueued > 0) { + bool block = true; + if (bftw_queue_waiting(queue) && state->nthreads == 1) { + // With only one background thread, balance the work + // between it and the main thread + block = false; + } + + if (bftw_ioq_pop(state, block) < 0) { + break; + } } - // Block if we have no other files/dirs to visit - if (!bftw_queue_waiting(&state->dirq) && bftw_queue_empty(&state->fileq)) { - return true; + struct bftw_file *file = bftw_queue_pop(queue); + if (!file) { + return false; } - return false; + while (file->ioqueued) { + bftw_ioq_pop(state, true); + } + + state->file = file; + return true; } /** Pop a directory to read from the queue. */ @@ -1220,32 +1392,143 @@ static bool bftw_pop_dir(struct bftw_state *state) { if (state->strategy == BFTW_BFS && bftw_queue_ready(&state->fileq)) { return false; } + } else if (!bftw_queue_ready(&state->dirq)) { + // Don't block if we have files ready to visit + if (bftw_queue_ready(&state->fileq)) { + return false; + } + } + + return bftw_pop(state, &state->dirq); +} + +/** Figure out bfs_stat() flags. */ +static enum bfs_stat_flags bftw_stat_flags(const struct bftw_state *state, size_t depth) { + enum bftw_flags mask = BFTW_FOLLOW_ALL; + if (depth == 0) { + mask |= BFTW_FOLLOW_ROOTS; + } + + if (state->flags & mask) { + return BFS_STAT_TRYFOLLOW; } else { - while (!bftw_queue_ready(&state->dirq)) { - if (bftw_ioq_pop(state, bftw_block_for_dir(state)) < 0) { - break; - } + return BFS_STAT_NOFOLLOW; + } +} + +/** Check if a stat() call is necessary. */ +static bool bftw_must_stat(const struct bftw_state *state, size_t depth, enum bfs_type type, const char *name) { + if (state->flags & BFTW_STAT) { + return true; + } + + switch (type) { + case BFS_UNKNOWN: + return true; + + case BFS_DIR: + return state->flags & (BFTW_DETECT_CYCLES | BFTW_SKIP_MOUNTS | BFTW_PRUNE_MOUNTS); + + case BFS_LNK: + if (!(bftw_stat_flags(state, depth) & BFS_STAT_NOFOLLOW)) { + return true; + } + fallthru; + + default: +#if __linux__ + if (state->mtab && bfs_might_be_mount(state->mtab, name)) { + return true; } +#endif + return false; } +} - struct bftw_file *file = bftw_queue_pop(&state->dirq); - if (!file) { +/** stat() a file asynchronously. */ +static int bftw_ioq_stat(struct bftw_state *state, struct bftw_file *file) { + if (bftw_ioq_reserve(state) != 0) { + goto fail; + } + + int dfd = bftw_pin_parent(state, file); + if (dfd < 0 && dfd != AT_FDCWD) { + goto fail; + } + + struct bftw_cache *cache = &state->cache; + struct bfs_stat *buf = arena_alloc(&cache->stat_bufs); + if (!buf) { + goto unpin; + } + + enum bfs_stat_flags flags = bftw_stat_flags(state, file->depth); + if (ioq_stat(state->ioq, dfd, file->name, flags, buf, file) != 0) { + goto free; + } + + return 0; + +free: + arena_free(&cache->stat_bufs, buf); +unpin: + bftw_unpin_parent(state, file, false); +fail: + return -1; +} + +/** Check if we should stat() a file asynchronously. */ +static bool bftw_should_ioq_stat(struct bftw_state *state, struct bftw_file *file) { + // To avoid surprising users too much, process the roots in order + if (file->depth == 0) { return false; } - while (file->ioqueued) { - bftw_ioq_pop(state, true); +#ifdef S_IFWHT + // ioq_stat() does not do whiteout emulation like bftw_stat_impl() + if (file->type == BFS_WHT) { + return false; } +#endif - state->file = file; - return true; + return bftw_must_stat(state, file->depth, file->type, file->name); +} + +/** Call stat() on files that need it. */ +static void bftw_stat_files(struct bftw_state *state) { + while (true) { + struct bftw_file *file = bftw_queue_waiting(&state->fileq); + if (!file) { + break; + } + + if (!bftw_should_ioq_stat(state, file)) { + bftw_queue_skip(&state->fileq, file); + continue; + } + + if (!bftw_queue_balanced(&state->fileq)) { + break; + } + + if (bftw_ioq_stat(state, file) == 0) { + bftw_queue_detach(&state->fileq, file, true); + } else { + break; + } + } +} + +/** Push a file onto the queue. */ +static void bftw_push_file(struct bftw_state *state, struct bftw_file *file) { + bftw_queue_push(&state->fileq, file); + bftw_stat_files(state); } /** Pop a file to visit from the queue. */ static bool bftw_pop_file(struct bftw_state *state) { bfs_assert(!state->file); - state->file = bftw_queue_pop(&state->fileq); - return state->file; + return bftw_pop(state, &state->fileq); } /** Build the path to the current file. */ @@ -1334,6 +1617,8 @@ static int bftw_opendir(struct bftw_state *state) { return -1; } + bftw_queue_rebalance(&state->dirq, false); + state->dir = bftw_file_opendir(state, file, state->path); if (!state->dir) { state->direrror = errno; @@ -1364,47 +1649,6 @@ static int bftw_readdir(struct bftw_state *state) { return ret; } -/** Check if a stat() call is needed for this visit. */ -static bool bftw_need_stat(const struct bftw_state *state) { - if (state->flags & BFTW_STAT) { - return true; - } - - const struct BFTW *ftwbuf = &state->ftwbuf; - if (ftwbuf->type == BFS_UNKNOWN) { - return true; - } - - if (ftwbuf->type == BFS_LNK && !(ftwbuf->stat_flags & BFS_STAT_NOFOLLOW)) { - return true; - } - - if (ftwbuf->type == BFS_DIR) { - if (state->flags & (BFTW_DETECT_CYCLES | BFTW_SKIP_MOUNTS | BFTW_PRUNE_MOUNTS)) { - return true; - } -#if __linux__ - } else if (state->mtab) { - // Linux fills in d_type from the underlying inode, even when - // the directory entry is a bind mount point. In that case, we - // need to stat() to get the correct type. We don't need to - // check for directories because they can only be mounted over - // by other directories. - if (bfs_might_be_mount(state->mtab, ftwbuf->path + ftwbuf->nameoff)) { - return true; - } -#endif - } - - return false; -} - -/** Initialize bftw_stat cache. */ -static void bftw_stat_init(struct bftw_stat *cache) { - cache->buf = NULL; - cache->error = 0; -} - /** Open a file if necessary. */ static int bftw_ensure_open(struct bftw_state *state, struct bftw_file *file, const char *path) { int ret = file->fd; @@ -1436,9 +1680,7 @@ static void bftw_init_ftwbuf(struct bftw_state *state, enum bftw_visit visit) { ftwbuf->error = state->direrror; ftwbuf->at_fd = AT_FDCWD; ftwbuf->at_path = ftwbuf->path; - ftwbuf->stat_flags = BFS_STAT_NOFOLLOW; - bftw_stat_init(&ftwbuf->lstat_cache); - bftw_stat_init(&ftwbuf->stat_cache); + bftw_stat_init(&ftwbuf->stat_bufs, &state->stat_buf, &state->lstat_buf); struct bftw_file *parent = NULL; if (de) { @@ -1451,6 +1693,7 @@ static void bftw_init_ftwbuf(struct bftw_state *state, enum bftw_visit visit) { ftwbuf->depth = file->depth; ftwbuf->type = file->type; ftwbuf->nameoff = file->nameoff; + bftw_stat_fill(&ftwbuf->stat_bufs, &file->stat_bufs); } if (parent) { @@ -1468,22 +1711,15 @@ static void bftw_init_ftwbuf(struct bftw_state *state, enum bftw_visit visit) { ftwbuf->nameoff = xbaseoff(ftwbuf->path); } + ftwbuf->stat_flags = bftw_stat_flags(state, ftwbuf->depth); + if (ftwbuf->error != 0) { ftwbuf->type = BFS_ERROR; return; } - int follow_flags = BFTW_FOLLOW_ALL; - if (ftwbuf->depth == 0) { - follow_flags |= BFTW_FOLLOW_ROOTS; - } - bool follow = state->flags & follow_flags; - if (follow) { - ftwbuf->stat_flags = BFS_STAT_TRYFOLLOW; - } - const struct bfs_stat *statbuf = NULL; - if (bftw_need_stat(state)) { + if (bftw_must_stat(state, ftwbuf->depth, ftwbuf->type, ftwbuf->path + ftwbuf->nameoff)) { statbuf = bftw_stat(ftwbuf, ftwbuf->stat_flags); if (statbuf) { ftwbuf->type = bfs_mode_to_type(statbuf->mode); @@ -1522,6 +1758,11 @@ static bool bftw_is_mount(struct bftw_state *state, const char *name) { return statbuf && statbuf->dev != parent->dev; } +/** Check if bfs_stat() was called from the main thread. */ +static bool bftw_stat_was_sync(const struct bftw_state *state, const struct bfs_stat *buf) { + return buf == &state->stat_buf || buf == &state->lstat_buf; +} + /** Invoke the callback. */ static enum bftw_action bftw_call_back(struct bftw_state *state, const char *name, enum bftw_visit visit) { if (visit == BFTW_POST && !(state->flags & BFTW_POST_ORDER)) { @@ -1541,31 +1782,43 @@ static enum bftw_action bftw_call_back(struct bftw_state *state, const char *nam return BFTW_STOP; } + enum bftw_action ret = BFTW_PRUNE; if ((state->flags & BFTW_SKIP_MOUNTS) && bftw_is_mount(state, name)) { - return BFTW_PRUNE; + goto done; } - enum bftw_action ret = state->callback(ftwbuf, state->ptr); + ret = state->callback(ftwbuf, state->ptr); switch (ret) { case BFTW_CONTINUE: - if (visit != BFTW_PRE) { - return BFTW_PRUNE; - } - if (ftwbuf->type != BFS_DIR) { - return BFTW_PRUNE; - } - if ((state->flags & BFTW_PRUNE_MOUNTS) && bftw_is_mount(state, name)) { - return BFTW_PRUNE; + if (visit != BFTW_PRE || ftwbuf->type != BFS_DIR) { + ret = BFTW_PRUNE; + } else if (state->flags & BFTW_PRUNE_MOUNTS) { + if (bftw_is_mount(state, name)) { + ret = BFTW_PRUNE; + } } - fallthru; + break; + case BFTW_PRUNE: case BFTW_STOP: - return ret; + break; default: state->error = EINVAL; return BFTW_STOP; } + +done: + if (state->fileq.flags & BFTW_QBALANCE) { + // Detect any main-thread stat() calls to rebalance the queue + const struct bfs_stat *buf = bftw_cached_stat(ftwbuf, BFS_STAT_FOLLOW); + const struct bfs_stat *lbuf = bftw_cached_stat(ftwbuf, BFS_STAT_NOFOLLOW); + if (bftw_stat_was_sync(state, buf) || bftw_stat_was_sync(state, lbuf)) { + bftw_queue_rebalance(&state->fileq, false); + } + } + + return ret; } /** @@ -1589,8 +1842,13 @@ static int bftw_gc(struct bftw_state *state, enum bftw_gc_flags flags) { int ret = 0; struct bftw_file *file = state->file; - if (file && state->dir) { - bftw_unpin_dir(state, file, true); + if (file) { + if (state->dir) { + bftw_cache_unpin(&state->cache, file); + } + if (file->dir) { + bftw_delayed_unwrap(state, file); + } } state->dir = NULL; state->de = NULL; @@ -1607,7 +1865,7 @@ static int bftw_gc(struct bftw_state *state, enum bftw_gc_flags flags) { } state->direrror = 0; - while ((file = SLIST_POP(&state->to_close))) { + while ((file = SLIST_POP(&state->to_close, ready))) { bftw_unwrapdir(state, file); } @@ -1685,6 +1943,7 @@ static void bftw_flush(struct bftw_state *state) { bftw_list_sort(&state->fileq.buffer); } bftw_queue_flush(&state->fileq); + bftw_stat_files(state); bftw_queue_flush(&state->dirq); bftw_ioq_opendirs(state); @@ -1704,22 +1963,46 @@ static int bftw_closedir(struct bftw_state *state) { static void bftw_save_ftwbuf(struct bftw_file *file, const struct BFTW *ftwbuf) { file->type = ftwbuf->type; - const struct bfs_stat *statbuf = ftwbuf->stat_cache.buf; - if (!statbuf || (ftwbuf->stat_flags & BFS_STAT_NOFOLLOW)) { - statbuf = ftwbuf->lstat_cache.buf; - } + const struct bfs_stat *statbuf = bftw_cached_stat(ftwbuf, ftwbuf->stat_flags); if (statbuf) { file->dev = statbuf->dev; file->ino = statbuf->ino; } } +/** Check if we should buffer a file instead of visiting it. */ +static bool bftw_buffer_file(const struct bftw_state *state, const struct bftw_file *file, const char *name) { + if (!name) { + // Already buffered + return false; + } + + if (state->flags & BFTW_BUFFER) { + return true; + } + + // If we need to call stat(), and can do it async, buffer this file + if (!state->ioq) { + return false; + } + + if (!bftw_queue_balanced(&state->fileq)) { + // stat() would run synchronously anyway + return false; + } + + size_t depth = file ? file->depth + 1 : 1; + enum bfs_type type = state->de ? state->de->type : BFS_UNKNOWN; + return bftw_must_stat(state, depth, type, name); +} + /** Visit and/or enqueue the current file. */ static int bftw_visit(struct bftw_state *state, const char *name) { + struct bftw_cache *cache = &state->cache; struct bftw_file *file = state->file; - if (name && (state->flags & BFTW_BUFFER)) { - file = bftw_file_new(&state->cache, file, name); + if (bftw_buffer_file(state, file, name)) { + file = bftw_file_new(cache, file, name); if (!file) { state->error = errno; return -1; @@ -1729,14 +2012,14 @@ static int bftw_visit(struct bftw_state *state, const char *name) { file->type = state->de->type; } - bftw_queue_push(&state->fileq, file); + bftw_push_file(state, file); return 0; } switch (bftw_call_back(state, name, BFTW_PRE)) { case BFTW_CONTINUE: if (name) { - file = bftw_file_new(&state->cache, state->file, name); + file = bftw_file_new(cache, state->file, name); } else { state->file = NULL; } @@ -1746,6 +2029,7 @@ static int bftw_visit(struct bftw_state *state, const char *name) { } bftw_save_ftwbuf(file, &state->ftwbuf); + bftw_stat_recycle(cache, file); bftw_push_dir(state, file); return 0; @@ -1757,10 +2041,22 @@ static int bftw_visit(struct bftw_state *state, const char *name) { } default: + if (file && !name) { + bftw_gc(state, BFTW_VISIT_NONE); + } return -1; } } +/** Drain a bftw_queue. */ +static void bftw_drain(struct bftw_state *state, struct bftw_queue *queue) { + bftw_queue_flush(queue); + + while (bftw_pop(state, queue)) { + bftw_gc(state, BFTW_VISIT_NONE); + } +} + /** * Dispose of the bftw() state. * @@ -1777,11 +2073,9 @@ static int bftw_state_destroy(struct bftw_state *state) { state->ioq = NULL; } - bftw_queue_flush(&state->dirq); - bftw_queue_flush(&state->fileq); - do { - bftw_gc(state, BFTW_VISIT_NONE); - } while (bftw_pop_dir(state) || bftw_pop_file(state)); + bftw_gc(state, BFTW_VISIT_NONE); + bftw_drain(state, &state->dirq); + bftw_drain(state, &state->fileq); ioq_destroy(ioq); @@ -1823,6 +2117,7 @@ static int bftw_impl(struct bftw_state *state) { if (bftw_visit(state, NULL) != 0) { return -1; } + bftw_flush(state); } return 0; diff --git a/src/bftw.h b/src/bftw.h index e325d14..2805361 100644 --- a/src/bftw.h +++ b/src/bftw.h @@ -26,12 +26,14 @@ enum bftw_visit { * Cached bfs_stat() info for a file. */ struct bftw_stat { - /** A pointer to the bfs_stat() buffer, if available. */ - const struct bfs_stat *buf; - /** Storage for the bfs_stat() buffer, if needed. */ - struct bfs_stat storage; - /** The cached error code, if any. */ - int error; + /** The bfs_stat(BFS_STAT_FOLLOW) buffer. */ + const struct bfs_stat *stat_buf; + /** The bfs_stat(BFS_STAT_NOFOLLOW) buffer. */ + const struct bfs_stat *lstat_buf; + /** The cached bfs_stat(BFS_STAT_FOLLOW) error. */ + int stat_err; + /** The cached bfs_stat(BFS_STAT_NOFOLLOW) error. */ + int lstat_err; }; /** @@ -62,10 +64,8 @@ struct BFTW { /** Flags for bfs_stat(). */ enum bfs_stat_flags stat_flags; - /** Cached bfs_stat() info for BFS_STAT_NOFOLLOW. */ - struct bftw_stat lstat_cache; - /** Cached bfs_stat() info for BFS_STAT_FOLLOW. */ - struct bftw_stat stat_cache; + /** Cached bfs_stat() info. */ + struct bftw_stat stat_bufs; }; /** diff --git a/src/eval.c b/src/eval.c index 6dc73d8..dfeaa1e 100644 --- a/src/eval.c +++ b/src/eval.c @@ -1234,7 +1234,7 @@ static bool eval_file_unique(struct bfs_eval *state, struct trie *seen) { /** * Log a stat() call. */ -static void debug_stat(const struct bfs_ctx *ctx, const struct BFTW *ftwbuf, const struct bftw_stat *cache, enum bfs_stat_flags flags) { +static void debug_stat(const struct bfs_ctx *ctx, const struct BFTW *ftwbuf, enum bfs_stat_flags flags, int err) { bfs_debug_prefix(ctx, DEBUG_STAT); fprintf(stderr, "bfs_stat("); @@ -1254,10 +1254,10 @@ static void debug_stat(const struct bfs_ctx *ctx, const struct BFTW *ftwbuf, con DEBUG_FLAG(flags, BFS_STAT_TRYFOLLOW); DEBUG_FLAG(flags, BFS_STAT_NOSYNC); - fprintf(stderr, ") == %d", cache->buf ? 0 : -1); + fprintf(stderr, ") == %d", err ? 0 : -1); - if (cache->error) { - fprintf(stderr, " [%d]", cache->error); + if (err) { + fprintf(stderr, " [%d]", err); } fprintf(stderr, "\n"); @@ -1271,14 +1271,14 @@ static void debug_stats(const struct bfs_ctx *ctx, const struct BFTW *ftwbuf) { return; } - const struct bfs_stat *statbuf = ftwbuf->stat_cache.buf; - if (statbuf || ftwbuf->stat_cache.error) { - debug_stat(ctx, ftwbuf, &ftwbuf->stat_cache, BFS_STAT_FOLLOW); + const struct bftw_stat *bufs = &ftwbuf->stat_bufs; + + if (bufs->stat_err >= 0) { + debug_stat(ctx, ftwbuf, BFS_STAT_FOLLOW, bufs->stat_err); } - const struct bfs_stat *lstatbuf = ftwbuf->lstat_cache.buf; - if ((lstatbuf && lstatbuf != statbuf) || ftwbuf->lstat_cache.error) { - debug_stat(ctx, ftwbuf, &ftwbuf->lstat_cache, BFS_STAT_NOFOLLOW); + if (bufs->lstat_err >= 0) { + debug_stat(ctx, ftwbuf, BFS_STAT_NOFOLLOW, bufs->lstat_err); } } diff --git a/tests/bfs/execdir_plus.sh b/tests/bfs/execdir_plus.sh index f66b898..6f24bdc 100644 --- a/tests/bfs/execdir_plus.sh +++ b/tests/bfs/execdir_plus.sh @@ -1,4 +1,4 @@ tree=$(invoke_bfs -D tree 2>&1 -quit) [[ "$tree" == *"-S dfs"* ]] && skip -bfs_diff basic -execdir "$TESTS/sort-args.sh" {} + +bfs_diff -j1 basic -execdir "$TESTS/sort-args.sh" {} + -- cgit v1.2.3 From 39ce13d45c5881d84e2de98243052811e3648224 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 28 Feb 2024 22:10:06 -0500 Subject: tests/bfs/D_opt: Don't rely on directory link counts Fixes: https://github.com/tavianator/bfs/issues/131 --- tests/bfs/D_opt.out | 11 ----------- tests/bfs/D_opt.sh | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) (limited to 'tests/bfs') diff --git a/tests/bfs/D_opt.out b/tests/bfs/D_opt.out index 3b461cf..6218a0c 100644 --- a/tests/bfs/D_opt.out +++ b/tests/bfs/D_opt.out @@ -1,18 +1,7 @@ basic/a basic/b -basic/c basic/c/d -basic/e basic/e/f -basic/g -basic/g/h -basic/i -basic/j basic/j/foo -basic/k -basic/k/foo basic/k/foo/bar -basic/l -basic/l/foo -basic/l/foo/bar basic/l/foo/bar/baz diff --git a/tests/bfs/D_opt.sh b/tests/bfs/D_opt.sh index d95cf86..c14fe70 100644 --- a/tests/bfs/D_opt.sh +++ b/tests/bfs/D_opt.sh @@ -1 +1 @@ -bfs_diff -D opt -nohidden -not \( -type b -o -type c \) -links -5 -links -10 -not -hidden basic +bfs_diff -D opt -nohidden -not \( -type c -o -type d \) -links -5 -links -10 -not -hidden basic -- cgit v1.2.3 From 912d2b94cf6ff0871c07325af5ed520a2bc97722 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 20 Mar 2024 10:44:34 -0400 Subject: Implement -limit N Closes: https://github.com/tavianator/bfs/issues/133 --- completions/bfs.bash | 3 +-- completions/bfs.fish | 1 + completions/bfs.zsh | 1 + docs/USAGE.md | 34 ++++++++++++++++++++++++++++++++++ docs/bfs.1 | 5 +++++ src/eval.c | 13 +++++++++++++ src/eval.h | 1 + src/opt.c | 1 + src/parse.c | 38 +++++++++++++++++++++++++++++++++++++- tests/bfs/limit.out | 4 ++++ tests/bfs/limit.sh | 1 + tests/bfs/limit_0.sh | 1 + tests/bfs/limit_implicit_print.sh | 1 + tests/bfs/limit_incomplete.sh | 1 + tests/bfs/limit_one.sh | 1 + 15 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 tests/bfs/limit.out create mode 100644 tests/bfs/limit.sh create mode 100644 tests/bfs/limit_0.sh create mode 100644 tests/bfs/limit_implicit_print.sh create mode 100644 tests/bfs/limit_incomplete.sh create mode 100644 tests/bfs/limit_one.sh (limited to 'tests/bfs') diff --git a/completions/bfs.bash b/completions/bfs.bash index 2f52e8d..db582da 100644 --- a/completions/bfs.bash +++ b/completions/bfs.bash @@ -37,6 +37,7 @@ _bfs() { -ipath -iregex -iwholename + -limit -links -lname -maxdepth @@ -94,8 +95,6 @@ _bfs() { -depth -follow -ignore_readdir_race - -maxdepth - -mindepth -mount -nocolor -noignore_readdir_race diff --git a/completions/bfs.fish b/completions/bfs.fish index 3f399e7..1303639 100644 --- a/completions/bfs.fish +++ b/completions/bfs.fish @@ -133,6 +133,7 @@ complete -c bfs -o fls -d "Like -ls, but write to specified file" -F complete -c bfs -o fprint -d "Like -print, but write to specified file" -F complete -c bfs -o fprint0 -d "Like -print0, but write to specified file" -F complete -c bfs -o fprintf -d "Like -printf, but write to specified file" -F +complete -c bfs -o limit -d "Limit the number of results" -x complete -c bfs -o ls -d "List files like ls -dils" complete -c bfs -o print -d "Print the path to the found file" complete -c bfs -o print0 -d "Like -print, but use the null character as a separator rather than newlines" diff --git a/completions/bfs.zsh b/completions/bfs.zsh index 3d7dc3a..51b5029 100644 --- a/completions/bfs.zsh +++ b/completions/bfs.zsh @@ -133,6 +133,7 @@ args=( '*-fprint0[print the path to the found file using null character as separator, but write to FILE instead of standard output]:output file:_files' '*-fprintf[print according to format string, but write to FILE instead of standard output]:output file:_files:output format' + '*-limit[quit after N results]:maximum result count' '*-ls[list files like ls -dils]' '*-print[print the path to the found file]' '*-print0[print the path to the found file using null character as separator]' diff --git a/docs/USAGE.md b/docs/USAGE.md index 3efdee0..071c95b 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -130,6 +130,40 @@ Unlike `-prune`, `-exclude` even works in combination with `-depth`/`-delete`. --- +### `-limit` + +The `-limit N` action makes `bfs` quit once it gets evaluated `N` times. +Placing it after an action like `-print` limits the number of results that get printed, for example: + +```console +$ bfs -s -type f -name '*.txt' +./1.txt +./2.txt +./3.txt +./4.txt +$ bfs -s -type f -name '*.txt' -print -limit 2 +./1.txt +./2.txt +``` + +This is similar to + +```console +$ bfs -s -type f -name '*.txt' | head -n2 +``` + +but more powerful because you can apply separate limits to different expressions: + +```console +$ bfs \( -name '*.txt' -print -limit 3 -o -name '*.log' -print -limit 4 \) -limit 5 +[At most 3 .txt files, at most 4 .log files, and at most 5 in total] +``` + +and more efficient because it will quit immediately. +When piping to `head`, `bfs` will only quit *after* it tries to output too many results. + +--- + ### `-hidden`/`-nohidden` `-hidden` matches "hidden" files (dotfiles). diff --git a/docs/bfs.1 b/docs/bfs.1 index 2ecb891..3a4f15a 100644 --- a/docs/bfs.1 +++ b/docs/bfs.1 @@ -725,6 +725,11 @@ but write to instead of standard output. .RE .TP +\fB\-limit \fIN\fR +Quit once this action is evaluated +.I N +times. +.TP .B \-ls List files like .B ls diff --git a/src/eval.c b/src/eval.c index 9e55964..2f06858 100644 --- a/src/eval.c +++ b/src/eval.c @@ -840,6 +840,19 @@ error: return true; } +/** + * -limit action. + */ +bool eval_limit(const struct bfs_expr *expr, struct bfs_eval *state) { + long long evals = expr->evaluations + 1; + if (evals >= expr->num) { + state->action = BFTW_STOP; + state->quit = true; + } + + return true; +} + /** * -prune action. */ diff --git a/src/eval.h b/src/eval.h index 98bbc08..f7f6c77 100644 --- a/src/eval.h +++ b/src/eval.h @@ -88,6 +88,7 @@ bool eval_fprint(const struct bfs_expr *expr, struct bfs_eval *state); bool eval_fprint0(const struct bfs_expr *expr, struct bfs_eval *state); bool eval_fprintf(const struct bfs_expr *expr, struct bfs_eval *state); bool eval_fprintx(const struct bfs_expr *expr, struct bfs_eval *state); +bool eval_limit(const struct bfs_expr *expr, struct bfs_eval *state); bool eval_prune(const struct bfs_expr *expr, struct bfs_eval *state); bool eval_quit(const struct bfs_expr *expr, struct bfs_eval *state); diff --git a/src/opt.c b/src/opt.c index 76965de..a470d25 100644 --- a/src/opt.c +++ b/src/opt.c @@ -1181,6 +1181,7 @@ static struct bfs_expr *annotate_visit(struct bfs_opt *opt, struct bfs_expr *exp eval_fprint0, eval_fprintf, eval_fprintx, + eval_limit, eval_prune, eval_true, // Non-returning diff --git a/src/parse.c b/src/parse.c index 3b7386d..2dfcab2 100644 --- a/src/parse.c +++ b/src/parse.c @@ -97,6 +97,8 @@ struct bfs_parser { char **last_arg; /** A "-depth"-type argument, if any. */ char **depth_arg; + /** A "-limit" argument, if any. */ + char **limit_arg; /** A "-prune" argument, if any. */ char **prune_arg; /** A "-mount" argument, if any. */ @@ -733,7 +735,7 @@ static struct bfs_expr *parse_action(struct bfs_parser *parser, bfs_eval_fn *eva return NULL; } - if (eval_fn != eval_prune && eval_fn != eval_quit) { + if (eval_fn != eval_limit && eval_fn != eval_prune && eval_fn != eval_quit) { parser->implicit_print = false; } @@ -1569,6 +1571,29 @@ static struct bfs_expr *parse_jobs(struct bfs_parser *parser, int arg1, int arg2 return expr; } +/** + * Parse -limit N. + */ +static struct bfs_expr *parse_limit(struct bfs_parser *parser, int arg1, int arg2) { + struct bfs_expr *expr = parse_unary_action(parser, eval_limit); + if (!expr) { + return NULL; + } + + char **arg = &expr->argv[1]; + if (!parse_int(parser, arg, *arg, &expr->num, IF_LONG_LONG)) { + return NULL; + } + + if (expr->num <= 0) { + parse_expr_error(parser, expr, "The ${blu}%s${rs} must be at least ${bld}1${rs}.\n", expr->argv[0]); + return NULL; + } + + parser->limit_arg = expr->argv; + return expr; +} + /** * Parse -links N. */ @@ -2845,6 +2870,8 @@ static struct bfs_expr *parse_help(struct bfs_parser *parser, int arg1, int arg2 cfprintf(cout, " ${blu}-fprintf${rs} ${bld}FILE${rs} ${bld}FORMAT${rs}\n"); cfprintf(cout, " Like ${blu}-ls${rs}/${blu}-print${rs}/${blu}-print0${rs}/${blu}-printf${rs}, but write to ${bld}FILE${rs} instead of standard\n" " output\n"); + cfprintf(cout, " ${blu}-limit${rs} ${bld}N${rs}\n"); + cfprintf(cout, " Quit after this action is evaluated ${bld}N${rs} times\n"); cfprintf(cout, " ${blu}-ls${rs}\n"); cfprintf(cout, " List files like ${ex}ls${rs} ${bld}-dils${rs}\n"); cfprintf(cout, " ${blu}-print${rs}\n"); @@ -2968,6 +2995,7 @@ static const struct table_entry parse_table[] = { {"-iregex", T_TEST, parse_regex, BFS_REGEX_ICASE}, {"-iwholename", T_TEST, parse_path, true}, {"-j", T_FLAG, parse_jobs, 0, 0, true}, + {"-limit", T_ACTION, parse_limit}, {"-links", T_TEST, parse_links}, {"-lname", T_TEST, parse_lname, false}, {"-ls", T_ACTION, parse_ls}, @@ -3330,6 +3358,14 @@ static struct bfs_expr *parse_whole_expr(struct bfs_parser *parser) { } if (parser->implicit_print) { + char **limit = parser->limit_arg; + if (limit) { + parse_argv_error(parser, parser->limit_arg, 2, + "With ${blu}%s${rs}, you must specify an action explicitly; for example, ${blu}-print${rs} ${blu}%s${rs} ${bld}%s${rs}.\n", + limit[0], limit[0], limit[1]); + return NULL; + } + struct bfs_expr *print = parse_new_expr(parser, eval_fprint, 1, &fake_print_arg); if (!print) { return NULL; diff --git a/tests/bfs/limit.out b/tests/bfs/limit.out new file mode 100644 index 0000000..ea94276 --- /dev/null +++ b/tests/bfs/limit.out @@ -0,0 +1,4 @@ +basic/a +basic/b +basic/c/d +basic/e/f diff --git a/tests/bfs/limit.sh b/tests/bfs/limit.sh new file mode 100644 index 0000000..84b605f --- /dev/null +++ b/tests/bfs/limit.sh @@ -0,0 +1 @@ +bfs_diff -s basic -type f -print -limit 4 diff --git a/tests/bfs/limit_0.sh b/tests/bfs/limit_0.sh new file mode 100644 index 0000000..3ce26de --- /dev/null +++ b/tests/bfs/limit_0.sh @@ -0,0 +1 @@ +! invoke_bfs basic -print -limit 0 diff --git a/tests/bfs/limit_implicit_print.sh b/tests/bfs/limit_implicit_print.sh new file mode 100644 index 0000000..cdb059d --- /dev/null +++ b/tests/bfs/limit_implicit_print.sh @@ -0,0 +1 @@ +! invoke_bfs basic -type f -limit 1 diff --git a/tests/bfs/limit_incomplete.sh b/tests/bfs/limit_incomplete.sh new file mode 100644 index 0000000..2d1e842 --- /dev/null +++ b/tests/bfs/limit_incomplete.sh @@ -0,0 +1 @@ +! invoke_bfs basic -print -limit diff --git a/tests/bfs/limit_one.sh b/tests/bfs/limit_one.sh new file mode 100644 index 0000000..3f8181c --- /dev/null +++ b/tests/bfs/limit_one.sh @@ -0,0 +1 @@ +! invoke_bfs basic -print -limit one -- cgit v1.2.3