2023-08-31 11:43:23 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
|
|
|
|
|
|
|
source "${GIT_ROOT}/_assets/scripts/colors.sh"
|
|
|
|
|
2024-02-26 15:04:20 +00:00
|
|
|
if [[ $UNIT_TEST_RERUN_FAILS == 'true' ]]; then
|
|
|
|
GOTESTSUM_EXTRAFLAGS="${GOTESTSUM_EXTRAFLAGS} --rerun-fails"
|
|
|
|
elif [[ $UNIT_TEST_FAILFAST == 'true' ]]; then
|
2023-11-22 16:48:04 +00:00
|
|
|
GOTEST_EXTRAFLAGS="${GOTEST_EXTRAFLAGS} -failfast"
|
2023-11-21 15:39:11 +00:00
|
|
|
fi
|
|
|
|
|
2024-02-26 15:04:20 +00:00
|
|
|
if [[ $UNIT_TEST_USE_DEVELOPMENT_LOGGER == 'false' ]]; then
|
|
|
|
if [[ -z $BUILD_TAGS ]]; then
|
|
|
|
BUILD_TAGS="test_silent"
|
|
|
|
else
|
|
|
|
BUILD_TAGS="${BUILD_TAGS},test_silent"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
if [[ -z "${UNIT_TEST_COUNT}" ]]; then
|
|
|
|
UNIT_TEST_COUNT=1
|
|
|
|
fi
|
|
|
|
|
2024-07-22 11:08:47 +00:00
|
|
|
UNIT_TEST_PACKAGE_TIMEOUT="3m"
|
|
|
|
UNIT_TEST_PACKAGE_TIMEOUT_EXTENDED="36m"
|
2023-11-22 16:48:04 +00:00
|
|
|
|
2023-11-21 15:39:11 +00:00
|
|
|
redirect_stdout() {
|
|
|
|
output_file=$1
|
|
|
|
|
2024-02-28 20:04:27 +00:00
|
|
|
if [[ "${CI}" == 'true' ]]; then
|
2023-11-21 15:39:11 +00:00
|
|
|
cat > "${output_file}";
|
|
|
|
else
|
|
|
|
tee "${output_file}";
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:25:58 +00:00
|
|
|
has_extended_timeout() {
|
|
|
|
local package
|
|
|
|
for package in ${UNIT_TEST_PACKAGES_WITH_EXTENDED_TIMEOUT}; do
|
|
|
|
if [[ "$1" == "${package}" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-02-29 07:59:26 +00:00
|
|
|
is_parallelizable() {
|
|
|
|
local package
|
|
|
|
for package in ${UNIT_TEST_PACKAGES_NOT_PARALLELIZABLE}; do
|
|
|
|
if [[ "$1" == "${package}" ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-02-28 20:04:27 +00:00
|
|
|
run_test_for_package() {
|
|
|
|
local package=$1
|
|
|
|
local iteration=$2
|
|
|
|
echo -e "${GRN}Testing:${RST} ${package} Iteration:${iteration}"
|
2023-08-31 11:43:23 +00:00
|
|
|
package_dir=$(go list -f "{{.Dir}}" "${package}")
|
2024-02-28 20:04:27 +00:00
|
|
|
output_file="${package_dir}/test_${iteration}.log"
|
2024-05-08 08:25:01 +00:00
|
|
|
coverage_file="${package_dir}/test_${iteration}.coverage.out"
|
2023-08-31 11:43:23 +00:00
|
|
|
|
2024-01-24 13:25:58 +00:00
|
|
|
if has_extended_timeout "${package}"; then
|
|
|
|
package_timeout="${UNIT_TEST_PACKAGE_TIMEOUT_EXTENDED}"
|
|
|
|
else
|
|
|
|
package_timeout="${UNIT_TEST_PACKAGE_TIMEOUT}"
|
|
|
|
fi
|
|
|
|
|
2024-02-28 20:04:27 +00:00
|
|
|
local report_file="${package_dir}/report_${iteration}.xml"
|
|
|
|
local rerun_report_file="${package_dir}/report_rerun_fails_${iteration}.txt"
|
2024-02-29 07:59:26 +00:00
|
|
|
local exit_code_file="${package_dir}/exit_code_${iteration}.txt"
|
2024-02-28 20:04:27 +00:00
|
|
|
|
2024-02-26 15:04:20 +00:00
|
|
|
gotestsum_flags="${GOTESTSUM_EXTRAFLAGS}"
|
|
|
|
if [[ "${CI}" == 'true' ]]; then
|
2024-02-28 20:04:27 +00:00
|
|
|
gotestsum_flags="${gotestsum_flags} --junitfile=${report_file} --rerun-fails-report=${rerun_report_file}"
|
2024-02-26 15:04:20 +00:00
|
|
|
fi
|
|
|
|
|
2024-06-19 10:40:52 +00:00
|
|
|
# Cleanup previous coverage reports
|
|
|
|
rm -f ${package_dir}/coverage.out.rerun.*
|
|
|
|
|
|
|
|
PACKAGE_DIR=${package_dir} gotestsum --packages="${package}" ${gotestsum_flags} --raw-command -- \
|
|
|
|
./_assets/scripts/test-with-coverage.sh \
|
|
|
|
${package} \
|
2024-02-26 15:04:20 +00:00
|
|
|
-v ${GOTEST_EXTRAFLAGS} \
|
2024-01-24 13:25:58 +00:00
|
|
|
-timeout "${package_timeout}" \
|
2024-02-28 20:04:27 +00:00
|
|
|
-count 1 \
|
2024-06-21 11:10:44 +00:00
|
|
|
-tags "${BUILD_TAGS}" | \
|
2023-11-21 15:39:11 +00:00
|
|
|
redirect_stdout "${output_file}"
|
2023-08-31 11:43:23 +00:00
|
|
|
|
2024-06-21 11:10:44 +00:00
|
|
|
local go_test_exit=$?
|
|
|
|
|
2024-06-19 10:40:52 +00:00
|
|
|
# Merge package coverage results
|
|
|
|
go run ./cmd/test-coverage-utils/gocovmerge.go ${package_dir}/coverage.out.rerun.* > ${coverage_file}
|
|
|
|
|
|
|
|
# Cleanup coverage reports
|
|
|
|
rm -f ${package_dir}/coverage.out.rerun.*
|
|
|
|
|
2024-02-29 07:59:26 +00:00
|
|
|
echo "${go_test_exit}" > "${exit_code_file}"
|
|
|
|
if [[ "${go_test_exit}" -ne 0 ]]; then
|
|
|
|
if [[ "${CI}" == 'true' ]]; then
|
|
|
|
echo -e "${YLW}Failed, see the log:${RST} ${BLD}${output_file}${RST}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
return ${go_test_exit}
|
|
|
|
}
|
2023-11-22 10:37:05 +00:00
|
|
|
|
2024-05-08 08:25:01 +00:00
|
|
|
if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then
|
|
|
|
cc-test-reporter before-build
|
|
|
|
fi
|
|
|
|
|
2024-05-22 11:40:40 +00:00
|
|
|
echo -e "${GRN}Testing HEAD:${RST} $(git rev-parse HEAD)"
|
|
|
|
|
2024-06-19 16:57:44 +00:00
|
|
|
rm -rf ./**/*.coverage.out
|
|
|
|
|
2024-02-28 20:04:27 +00:00
|
|
|
for package in ${UNIT_TEST_PACKAGES}; do
|
|
|
|
for ((i=1; i<=UNIT_TEST_COUNT; i++)); do
|
2024-02-29 07:59:26 +00:00
|
|
|
if ! is_parallelizable "${package}" || [[ "$UNIT_TEST_FAILFAST" == 'true' ]]; then
|
|
|
|
run_test_for_package "${package}" "${i}"
|
2024-03-06 15:30:19 +00:00
|
|
|
go_test_exit=$?
|
2024-02-28 20:04:27 +00:00
|
|
|
if [[ "$UNIT_TEST_FAILFAST" == 'true' ]]; then
|
2024-02-29 07:59:26 +00:00
|
|
|
if [[ "${go_test_exit}" -ne 0 ]]; then
|
|
|
|
exit "${go_test_exit}"
|
|
|
|
fi
|
2024-02-28 20:04:27 +00:00
|
|
|
fi
|
2024-02-29 07:59:26 +00:00
|
|
|
else
|
|
|
|
run_test_for_package "${package}" "${i}" &
|
2024-02-28 20:04:27 +00:00
|
|
|
fi
|
|
|
|
done
|
2024-02-29 07:59:26 +00:00
|
|
|
wait # Wait for all background jobs to finish
|
2023-08-31 11:43:23 +00:00
|
|
|
done
|
2023-11-21 15:39:11 +00:00
|
|
|
|
2024-05-08 20:57:50 +00:00
|
|
|
# Gather test coverage results
|
2024-06-26 15:16:16 +00:00
|
|
|
rm -f c.out c-full.out
|
|
|
|
go run ./cmd/test-coverage-utils/gocovmerge.go $(find -iname "*.coverage.out") >> c-full.out
|
|
|
|
|
|
|
|
# Filter out test coverage for packages in ./cmd
|
|
|
|
grep -v '^github.com/status-im/status-go/cmd/' c-full.out > c.out
|
2024-05-08 20:57:50 +00:00
|
|
|
|
2024-07-10 12:37:50 +00:00
|
|
|
# Generate HTML coverage report
|
|
|
|
go tool cover -html c.out -o test-coverage.html
|
|
|
|
|
2024-05-08 20:57:50 +00:00
|
|
|
if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then
|
2024-06-10 02:00:10 +00:00
|
|
|
# https://docs.codeclimate.com/docs/jenkins#jenkins-ci-builds
|
|
|
|
GIT_COMMIT=$(git log | grep -m1 -oE '[^ ]+$')
|
2024-07-10 12:37:50 +00:00
|
|
|
cc-test-reporter format-coverage --prefix=github.com/status-im/status-go # To generate 'coverage/codeclimate.json'
|
2024-06-10 02:00:10 +00:00
|
|
|
cc-test-reporter after-build --prefix=github.com/status-im/status-go
|
2024-05-08 20:57:50 +00:00
|
|
|
fi
|
|
|
|
|
2024-02-29 07:59:26 +00:00
|
|
|
shopt -s globstar nullglob # Enable recursive globbing
|
|
|
|
if [[ "${UNIT_TEST_COUNT}" -gt 1 ]]; then
|
|
|
|
for exit_code_file in "${GIT_ROOT}"/**/exit_code_*.txt; do
|
|
|
|
read exit_code < "${exit_code_file}"
|
|
|
|
if [[ "${exit_code}" -ne 0 ]]; then
|
|
|
|
mkdir -p "${GIT_ROOT}/reports"
|
|
|
|
"${GIT_ROOT}/_assets/scripts/test_stats.py" | redirect_stdout "${GIT_ROOT}/reports/test_stats.txt"
|
|
|
|
exit ${exit_code}
|
|
|
|
fi
|
|
|
|
done
|
2024-05-22 11:40:40 +00:00
|
|
|
fi
|