Skip to content

Commit

Permalink
Run unit tests in multiple modules
Browse files Browse the repository at this point in the history
In repositories with multiple modules, tests must be run in each
module separately, excluding any packages which are in a
“sub-module”.

This fixes unit_test.sh to look for modules, and run the tests in each
module. If tests fail, subsequent modules will still be tested, but
the overall result will reflect the failure.

Fixes: #626
Signed-off-by: Stephen Kitt <[email protected]>
  • Loading branch information
skitt authored and tpantelis committed Sep 10, 2021
1 parent e581e97 commit 60a7326
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
15 changes: 12 additions & 3 deletions scripts/shared/lib/find_functions
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# shellcheck shell=bash
function _find_pkg_dirs() {
function _build_find_exclude() {
local find_exclude
excluded_dirs+=" vendor .git .trash-cache bin"

for dir in $excluded_dirs; do
find_exclude+=" -path ./$dir -prune -o"
done

# shellcheck disable=SC2086
find . ${find_exclude} -path "$1" -printf "%h\n" | sort -u
echo "${find_exclude}"
}

function _find_pkg_dirs() {
# shellcheck disable=SC2046
find . $(_build_find_exclude) -path "$1" -printf "%h\n" | sort -u
}

function find_modules() {
# shellcheck disable=SC2046
find . $(_build_find_exclude) -name go.mod -printf "%h\n" | sort -u
}

function find_unit_test_dirs() {
Expand Down
27 changes: 23 additions & 4 deletions scripts/shared/unit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@ source ${SCRIPTS_DIR}/lib/find_functions

echo "Looking for packages to test"

packages="$(find_unit_test_dirs "$@")"
modules=($(find_modules))

echo "Running tests in ${packages}"
[ "${ARCH}" == "amd64" ] && race=-race
${GO:-go} test -v ${race} -cover ${packages} -ginkgo.v -ginkgo.trace -ginkgo.reportPassed -ginkgo.reportFile junit.xml
result=0

for module in "${modules[@]}"; do
printf "Looking for tests in module %s\n" ${module}

excluded_modules=""
for exc_module in "${modules[@]}"; do
if [ "$exc_module" != "$module" -a "$exc_module" != "." ]; then
excluded_modules+=" ${exc_module:2}"
fi
done

packages="$(cd $module; find_unit_test_dirs "$excluded_modules" "$@" | tr '\n' ' ')"

if [ -n "${packages}" ]; then
echo "Running tests in ${packages}"
[ "${ARCH}" == "amd64" ] && race=-race
(cd $module && ${GO:-go} test -v ${race} -cover ${packages} -ginkgo.v -ginkgo.trace -ginkgo.reportPassed -ginkgo.reportFile junit.xml) || result=1
fi
done

exit $result

0 comments on commit 60a7326

Please sign in to comment.