Skip to content

Allow nested inclusions (sourcing tests from within tests) #175

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions shunit2
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ _shunit_prepForSourcing() {
unset _shunit_script_
}

# Extract list of functions to run tests against.
# Extract list of functions to run tests against for a single script
#
# Args:
# script: string: name of script to extract functions from
Expand All @@ -1279,6 +1279,46 @@ _shunit_extractTestFunctions() {
unset _shunit_regex_ _shunit_script_
}

# Identify all files sourced from a single test script.
#
# Args:
# script: path of script to check for inclusions
#
# Returns:
# string of included script paths
_shunit_sourcesForFile() {
grep '^\. ' "$1" | \
while read shunit_dot_ shunit_file_; do
eval echo "${shunit_file_}"
done | grep -v "/shunit2$" | sort -u
}

# Recursively identify all files sourced from a test script.
#
# Args:
# script: path of script to check for inclusions
#
# Returns:
# string of included script paths
_shunit_allTestScripts() {
for shunit_script_ in `_shunit_sourcesForFile "$1"`; do
echo "${shunit_script_}"
_shunit_allTestScripts "${shunit_script_}"
done
}

# Extract list of functions to run tests against.
#
# Args:
# script: string: name of root script to extract functions from
# Returns:
# string: of function names
_shunit_extractAllTestFunctions() {
for shunit_script_ in `_shunit_allTestScripts "$1"`; do
_shunit_extractTestFunctions "${shunit_script_}"
done
}

#------------------------------------------------------------------------------
# Main.
#
Expand Down Expand Up @@ -1353,7 +1393,7 @@ fi

# If no tests or suite specified, dynamically build a list of functions.
if ${__SHUNIT_BUILTIN} [ -z "${__shunit_suite}" ]; then
shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"`
shunit_funcs_=`_shunit_extractAllTestFunctions "${__shunit_script}"`
for shunit_func_ in ${shunit_funcs_}; do
suite_addTest "${shunit_func_}"
done
Expand Down