Files
shell-scripting-templates/test/files.bats
Nathaniel Landau 8bb89541e8 Squashed commit of the following:
commit 61bf734812cb62ba6e0ec224bc15f7928705a8a2
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Oct 21 15:44:21 2021 -0400

    Major overhaul continued

     - rename templates
     - add checks utilities
     - add new array utilities
     - rename files
     - add assorted utilities
     - improve documentation

commit 546178fff3b526f492eb0eeffc63f79537e75de3
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 16:31:14 2021 -0400

    Update conventions

commit f6d0642f85518efda9c5d8472b99d1c14163e381
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 09:47:09 2021 -0400

    minor formatting changes

commit 2217612b55e3f9faf803a2d0c937ea2261206505
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 17:59:09 2021 -0400

    add new functions

commit 347ba7aa738dcd6a5ad9d70886b38da3a17dc89e
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 12:06:44 2021 -0400

    major overhaul

    - Add standaloneTemplate.sh
    - Rework README
    - Refactor inline documentation
    - Enforce coding standards
    - Remove CSV utilities
    - Add new array utilities
    - add _useGNUutils_
    - more ...

commit cd8e0d49aef25eeaf6b3e71a3c9e1f29ab9b06f5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 17 09:56:08 2021 -0400

    Add debug functions

commit f7c5c0a3d19815dcc6ba80b5f5a2ebb77ef88b07
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sat Oct 16 21:10:01 2021 -0400

    add new array functions

    _joinArray_, _isEmptyArray_, _sortArray_, _reverseSortArray_, and _mergearrays_

commit d8bc3d8cabdbcee3c479f97b43a45bdfe3bdafe0
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 15 17:27:12 2021 -0400

    add _columnize_

commit 2fd2ae9435f476bc3968c3eb0d793db4bf1d9eaf
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Oct 11 22:17:45 2021 -0400

    _progressBar_: Fix unbound variable

commit e8933d15fc955a1acc665e9a081f131e681855d5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 10 11:50:42 2021 -0400

    _alert_: header now underlined

commit c9ce894361dec7d3513c038794a155519baf26bc
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:49:42 2021 -0400

    _alert_: line numbers to gray

commit 4aaddd336ce613f629a7e6a62ef3b27ffc24d22d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 8 15:05:20 2021 -0400

    _usage_ to stdout

commit e2372fc3122ec1f20acc27f04d29b3785f014e25
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:38:26 2021 -0400

    _setPATH_: remove unneeded logic

commit e60c75b6c954ac4bd146e2758252168027b9a43d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:25:38 2021 -0400

    _findSource_: bugfix

commit 0e84912e1ccd7203e5beff9f8737f8374f4aa5d8
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 16:29:25 2021 -0400

    add requirements to documentation

commit 2c24843e3ada591e1868a94416e40b5ac0aa4994
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 15:34:10 2021 -0400

    _uniqueFilename_: improve extension handling

commit 08bc2dfdcc8632efee9179e9c960a574fc17cf0c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 27 15:13:53 2021 -0400

    improve hooks script

commit 641918f1559d3b3aa38a9bbdf418938b2b81c176
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 24 08:16:52 2021 -0400

    _inArry_: case insensitivity

commit eae10f170680540fdb4a1222add7e54f8785ea63
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:31:44 2021 -0400

    clean up alerting

commit 700acd56f57fd57db84ef0e232ef41cdd7aee43c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:22:11 2021 -0400

    refactor _execute_

commit d893f86900a9fed9d91a0c9cc06c13b6b34d9926
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:19:18 2021 -0400

    'fatal' replaces 'die'

commit 3326857bf127bef36cd9982246aa5b826d796d0a
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 17 08:29:50 2021 -0400

    _execute_: ensure quiet and verbose work together
2021-10-21 16:03:27 -04:00

522 lines
13 KiB
Bash
Executable File

#!/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/files.bash"
BASEHELPERS="${ROOTDIR}/utilities/misc.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
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
setup() {
TESTDIR="$(temp_make)"
curPath="${PWD}"
BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}"
BATSLIB_FILE_PATH_ADD='<temp>'
pushd "${TESTDIR}" &>/dev/null
######## DEFAULT FLAGS ########
LOGFILE="${TESTDIR}/logs/log.txt"
QUIET=false
LOGLEVEL=OFF
VERBOSE=false
FORCE=false
DRYRUN=false
PASS=123
set -o errtrace
set -o nounset
set -o pipefail
}
teardown() {
set +o nounset
set +o errtrace
set +o pipefail
popd &>/dev/null
temp_del "${TESTDIR}"
}
######## FIXTURES ########
YAML1="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml"
YAML1parse="${BATS_TEST_DIRNAME}/fixtures/yaml1.yaml.txt"
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 ""
}
@test "_decryptFile_" {
run _decryptFile_ "${encrypted}" "test-decrypted.md"
assert_success
assert_file_exist "test-decrypted.md"
run cat "test-decrypted.md"
assert_success
assert_line --index 0 "# About"
assert_line --index 1 "This repository contains everything needed to bootstrap and configure new Mac computer. Included here are:"
}
@test "_encryptFile_" {
run _encryptFile_ "${unencrypted}" "test-encrypted.md.enc"
assert_success
assert_file_exist "test-encrypted.md.enc"
run cat "test-encrypted.md.enc"
assert_line --index 0 --partial "Salted__"
}
_testBackupFile_() {
@test "_backupFile_: no source" {
run _backupFile_ "testfile"
assert_failure
}
@test "_backupFile_: simple backup" {
touch "testfile"
run _backupFile_ "testfile"
assert_success
assert_file_exist "testfile.bak"
assert_file_exist "testfile"
}
@test "_backupFile_: backup and unique name" {
touch "testfile"
touch "testfile.bak"
run _backupFile_ "testfile"
assert_success
assert_file_exist "testfile.bak"
assert_file_exist "testfile"
assert_file_exist "testfile.bak.1"
}
@test "_backupFile_: move" {
touch "testfile"
run _backupFile_ -m "testfile"
assert_success
assert_file_exist "testfile.bak"
assert_file_not_exist "testfile"
}
@test "_backupFile_: directory" {
touch "testfile"
run _backupFile_ -d "testfile"
assert_success
assert_file_exist "backup/testfile"
assert_file_exist "testfile"
}
@test "_backupFile_: move to directory w/ custom name" {
touch "testfile"
run _backupFile_ -dm "testfile" "dir"
assert_success
assert_file_exist "dir/testfile"
assert_file_not_exist "testfile"
}
}
_testListFiles_() {
@test "_listFiles_: glob" {
touch yestest{1,2,3}.txt
touch notest{1,2,3}.txt
run _listFiles_ g "yestest*.txt" "${TESTDIR}"
assert_success
assert_output --partial "yestest1.txt"
refute_output --partial "notest1.txt"
}
@test "_listFiles_: regex" {
touch yestest{1,2,3}.txt
touch notest{1,2,3}.txt
run _listFiles_ regex ".*notest[0-9]\.txt" "${TESTDIR}"
assert_success
refute_output --partial "yestest1.txt"
assert_output --partial "notest1.txt"
}
@test "_listFiles: fail no args" {
run _listFiles_
assert_failure
}
@test "_listFiles: fail one arg" {
run _listFiles_ "g"
assert_failure
}
}
_testParseFilename_() {
@test "_parseFilename_: file with one extension" {
touch "testfile.txt"
VERBOSE=true
run _parseFilename_ "testfile.txt"
set +o nounset
assert_success
assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.txt$"
assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.txt$"
assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*"
assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: txt$"
assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$"
}
@test "_parseFilename_: file with dots in name" {
touch "testfile.for.testing.txt"
VERBOSE=true
run _parseFilename_ "testfile.for.testing.txt"
set +o nounset
assert_success
assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.for\.testing\.txt$"
assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.for\.testing\.txt$"
assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*"
assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: txt$"
assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile\.for\.testing$"
}
@test "_parseFilename_: file with no extension" {
touch "testfile"
VERBOSE=true
run _parseFilename_ "testfile"
set +o nounset
assert_success
assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile$"
assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile$"
assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*"
assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: $"
assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$"
}
@test "_parseFilename_: file with tar.gz" {
touch "testfile.tar.gz"
VERBOSE=true
run _parseFilename_ "testfile.tar.gz"
set +o nounset
assert_success
assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.tar\.gz$"
assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.tar\.gz$"
assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*"
assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: tar\.gz$"
assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$"
}
@test "_parseFilename_: file with three extensions" {
touch "testfile.tar.gzip.bzip"
VERBOSE=true
run _parseFilename_ -n3 "testfile.tar.gzip.bzip"
set +o nounset
assert_success
assert_line --index 0 --regexp "\[ debug\].*{PARSE_FULL}: /.*testfile\.tar\.gzip\.bzip$"
assert_line --index 1 --regexp "\[ debug\].*${PARSE_BASE}: testfile\.tar\.gzip\.bzip$"
assert_line --index 2 --regexp "\[ debug\].*${PARSE_PATH}: /.*"
assert_line --index 3 --regexp "\[ debug\].*${PARSE_EXT}: tar\.gzip\.bzip$"
assert_line --index 4 --regexp "\[ debug\].*${PARSE_BASENOEXT}: testfile$"
}
}
_testMakeSymlink_() {
@test "_makeSymlink_: Fail with no source fire" {
run _makeSymlink_ "sourceFile" "destFile"
assert_failure
}
@test "_makeSymlink_: fail with no specified destination" {
touch "test.txt"
run _makeSymlink_ "test.txt"
assert_failure
}
@test "_makeSymlink_: make link" {
touch "test.txt"
touch "test2.txt"
run _makeSymlink_ "${TESTDIR}/test.txt" "${TESTDIR}/test2.txt"
assert_success
assert_output --regexp "\[ info\] symlink /.*/test\.txt → /.*/test2\.txt"
assert_link_exist "test2.txt"
assert_file_exist "test2.txt.bak"
}
@test "_makeSymlink_: Ignore already existing links" {
touch "test.txt"
ln -s "$(realpath test.txt)" "${TESTDIR}/test2.txt"
run _makeSymlink_ "$(realpath test.txt)" "${TESTDIR}/test2.txt"
assert_success
assert_link_exist "test2.txt"
assert_output --regexp "\[ info\] Symlink already exists: /.*/test\.txt → /.*/test2\.txt"
}
@test "_makeSymlink_: Ignore already existing links - quiet" {
touch "test.txt"
ln -s "$(realpath test.txt)" "${TESTDIR}/test2.txt"
run _makeSymlink_ -c "$(realpath test.txt)" "${TESTDIR}/test2.txt"
assert_success
assert_link_exist "test2.txt"
assert_output ""
}
@test "_makeSymlink_: Ignore already existing links - dryrun" {
DRYRUN=true
touch "test.txt"
ln -s "$(realpath test.txt)" "${TESTDIR}/test2.txt"
run _makeSymlink_ "$(realpath test.txt)" "${TESTDIR}/test2.txt"
assert_success
assert_link_exist "test2.txt"
assert_output --regexp "\[ dryrun\] Symlink already exists: /.*/test\.txt → /.*/test2\.txt"
}
@test "_makeSymlink_: Don't make backup" {
touch "test.txt"
touch "test2.txt"
run _makeSymlink_ -n "${TESTDIR}/test.txt" "${TESTDIR}/test2.txt"
assert_success
assert_output --regexp "\[ info\] symlink /.*/test\.txt → /.*/test2\.txt"
assert_link_exist "test2.txt"
assert_file_not_exist "test2.txt.bak"
}
}
_testParseYAML_() {
@test "_parseYAML: success" {
run _parseYAML_ "$YAML1" ""
assert_success
assert_output "$( cat "$YAML1parse")"
}
@test "_parseYAML_: empty file" {
touch empty.yaml
run _parseYAML_ "empty.yaml"
assert_failure
}
@test "_parseYAML_: no file" {
run _parseYAML_ "empty.yaml"
assert_failure
}
}
@test "_readFile_: Failure" {
run _readFile_ "testfile.txt"
assert_failure
}
@test "_readFile_: Reads files line by line" {
echo -e "line 1\nline 2\nline 3" > testfile.txt
run _readFile_ "testfile.txt"
assert_line --index 0 'line 1'
assert_line --index 2 'line 3'
}
@test "_sourceFile_ failure" {
run _sourceFile_ "someNonExistantFile"
assert_failure
assert_output --partial "[ fatal] Attempted to source 'someNonExistantFile'. Not found"
}
@test "_sourceFile_ success" {
echo "echo 'hello world'" > "testSourceFile.txt"
run _sourceFile_ "testSourceFile.txt"
assert_success
assert_output "hello world"
}
@test "_createUniqueFilename_: no extension" {
touch "test"
run _createUniqueFilename_ "test"
assert_output --regexp ".*/test\.1$"
}
@test "_createUniqueFilename_: no extension - internal integer" {
touch "test"
touch "test.1"
run _createUniqueFilename_ -i "test"
assert_output --regexp ".*/test\.2$"
}
@test "_createUniqueFilename_: Count to 3" {
touch "test.txt"
touch "test.txt.1"
touch "test.txt.2"
run _createUniqueFilename_ "test.txt"
assert_output --regexp ".*/test\.txt\.3$"
}
@test "_createUniqueFilename_: internal integer" {
touch "test.txt"
touch "test.1.txt"
touch "test.2.txt"
run _createUniqueFilename_ -i "test.txt"
assert_output --regexp ".*/test\.3\.txt$"
}
@test "_createUniqueFilename_: two extensions" {
touch "test.tar.gz"
touch "test.1.tar.gz"
touch "test.2.tar.gz"
run _createUniqueFilename_ -i "test.tar.gz"
assert_output --regexp ".*/test\.3\.tar.gz$"
}
@test "_createUniqueFilename_: Don't confuse existing numbers" {
touch "test-2.txt"
run _createUniqueFilename_ "test-2.txt"
assert_output --regexp ".*/test-2\.txt\.1$"
}
@test "_createUniqueFilename_: User specified separator" {
touch "test.txt"
run _createUniqueFilename_ "test.txt" " "
assert_output --regexp ".*/test\.txt 1$"
}
@test "_createUniqueFilename_: failure" {
run _createUniqueFilename_
assert_failure
}
@test "_fileName_: with extension" {
run _fileName_ "./path/to/file/test.txt"
assert_success
assert_output "test.txt"
}
@test "_fileName_: without extension" {
run _fileName_ "path/to/file/test"
assert_success
assert_output "test"
}
@test "_fileBasename_" {
run _fileBasename_ "path/to/file/test.txt"
assert_success
assert_output "test"
}
@test "_fileExtension_: simple extension" {
run _fileExtension_ "path/to/file/test.txt"
assert_success
assert_output "txt"
}
@test "_fileExtension_: no extension" {
run _fileExtension_ "path/to/file/test"
assert_failure
}
@test "_fileExtension_: two level extension" {
run _fileExtension_ "path/to/file/test.tar.bz2"
assert_success
assert_output "tar.bz2"
}
@test "_fileDirectory_" {
run _fileDirectory_ "path/to/file/test.txt"
assert_success
assert_output "path/to/file"
}
@test "_fileAbsPath_: file" {
touch "./test.txt"
run _fileAbsPath_ "./test.txt"
assert_success
assert_output --regexp "/.*/files\.bats.*/test\.txt$"
}
@test "_fileAbsPath_: directory" {
mkdir "./testdir"
run _fileAbsPath_ "./testdir"
assert_success
assert_output --regexp "/.*/files\.bats.*/testdir$"
}
@test "_fileAbsPath_: fail when not found" {
run _fileAbsPath_ "./test.txt"
assert_failure
}
@test "_fileContains_: No match" {
echo "some text" > "./test.txt"
run _fileContains_ "./test.txt" "nothing here"
assert_failure
}
@test "_fileContains_: Pattern matched" {
echo "some text" > "./test.txt"
run _fileContains_ "./test.txt" "some*"
assert_success
}
_testBackupFile_
_testListFiles_
_testParseFilename_
_testMakeSymlink_
_testParseYAML_