From 0a610caf67005702ed621bbbf43e5a161945bfd4 Mon Sep 17 00:00:00 2001 From: BushraS-Protean Date: Tue, 18 Mar 2025 13:16:21 +0530 Subject: [PATCH] Update beckn_ci.yml changes for coverage test --- .github/workflows/beckn_ci.yml | 57 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/.github/workflows/beckn_ci.yml b/.github/workflows/beckn_ci.yml index a260e39..348219a 100644 --- a/.github/workflows/beckn_ci.yml +++ b/.github/workflows/beckn_ci.yml @@ -12,7 +12,7 @@ jobs: lint_and_test: runs-on: ubuntu-latest if: github.event_name == 'pull_request' - timeout-minutes: 5 + timeout-minutes: 10 # Increased timeout due to additional steps steps: # 1. Checkout the code from the test branch (triggered by PR) - name: Checkout code @@ -29,41 +29,52 @@ jobs: run: | 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 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 - name: Run unit tests with coverage run: | - # Find all directories with Go test files and run `go test` on them - find ./ -type f -name '*_test.go' -exec dirname {} \; | sort -u | while read dir; do - echo "Running tests in $dir" - go test -v -coverprofile=coverage.out $dir - go tool cover -func=coverage.out | tee coverage.txt + # Create a directory to store coverage files + mkdir -p $GITHUB_WORKSPACE/coverage_files + + # Find all *_test.go files and run `go test` for each + 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 - # 6. Check if coverage is >= 90%, but only check coverage if tests exist - - name: Check coverage percentage + # 6. List the generated coverage files for debugging purposes + #- 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: | - # Check if coverage file exists - if [ -f coverage.out ]; then - # Extract total coverage percentage from the output - coverage=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') + # Loop through each coverage file in the coverage_files directory + for coverage_file in $GITHUB_WORKSPACE/coverage_files/coverage_*.out; do + echo "Checking coverage for $coverage_file" + + # 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%" - echo "Total coverage: $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 - echo "Coverage is below 90%. Failing the job." + echo "Coverage for $coverage_file is below 90%. Failing the job." exit 1 - else - echo "Coverage is 90% or above. Continuing the job." fi - else - echo "No coverage file found. Skipping coverage check." - fi + done # 7. Build the Go code #- name: Build Go code