Add new function and improve comments

This commit is contained in:
Nathaniel Landau
2021-11-05 20:38:47 -04:00
parent 962827608f
commit 5059f5f73b
10 changed files with 213 additions and 15 deletions

View File

@@ -11,6 +11,8 @@ SOURCEFILE="${ROOTDIR}/utilities/files.bash"
BASEHELPERS="${ROOTDIR}/utilities/misc.bash"
ALERTS="${ROOTDIR}/utilities/alerts.bash"
PATH="/usr/local/opt/gnu-tar/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/grep/libexec/gnubin:${PATH}"
if test -f "${SOURCEFILE}" >&2; then
source "${SOURCEFILE}"
else
@@ -68,6 +70,7 @@ teardown() {
}
######## FIXTURES ########
TEXT="${BATS_TEST_DIRNAME}/fixtures/text.txt"
YAML1="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml"
YAML1parse="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml.txt"
unencrypted="${BATS_TEST_DIRNAME}/fixtures/test.md"
@@ -433,6 +436,61 @@ _testParseYAML_() {
assert_success
}
@test "_printFileBetween_: match case-insensitive" {
run _printFileBetween_ -i "^#+ orange1" "^#+$" "${TEXT}"
assert_success
assert_line --index 0 "############ Orange1 ############"
assert_line --index 1 "# 1"
assert_line --index 2 "# 2"
assert_line --index 3 "# 3"
assert_line --index 4 "# 4"
assert_line --index 5 "#################################"
refute_output --regexp "Grape|Orange2"
}
@test "_printFileBetween_: match case-insensitive - greedy" {
run _printFileBetween_ -ig "^#+ orange" "##" "${TEXT}"
assert_success
assert_line --index 0 "############ Orange1 ############"
assert_line --index 1 "# 1"
assert_line --index 2 "# 2"
assert_line --index 3 "# 3"
assert_line --index 4 "# 4"
assert_line --index 5 "#################################"
assert_line --index 6 "############ Orange2 ############"
assert_line --index 7 "# 1"
assert_line --index 8 "# 2"
assert_line --index 9 "# 3"
assert_line --index 10 "# 4"
assert_line --index 11 "#################################"
refute_output --regexp "Grape"
}
@test "_printFileBetween_: no match" {
run _printFileBetween_ "^#+ orange1" "^#+$" "${TEXT}"
assert_failure
}
@test "_printFileBetween_: remove lines" {
run _printFileBetween_ -ri "^[A-Z0-9]+\(\)" "^ *}.*" "${TEXT}"
assert_success
assert_line --index 0 --partial "# buf : Backup file with time stamp"
assert_line --index 5 --regexp "^ *cp -a .*"
refute_output --regexp "buf\(\) {"
refute_output --regexp '}[^"_]'
refute_output --regexp "md5Check"
}
@test "_printFileBetween_: remove lines - greedy" {
run _printFileBetween_ -gr "^[a-zA-Z0-9]+\(\)" "^ *}.*" "${TEXT}"
assert_success
assert_line --index 0 --partial "# buf : Backup file with time stamp"
assert_line --index 5 --regexp "^ *cp -a .*"
refute_output --regexp "buf\(\) {"
assert_line --index 29 --regexp "^ *fi.*"
assert_output --regexp "md5Check"
}
_testBackupFile_
_testListFiles_
_testMakeSymlink_

59
test/fixtures/text.txt vendored Normal file
View File

@@ -0,0 +1,59 @@
############ Orange1 ############
# 1
# 2
# 3
# 4
#################################
############ Orange2 ############
# 1
# 2
# 3
# 4
#################################
############ Grape ############
# 1
# 2
# 3
# 4
#################################
buf() {
# buf : Backup file with time stamp
local filename
local filetime
filename="${1}"
filetime=$(date +%Y%m%d_%H%M%S)
cp -a "${filename}" "${filename}_${filetime}"
}
md5Check() {
# DESC: Compares an md5 hash to the md5 hash of a file
# ARGS: None
# OUTS: None
# USAGE: md5Check <md5> <filename>
local opt
local OPTIND=1
local md5="$1"
local file="$2"
if ! command -v md5sum &>/dev/null; then
echo "Can not find 'md5sum' utility"
return 1
fi
# Get md5 has of file
local filemd5
filemd5="$(md5sum "${file}")"
if [[ $filemd5 == "$md5" ]]; then
success "The two md5 hashes match"
return 0
else
warning "The two md5 hashes do not match"
return 1
fi
}