From e82a3e3e849fce4d017f3a74d9a66789a1e1cc1c Mon Sep 17 00:00:00 2001 From: Mike Kolesnik Date: Sun, 29 May 2022 09:55:36 +0300 Subject: [PATCH] Fix unit test directory finding This fixes unit test directory name expansion, while supporting directory names with whitespace characters. Fold `lib/find_functions` into `unit_test.sh` since it's only used in this script. Signed-off-by: Mike Kolesnik --- scripts/shared/lib/find_functions | 26 ---------------- scripts/shared/unit_test.sh | 50 ++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 37 deletions(-) delete mode 100644 scripts/shared/lib/find_functions diff --git a/scripts/shared/lib/find_functions b/scripts/shared/lib/find_functions deleted file mode 100644 index 88382fc6e..000000000 --- a/scripts/shared/lib/find_functions +++ /dev/null @@ -1,26 +0,0 @@ -# shellcheck shell=bash -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 - - 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() { - local excluded_dirs="${*}" - _find_pkg_dirs "./*/*_test.go" -} diff --git a/scripts/shared/unit_test.sh b/scripts/shared/unit_test.sh index 6fa0fbe8a..7f60e42bd 100755 --- a/scripts/shared/unit_test.sh +++ b/scripts/shared/unit_test.sh @@ -3,11 +3,36 @@ set -e source "${SCRIPTS_DIR}/lib/debug_functions" -source "${SCRIPTS_DIR}/lib/find_functions" + +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 + + echo "${find_exclude}" +} + +function _find_pkg_dirs() { + # shellcheck disable=SC2046 + find . $(_build_find_exclude) -path "$1" -printf "%h\0" | sort -z -u +} + +function find_modules() { + # shellcheck disable=SC2046 + find . $(_build_find_exclude) -name go.mod -printf "%h\0" | sort -z -u +} + +function find_unit_test_dirs() { + local excluded_dirs="${*}" + _find_pkg_dirs "./*/*_test.go" +} echo "Looking for packages to test" -modules=($(find_modules)) +readarray -d '' modules < <(find_modules) result=0 @@ -16,18 +41,21 @@ for module in "${modules[@]}"; do excluded_modules="" for exc_module in "${modules[@]}"; do - if [ "$exc_module" != "$module" -a "$exc_module" != "." ]; then - excluded_modules+=" ${exc_module:2}" - fi + 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' ' ')" + # Run in subshell to return to base directory in any case the subshell exits + ( + cd "$module" + readarray -d '' packages < <(find_unit_test_dirs "$excluded_modules" "$@") + [ "${#packages[@]}" -gt 0 ] || exit 0 - 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 + 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=1 + ) done exit $result