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-09-24 15:33:26 +00:00
|
|
|
source "${GIT_ROOT}/_assets/scripts/codecov.sh"
|
2023-08-31 11:43:23 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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-08-26 09:03:26 +00:00
|
|
|
run_test_for_packages() {
|
2024-09-03 11:50:09 +00:00
|
|
|
local packages="$1"
|
|
|
|
local iteration="$2"
|
|
|
|
local count="$3"
|
|
|
|
local single_timeout="$4"
|
|
|
|
local log_message="$5"
|
|
|
|
|
|
|
|
local output_file="test_${iteration}.log"
|
|
|
|
local coverage_file="test_${iteration}.coverage.out"
|
|
|
|
local report_file="report_${iteration}.xml"
|
|
|
|
local rerun_report_file="report_rerun_fails_${iteration}.txt"
|
|
|
|
local exit_code_file="exit_code_${iteration}.txt"
|
|
|
|
local timeout="$(( single_timeout * count))m"
|
|
|
|
|
|
|
|
if [[ "${UNIT_TEST_DRY_RUN}" == 'true' ]]; then
|
|
|
|
echo -e "${GRN}Dry run ${iteration}. message:${RST} ${log_message}\n"\
|
|
|
|
"${YLW}Dry run ${iteration}. packages:${RST} ${packages}\n"\
|
|
|
|
"${YLW}Dry run ${iteration}. count:${RST} ${count}\n"\
|
|
|
|
"${YLW}Dry run ${iteration}. timeout:${RST} ${timeout}"
|
|
|
|
return 0
|
|
|
|
fi
|
2024-01-24 13:25:58 +00:00
|
|
|
|
2024-09-03 11:50:09 +00:00
|
|
|
echo -e "${GRN}Testing:${RST} ${log_message}. Iteration ${iteration}. -test.count=${count}. Timeout: ${timeout}"
|
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-09-03 11:50:09 +00:00
|
|
|
# Prepare env variables for `test-with-coverage.sh`
|
|
|
|
export TEST_WITH_COVERAGE_PACKAGES="${packages}"
|
|
|
|
export TEST_WITH_COVERAGE_COUNT="${count}"
|
|
|
|
export TEST_WITH_COVERAGE_REPORTS_DIR="$(mktemp -d)"
|
|
|
|
|
2024-06-19 10:40:52 +00:00
|
|
|
# Cleanup previous coverage reports
|
2024-09-03 11:50:09 +00:00
|
|
|
rm -f "${TEST_WITH_COVERAGE_REPORTS_DIR}/coverage.out.rerun.*"
|
2024-06-19 10:40:52 +00:00
|
|
|
|
2024-08-26 09:03:26 +00:00
|
|
|
# Run tests
|
2024-09-03 11:50:09 +00:00
|
|
|
gotestsum --packages="${packages}" ${gotestsum_flags} --raw-command -- \
|
2024-06-19 10:40:52 +00:00
|
|
|
./_assets/scripts/test-with-coverage.sh \
|
2024-09-03 11:50:09 +00:00
|
|
|
${GOTEST_EXTRAFLAGS} \
|
|
|
|
-timeout "${timeout}" \
|
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
|
2024-09-03 11:50:09 +00:00
|
|
|
go run ./cmd/test-coverage-utils/gocovmerge.go ${TEST_WITH_COVERAGE_REPORTS_DIR}/coverage.out.rerun.* > ${coverage_file}
|
2024-09-24 15:33:26 +00:00
|
|
|
rm -f "${TEST_WITH_COVERAGE_REPORTS_DIR}/coverage.out.rerun.*"
|
2024-06-19 10:40:52 +00:00
|
|
|
|
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-06-19 16:57:44 +00:00
|
|
|
rm -rf ./**/*.coverage.out
|
|
|
|
|
2024-08-26 09:03:26 +00:00
|
|
|
echo -e "${GRN}Testing HEAD:${RST} $(git rev-parse HEAD)"
|
2024-09-03 11:50:09 +00:00
|
|
|
|
|
|
|
DEFAULT_TIMEOUT_MINUTES=5
|
|
|
|
PROTOCOL_TIMEOUT_MINUTES=45
|
|
|
|
|
|
|
|
HAS_PROTOCOL_PACKAGE=true
|
|
|
|
if [[ $(echo "${UNIT_TEST_PACKAGES}" | grep -E '\s?\S+protocol\s+') == "" ]]; then
|
|
|
|
HAS_PROTOCOL_PACKAGE=false
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $HAS_PROTOCOL_PACKAGE == 'false' ]]; then
|
|
|
|
# This is the default single-line flow for testing all packages
|
|
|
|
# The `else` branch is temporary and will be removed once the `protocol` package runtime is optimized.
|
|
|
|
run_test_for_packages "${UNIT_TEST_PACKAGES}" "0" "${UNIT_TEST_COUNT}" "${DEFAULT_TIMEOUT_MINUTES}" "All packages"
|
|
|
|
else
|
|
|
|
# Spawn a process to test all packages except `protocol`
|
|
|
|
UNIT_TEST_PACKAGES_FILTERED=$(echo "${UNIT_TEST_PACKAGES}" | tr ' ' '\n' | grep -v '/protocol$' | tr '\n' ' ')
|
|
|
|
run_test_for_packages "${UNIT_TEST_PACKAGES_FILTERED}" "0" "${UNIT_TEST_COUNT}" "${DEFAULT_TIMEOUT_MINUTES}" "All packages except 'protocol'" &
|
|
|
|
|
|
|
|
# Spawn separate processes to run `protocol` package
|
|
|
|
for ((i=1; i<=UNIT_TEST_COUNT; i++)); do
|
|
|
|
run_test_for_packages github.com/status-im/status-go/protocol "${i}" 1 "${PROTOCOL_TIMEOUT_MINUTES}" "Only 'protocol' package" &
|
|
|
|
done
|
|
|
|
|
|
|
|
wait
|
|
|
|
fi
|
2023-11-21 15:39:11 +00:00
|
|
|
|
2024-09-09 15:28:10 +00:00
|
|
|
# When running in PRs (count=1), early exit if any test failed.
|
|
|
|
# When running nightly (count>1), generate test stats ant coverage reports anyway.
|
|
|
|
if [[ $UNIT_TEST_COUNT -eq 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
|
|
|
|
echo -e "${RED}Testing failed${RST}, exit code: ${exit_code}"
|
|
|
|
exit ${exit_code}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2024-09-04 10:24:26 +00:00
|
|
|
|
2024-05-08 20:57:50 +00:00
|
|
|
# Gather test coverage results
|
2024-09-03 11:50:09 +00:00
|
|
|
merged_coverage_report="coverage_merged.out"
|
|
|
|
final_coverage_report="c.out" # Name expected by cc-test-reporter
|
|
|
|
coverage_reports=$(find . -iname "*.coverage.out")
|
|
|
|
rm -f ${final_coverage_report} ${merged_coverage_report}
|
|
|
|
|
|
|
|
echo -e "${GRN}Gathering test coverage results: ${RST} output: ${merged_coverage_report}, input: ${coverage_reports}"
|
|
|
|
echo $coverage_reports | xargs go run ./cmd/test-coverage-utils/gocovmerge.go > ${merged_coverage_report}
|
2024-06-26 15:16:16 +00:00
|
|
|
|
|
|
|
# Filter out test coverage for packages in ./cmd
|
2024-09-03 11:50:09 +00:00
|
|
|
echo -e "${GRN}Filtering test coverage packages:${RST} ./cmd"
|
|
|
|
grep -v '^github.com/status-im/status-go/cmd/' ${merged_coverage_report} > ${final_coverage_report}
|
2024-05-08 20:57:50 +00:00
|
|
|
|
2024-07-10 12:37:50 +00:00
|
|
|
# Generate HTML coverage report
|
2024-09-24 15:33:26 +00:00
|
|
|
convert_coverage_to_html ${final_coverage_report} "test-coverage.html"
|
2024-07-10 12:37:50 +00:00
|
|
|
|
2024-09-03 11:50:09 +00:00
|
|
|
# Upload coverage report to CodeClimate
|
2024-05-08 20:57:50 +00:00
|
|
|
if [[ $UNIT_TEST_REPORT_CODECLIMATE == 'true' ]]; then
|
2024-09-03 11:50:09 +00:00
|
|
|
echo -e "${GRN}Uploading coverage report to CodeClimate${RST}"
|
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-09-05 09:11:16 +00:00
|
|
|
if [[ $UNIT_TEST_REPORT_CODECOV == 'true' ]]; then
|
2024-09-24 15:33:26 +00:00
|
|
|
report_to_codecov "report_*.xml" ${final_coverage_report} "unit"
|
2024-09-05 09:11:16 +00:00
|
|
|
fi
|
|
|
|
|
2024-09-03 11:50:09 +00:00
|
|
|
# Generate report with test stats
|
2024-02-29 07:59:26 +00:00
|
|
|
shopt -s globstar nullglob # Enable recursive globbing
|
|
|
|
if [[ "${UNIT_TEST_COUNT}" -gt 1 ]]; then
|
2024-09-03 11:50:09 +00:00
|
|
|
for exit_code_file in "${GIT_ROOT}"/**/exit_code_*.txt; do
|
2024-02-29 07:59:26 +00:00
|
|
|
read exit_code < "${exit_code_file}"
|
|
|
|
if [[ "${exit_code}" -ne 0 ]]; then
|
2024-09-03 11:50:09 +00:00
|
|
|
echo -e "${GRN}Generating test stats${RST}, exit code: ${exit_code}"
|
2024-02-29 07:59:26 +00:00
|
|
|
mkdir -p "${GIT_ROOT}/reports"
|
2024-09-03 11:50:09 +00:00
|
|
|
"${GIT_ROOT}/_assets/scripts/test_stats.py" | tee "${GIT_ROOT}/reports/test_stats.txt"
|
2024-02-29 07:59:26 +00:00
|
|
|
exit ${exit_code}
|
|
|
|
fi
|
|
|
|
done
|
2024-05-22 11:40:40 +00:00
|
|
|
fi
|
2024-09-03 11:50:09 +00:00
|
|
|
|
|
|
|
echo -e "${GRN}Testing finished${RST}"
|