From 13729f370881557f8f7c21692732d093ce5b06cd Mon Sep 17 00:00:00 2001 From: Kerin Millar Date: Tue, 6 Aug 2024 05:58:03 +0100 Subject: Make _select_by_mtime() work correctly for paths read from STDIN The _select_by_mtime() function is called by both newest() and oldest(). Pathnames may be specified as positional parameters or as NUL-separated records to be read from the standard input. Unfortunately, the latter interface does not work at all. Rectify this by checking whether the number of parameters is greater then 0, rather than greater than or equal to 0. Also, extend the existing test case in such a way that the interface in question is tested. Signed-off-by: Kerin Millar Signed-off-by: Sam James --- functions.sh | 2 +- test-functions | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/functions.sh b/functions.sh index 561a98f..177dad8 100644 --- a/functions.sh +++ b/functions.sh @@ -680,7 +680,7 @@ _select_by_mtime() sort_opt=$1 shift - if [ "$#" -ge 0 ]; then + if [ "$#" -gt 0 ]; then printf '%s\0' "$@" else cat diff --git a/test-functions b/test-functions index 940f07d..989cc4c 100755 --- a/test-functions +++ b/test-functions @@ -445,8 +445,6 @@ test_newest() { eq 0 non-existent older/file \ ge 1 older/file newer/file - row=0 - callback() { shift test_description="newest $(quote_args "$@")" @@ -461,6 +459,24 @@ test_newest() { fi } + row=0 + iterate_tests 4 "$@" + + callback() { + shift + test_description="printf '%s\\0' $(quote_args "$@") | newest" + row=$(( row + 1 )) + printf '%s\0' "$@" | + if [ "${row}" -le 2 ]; then + newest + elif [ "${row}" -le 8 ]; then + test "$(newest)" = "newer/file" + else + test "$(newest)" = "older/file" + fi + } + + row=0 iterate_tests 4 "$@" } -- cgit v1.2.3-65-gdbad