Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add hook for checking minimum required coverage #17

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@
files: '\.go$'
language: "script"
description: "Runs `go-ruleguard`, install https://github.com/quasilyte/go-ruleguard"

- id: go-coverage
name: "go-coverage"
entry: run-go-coverage.sh
files: '\.go$'
language: "script"
description: "Runs `go-coverage`, requires golang"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ repos:
- id: golangci-lint # requires github.com/golangci/golangci-lint
args: [--config=.github/linters/.golangci.yml] # optional
- id: go-ruleguard # requires https://github.com/quasilyte/go-ruleguard
args: [rules/rules.go] # required
args: [rules/rules.go] # required
- id: go-coverage
args: [.coverage_config] # file should have param COVERAGE=90
```

## Contributing
Expand Down
48 changes: 48 additions & 0 deletions run-go-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

# Check if the file path argument is provided
if [ $# -eq 0 ]; then
echo "Error: Please provide the path to the coverage threshold file."
exit 1
fi

# Assign the file path to a variable
coverage_file="$1"

# Check if the input file exists
if [ ! -f "$coverage_file" ]; then
echo "Error: Coverage threshold file not found at $coverage_file."
exit 1
fi

# Read the coverage threshold from the file
coverage_line=$(grep -E "^COVERAGE=[0-9]+(\.[0-9]+)?$" "$coverage_file")

# Check if the coverage line is found and extract the coverage value
if [ -z "$coverage_line" ]; then
echo "Error: Invalid format in $coverage_file. Expected format: COVERAGE=90"
exit 1
fi

coverage_threshold=$(echo "$coverage_line" | cut -d '=' -f 2)

# Check if the coverage threshold is a valid number
if ! [[ "$coverage_threshold" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
echo "Error: Invalid coverage threshold in $coverage_file."
exit 1
fi

# Run the Golang tests and get coverage
coverage=$((go test -coverprofile=coverage.out ./... && go tool cover -func=coverage.out) | grep "total:" | awk '{print $3}' | sed 's/%//')

# Print the coverage
echo "Code Coverage: ${coverage}%"

# Compare coverage with the threshold
if (( $(echo "$coverage >= $coverage_threshold" | bc -l) )); then
echo "Coverage is greater than or equal to the $coverage_threshold. Continuing..."
else
echo "Coverage is below the threshold. Exiting..."
rm coverage.out
exit 1
fi