mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-11 22:43:47 -05:00
Unit tests
This commit is contained in:
226
test/files.bats
226
test/files.bats
@@ -52,6 +52,8 @@ setup() {
|
||||
VERBOSE=false
|
||||
FORCE=false
|
||||
DRYRUN=false
|
||||
PASS=123
|
||||
|
||||
}
|
||||
|
||||
teardown() {
|
||||
@@ -62,8 +64,6 @@ teardown() {
|
||||
######## 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"
|
||||
|
||||
@@ -75,6 +75,24 @@ encrypted="${BATS_TEST_DIRNAME}/fixtures/test.md.enc"
|
||||
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" {
|
||||
@@ -152,19 +170,223 @@ _testListFiles_() {
|
||||
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_: fail with no file" {
|
||||
run _parseFilename_ "somenonexistantfile"
|
||||
assert_failure
|
||||
assert_output --partial "Can't locate a file to parse"
|
||||
}
|
||||
|
||||
@test "_parseFilename_: file with one extension" {
|
||||
touch "testfile.txt"
|
||||
VERBOSE=true
|
||||
run _parseFilename_ "testfile.txt"
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
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$"
|
||||
}
|
||||
|
||||
# _parseFilename_ "test.tar.gz"
|
||||
# _parseFilename_ "test.tar.gzip"
|
||||
}
|
||||
|
||||
_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_: 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 "_uniqueFileName_: Count to 3" {
|
||||
touch "test.txt"
|
||||
touch "test.txt.1"
|
||||
touch "test.txt.2"
|
||||
|
||||
run _uniqueFileName_ "test.txt"
|
||||
assert_output --regexp ".*/test\.txt\.3$"
|
||||
}
|
||||
|
||||
@test "_uniqueFileName_: Don't confuse existing numbers" {
|
||||
touch "test-2.txt"
|
||||
|
||||
run _uniqueFileName_ "test-2.txt"
|
||||
assert_output --regexp ".*/test-2\.txt\.1$"
|
||||
}
|
||||
|
||||
@test "_uniqueFileName_: User specified separator" {
|
||||
touch "test.txt"
|
||||
|
||||
run _uniqueFileName_ "test.txt" " "
|
||||
assert_output --regexp ".*/test\.txt 1$"
|
||||
}
|
||||
|
||||
@test "_uniqueFileName_: failure" {
|
||||
run _uniqueFileName_
|
||||
|
||||
assert_failure
|
||||
}
|
||||
|
||||
_testBackupFile_
|
||||
_testListFiles_
|
||||
_testParseFilename_
|
||||
_testMakeSymlink_
|
||||
_testParseYAML_
|
||||
|
||||
Reference in New Issue
Block a user