commit:     6761163b5c8667a1a111683f5bf72e5dff4e604e
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu May 16 12:55:08 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri May 17 01:40:20 2024 +0000
URL:        
https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=6761163b

Add a die() function

For now, it will only be defined if no utility, function or alias
already exists by that name. Like the exit builtin, it is able to
preserve the value of $? but will never exit with a status of 0. For
example, the following will preserve the exit status of failing_command.

failing_command || die "gadzooks"

Whereas the following will ignore the value of $? - being that it is 0 -
and instead exit with a status of 1.

if prevailing_command; then
        die "fiddlesticks"
fi

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Bug: https://bugs.gentoo.org/878505

 functions.sh   | 20 ++++++++++++++++++++
 test-functions | 39 +++++++++++++++++++++++++--------------
 2 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/functions.sh b/functions.sh
index e494765..b0170f9 100644
--- a/functions.sh
+++ b/functions.sh
@@ -30,6 +30,26 @@ chdir()
        CDPATH= cd -- "$@"
 }
 
+#
+# Prints a diagnostic message prefixed with the basename of the running script
+# before exiting. It shall preserve the value of $? as it was at the time of
+# invocation unless its value was 0, in which case the exit status shall be 1.
+#
+if ! command -v die >/dev/null; then
+       die()
+       {
+               case $? in
+                       0)
+                               genfun_status=1
+                               ;;
+                       *)
+                               genfun_status=$?
+               esac
+               printf '%s: %s\n' "${0##*/}" "$*" >&2
+               exit "${genfun_status}"
+       }
+fi
+
 #
 # Prints a message indicating the onset of a given process, provided that
 # EINFO_QUIET is false. It is expected that eend eventually be called, so as to

diff --git a/test-functions b/test-functions
index a2d15b4..e087a3c 100755
--- a/test-functions
+++ b/test-functions
@@ -68,6 +68,28 @@ test_chdir_noop() {
        iterate_tests 2 "$@"
 }
 
+test_die() {
+       set -- \
+               1     0 \
+               2     2 \
+               126 126 \
+               255 255
+
+       callback() {
+               test_description="( exit "$2" ); die"
+               ( exit "$2" )
+               stderr=$(die "$2" 2>&1)
+               retval=$?
+               if [ "${stderr}" = "test-functions: $2" ]; then
+                       return "${retval}"
+               else
+                       return 1
+               fi
+       }
+
+       iterate_tests 2 "$@"
+}
+
 test_ebegin() {
        _eprint() {
                shift
@@ -380,24 +402,12 @@ iterate_tests() {
                        fi
                done
                eval "${code}"
-               case $? in
-                       0)
-                               test "$?" -eq "$1"
-                               ;;
-                       *)
-                               test "$?" -ge "$1"
-               esac
-               if [ "$?" -eq 0 ]; then
+               if [ "$?" -eq "$1" ]; then
                        passed=$((passed + 1))
                else
                        printf 'not '
                fi
-               if [ "$1" -eq 0 ]; then
-                       expected=$1
-               else
-                       expected=">=$1"
-               fi
-               printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" 
"${test_description}" "${expected}"
+               printf 'ok %d - %s (expecting %s)\n' "$((testnum += 1))" 
"${test_description}" "$1"
                shift "${slice_width}"
        done
        return "$(( passed < total ))"
@@ -445,6 +455,7 @@ test_is_identifier || rc=1
 test_is_int || rc=1
 test_is_visible || rc=1
 test_yesno || rc=1
+test_die || rc=1
 
 cleanup_tmpdir
 

Reply via email to