GAIA-X4PLC-AAD / portal

1 stars 2 forks source link

Git workflow: Check missing tests #105

Open devbysp opened 2 months ago

devbysp commented 2 months ago

Description

The git workflow checks only coverage for files for which a test has already been written. Files which have been changed but there are no test at all will not considered in the test coverage.

Acceptance criteria

devbysp commented 2 months ago

Some work has already been done.

.github/workflows/branch-push.yaml

name: CI pipeline

on:
  pull_request:
    branches:
      - "**"  # Runs on all pull requests

jobs:
  check-test-coverage:
    # The coverage is configured inside 'jest.config.js' by setting the 'coverageThreshold'
    name: Checking test coverage (must be over 90%)
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [ 20.17 ]  # Define Node.js latest LTS version (update as needed)

    steps:
     ...

      # Step 6: Make 'check-missing-tests.sh' executable
      - name: Make script 'check-missing-tests.sh' executable
        run: chmod +x ./check-missing-tests.sh

      # Step 7: Fail the job if there are uncovered files (js, ts, jsx, tsx)
      - name: Check missing tests
        run: ./check-missing-tests.sh

check-missing-tests.sh

#!/bin/bash
git fetch origin main

# Step 1: Get the list of changed JS, TS, JSX and TSX files in the src directory
CHANGED_FILES=$(git diff --name-only origin/main..HEAD -- 'src/*.[jt]s' 'src/*.[jt]sx')

# Initialize an empty array to hold files without tests
MISSING_TEST_FILES=()

# Step 2: Loop through each changed file and check if it has a corresponding test file
for FILE in $CHANGED_FILES; do
  # Extract the file path and replace "src/" with "tests/" and add ".test" postfix
  TEST_FILE=$(echo "$FILE" | sed 's|^src/|tests/|' | sed 's|\.[jt]sx\?$|.test&|')

  # Check if the corresponding test file exists
  if [[ ! -f "$TEST_FILE" ]]; then
    MISSING_TEST_FILES+=("$FILE") # Add to missing test files array if test is not found
  fi
done

# Step 3: Report and fail if there are any missing test files
if [ ${#MISSING_TEST_FILES[@]} -ne 0 ]; then
  echo "The following source files do not have corresponding test files:"
  for MISSING_FILE in "${MISSING_TEST_FILES[@]}"; do
    echo "$MISSING_FILE"
  done
  exit 1 # Fail the workflow
else
  echo "All changed files have corresponding test files."
  exit 0 # Success
fi

image

devbysp commented 1 month ago

Pull Request

Description

devbysp commented 1 month ago

Pull Request 2-3

Fixed issues

image