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 2 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+297 −0
Diff settings

Always

Just for now

@@ -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
164 shunit2
@@ -573,6 +573,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_
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_
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_
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_
return ${shunit_return}
}
# shellcheck disable=SC2016,SC2034
_ASSERT_DIRECTORY_DOES_NOT_EXIST_='eval assertDirectoryDoesNotExist --lineno "${LINENO:-}"'

#-----------------------------------------------------------------------------
# Failure functions.
#
@@ -246,6 +246,110 @@ testAssertFalse() {
th_assertFalseWithError 'too many arguments' $? "${stdoutF}" "${stderrF}"
}

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

@@ -226,6 +226,13 @@ _th_showOutput() {
unset _th_return_ _th_stdout_ _th_stderr_
}

# Constants used to test file and directory assertions
TH_EXISTING_DIRECTORY=$(exec 2>/dev/null;cd -- "$(dirname "$0")" || exit; unset PWD; /usr/bin/pwd || /bin/pwd || pwd)
TH_EXISTING_FILE="${TH_EXISTING_DIRECTORY}/$0"
TH_NON_EXISTING_FILE="$TH_EXISTING_DIRECTORY/non-existing-file"
TH_NON_EXISTING_DIRECTORY="$TH_EXISTING_DIRECTORY/non-existing-directory"
export TH_EXISTING_DIRECTORY TH_EXISTING_FILE TH_NON_EXISTING_FILE TH_NON_EXISTING_DIRECTORY

#
# Main.
#
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.