From 507d636d19b5cfd5332db67e1799be992eb8c398 Mon Sep 17 00:00:00 2001 From: Subhransu <47723536+Shubhranshu153@users.noreply.github.com> Date: Wed, 6 Nov 2024 02:16:42 +0530 Subject: [PATCH] fix: add unit test coverage (#1166) Signed-off-by: Shubharanshu Mahapatra --- Makefile | 4 ++- coverage/coverage.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 coverage/coverage.go diff --git a/Makefile b/Makefile index c5adf413f..06e53cfc0 100644 --- a/Makefile +++ b/Makefile @@ -251,9 +251,11 @@ check-licenses: GOBIN=$(GOBIN) go install github.com/google/go-licenses $(GOBIN)/go-licenses check --ignore golang.org/x,github.com/runfinch/finch --ignore github.com/multiformats/go-base36 --allowed_licenses Apache-2.0,BSD-2-Clause,BSD-3-Clause,ISC,MIT --include_tests ./... +COVERAGE_THRESH = 60 .PHONY: test-unit test-unit: - go test $(shell go list ./... | grep -v e2e | grep -v benchmark | grep -v mocks) -shuffle on + go test -cover -coverprofile=coverage.out $(shell go list ./... | grep -v e2e | grep -v benchmark | grep -v mocks | grep -v version | grep -v flog | grep -v system | grep -v fmemory | grep -v coverage |grep -v devcontainer_patch) -shuffle on + go run coverage/coverage.go $(COVERAGE_THRESH) # test-e2e assumes the VM instance doesn't exist, please make sure to remove it before running. # diff --git a/coverage/coverage.go b/coverage/coverage.go new file mode 100644 index 000000000..75e63719e --- /dev/null +++ b/coverage/coverage.go @@ -0,0 +1,66 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package main for unit test coverage parsing +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "os/exec" + "strconv" + "strings" +) + +func main() { + threshold := 100.0 + if len(os.Args) > 1 { + argThreshold, err := strconv.ParseFloat(os.Args[1], 64) + if err != nil { + fmt.Fprintln(os.Stderr, "Invalid threshold value. Please provide a number.") + os.Exit(1) + } + threshold = argThreshold + } + + cmd := exec.Command("go", "tool", "cover", "-func=coverage.out") + output, err := cmd.Output() + if err != nil { + fmt.Fprintln(os.Stderr, "Error executing coverage command:", err) + os.Exit(1) + } + + var coverage float64 + scanner := bufio.NewScanner(strings.NewReader(string(output))) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "total:") { + parts := strings.Fields(line) + if len(parts) > 2 { + coverageStr := strings.TrimSuffix(parts[2], "%") + coverage, err = strconv.ParseFloat(coverageStr, 64) + if err != nil { + fmt.Fprintln(os.Stderr, "Error parsing coverage:", err) + os.Exit(1) + } + coverage = math.Round(coverage) + fmt.Printf("Total Coverage: %.0f%%\n", coverage) + break + } + } + } + + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "Error reading coverage output:", err) + os.Exit(1) + } + + if coverage < threshold { + fmt.Fprintf(os.Stderr, "Coverage %.0f%% is below the %.0f%% threshold\n", coverage, threshold) + os.Exit(1) + } + + fmt.Printf("Coverage %.0f%% meets the %.0f%% threshold\n", coverage, threshold) +}