From b1df496e95dfa1f7cc543bda7b12acf321283ae8 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Wed, 21 Jul 2021 13:09:35 -0400 Subject: [PATCH] tests --- test/alerts.bats | 2 +- test/arrays.bats | 2 +- test/baseHelpers.bats | 2 +- test/dates.bats | 2 +- test/files.bats | 74 ++++++++++++++++++++++++++++++++++++------- utilities/files.bash | 2 +- 6 files changed, 68 insertions(+), 16 deletions(-) diff --git a/test/alerts.bats b/test/alerts.bats index 1c2ceab..dad7509 100755 --- a/test/alerts.bats +++ b/test/alerts.bats @@ -3,7 +3,7 @@ load 'test_helper/bats-support/load' load 'test_helper/bats-file/load' -load 'test_helper/bats-asser/load' +load 'test_helper/bats-assert/load' ######## SETUP TESTS ######## ROOTDIR="$(git rev-parse --show-toplevel)" diff --git a/test/arrays.bats b/test/arrays.bats index 7371cfd..64e1a7e 100755 --- a/test/arrays.bats +++ b/test/arrays.bats @@ -3,7 +3,7 @@ load 'test_helper/bats-support/load' load 'test_helper/bats-file/load' -load 'test_helper/bats-asser/load' +load 'test_helper/bats-assert/load' ######## SETUP TESTS ######## ROOTDIR="$(git rev-parse --show-toplevel)" diff --git a/test/baseHelpers.bats b/test/baseHelpers.bats index 6cade5b..318cc50 100755 --- a/test/baseHelpers.bats +++ b/test/baseHelpers.bats @@ -3,7 +3,7 @@ load 'test_helper/bats-support/load' load 'test_helper/bats-file/load' -load 'test_helper/bats-asser/load' +load 'test_helper/bats-assert/load' ######## SETUP TESTS ######## ROOTDIR="$(git rev-parse --show-toplevel)" diff --git a/test/dates.bats b/test/dates.bats index 3d4b149..525b862 100755 --- a/test/dates.bats +++ b/test/dates.bats @@ -3,7 +3,7 @@ load 'test_helper/bats-support/load' load 'test_helper/bats-file/load' -load 'test_helper/bats-asser/load' +load 'test_helper/bats-assert/load' ######## SETUP TESTS ######## ROOTDIR="$(git rev-parse --show-toplevel)" diff --git a/test/files.bats b/test/files.bats index 518c5a7..b539ab2 100755 --- a/test/files.bats +++ b/test/files.bats @@ -3,7 +3,7 @@ load 'test_helper/bats-support/load' load 'test_helper/bats-file/load' -load 'test_helper/bats-asser/load' +load 'test_helper/bats-assert/load' ######## SETUP TESTS ######## ROOTDIR="$(git rev-parse --show-toplevel)" @@ -43,7 +43,7 @@ setup() { BATSLIB_FILE_PATH_REM="#${TEST_TEMP_DIR}" BATSLIB_FILE_PATH_ADD='' - pushd "${TESTDIR}" >&2 + pushd "${TESTDIR}" &>/dev/null ######## DEFAUL FLAGS ######## LOGFILE="${TESTDIR}/logs/log.txt" @@ -55,7 +55,7 @@ setup() { } teardown() { - popd >&2 + popd &>/dev/null temp_del "${TESTDIR}" } @@ -83,23 +83,75 @@ _testBackupFile_() { assert_failure } - @test "_backupFile_: backup file" { + @test "_backupFile_: simple backup" { touch "testfile" - run _backupFile_ -d "testfile" "backup-files" + run _backupFile_ "testfile" assert_success - assert [ -f "backup-files/testfile" ] + assert_file_exist "testfile.bak" + assert_file_exist "testfile" } - @test "_backupFile_: default destination & rename" { - mkdir backup - touch "testfile" "backup/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 [ -f "backup/testfile-2" ] + 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" + } } @@ -114,5 +166,5 @@ _testBackupFile_() { - _testBackupFile_ +_testListFiles_ diff --git a/utilities/files.bash b/utilities/files.bash index 17fb5d3..fd406b8 100644 --- a/utilities/files.bash +++ b/utilities/files.bash @@ -23,6 +23,7 @@ _listFiles_() { case "$t" in glob | Glob | g | G) while read -r fileMatch; do + e="$(realpath "${fileMatch}")" echo "${e}" done < <(find "${d}" -iname "${p}" -type f -maxdepth 1 | sort) ;; @@ -37,7 +38,6 @@ _listFiles_() { return 1 ;; esac - } _backupFile_() {