Fixing bug in try

This commit is contained in:
Sławomir Śledź 2018-03-10 23:23:23 +01:00
parent 6de7798c8a
commit c3425e0933
2 changed files with 4 additions and 2 deletions

View file

@ -173,12 +173,12 @@ catch() {
local val=$(2>&1 eval "$cmd"; echo $?)
local cnt=$(list $val | wc -l)
local status=$(list $val | last)
list "$cmd" $status $(list $val | take $((cnt - 1)) | unlist | tup) | $f
$f < <(list "$cmd" $status $(list $val | take $((cnt - 1)) | unlist | tup))
}
try() {
local f="$@"
catch lambda cmd status val . '[[ $status -eq 0 ]] && tupl $val || list $status | '$f
catch lambda cmd status val . '[[ $status -eq 0 ]] && tupx 1- $val | unlist || { '"$f"' < <(list $status); }'
}
ret() {

View file

@ -4,6 +4,8 @@ testTry() {
assertEquals 1 "$(echo 'expr 2 / 2' | try lambda _ . 'ret 0')"
assertEquals 0 "$(echo 'expr 2 / 0' | try lambda _ . 'ret 0')"
assertEquals 2 "$(echo 'expr 2 / 0' | try lambda status . 'ret $status')"
assertEquals 'already up to date' "$(echo 'echo already up to date' | try lambda _ . 'ret error')"
assertEquals 'error exit 1' "$(try λ _ . 'echo "error"; echo exit 1' < <(echo fgit pull) | unlist)"
}
. ./shunit2-init.sh