add _setPath_ option to fail if dir not found

This commit is contained in:
Nathaniel Landau
2021-11-11 23:52:42 -05:00
parent 395e8cef6b
commit bbe2a9e0d4
3 changed files with 59 additions and 2 deletions

View File

@@ -390,12 +390,34 @@ _setPATH_() {
# Add directories to $PATH so script can find executables
# ARGS:
# $@ - One or more paths
# OUTS: Adds items to $PATH
# OPTS:
# -x - Fail if directories are not found
# OUTS:
# 0: Success
# 1: Failure
# Adds items to $PATH
# USAGE:
# _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local opt
local OPTIND=1
local _failIfNotFound=false
while getopts ":xX" opt; do
case ${opt} in
x | X) _failIfNotFound=true ;;
*)
{
error "Unrecognized option '${1}' passed to _backupFile_" "${LINENO}"
return 1
}
;;
esac
done
shift $((OPTIND - 1))
local _newPath
for _newPath in "$@"; do
@@ -411,6 +433,9 @@ _setPATH_() {
fi
else
debug "_setPATH_: can not find: ${_newPath}"
if [[ ${_failIfNotFound} == true ]]; then
return 1
fi
continue
fi
done

View File

@@ -74,6 +74,13 @@ teardown() {
assert_success
}
@test "_setPATH_: fail on dir not found" {
mkdir -p "${TESTDIR}/testing/from/bats"
mkdir -p "${TESTDIR}/testing/from/bats_again"
run _setPATH_ -x "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/again" "${TESTDIR}/testing/from/bats_again"
assert_failure
}
@test "_setPATH_: success" {
mkdir -p "${TESTDIR}/testing/from/bats"
mkdir -p "${TESTDIR}/testing/from/bats_again"

View File

@@ -93,12 +93,34 @@ _setPATH_() {
# Add directories to $PATH so script can find executables
# ARGS:
# $@ - One or more paths
# OUTS: Adds items to $PATH
# OPTS:
# -x - Fail if directories are not found
# OUTS:
# 0: Success
# 1: Failure
# Adds items to $PATH
# USAGE:
# _setPATH_ "/usr/local/bin" "${HOME}/bin" "$(npm bin)"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local opt
local OPTIND=1
local _failIfNotFound=false
while getopts ":xX" opt; do
case ${opt} in
x | X) _failIfNotFound=true ;;
*)
{
error "Unrecognized option '${1}' passed to _backupFile_" "${LINENO}"
return 1
}
;;
esac
done
shift $((OPTIND - 1))
local _newPath
for _newPath in "$@"; do
@@ -114,6 +136,9 @@ _setPATH_() {
fi
else
debug "_setPATH_: can not find: ${_newPath}"
if [[ ${_failIfNotFound} == true ]]; then
return 1
fi
continue
fi
done