From 306675b2d02e088bdaf0056af46173f3f6417e05 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Fri, 16 Jun 2023 20:12:10 -0700 Subject: [PATCH] Code coverage. (#39) * Code coverage. * Code coverage. * Remove commands. --- .github/workflows/go.yml | 9 ++++++--- scripts/coverage.sh | 25 +++++++++++++++++++++++++ test_helper.go | 3 --- 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100755 scripts/coverage.sh diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0204827..a288042 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -31,6 +31,9 @@ jobs: run: go build -v ./... - name: Go test - run: go test -v ./... - - \ No newline at end of file + run: go test -v -coverprofile=coverage.out ./... + + - name: Go coverage + env: + CODE_COVERAGE_THRESHOLD: 100 + run: ./scripts/coverage.sh diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100755 index 0000000..2e5dc93 --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +COVERAGE_FILE=coverage.out + +if [ -z "$CODE_COVERAGE_THRESHOLD" ] +then + echo "No code coverage threshold is set." + exit 0 +fi + +echo "Code coverage threshold ${CODE_COVERAGE_THRESHOLD}%" + +if [ ! -e "$COVERAGE_FILE" ] +then + echo "Coverage file ${COVERAGE_FILE} is not found." + exit 1 +fi + +CODE_COVERAGE=$(go tool cover -func=coverage.out | grep "total:" | grep -E -o '[0-9]+\.[0-9]+') + +if awk "BEGIN { exit !($CODE_COVERAGE < $CODE_COVERAGE_THRESHOLD)}" +then + echo "Current code coverage ${CODE_COVERAGE}% is below the ${CODE_COVERAGE_THRESHOLD}% threshold." + exit 1 +fi diff --git a/test_helper.go b/test_helper.go index da7ecd5..ab3319f 100644 --- a/test_helper.go +++ b/test_helper.go @@ -1,6 +1,3 @@ -//go:build !test -// +build !test - package checker import "testing"