Update beckn_ci.yml

changes for coverage test
This commit is contained in:
BushraS-Protean
2025-03-18 13:16:21 +05:30
committed by GitHub
parent 9722c3bf68
commit 0a610caf67

View File

@@ -12,7 +12,7 @@ jobs:
lint_and_test: lint_and_test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.event_name == 'pull_request' if: github.event_name == 'pull_request'
timeout-minutes: 5 timeout-minutes: 10 # Increased timeout due to additional steps
steps: steps:
# 1. Checkout the code from the test branch (triggered by PR) # 1. Checkout the code from the test branch (triggered by PR)
- name: Checkout code - name: Checkout code
@@ -29,41 +29,52 @@ jobs:
run: | run: |
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# 4. Run golangci-lint on the entire APP_DIRECTORY (including subdirectories) ${{ env.APP_DIRECTORY }} # 4. Run golangci-lint on the entire repo, starting from the root directory
- name: Run golangci-lint - name: Run golangci-lint
run: | run: |
golangci-lint run ./... golangci-lint run ./... # This will lint all Go files in the repo and subdirectories
# 5. Run unit tests with coverage in the entire repository # 5. Run unit tests with coverage in the entire repository
- name: Run unit tests with coverage - name: Run unit tests with coverage
run: | run: |
# Find all directories with Go test files and run `go test` on them # Create a directory to store coverage files
find ./ -type f -name '*_test.go' -exec dirname {} \; | sort -u | while read dir; do mkdir -p $GITHUB_WORKSPACE/coverage_files
echo "Running tests in $dir"
go test -v -coverprofile=coverage.out $dir # Find all *_test.go files and run `go test` for each
go tool cover -func=coverage.out | tee coverage.txt find ./ -type f -name '*_test.go' | while read test_file; do
# Get the directory of the test file
test_dir=$(dirname "$test_file")
# Get the name of the Go file associated with the test
go_file="${test_file/_test.go/.go}"
# Run tests and store coverage for each Go file in a separate file
echo "Running tests in $test_dir for $go_file"
go test -v -coverprofile=$GITHUB_WORKSPACE/coverage_files/coverage_$(basename "$go_file" .go).out $test_dir
done done
# 6. Check if coverage is >= 90%, but only check coverage if tests exist # 6. List the generated coverage files for debugging purposes
- name: Check coverage percentage #- name: List coverage files
#run: |
#echo "Listing all generated coverage files:"
#ls -l $GITHUB_WORKSPACE/coverage_files/
# 7. Check coverage for each generated coverage file
- name: Check coverage for each test file
run: | run: |
# Check if coverage file exists # Loop through each coverage file in the coverage_files directory
if [ -f coverage.out ]; then for coverage_file in $GITHUB_WORKSPACE/coverage_files/coverage_*.out; do
# Extract total coverage percentage from the output echo "Checking coverage for $coverage_file"
coverage=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
echo "Total coverage: $coverage%" # Get the coverage percentage for each file
coverage=$(go tool cover -func=$coverage_file | grep total | awk '{print $3}' | sed 's/%//')
echo "Coverage for $coverage_file: $coverage%"
# Check if coverage is greater than or equal to 90% # If coverage is below threshold (90%), fail the job
if (( $(echo "$coverage < 90" | bc -l) )); then if (( $(echo "$coverage < 90" | bc -l) )); then
echo "Coverage is below 90%. Failing the job." echo "Coverage for $coverage_file is below 90%. Failing the job."
exit 1 exit 1
else
echo "Coverage is 90% or above. Continuing the job."
fi fi
else done
echo "No coverage file found. Skipping coverage check."
fi
# 7. Build the Go code # 7. Build the Go code
#- name: Build Go code #- name: Build Go code