Skip to content

Commit

Permalink
fix: add unit test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Nov 1, 2024
1 parent 9c72ad1 commit e28ceb8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/unit-test-coverage-helper/coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"bufio"
"fmt"
"math"
"os"
"os/exec"
"strconv"
"strings"
)

func main() {
const threshold = 45.0 // Set your desired threshold here

// Execute the coverage command
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)
}

// Parse the output to find the total coverage percentage
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)
}
}
2 changes: 2 additions & 0 deletions .github/workflows/ci-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ jobs:
go-version-file: go.mod
cache: false
- run: make test-unit

- run: |
go run .github/unit-test-coverage-helper/coverage.go
# 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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/e2e-linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"
Expand Down

0 comments on commit e28ceb8

Please sign in to comment.