Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file and directory assertions #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
164 changes: 164 additions & 0 deletions shunit2
Expand Up @@ -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.
#
Expand Down
117 changes: 117 additions & 0 deletions shunit2_asserts_test.sh
Expand Up @@ -375,8 +375,125 @@ testTooManyArguments() {
done
}

testAssertFileExists() {
( assertFileExists "$TH_EXISTING_FILE" >"${stdoutF}" 2>"${stderrF}" )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using a variable (i.e. ${TH_EXISTING_FILE}), be explicit (see comment later) about which file you are testing for. This makes the code easier to read, and easier to debug.

E.g., if you chose the name 'this_file_exists', then use exactly that filename in the test rather than a variable.

With unit tests, it is best not to "hide" info inside of variables as it makes it more difficult to read the test, and definitely more difficult to debug as one cannot search for the failed strings as easily.

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.
Expand Down