diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 7c27e1c..bddfbdb 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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" diff --git a/README.md b/README.md index 635d52e..0ea0b81 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/run-go-coverage.sh b/run-go-coverage.sh new file mode 100755 index 0000000..e6b1c79 --- /dev/null +++ b/run-go-coverage.sh @@ -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 \ No newline at end of file