Skip to content

Commit 507d636

Browse files
fix: add unit test coverage (runfinch#1166)
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 9c72ad1 commit 507d636

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

Diff for: Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ check-licenses:
251251
GOBIN=$(GOBIN) go install github.com/google/go-licenses
252252
$(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 ./...
253253

254+
COVERAGE_THRESH = 60
254255
.PHONY: test-unit
255256
test-unit:
256-
go test $(shell go list ./... | grep -v e2e | grep -v benchmark | grep -v mocks) -shuffle on
257+
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
258+
go run coverage/coverage.go $(COVERAGE_THRESH)
257259

258260
# test-e2e assumes the VM instance doesn't exist, please make sure to remove it before running.
259261
#

Diff for: coverage/coverage.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Package main for unit test coverage parsing
5+
package main
6+
7+
import (
8+
"bufio"
9+
"fmt"
10+
"math"
11+
"os"
12+
"os/exec"
13+
"strconv"
14+
"strings"
15+
)
16+
17+
func main() {
18+
threshold := 100.0
19+
if len(os.Args) > 1 {
20+
argThreshold, err := strconv.ParseFloat(os.Args[1], 64)
21+
if err != nil {
22+
fmt.Fprintln(os.Stderr, "Invalid threshold value. Please provide a number.")
23+
os.Exit(1)
24+
}
25+
threshold = argThreshold
26+
}
27+
28+
cmd := exec.Command("go", "tool", "cover", "-func=coverage.out")
29+
output, err := cmd.Output()
30+
if err != nil {
31+
fmt.Fprintln(os.Stderr, "Error executing coverage command:", err)
32+
os.Exit(1)
33+
}
34+
35+
var coverage float64
36+
scanner := bufio.NewScanner(strings.NewReader(string(output)))
37+
for scanner.Scan() {
38+
line := scanner.Text()
39+
if strings.Contains(line, "total:") {
40+
parts := strings.Fields(line)
41+
if len(parts) > 2 {
42+
coverageStr := strings.TrimSuffix(parts[2], "%")
43+
coverage, err = strconv.ParseFloat(coverageStr, 64)
44+
if err != nil {
45+
fmt.Fprintln(os.Stderr, "Error parsing coverage:", err)
46+
os.Exit(1)
47+
}
48+
coverage = math.Round(coverage)
49+
fmt.Printf("Total Coverage: %.0f%%\n", coverage)
50+
break
51+
}
52+
}
53+
}
54+
55+
if err := scanner.Err(); err != nil {
56+
fmt.Fprintln(os.Stderr, "Error reading coverage output:", err)
57+
os.Exit(1)
58+
}
59+
60+
if coverage < threshold {
61+
fmt.Fprintf(os.Stderr, "Coverage %.0f%% is below the %.0f%% threshold\n", coverage, threshold)
62+
os.Exit(1)
63+
}
64+
65+
fmt.Printf("Coverage %.0f%% meets the %.0f%% threshold\n", coverage, threshold)
66+
}

0 commit comments

Comments
 (0)