add tests

This commit is contained in:
Nathaniel Landau
2021-07-21 07:17:31 -04:00
parent 37f34a4287
commit cda0de5f1d
8 changed files with 1141 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
# Shell Scripting Templates and Utilities
A collection of shell scripting utilities and templates used to ease the creation of BASH scripts.
A collection of shell scripting utilities and templates used to ease the creation of BASH scripts. [BATS](https://github.com/bats-core/bats-core) provides unit testing capabilities. All tests are in the `tests/` repo.
## Bash Script Template Usage
To create a new script, copy `scriptTemplate.sh` to a new file and make it executable `chmod 755 [newscript].sh`. Place your custom script logic within the `_mainScript_` function at the top of the script.

231
test/alerts.bats Executable file
View File

@@ -0,0 +1,231 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-asser/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
ALERTS="${ROOTDIR}/utilities/alerts.bash"
if test -f "${ALERTS}" >&2; then
source "${ALERTS}"
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
######## DEFAUL FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
}
teardown() {
popd >&2
temp_del "${TESTDIR}"
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_alert_: success" {
run success "testing"
assert_output --regexp "\[success\] testing"
}
@test "_alert_: quiet" {
QUIET=true
run notice "testing"
assert_success
refute_output --partial "testing"
}
@test "_alert_: verbose" {
run verbose "testing"
refute_output --regexp "\[ debug\] testing"
VERBOSE=true
run verbose "testing"
assert_output --regexp "\[ debug\] testing"
}
@test "_alert_: warning" {
run warning "testing"
assert_output --regexp "\[warning\] testing"
}
@test "_alert_: notice" {
run notice "testing"
assert_output --regexp "\[ notice\] testing"
}
@test "_alert_: notice: with LINE" {
run notice "testing" "$LINENO"
assert_output --regexp ".*\[ notice\] testing \(line: [0-9]{1,3}\)"
}
@test "_alert_: refute debug" {
run debug "testing"
refute_output --partial "[ debug] testing"
}
@test "_alert_: assert debug" {
VERBOSE=true
run debug "testing"
assert_output --partial "[ debug] testing"
}
@test "_alert_: header" {
run header "testing"
assert_output --regexp "\[ header\] == testing =="
}
@test "_alert_: info" {
run info "testing"
assert_output --regexp "[0-9]+:[0-9]+:[0-9]+ (AM|PM) \[ info\] testing"
}
@test "_alert_: fatal: with LINE" {
run fatal "testing" "$LINENO"
assert_line --index 0 --regexp ".*\[ fatal\] testing \(line: [0-9]{1,3}\) \( run:.*\)"
}
@test "_alert_: error" {
run error "testing"
assert_output --regexp ".*\[ error\] testing \( run:.*\)"
}
@test "_alert_: input" {
run input "testing"
assert_output --partial "[ input] testing"
}
@test "_alert_: logging FATAL" {
LOGLEVEL=FATAL
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
run fatal "testing fatal"
assert_file_exist "${LOGFILE}"
run cat "${LOGFILE}"
assert_line --index 0 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ fatal\] \[.*\] testing fatal \("
assert_line --index 1 ""
assert_line --index 2 ""
assert_line --index 3 ""
assert_line --index 4 ""
assert_line --index 5 ""
}
@test "_alert_: logging ERROR" {
LOGLEVEL=ERROR
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
assert_file_exist "${LOGFILE}"
run cat "${LOGFILE}"
assert_line --index 0 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error"
assert_line --index 1 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error 2"
assert_line --index 2 ""
assert_line --index 3 ""
assert_line --index 4 ""
assert_line --index 5 ""
}
@test "_alert_: logging WARN" {
LOGLEVEL=WARN
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
assert_file_exist "${LOGFILE}"
run cat "${LOGFILE}"
assert_line --index 0 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error"
assert_line --index 1 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error 2"
assert_line --index 2 --regexp "[0-9]+:[0-9]+:[0-9]+ \[warning\] \[.*\] testing warning"
assert_line --index 3 ""
assert_line --index 4 ""
assert_line --index 5 ""
}
@test "_alert_: logging INFO" {
LOGLEVEL=INFO
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
assert_file_exist "${LOGFILE}"
run cat "${LOGFILE}"
assert_line --index 0 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error"
assert_line --index 1 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error 2"
assert_line --index 2 --regexp "[0-9]+:[0-9]+:[0-9]+ \[warning\] \[.*\] testing warning"
assert_line --index 3 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ notice\] \[.*\] testing notice"
assert_line --index 4 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ info\] \[.*\] testing info"
assert_line --index 5 ""
}
@test "_alert_: logging DEBUG" {
LOGLEVEL=DEBUG
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
assert_file_exist "${LOGFILE}"
run cat "${LOGFILE}"
assert_line --index 0 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error"
assert_line --index 1 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ error\] \[.*\] testing error 2"
assert_line --index 2 --regexp "[0-9]+:[0-9]+:[0-9]+ \[warning\] \[.*\] testing warning"
assert_line --index 3 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ notice\] \[.*\] testing notice"
assert_line --index 4 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ info\] \[.*\] testing info"
assert_line --index 5 --regexp "[0-9]+:[0-9]+:[0-9]+ \[ debug\] \[.*\] testing debug"
}
@test "_alert_: logging OFF" {
LOGLEVEL=OFF
run error "testing error"
run error "testing error 2"
run warning "testing warning"
run notice "testing notice"
run info "testing info"
run debug "testing debug"
assert_file_not_exist "${LOGFILE}"
}

97
test/arrays.bats Executable file
View File

@@ -0,0 +1,97 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-asser/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/arrays.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}"
else
echo "Sourcefile not found: ${ALERTS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() {
# Set arrays
A=(one two three 1 2 3)
B=(1 2 3 4 5 6)
DUPES=(1 2 3 1 2 3)
######## DEFAUL FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_inArray_: success" {
run _inArray_ one "${A[@]}"
assert_success
}
@test "_inArray_: failure" {
run _inArray_ ten "${A[@]}"
assert_failure
}
@test "_join_: Join array comma" {
run _join_ , "${B[@]}"
assert_output "1,2,3,4,5,6"
}
@test "_join_: Join array space" {
run _join_ " " "${B[@]}"
assert_output "1 2 3 4 5 6"
}
@test "_join_: Join string complex" {
run _join_ , a "b c" d
assert_output "a,b c,d"
}
@test "_join_: join string simple" {
run _join_ / var usr tmp
assert_output "var/usr/tmp"
}
@test "_setdiff_: Print elements not common to arrays" {
run _setdiff_ "${A[*]}" "${B[*]}"
assert_output "one two three"
run _setdiff_ "${B[*]}" "${A[*]}"
assert_output "4 5 6"
}
@test "_removeDupes_: remove duplicates" {
run _removeDupes_ "${DUPES[@]}"
assert_line --index 0 "3"
assert_line --index 1 "2"
assert_line --index 2 "1"
assert_line --index 3 ""
}

244
test/baseHelpers.bats Executable file
View File

@@ -0,0 +1,244 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-asser/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/baseHelpers.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}"
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
######## DEFAUL FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=ERROR
VERBOSE=false
FORCE=false
DRYRUN=false
}
teardown() {
popd >&2
temp_del "${TESTDIR}"
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
_testCheckBinary_() {
@test "_checkBinary_: true" {
run _checkBinary_ "vi"
assert_success
}
@test "_checkBinary_: false" {
run _checkBinary_ "someNonexistantBinary"
assert_failure
}
}
_testExecute_() {
@test "_execute_: Debug command" {
DRYRUN=true
run _execute_ "rm testfile.txt"
assert_success
assert_output --partial "[ dryrun] rm testfile.txt"
}
@test "_execute_: No command" {
run _execute_
assert_failure
assert_output --regexp "_execute_ needs a command$"
}
@test "_execute_: Bad command" {
run _execute_ "rm nonexistant.txt"
assert_failure
assert_output --partial "[warning] rm nonexistant.txt"
}
@test "_execute_ -e: Bad command" {
run _execute_ -e "rm nonexistant.txt"
assert_failure
assert_output "error: rm nonexistant.txt"
}
@test "_execute_ -p: Return 0 on bad command" {
run _execute_ -p "rm nonexistant.txt"
assert_success
assert_output --partial "[warning] rm nonexistant.txt"
}
@test "_execute_: Good command" {
touch "testfile.txt"
run _execute_ "rm testfile.txt"
assert_success
assert_output --partial "[ info] rm testfile.txt"
assert_file_not_exist "testfile.txt"
}
@test "_execute_: Good command - no output" {
touch "testfile.txt"
run _execute_ -q "rm testfile.txt"
assert_success
refute_output --partial "[ info] rm testfile.txt"
assert_file_not_exist "testfile.txt"
}
@test "_execute_ -s: Good command" {
touch "testfile.txt"
run _execute_ -s "rm testfile.txt"
assert_success
assert_output --partial "[success] rm testfile.txt"
assert_file_not_exist "testfile.txt"
}
@test "_execute_ -v: Good command" {
touch "testfile.txt"
run _execute_ -v "rm -v testfile.txt"
assert_success
assert_line --index 0 "removed 'testfile.txt'"
assert_line --index 1 --partial "[ info] rm -v testfile.txt"
assert_file_not_exist "testfile.txt"
}
@test "_execute_ -ev: Good command" {
touch "testfile.txt"
run _execute_ -ve "rm -v testfile.txt"
assert_success
assert_line --index 0 "removed 'testfile.txt'"
assert_line --index 1 --partial "rm -v testfile.txt"
assert_file_not_exist "testfile.txt"
}
}
_testFindBaseDirectory_() {
@test "_findBaseDir_" {
run _findBaseDir_
assert_output --regexp "^/usr/local/Cellar/bats-core/[0-9]\.[0-9]\.[0-9]"
}
}
_testHaveFunction_() {
@test "_haveFunction_: Success" {
run _haveFunction_ "_haveFunction_"
assert_success
}
@test "_haveFunction_: Failure" {
run _haveFunction_ "_someUndefinedFunction_"
assert_failure
}
}
_testProgressBar_() {
@test "_progressBar_: verbose" {
verbose=true
run _progressBar_ 100
assert_success
assert_output ""
verbose=false
}
@test "_progressBar_: quiet" {
quiet=true
run _progressBar_ 100
assert_success
assert_output ""
quiet=false
}
}
_testSeekConfirmation_() {
@test "_seekConfirmation_: yes" {
run _seekConfirmation_ 'test' <<<"y"
assert_success
assert_output --partial "[ input] test"
}
@test "_seekConfirmation_: no" {
run _seekConfirmation_ 'test' <<<"n"
assert_failure
assert_output --partial "[ input] test"
}
@test "_seekConfirmation_: Force" {
FORCE=true
run _seekConfirmation_ "test"
assert_success
assert_output --partial "test"
}
@test "_seekConfirmation_: Quiet" {
QUIET=true
run _seekConfirmation_ 'test' <<<"y"
assert_success
refute_output --partial "test"
quiet=false
}
}
_testSetPATH_() {
@test "_setPATH_" {
_setPATH_ "/testing/from/bats" "/testing/again"
run echo "$PATH"
assert_output --regexp "/testing/from/bats"
assert_output --regexp "/testing/again"
}
}
_testCheckBinary_
_testExecute_
_testFindBaseDirectory_
_testHaveFunction_
_testProgressBar_
_testSeekConfirmation_
_testSetPATH_

431
test/dates.bats Executable file
View File

@@ -0,0 +1,431 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-asser/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/dates.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}"
else
echo "Sourcefile not found: ${ALERTS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
setup() {
TESTDIR="$(temp_make)"
######## DEFAUL FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=OFF
VERBOSE=true
FORCE=false
DRYRUN=false
}
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
@test "_monthToNumber_: 1" {
run _monthToNumber_ "dec"
assert_success
assert_output "12"
}
@test "_monthToNumber_: 2" {
run _monthToNumber_ "MARCH"
assert_success
assert_output "3"
}
@test "_monthToNumber_: Fail" {
run _monthToNumber_ "somethingthatbreaks"
assert_failure
}
@test "_numberToMonth_: 1" {
run _numberToMonth_ "1"
assert_success
assert_output "January"
}
@test "_numberToMonth_: 2" {
run _numberToMonth_ "02"
assert_success
assert_output "February"
}
@test "_numberToMonth_: Fail" {
run _numberToMonth_ "13"
assert_failure
}
@test "_parseDate_: YYYY MM DD 1" {
run _parseDate_ "2019 06 01"
assert_success
assert_output --regexp "_parseDate_found: +2019 06 01"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +June"
assert_output --regexp "_parseDate_month: +6"
}
@test "_parseDate_: YYYY MM DD 2" {
run _parseDate_ "this is text 2019-06-01 and more text"
assert_success
assert_output --regexp "_parseDate_found: +2019-06-01"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +June"
assert_output --regexp "_parseDate_month: +6"
assert_output --regexp "_parseDate_day: +1"
}
@test "_parseDate_: YYYY MM DD fail 1" {
run _parseDate_ "this is text 2019-99-01 and more text"
assert_failure
}
@test "_parseDate_: YYYY MM DD fail 2" {
run _parseDate_ "this is text 2019-06-99 and more text"
assert_failure
}
@test "_parseDate_: Month DD, YYYY" {
run _parseDate_ "this is text Oct 22, 2019 and more text"
assert_success
assert_output --regexp "_parseDate_found: +Oct 22, +2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: Month DD YYYY" {
run _parseDate_ "Oct 22 2019"
assert_success
assert_output --regexp "_parseDate_found: +Oct 22 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: Month DD, YY" {
run _parseDate_ "this is text Oct 22, 19 and more text"
assert_success
assert_output --regexp "_parseDate_found: +Oct 22, 19"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: Month DD YY" {
run _parseDate_ "Oct 22 19"
assert_success
assert_output --regexp "_parseDate_found: +Oct 22 19"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: DD Month, YYYY" {
run _parseDate_ "22 June, 2019 and more text"
assert_success
assert_output --regexp "_parseDate_found: +22 June, 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +June"
assert_output --regexp "_parseDate_month: +6"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: DD Month YYYY" {
run _parseDate_ "some text66-here-22 June 2019 and more text"
assert_success
assert_output --regexp "_parseDate_found: +22 June 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +June"
assert_output --regexp "_parseDate_month: +6"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: MM DD YYYY 1" {
run _parseDate_ "this is text 12 22 2019 and more text"
assert_success
assert_output --regexp "_parseDate_found: +12 22 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +December"
assert_output --regexp "_parseDate_month: +12"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: MM DD YYYY 2" {
run _parseDate_ "12 01 2019"
assert_success
assert_output --regexp "_parseDate_found: +12 01 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +December"
assert_output --regexp "_parseDate_month: +12"
assert_output --regexp "_parseDate_day: +1"
}
@test "_parseDate_: MM DD YYYY 3" {
run _parseDate_ "a-test-01-12-2019-is here"
assert_success
assert_output --regexp "_parseDate_found: +01-12-2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +12"
}
@test "_parseDate_: DD MM YYYY 1 " {
run _parseDate_ "a-test-22/12/2019-is here"
assert_success
assert_output --regexp "_parseDate_found: +22/12/2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +December"
assert_output --regexp "_parseDate_month: +12"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: DD MM YYYY 2 " {
run _parseDate_ "a-test-32/12/2019-is here"
assert_failure
}
@test "_parseDate_: DD MM YY" {
run _parseDate_ "a-test-22-12-19-is here"
assert_success
assert_output --regexp "_parseDate_found: +22-12-19"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +December"
assert_output --regexp "_parseDate_month: +12"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: MM DD YY 1 " {
run _parseDate_ "a-test-12/22/19-is here"
assert_success
assert_output --regexp "_parseDate_found: +12/22/19"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +December"
assert_output --regexp "_parseDate_month: +12"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: MM DD YY 2 " {
run _parseDate_ "6 8 19"
assert_success
assert_output --regexp "_parseDate_found: +6 8 19"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +June"
assert_output --regexp "_parseDate_month: +6"
assert_output --regexp "_parseDate_day: +8"
}
@test "_parseDate_: MM DD YY 3 " {
run _parseDate_ "6 8 191"
assert_failure
}
@test "_parseDate_: MM DD YY 4 " {
run _parseDate_ "6 34 19"
assert_failure
}
@test "_parseDate_: MM DD YY 5 " {
run _parseDate_ "34 12 19"
assert_failure
}
@test "_parseDate_: Month, YYYY 1 " {
run _parseDate_ "a-test-January, 2019-is here"
assert_success
assert_output --regexp "_parseDate_found: +January, 2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +1"
}
@test "_parseDate_: Month, YYYY 2 " {
run _parseDate_ "mar-2019"
assert_success
assert_output --regexp "_parseDate_found: +mar-2019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +March"
assert_output --regexp "_parseDate_month: +3"
assert_output --regexp "_parseDate_day: +1"
}
@test "_parseDate_: YYYYMMDDHHMM 1" {
run _parseDate_ "201901220228"
assert_success
assert_output --regexp "_parseDate_found: +201901220228"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +22"
assert_output --regexp "_parseDate_hour: +2"
assert_output --regexp "_parseDate_minute: +28"
}
@test "_parseDate_: YYYYMMDDHHMM 2" {
run _parseDate_ "asdf 201901220228asdf "
assert_success
assert_output --regexp "_parseDate_found: +201901220228"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +22"
assert_output --regexp "_parseDate_hour: +2"
assert_output --regexp "_parseDate_minute: +28"
}
@test "_parseDate_: YYYYMMDDHH 1" {
run _parseDate_ "asdf 2019012212asdf "
assert_success
assert_output --regexp "_parseDate_found: +2019012212"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +22"
assert_output --regexp "_parseDate_hour: +12"
assert_output --regexp "_parseDate_minute: +00"
}
@test "_parseDate_: YYYYMMDDHH 2" {
run _parseDate_ "2019012212"
assert_success
assert_output --regexp "_parseDate_found: +2019012212"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +22"
assert_output --regexp "_parseDate_hour: +12"
assert_output --regexp "_parseDate_minute: +00"
}
@test "_parseDate_: MMDDYYYY 1" {
run _parseDate_ "01222019"
assert_success
assert_output --regexp "_parseDate_found: +01222019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: MMDDYYYY 2" {
run _parseDate_ "asdf 11222019 asdf"
assert_success
assert_output --regexp "_parseDate_found: +11222019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +November"
assert_output --regexp "_parseDate_month: +11"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: DDMMYYYY 1" {
run _parseDate_ "16012019"
assert_success
assert_output --regexp "_parseDate_found: +16012019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +January"
assert_output --regexp "_parseDate_month: +1"
assert_output --regexp "_parseDate_day: +16"
}
@test "_parseDate_: DDMMYYYY 2" {
run _parseDate_ "asdf 16112019 asdf"
assert_success
assert_output --regexp "_parseDate_found: +16112019"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +November"
assert_output --regexp "_parseDate_month: +11"
assert_output --regexp "_parseDate_day: +16"
}
@test "_parseDate_: YYYYDDMM " {
run _parseDate_ "20192210"
assert_success
assert_output --regexp "_parseDate_found: +20192210"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: YYYYMMDD 1" {
run _parseDate_ "20191022"
assert_success
assert_output --regexp "_parseDate_found: +20191022"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +22"
}
@test "_parseDate_: YYYYMMDD 2" {
run _parseDate_ "20191010"
assert_success
assert_output --regexp "_parseDate_found: +20191010"
assert_output --regexp "_parseDate_year: +2019"
assert_output --regexp "_parseDate_monthName: +October"
assert_output --regexp "_parseDate_month: +10"
assert_output --regexp "_parseDate_day: +10"
}
@test "_parseDate_: YYYYMMDD fail" {
run _parseDate_ "20199910"
assert_failure
}
@test "_parseDate_: fail - no input" {
run _parseDate_
assert_failure
}
@test "_parseDate_: fail - no date" {
run _parseDate_ "a string with some numbers 1234567"
assert_failure
}
@test "_formatDate_: default" {
run _formatDate_ "jan 21, 2019"
assert_success
assert_output "2019-01-21"
}
@test "_formatDate_: custom format " {
run _formatDate_ "2019-12-27" "+%m %d, %Y"
assert_success
assert_output "12 27, 2019"
}
@test "_formatDate_: fail - no input " {
run _formatDate_
assert_failure
}

118
test/files.bats Executable file
View File

@@ -0,0 +1,118 @@
#!/usr/bin/env bats
#shellcheck disable
load 'test_helper/bats-support/load'
load 'test_helper/bats-file/load'
load 'test_helper/bats-asser/load'
######## SETUP TESTS ########
ROOTDIR="$(git rev-parse --show-toplevel)"
SOURCEFILE="${ROOTDIR}/utilities/files.bash"
BASEHELPERS="${ROOTDIR}/utilities/baseHelpers.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 "${BASEHELPERS}" >&2; then
source "${BASEHELPERS}"
else
echo "Sourcefile not found: ${BASEHELPERS}" >&2
printf "Can not run tests.\n" >&2
exit 1
fi
if test -f "${ALERTS}" >&2; then
source "${ALERTS}"
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
######## DEFAUL FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=OFF
VERBOSE=false
FORCE=false
DRYRUN=false
}
teardown() {
popd >&2
temp_del "${TESTDIR}"
}
######## FIXTURES ########
YAML1="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml"
YAML1parse="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml.txt"
YAML2="${BATS_TEST_DIRNAME}/fixtures/yaml2.yaml"
JSON="${BATS_TEST_DIRNAME}/fixtures/json.json"
unencrypted="${BATS_TEST_DIRNAME}/fixtures/test.md"
encrypted="${BATS_TEST_DIRNAME}/fixtures/test.md.enc"
######## RUN TESTS ########
@test "Sanity..." {
run true
assert_success
assert_output ""
}
_testBackupFile_() {
@test "_backupFile_: no source" {
run _backupFile_ "testfile"
assert_failure
}
@test "_backupFile_: backup file" {
touch "testfile"
run _backupFile_ -d "testfile" "backup-files"
assert_success
assert [ -f "backup-files/testfile" ]
}
@test "_backupFile_: default destination & rename" {
mkdir backup
touch "testfile" "backup/testfile"
run _backupFile_ -d "testfile"
assert_success
assert [ -f "backup/testfile-2" ]
}
}
_testBackupFile_

View File

@@ -149,3 +149,20 @@ die() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
fatal() { _alert_ fatal "${1}" "${2-}"; _safeExit_ "1" ; }
debug() { _alert_ debug "${1}" "${2-}"; }
verbose() { _alert_ debug "${1}" "${2-}"; }
_functionStack_() {
# DESC: Prints the function stack in use
# ARGS: None
# OUTS: Prints [function]:[file]:[line]
# NOTE: Does not print functions from the alert class
local _i
funcStackResponse=()
for ((_i = 1; _i < ${#BASH_SOURCE[@]}; _i++)); do
case "${FUNCNAME[$_i]}" in "_alert_" | "_trapCleanup_" | fatal | error | warning | verbose | debug | die) continue ;; esac
funcStackResponse+=("${FUNCNAME[$_i]}:$(basename ${BASH_SOURCE[$_i]}):${BASH_LINENO[$_i - 1]}")
done
printf "( "
printf %s "${funcStackResponse[0]}"
printf ' < %s' "${funcStackResponse[@]:1}"
printf ' )\n'
}

View File

@@ -1,3 +1,4 @@
_inArray_() {
# DESC: Determine if a value is in an array
# ARGS: $1 (Required) - Value to search for
@@ -74,7 +75,7 @@ _removeDupes_() {
# DESC: Removes duplicate array elements.
# ARGS: $1 (Required) - Input array
# OUTS: Prints de-duped elements to standard out
# USAGE: _removeDups_ "${array@]}"
# USAGE: _removeDups_ "${array[@]}"
# NOTE: List order may not stay the same.
# https://github.com/dylanaraps/pure-bash-bible
declare -A tmp_array