minor changes

This commit is contained in:
Nathaniel Landau
2021-10-25 17:54:28 -04:00
parent a753723ce8
commit d984b7016c
8 changed files with 202 additions and 205 deletions

View File

@@ -13,7 +13,6 @@ _mainScript_() {
input "This is input text" input "This is input text"
} }
#/_mainsScript_() #/_mainsScript_()
# ################################## Flags and defaults # ################################## Flags and defaults

View File

@@ -14,7 +14,6 @@ _mainScript_() {
input "This is input text" input "This is input text"
} }
# end _mainScript_ # end _mainScript_
# ################################## Flags and defaults # ################################## Flags and defaults
@@ -33,8 +32,6 @@ declare -a ARGS=()
# ################################## Functions required for this template to work # ################################## Functions required for this template to work
# Functions for providing alerts to the user and printing them to the log
_setColors_() { _setColors_() {
# DESC: # DESC:
# Sets colors use for alerts. # Sets colors use for alerts.
@@ -401,7 +398,7 @@ _setPATH_() {
fi fi
else else
debug "_setPATH_: can not find: ${_newPath}" debug "_setPATH_: can not find: ${_newPath}"
return 1 return 0
fi fi
done done
return 0 return 0

View File

@@ -1,5 +1,4 @@
#!/usr/bin/env bats #!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load' load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load' load 'test_helper/bats-file/load'
@@ -7,25 +6,16 @@ load 'test_helper/bats-assert/load'
######## SETUP TESTS ######## ######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)" ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/template_utils.bash" s="${ROOTDIR}/template.sh"
ALERTS="${ROOTDIR}/utilities/alerts.bash"
if test -f "${SOURCEFILE}" >&2; then if [ -f "${s}" ]; then
source "${SOURCEFILE}" base="$(basename "${s}")"
else else
echo "Sourcefile not found: ${SOURCEFILE}" >&2 printf "No executable '${s}' found.\n" >&2
printf "Can not run tests.\n" >&2 printf "Can not run tests.\n" >&2
exit 1 exit 1
fi fi
if test -f "${ALERTS}" >&2; then
source "${ALERTS}"
_setColors_ #Set color constants
else
echo "Sourcefile not found: ${ALERTS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() { setup() {
@@ -35,59 +25,104 @@ setup() {
BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}" BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}"
BATSLIB_FILE_PATH_ADD='<temp>' BATSLIB_FILE_PATH_ADD='<temp>'
s="$s --logfile=${TESTDIR}/logs/log.txt" # Logs go to temp directory
pushd "${TESTDIR}" >&2 pushd "${TESTDIR}" >&2
######## DEFAULT FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
set -o errtrace
set -o nounset
set -o pipefail
} }
teardown() { teardown() {
set +o nounset
set +o errtrace
set +o pipefail
popd >&2 popd >&2
temp_del "${TESTDIR}" temp_del "${TESTDIR}"
} }
######## RUN TESTS ########
@test "Sanity..." {
run true
######## RUN TESTS ##########
@test "sanity" {
run true
assert_success
assert [ "$output" = "" ]
}
@test "Fail - fail on bad args and create logfile" {
run $s -K
assert_failure
assert_output --partial "[ fatal] invalid option: -K"
assert_file_exist "${TESTDIR}/logs/log.txt"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ fatal\] .* invalid option: -K \(.*"
}
@test "success" {
run $s
assert_success
assert_output --partial "[ info] This is info text"
assert_output --partial "[ notice] This is notice text"
assert_output --partial "[ dryrun] This is dryrun text"
assert_output --partial "[warning] This is warning text"
assert_output --partial "[ error] This is error text"
assert_output --partial "[success] This is success text"
assert_output --partial "[ input] This is input text"
assert_file_exist "${TESTDIR}/logs/log.txt"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ error\] \[.*\] This is error text \( _mainScript_:${base}.* \)"
assert_line --index 1 ""
}
@test "success and INFO level log" {
run $s --loglevel=INFO
assert_success
assert_output --partial "[ info] This is info text"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ info\].*This is info text"
assert_line --index 1 --regexp "\[ notice\].*This is notice text"
assert_line --index 2 --regexp "\[warning\].*This is warning text"
assert_line --index 3 --regexp "\[ error\].*This is error text"
assert_line --index 4 --regexp "\[success\].*This is success text"
assert_line --index 5 ""
}
@test "success and NOTICE level log" {
run $s --loglevel=NOTICE
assert_success
assert_output --partial "[ info] This is info text"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ notice\].*This is notice text"
assert_line --index 1 --regexp "\[warning\].*This is warning text"
assert_line --index 2 --regexp "\[ error\].*This is error text"
assert_line --index 3 --regexp "\[success\].*This is success text"
assert_line --index 4 ""
}
@test "Usage (-h)" {
run $s -h
assert_success
assert_line --partial --index 0 "$base [OPTION]... [FILE]..."
}
@test "Usage (--help)" {
run $s --help
assert_success
assert_line --partial --index 0 "$base [OPTION]... [FILE]..."
}
@test "quiet (-q)" {
run $s -q --loglevel=INFO
assert_success assert_success
assert_output "" assert_output ""
}
@test "_setPATH_: fail on dir not found" { run cat "${TESTDIR}/logs/log.txt"
mkdir -p "${TESTDIR}/testing/from/bats" run cat "${TESTDIR}/logs/log.txt"
mkdir -p "${TESTDIR}/testing/from/bats_again" assert_line --index 0 --regexp "\[ info\].*This is info text"
run _setPATH_ "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/again" "${TESTDIR}/testing/from/bats_again" assert_line --index 1 --regexp "\[ notice\].*This is notice text"
assert_failure assert_line --index 2 --regexp "\[warning\].*This is warning text"
} assert_line --index 3 --regexp "\[ error\].*This is error text"
assert_line --index 4 --regexp "\[success\].*This is success text"
@test "_setPATH_: success" { assert_line --index 5 ""
mkdir -p "${TESTDIR}/testing/from/bats"
mkdir -p "${TESTDIR}/testing/from/bats_again"
_setPATH_ "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/from/bats_again"
run echo "${PATH}"
assert_output --regexp "/testing/from/bats"
refute_output --regexp "/testing/again"
assert_output --regexp "/testing/from/bats_again"
}
@test "_makeTempDir_" {
VERBOSE=true
run _makeTempDir_
assert_success
assert_output --regexp "\\\$TMP_DIR=/.*\.[0-9]+\.[0-9]+\.[0-9]+$"
} }

View File

@@ -1,128 +0,0 @@
#!/usr/bin/env bats
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-assert/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
s="${ROOTDIR}/template_source_utils.sh"
if [ -f "${s}" ]; then
base="$(basename "${s}")"
else
printf "No executable '${s}' found.\n" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() {
TESTDIR="$(temp_make)"
curPath="${PWD}"
BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}"
BATSLIB_FILE_PATH_ADD='<temp>'
s="$s --logfile=${TESTDIR}/logs/log.txt" # Logs go to temp directory
pushd "${TESTDIR}" >&2
}
teardown() {
popd >&2
temp_del "${TESTDIR}"
}
######## RUN TESTS ##########
@test "sanity" {
run true
assert_success
assert [ "$output" = "" ]
}
@test "Fail - fail on bad args and create logfile" {
run $s -K
assert_failure
assert_output --partial "[ fatal] invalid option: -K"
assert_file_exist "${TESTDIR}/logs/log.txt"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ fatal\] .* invalid option: -K \(.*"
}
@test "success" {
run $s
assert_success
assert_output --partial "[ info] This is info text"
assert_output --partial "[ notice] This is notice text"
assert_output --partial "[ dryrun] This is dryrun text"
assert_output --partial "[warning] This is warning text"
assert_output --partial "[ error] This is error text"
assert_output --partial "[success] This is success text"
assert_output --partial "[ input] This is input text"
assert_file_exist "${TESTDIR}/logs/log.txt"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ error\] \[.*\] This is error text \( _mainScript_:${base}.* \)"
assert_line --index 1 ""
}
@test "success and INFO level log" {
run $s --loglevel=INFO
assert_success
assert_output --partial "[ info] This is info text"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ info\].*This is info text"
assert_line --index 1 --regexp "\[ notice\].*This is notice text"
assert_line --index 2 --regexp "\[warning\].*This is warning text"
assert_line --index 3 --regexp "\[ error\].*This is error text"
assert_line --index 4 --regexp "\[success\].*This is success text"
assert_line --index 5 ""
}
@test "success and NOTICE level log" {
run $s --loglevel=NOTICE
assert_success
assert_output --partial "[ info] This is info text"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ notice\].*This is notice text"
assert_line --index 1 --regexp "\[warning\].*This is warning text"
assert_line --index 2 --regexp "\[ error\].*This is error text"
assert_line --index 3 --regexp "\[success\].*This is success text"
assert_line --index 4 ""
}
@test "Usage (-h)" {
run $s -h
assert_success
assert_line --partial --index 0 "$base [OPTION]... [FILE]..."
}
@test "Usage (--help)" {
run $s --help
assert_success
assert_line --partial --index 0 "$base [OPTION]... [FILE]..."
}
@test "quiet (-q)" {
run $s -q --loglevel=INFO
assert_success
assert_output ""
run cat "${TESTDIR}/logs/log.txt"
run cat "${TESTDIR}/logs/log.txt"
assert_line --index 0 --regexp "\[ info\].*This is info text"
assert_line --index 1 --regexp "\[ notice\].*This is notice text"
assert_line --index 2 --regexp "\[warning\].*This is warning text"
assert_line --index 3 --regexp "\[ error\].*This is error text"
assert_line --index 4 --regexp "\[success\].*This is success text"
assert_line --index 5 ""
}

93
test/template_utils.bats Executable file
View File

@@ -0,0 +1,93 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-assert/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/template_utils.bash"
ALERTS="${ROOTDIR}/utilities/alerts.bash"
if test -f "${SOURCEFILE}" >&2; then
source "${SOURCEFILE}"
else
echo "Sourcefile not found: ${SOURCEFILE}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
if test -f "${ALERTS}" >&2; then
source "${ALERTS}"
_setColors_ #Set color constants
else
echo "Sourcefile not found: ${ALERTS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() {
TESTDIR="$(temp_make)"
curPath="${PWD}"
BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}"
BATSLIB_FILE_PATH_ADD='<temp>'
pushd "${TESTDIR}" >&2
######## DEFAULT FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
set -o errtrace
set -o nounset
set -o pipefail
}
teardown() {
set +o nounset
set +o errtrace
set +o pipefail
popd >&2
temp_del "${TESTDIR}"
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_setPATH_: succeed on dir not found" {
mkdir -p "${TESTDIR}/testing/from/bats"
mkdir -p "${TESTDIR}/testing/from/bats_again"
run _setPATH_ "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/again" "${TESTDIR}/testing/from/bats_again"
assert_success
}
@test "_setPATH_: success" {
mkdir -p "${TESTDIR}/testing/from/bats"
mkdir -p "${TESTDIR}/testing/from/bats_again"
_setPATH_ "${TESTDIR}/testing/from/bats" "${TESTDIR}/testing/from/bats_again"
run echo "${PATH}"
assert_output --regexp "/testing/from/bats"
refute_output --regexp "/testing/again"
assert_output --regexp "/testing/from/bats_again"
}
@test "_makeTempDir_" {
VERBOSE=true
run _makeTempDir_
assert_success
assert_output --regexp "\\\$TMP_DIR=/.*\.[0-9]+\.[0-9]+\.[0-9]+$"
}

View File

@@ -318,8 +318,10 @@ _clearLine_() {
[ ! "$(declare -f "_isTerminal_")" ] && fatal "${FUNCNAME[0]} needs function _isTerminal_" [ ! "$(declare -f "_isTerminal_")" ] && fatal "${FUNCNAME[0]} needs function _isTerminal_"
local i
if _isTerminal_; then if _isTerminal_; then
local _line=${1:-1} for ((i = 0; i < $1; i++)); do
printf "\033[%sA\033[2K" "${_line}" printf "\033[A\033[2K"
done
fi fi
} }

View File

@@ -278,26 +278,25 @@ _rootAvailable_() {
if [[ ${EUID} -eq 0 ]]; then if [[ ${EUID} -eq 0 ]]; then
_superuser=true _superuser=true
elif [[ -z ${1:-} ]]; then elif [[ -z ${1:-} ]]; then
if command -v sudo >/dev/null 2>&1; then
debug 'Sudo: Updating cached credentials ...' debug 'Sudo: Updating cached credentials ...'
if ! sudo -v; then if sudo -v; then
warning "Sudo: Couldn't acquire credentials ..." if [[ $(sudo -H -- "$BASH" -c 'printf "%s" "$EUID"') -eq 0 ]]; then
else
_testEUID="$(sudo -H -- "$BASH" -c 'printf "%s" "$EUID"')"
if [[ ${_testEUID} -eq 0 ]]; then
_superuser=true _superuser=true
else
_superuser=false
fi fi
fi else
_superuser=false
fi fi
fi fi
if [[ -z ${superuser:-} ]]; then if [[ ${_superuser} == true ]]; then
debug 'Successfully acquired superuser credentials.'
return 0
else
debug 'Unable to acquire superuser credentials.' debug 'Unable to acquire superuser credentials.'
return 1 return 1
fi fi
debug 'Successfully acquired superuser credentials.'
return 0
} }
_varIsTrue_() { _varIsTrue_() {

View File

@@ -112,7 +112,7 @@ _setPATH_() {
fi fi
else else
debug "_setPATH_: can not find: ${_newPath}" debug "_setPATH_: can not find: ${_newPath}"
return 1 return 0
fi fi
done done
return 0 return 0