From 5aa477300ca2fecfcc57f56133bd3e3dd39649d9 Mon Sep 17 00:00:00 2001 From: Shubharanshu Mahapatra Date: Fri, 1 Nov 2024 04:29:51 +0530 Subject: [PATCH] fix: add unit test coverage Signed-off-by: Shubharanshu Mahapatra --- .github/workflows/ci-docs.yaml | 2 + .github/workflows/ci.yaml | 1 + .github/workflows/e2e-linux.yaml | 1 + Makefile | 4 +- coverage/coverage.go | 63 ++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 coverage/coverage.go diff --git a/.github/workflows/ci-docs.yaml b/.github/workflows/ci-docs.yaml index f22da601b..bc31364e1 100644 --- a/.github/workflows/ci-docs.yaml +++ b/.github/workflows/ci-docs.yaml @@ -24,6 +24,7 @@ on: - '!.github/workflows/e2e-macos.yaml' - '!.github/workflows/e2e-windows.yaml' - '!.github/workflows/e2e-linux.yaml' + - '!.github/workflows/ci.yaml' pull_request: branches: - main @@ -39,6 +40,7 @@ on: - '!.github/workflows/e2e-macos.yaml' - '!.github/workflows/e2e-windows.yaml' - '!.github/workflows/e2e-linux.yaml' + - '!.github/workflows/ci.yaml' jobs: git-secrets: diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8a940f611..4d85d1d34 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -96,6 +96,7 @@ jobs: go-version-file: go.mod cache: false - run: make test-unit + # It's recommended to run golangci-lint in a job separate from other jobs (go test, etc) because different jobs run in parallel. go-linter: name: lint diff --git a/.github/workflows/e2e-linux.yaml b/.github/workflows/e2e-linux.yaml index ee3d3c1ff..a5a86cc8e 100644 --- a/.github/workflows/e2e-linux.yaml +++ b/.github/workflows/e2e-linux.yaml @@ -44,6 +44,7 @@ jobs: - name: Check repo out manually if: ${{ (startsWith(inputs.os, 'amazon') && inputs.version == '2' ) }} run: | + echo Hello git clone https://github.com/${GITHUB_REPOSITORY}.git . if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then git config --add remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" diff --git a/Makefile b/Makefile index c5adf413f..e498095fe 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 = 67 .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) -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..eb06bd778 --- /dev/null +++ b/coverage/coverage.go @@ -0,0 +1,63 @@ +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) + } else { + fmt.Printf("Coverage %.0f%% meets the %.0f%% threshold\n", coverage, threshold) + } +}