diff --git a/README.md b/README.md index 441cf67..528d62d 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,28 @@ Asserts that _value_ is _not null_, or in shell terms, a non-empty string. The _value_ may be a string or an integer as the later will be parsed as a non-empty string value. The _message_ is optional, and must be quoted. + assertFileExists [message] file_path + +Asserts that _file_path_ is a regular file (tested with `-f`). The _file_path_ +must be a string. The _message_ is optional, and must be quoted. + + assertFileDoesNotExist [message] file_path + +Asserts that _file_path_ is not a regular file (tested with `-f`). The +_file_path_ must be a string. The _message_ is optional, and must be quoted. + + assertDirectoryExists [message] directory_path + +Asserts that _directory_path_ is a regular directory (tested with `-d`). The +_directory_path_ must be a string. The _message_ is optional, and must be +quoted. + + assertDirectoryDoesNotExist [message] directory_path + +Asserts that _directory_path_ is not a regular directory (tested with `-d`). +The _directory_path_ must be a string. The _message_ is optional, and must be +quoted. + assertTrue [message] condition Asserts that a given shell test _condition_ is _true_. The condition can be as diff --git a/shunit2 b/shunit2 index 2850370..11cf68c 100755 --- a/shunit2 +++ b/shunit2 @@ -608,6 +608,170 @@ assertFalse() { # shellcheck disable=SC2016,SC2034 _ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"' +# Assert that a file exists. +# +# Args: +# message: string: failure message [optional] +# file: string: path of the file +# Returns: +# integer: success (TRUE/FALSE/ERROR constant) +assertFileExists() { + # shellcheck disable=SC2090 + ${_SHUNIT_LINENO_} + if command [ $# -lt 1 -o $# -gt 2 ]; then + _shunit_error "assertFileExists() requires one or two arguments; $# given" + _shunit_assertFail + return ${SHUNIT_ERROR} + fi + _shunit_shouldSkip && return ${SHUNIT_TRUE} + + shunit_message_=${__shunit_lineno} + if command [ $# -eq 2 ]; then + shunit_custom_message_=$1 + shunit_file_=$2 + shunit_message_="${shunit_message_}${shunit_custom_message_}" + else + shunit_file_=$1 + shunit_message_="${shunit_message_}The file '${shunit_file_}' does not exist" + fi + + shunit_return=${SHUNIT_TRUE} + if command [ -f "${shunit_file_}" ]; then + _shunit_assertPass + else + fail "${shunit_message_}" + shunit_return=${SHUNIT_FALSE} + fi + + unset shunit_message_ shunit_file_ shunit_custom_message_ + return ${shunit_return} +} +# shellcheck disable=SC2016,SC2034 +_ASSERT_FILE_EXISTS_='eval assertFileExists --lineno "${LINENO:-}"' + +# Assert that a file does not exist. +# +# Args: +# message: string: failure message [optional] +# file: string: path of the file +# Returns: +# integer: success (TRUE/FALSE/ERROR constant) +assertFileDoesNotExist() { + # shellcheck disable=SC2090 + ${_SHUNIT_LINENO_} + if command [ $# -lt 1 -o $# -gt 2 ]; then + _shunit_error "assertFileDoesNotExists() requires one or two arguments; $# given" + _shunit_assertFail + return ${SHUNIT_ERROR} + fi + _shunit_shouldSkip && return ${SHUNIT_TRUE} + + shunit_message_=${__shunit_lineno} + if command [ $# -eq 2 ]; then + shunit_custom_message_=$1 + shunit_file_=$2 + shunit_message_="${shunit_message_}${shunit_custom_message_}" + else + shunit_file_=$1 + shunit_message_="${shunit_message_}The file '${shunit_file_}' does exist" + fi + + shunit_return=${SHUNIT_TRUE} + if command [ -f "${shunit_file_}" ]; then + fail "${shunit_message_}" + shunit_return=${SHUNIT_FALSE} + else + _shunit_assertPass + fi + + unset shunit_message_ shunit_file_ shunit_custom_message_ + return ${shunit_return} +} +# shellcheck disable=SC2016,SC2034 +_ASSERT_FILE_DOES_NOT_EXIST_='eval assertFileDoesNotExists --lineno "${LINENO:-}"' + +# Assert that a directory exists. +# +# Args: +# message: string: failure message [optional] +# directory: string: path of the directory +# Returns: +# integer: success (TRUE/FALSE/ERROR constant) +assertDirectoryExists() { + # shellcheck disable=SC2090 + ${_SHUNIT_LINENO_} + if command [ $# -lt 1 -o $# -gt 2 ]; then + _shunit_error "assertDirectoryExists() requires one or two arguments; $# given" + _shunit_assertFail + return ${SHUNIT_ERROR} + fi + _shunit_shouldSkip && return ${SHUNIT_TRUE} + + shunit_message_=${__shunit_lineno} + if command [ $# -eq 2 ]; then + shunit_custom_message_=$1 + shunit_directory_=$2 + shunit_message_="${shunit_message_}${shunit_custom_message_}" + else + shunit_directory_=$1 + shunit_message_="${shunit_message_}The directory '${shunit_directory_}' does not exist" + fi + + shunit_return=${SHUNIT_TRUE} + if command [ -d "${shunit_directory_}" ]; then + _shunit_assertPass + else + fail "${shunit_message_}" + shunit_return=${SHUNIT_FALSE} + fi + + unset shunit_message_ shunit_directory_ shunit_custom_message_ + return ${shunit_return} +} +# shellcheck disable=SC2016,SC2034 +_ASSERT_DIRECTORY_EXISTS_='eval assertDirectoryExists --lineno "${LINENO:-}"' + +# Assert that a directory does not exist. +# +# Args: +# message: string: failure message [optional] +# directory: string: path of the directory +# Returns: +# integer: success (TRUE/FALSE/ERROR constant) +assertDirectoryDoesNotExist() { + # shellcheck disable=SC2090 + ${_SHUNIT_LINENO_} + if command [ $# -lt 1 -o $# -gt 2 ]; then + _shunit_error "assertDirectoryDoesNotExist() requires one or two arguments; $# given" + _shunit_assertFail + return ${SHUNIT_ERROR} + fi + _shunit_shouldSkip && return ${SHUNIT_TRUE} + + shunit_message_=${__shunit_lineno} + if command [ $# -eq 2 ]; then + shunit_custom_message_=$1 + shunit_directory_=$2 + shunit_message_="${shunit_message_}${shunit_custom_message_}" + else + shunit_directory_=$1 + shunit_message_="${shunit_message_}The directory '${shunit_directory_}' does exist" + fi + + shunit_return=${SHUNIT_TRUE} + if command [ -d "${shunit_directory_}" ]; then + fail "${shunit_message_}" + shunit_return=${SHUNIT_FALSE} + else + _shunit_assertPass + fi + + unset shunit_message_ shunit_directory_ shunit_custom_message_ + return ${shunit_return} +} +# shellcheck disable=SC2016,SC2034 +_ASSERT_DIRECTORY_DOES_NOT_EXIST_='eval assertDirectoryDoesNotExist --lineno "${LINENO:-}"' + #----------------------------------------------------------------------------- # Failure functions. # diff --git a/shunit2_asserts_test.sh b/shunit2_asserts_test.sh index 0625627..88a0bf7 100755 --- a/shunit2_asserts_test.sh +++ b/shunit2_asserts_test.sh @@ -375,8 +375,125 @@ testTooManyArguments() { done } +testAssertFileExists() { + ( assertFileExists "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file exists' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists "${MSG}" "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists "$TH_NON_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file does not exist' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists "${MSG}" "$TH_NON_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file does not exist, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory exists' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists "${MSG}" "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}" + + ( assertFileExists arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}" +} + +testAssertFileDoesNotExist() { + ( assertFileDoesNotExist "$TH_NON_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file does not exist' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist "${MSG}" "$TH_NON_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file does not exist, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file exists' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist "${MSG}" "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory exists' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist "${MSG}" "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}" + + ( assertFileDoesNotExist arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}" +} + +testAssertDirectoryExists() { + ( assertDirectoryExists "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory exists' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists "${MSG}" "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists "$TH_NON_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory does not exist' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists "${MSG}" "$TH_NON_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory does not exist, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file exists' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists "${MSG}" "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'file exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryExists arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}" +} + +testAssertDirectoryDoesNotExist() { + ( assertDirectoryDoesNotExist "$TH_NON_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory does not exist' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist "${MSG}" "$TH_NON_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'directory does not exist, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory exists' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist "${MSG}" "$TH_EXISTING_DIRECTORY" >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithOutput 'directory exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file exists' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist "${MSG}" "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" ) + th_assertTrueWithNoOutput 'file exists, with msg' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too few arguments' $? "${stdoutF}" "${stderrF}" + + ( assertDirectoryDoesNotExist arg1 arg2 arg3 >"${stdoutF}" 2>"${stderrF}" ) + th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}" +} + oneTimeSetUp() { th_oneTimeSetUp + + _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.$(date +%s)" + rm -rf "${_shunit_tmpDir_}" + mkdir "${_shunit_tmpDir_}" + TH_EXISTING_DIRECTORY="${_shunit_tmpDir_}/this_directory_exists" + TH_EXISTING_FILE="${_shunit_tmpDir_}/this_file_exists" + TH_NON_EXISTING_FILE="${_shunit_tmpDir_}/this_directory_does_not_exist" + TH_NON_EXISTING_DIRECTORY="${_shunit_tmpDir_}/this_file_does_not_exist" + mkdir "${TH_EXISTING_DIRECTORY}" + touch "${TH_EXISTING_FILE}" + + MSG='This is a test message' + } # showTestOutput for the most recently run test.