diff --git a/Makefile b/Makefile index bf8644e..fa0b2ce 100644 --- a/Makefile +++ b/Makefile @@ -38,3 +38,8 @@ deps-upgrade: # Removes all items from the Go module cache deps-clean-cache: go clean -modcache + +# Running code coverage +# Generates code coverage report and logs the results +coverage: + sh ./sh/go_deps.sh diff --git a/sh/go_deps.sh b/sh/go_deps.sh new file mode 100644 index 0000000..de39da4 --- /dev/null +++ b/sh/go_deps.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +# Set up directories and files +LOG_DIR="logs" +COVERAGE_FILE="$LOG_DIR/coverage.txt" +PROFILE_FILE="profile.out" + +# Ensure the logs directory exists +mkdir -p "$LOG_DIR" + +# Exit immediately if a command exits with a non-zero status +set -e + +# Initialize the coverage file +>"$COVERAGE_FILE" + +# Run tests with race detection and coverage for each package +for package in $(go list ./... | grep -v vendor); do + go test -race -coverprofile="$PROFILE_FILE" -covermode=atomic "$package" + + # Append profile output to the coverage file if it exists + if [ -f "$PROFILE_FILE" ]; then + cat "$PROFILE_FILE" >>"$COVERAGE_FILE" + rm "$PROFILE_FILE" + fi +done + +echo "🟢 Coverage results written to $COVERAGE_FILE"