Skip to content

Commit 36fb815

Browse files
author
Tony Zhang
committed
Adding e2e and ut combined coverage checking scripts
1 parent 0a0af97 commit 36fb815

File tree

6 files changed

+38
-37
lines changed

6 files changed

+38
-37
lines changed

.github/workflows/unit-test.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ jobs:
2626
- name: Install Go dependencies
2727
run: go mod download
2828

29-
- name: Build and Test
29+
- name: Build and Testga.
30+
working-directory: ${{ github.workspace }}
3031
run: |
3132
scripts/build.sh
3233
scripts/unit_test.sh
3334
env:
3435
CGO_CFLAGS: "-O -D__BLST_PORTABLE__" # Set the CGO flags to use the portable version of BLST
35-
36-
- name: Check Code Coverage
37-
run: |
38-
scripts/check_coverage_unit_test.sh

scripts/build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ then
3232
fi
3333
if [ "${COVERAGE_MODE:-}" == true ]
3434
then
35-
extra_build_args+=" -cover -race"
35+
# coverage mode has to be 'set' to merge with ut coverage which is 'set'
36+
# i.e. -race cannot be added here.
37+
extra_build_args+=" -cover"
3638
fi
3739

3840
go build -v -ldflags="-X 'github.com/ava-labs/avalanche-cli/cmd.Version=$VERSION' -X github.com/ava-labs/avalanche-cli/pkg/metrics.telemetryToken=$AVALANCHE_CLI_METRICS_TOKEN" $extra_build_args -o $BIN

scripts/check_coverage_unit_test.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

scripts/check_total_coverage.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
coverage_dir=$(PWD)/coverage
4+
combined_coverage_file=$coverage_dir/combined.txt
5+
COVERAGE_THRESHOLD=15.0 # percentage threshold of code coverage required
6+
7+
echo "Generating coverage report in text format..."
8+
go tool covdata merge -i=$coverage_dir/e2e,$coverage_dir/ut -o=$coverage_dir
9+
go tool covdata textfmt -i=./coverage -o $combined_coverage_file
10+
go tool cover -func $combined_coverage_file
11+
12+
echo "Checking total coverage..."
13+
14+
# print current test coverage from report
15+
current_test_coverage=$(go tool cover -func=$combined_coverage_file | grep -e "total" | awk '{print $3}')
16+
echo "Current test coverage is $current_test_coverage"
17+
18+
go tool cover -func=$combined_coverage_file | grep -e "total" | \
19+
awk -v coverageThreshold=$COVERAGE_THRESHOLD '{if (($3 - 0.0) < coverageThreshold) \
20+
{print "Coverage is less than", coverageThreshold, "%. Checking Failed"; exit 1} \
21+
else \
22+
{print "Coverage is greater than or equal to", coverageThreshold, "%. Checking Passed."; exit 0}}'

scripts/run.e2e.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ ACK_GINKGO_RC=true ~/go/bin/ginkgo build $extra_build_args ./tests/e2e
6060

6161
if [ "${COVERAGE_MODE:-}" == true ]
6262
then
63-
export GOCOVERDIR=coverage/e2e
63+
export GOCOVERDIR=$(PWD)/coverage/e2e
6464
echo 'Coverage mode enabled - re-creating coverage dir $GOCOVERDIR'
6565
echo 'It requires the CLI binary to be built by build.sh with COVERAGE_MODE=true too'
6666
rm -rf ${GOCOVERDIR}
@@ -76,11 +76,4 @@ if [[ ${EXIT_CODE} -gt 0 ]]; then
7676
exit ${EXIT_CODE}
7777
else
7878
echo "ALL SUCCESS!"
79-
80-
if [ "${COVERAGE_MODE:-}" == true ]
81-
then
82-
echo "Generating coverage report: ${GOCOVERDIR}/profile.txt"
83-
go tool covdata textfmt -i=${GOCOVERDIR} -o ${GOCOVERDIR}/profile.txt
84-
# go tool cover -func ${GOCOVERDIR}/profile.txt
85-
fi
8679
fi

scripts/unit_test.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
#!/usr/bin/env bash
22

3-
go test -v -coverprofile=coverage.out $(go list ./... | grep -v /tests/ | grep -v '/sdk/')
4-
#go tool cover -func=coverage.out
3+
set -ex
4+
5+
coverage_dir=$PWD/coverage/ut # should be under the same parent folder as e2e coverage dir
6+
7+
echo "Re-creating unit test coverage directory: $coverage_dir"
8+
rm -rf $coverage_dir
9+
mkdir -p $coverage_dir
10+
11+
go test -cover -v $(go list ./... | grep -v /tests/ | grep -v '/sdk/') -args -test.gocoverdir=$coverage_dir
12+

0 commit comments

Comments
 (0)