quaint-telephone-89068
11/01/2022, 5:24 PMshunit2_test
started failing because the shell_source
it uses was no longer present in the sandbox - I had to add an explicit dependency to get the test passing again.
The directory layout is:
build/pants/ci/
BUILD.pants
common.sh
test_common.sh
With `BUILD.pants`:
shell_sources()
shunit2_tests(name="tests", shell="bash")
And `common.sh`:
# shellcheck shell=bash
set -euo pipefail
# Run a pants goal, selecting targets using environment variables.
# Expects TARGETS to be set to 'all' or 'changed'. If ALLOW_EXPLICIT_TARGETS=true you
# can also put a space delimited list of targets in TARGETS.
#
# When calling this function, any additional args you want to set on your goal
# must be set via env vars.
#
# Usage example:
# source build/pants/ci/common.sh
# PANTS_FILTER_TARGET_TYPE=python_test,shunit2_test run_pants test
run_pants() {
if [[ $# -ne 1 ]]; then
echo "::error::Usage: run_pants <goal>" >&2
return 1
fi
local -r goal="$1"
if [[ -z "${PANTS_LOG:-}" ]]; then
PANTS_LOG=/dev/null
fi
local -a pants_args=("$goal")
if [[ "${TARGETS:-}" == "changed" ]]; then
if [[ -z ${PANTS_CHANGED_SINCE:-} ]]; then
echo "::error::Must set PANTS_CHANGED_SINCE if TARGETS=changed" >&2
return 1
fi
if [[ -z ${PANTS_CHANGED_DEPENDEES:-} ]]; then
echo "::error::Must set PANTS_CHANGED_DEPENDEES if TARGETS=changed" >&2
return 1
fi
elif [[ "${TARGETS:-}" == "all" ]]; then
if [[ -n ${PANTS_CHANGED_SINCE:-} ]]; then
echo "::warning::TARGETS=all, unsetting PANTS_CHANGED_SINCE" >&2
unset PANTS_CHANGED_SINCE
fi
pants_args=("${pants_args[@]}" ::)
elif [[ "${ALLOW_EXPLICIT_TARGETS:-false}" == "true" ]]; then
if [[ -z ${TARGETS:-} ]]; then
echo "::error::Must set TARGETS={changed,all,space delimited list of targets}" >&2
return 1
fi
# Expect TARGETS to be a space delimited list of pants targets.
# shellcheck disable=SC2034
readarray -td '' targets < <(xargs -n1 printf "%s\0" <<< "$TARGETS")
pants_args=("${pants_args[@]}" "${targets[@]}")
else
echo "::error::Must explicitly set TARGETS={changed,all}" >&2
return 1
fi
attempts=1
while [ "$attempts" -le 3 ]; do
if ./pants "${pants_args[@]}" |& tee "${PANTS_LOG}"; then
return 0
fi
# We see <https://github.com/pantsbuild/pants/issues/16778> a few times per day, and have
# hit a wall with debugging. Work around it for now by retrying.
if grep 'ImportError: No module named __pants_df_parser' "${PANTS_LOG}"; then
echo "::warning::Hit ImportError on __pants_df_parser, attempt #${attempts}"
rm -r "${PANTS_NAMED_CACHES_DIR}/pex_root"
attempts=$((attempts + 1))
continue
fi
if grep -E "${PANTS_NAMED_CACHES_DIR}/pex_root/venvs/[a-f0-9]+/[a-f0-9]+/pex.+No such file or directory" "${PANTS_LOG}"; then
echo "::warning::Hit 'No such file' error running 'pex', attempt #${attempts}"
rm -r "${PANTS_NAMED_CACHES_DIR}/pex_root"
attempts=$((attempts + 1))
continue
fi
# If we reach this point, we failed for some reason other than the ImportError bug.
break
done
return 1
}
And `test_common.sh`:
#!/usr/bin/env bash
oneTimeSetUp() {
source build/pants/ci/common.sh
}
setUp() {
cat << 'EOF' > ./pants
#!/usr/bin/env bash
echo PANTS_CHANGED_SINCE=${PANTS_CHANGED_SINCE}
echo PANTS_CHANGED_DEPENDEES=${PANTS_CHANGED_DEPENDEES}
echo Args: ${@}
EOF
chmod +x pants
}
testRunPantsMultiArg() {
run_pants foo bar
assertEquals 1 $?
}
testRunPantsTargetsAll() {
mapfile -t output < <(TARGETS=all run_pants lint)
assertEquals 'PANTS_CHANGED_SINCE=' "${output[0]}"
assertEquals 'PANTS_CHANGED_DEPENDEES=' "${output[1]}"
assertEquals 'Args: lint ::' "${output[2]}"
PANTS_CHANGED_SINCE=master TARGETS=all run_pants lint
# PANTS_CHANGED_SINCE should be ignored
assertEquals 0 $?
}
testRunPantsTargetsChanged() {
TARGETS=changed run_pants lint
assertEquals 1 $?
PANTS_CHANGED_SINCE=master TARGETS=changed run_pants lint
assertEquals 1 $?
mapfile -t output < <(
PANTS_CHANGED_SINCE=master PANTS_CHANGED_DEPENDEES=direct TARGETS=changed \
run_pants lint
)
assertEquals 'PANTS_CHANGED_SINCE=master' "${output[0]}"
assertEquals 'PANTS_CHANGED_DEPENDEES=direct' "${output[1]}"
assertEquals 'Args: lint' "${output[2]}"
}
testRunPantsExplicitTargets() {
TARGETS="foo bar" run_pants lint
assertEquals 1 $?
ALLOW_EXPLICIT_TARGETS=true run_pants lint
assertEquals 1 $?
mapfile -t output < <(
TARGETS="foo bar" ALLOW_EXPLICIT_TARGETS=true run_pants lint
)
assertEquals 'PANTS_CHANGED_SINCE=' "${output[0]}"
assertEquals 'PANTS_CHANGED_DEPENDEES=' "${output[1]}"
assertEquals 'Args: lint foo bar' "${output[2]}"
}
Pants version
2.14.0rc0 (and 2.14.0rc1, and the current HEAD of the 2.14.x branch).
OS
Both
pantsbuild/pantsuser
11/01/2022, 5:24 PM