-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
- Loading branch information
1 parent
9c72ad1
commit e28ceb8
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters