From c506784acd87813d3fa66cb9e4ebc8c54a86bec5 Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal Date: Mon, 27 Nov 2023 11:10:51 +0530 Subject: [PATCH 1/8] Add go coverage pre commit hook --- .pre-commit-hooks.yaml | 7 ++++++ run-go-coverage.sh | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 run-go-coverage.sh 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/run-go-coverage.sh b/run-go-coverage.sh new file mode 100644 index 0000000..ae6a438 --- /dev/null +++ b/run-go-coverage.sh @@ -0,0 +1,51 @@ +#!/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 threshold. Continuing..." +else + echo "Coverage is below the threshold. Exiting..." + rm coverage.out + exit 1 +fi + +# Clean up generated coverage file +rm coverage.out \ No newline at end of file From 777fdfa8a7b0eb05f30ae0ecf46c21594b3acd03 Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal <115535609+mappie-grofers@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:11:40 +0530 Subject: [PATCH 2/8] Add go coverage pre commit hook (#1) --- .pre-commit-hooks.yaml | 7 ++++++ run-go-coverage.sh | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 run-go-coverage.sh 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/run-go-coverage.sh b/run-go-coverage.sh new file mode 100644 index 0000000..ae6a438 --- /dev/null +++ b/run-go-coverage.sh @@ -0,0 +1,51 @@ +#!/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 threshold. Continuing..." +else + echo "Coverage is below the threshold. Exiting..." + rm coverage.out + exit 1 +fi + +# Clean up generated coverage file +rm coverage.out \ No newline at end of file From 4ef10efd77b564a2dec4a7e97c514eccc4c65c23 Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal Date: Mon, 27 Nov 2023 11:15:46 +0530 Subject: [PATCH 3/8] Add permission --- run-go-coverage.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 run-go-coverage.sh diff --git a/run-go-coverage.sh b/run-go-coverage.sh old mode 100644 new mode 100755 From 7dfadb3662b4eb4c967bcb70d135f5b02786e1ca Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal <115535609+mappie-grofers@users.noreply.github.com> Date: Mon, 27 Nov 2023 11:18:24 +0530 Subject: [PATCH 4/8] Add go coverage (#2) --- run-go-coverage.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 run-go-coverage.sh diff --git a/run-go-coverage.sh b/run-go-coverage.sh old mode 100644 new mode 100755 From 30aeb7c46a7370c46db3e5ddb5916f107904824d Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal Date: Mon, 27 Nov 2023 11:22:45 +0530 Subject: [PATCH 5/8] Add echo --- run-go-coverage.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/run-go-coverage.sh b/run-go-coverage.sh index ae6a438..55adbfa 100755 --- a/run-go-coverage.sh +++ b/run-go-coverage.sh @@ -9,6 +9,8 @@ fi # Assign the file path to a variable coverage_file="$1" +echo "Coverage threshold file: $coverage_file" + # Check if the input file exists if [ ! -f "$coverage_file" ]; then echo "Error: Coverage threshold file not found at $coverage_file." @@ -18,6 +20,7 @@ fi # Read the coverage threshold from the file coverage_line=$(grep -E "^COVERAGE=[0-9]+(\.[0-9]+)?$" "$coverage_file") +echo "Coverage threshold line: $coverage_line" # 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" From 854b3c770492891739d233844cc92071e7d6391e Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal Date: Mon, 27 Nov 2023 11:28:10 +0530 Subject: [PATCH 6/8] Add log --- run-go-coverage.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/run-go-coverage.sh b/run-go-coverage.sh index 55adbfa..a50091f 100755 --- a/run-go-coverage.sh +++ b/run-go-coverage.sh @@ -9,8 +9,6 @@ fi # Assign the file path to a variable coverage_file="$1" -echo "Coverage threshold file: $coverage_file" - # Check if the input file exists if [ ! -f "$coverage_file" ]; then echo "Error: Coverage threshold file not found at $coverage_file." @@ -20,7 +18,6 @@ fi # Read the coverage threshold from the file coverage_line=$(grep -E "^COVERAGE=[0-9]+(\.[0-9]+)?$" "$coverage_file") -echo "Coverage threshold line: $coverage_line" # 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" @@ -43,7 +40,7 @@ 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 threshold. Continuing..." + echo "Coverage is greater than or equal to the $coverage_threshold. Continuing..." else echo "Coverage is below the threshold. Exiting..." rm coverage.out From 07d23289fc9209b598da51087f31e6e662b23976 Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal Date: Mon, 27 Nov 2023 11:44:52 +0530 Subject: [PATCH 7/8] Add readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From fc0c914acab75189d0a26f8a6afdd9bfb7c81501 Mon Sep 17 00:00:00 2001 From: Mahesh Paliwal <115535609+mappie-grofers@users.noreply.github.com> Date: Tue, 16 Apr 2024 12:48:01 +0530 Subject: [PATCH 8/8] Fix (#3) --- run-go-coverage.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/run-go-coverage.sh b/run-go-coverage.sh index a50091f..e6b1c79 100755 --- a/run-go-coverage.sh +++ b/run-go-coverage.sh @@ -45,7 +45,4 @@ else echo "Coverage is below the threshold. Exiting..." rm coverage.out exit 1 -fi - -# Clean up generated coverage file -rm coverage.out \ No newline at end of file +fi \ No newline at end of file