Skip to content

Commit

Permalink
Fix unit test directory finding
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mkolesnik committed May 30, 2022
1 parent 4fbd096 commit e82a3e3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
26 changes: 0 additions & 26 deletions scripts/shared/lib/find_functions

This file was deleted.

50 changes: 39 additions & 11 deletions scripts/shared/unit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit e82a3e3

Please sign in to comment.