Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests check only run when a PR is opened or reopened #310

Closed
wants to merge 1 commit into from

Conversation

TahaTesser
Copy link
Collaborator

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes tests for all changed/updated/fixed behaviors.
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • The package compiles with the minimum Flutter version stated in the pubspec.yaml

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

@ulusoyca
Copy link
Collaborator

I improved your job and extended it. Now it lists the untested dart files.

jobs:
  check-for-tests:
    runs-on: ubuntu-latest
    steps:
      # Step 1: Checkout the code from the repository
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Fetch full history to allow full diff checking

      # Step 2: Identify changed .dart files
      - name: Identify changed .dart files
        id: identify-changes
        run: |
          # Get a list of all added or modified .dart files in the PR
          git diff --name-only --diff-filter=AM origin/master | grep '\.dart$' > changed_files.txt
          
          # Output the changed files for debugging
          echo "Changed .dart files:"
          cat changed_files.txt

      # Step 3: Check for corresponding test files
      - name: Check for corresponding test files
        id: check-tests
        run: |
          # Initialize an empty variable to store files without corresponding tests
          missing_tests=""

          # Loop over each changed .dart file
          while IFS= read -r file; do
            # Skip files that match ignore patterns
            if [[ $file =~ ^(lib/localization/|lib/features/self_service_training/view/manuals/|integration_test/|.*\.g\.dart|.*\.freezed\.dart)$ ]]; then
              continue
            fi
          
            # Check if the file is not a test file and whether it has a corresponding _test.dart file
            if [[ ! $file =~ _test\.dart$ ]]; then
              test_file="${file%.*}_test.dart"
              if ! grep -q "$test_file" changed_files.txt; then
                missing_tests="${missing_tests}${file}\n"
              fi
            fi
          done < changed_files.txt
          
          # Write the list of files missing tests to a file for the next step
          echo -e "$missing_tests" > missing_tests.txt
          
          # Output the files missing tests for debugging
          echo "Files missing tests:"
          cat missing_tests.txt

      # Step 4: Comment on the PR if tests are missing
      - name: Comment on PR if tests are missing
        uses: actions/[email protected]
        with:
          script: |
            const fs = require('fs');
            const missingFiles = fs.readFileSync('missing_tests.txt', 'utf-8').split('\n').filter(file => file);

            console.log("Missing files (parsed):", missingFiles);
            
            // Base comment message indicating the lack of tests, with a friendly tone
            let body = '👋 Hi there! It looks like some files in this PR do not have corresponding tests. 🧪 We aim to maintain high test coverage to ensure code quality and prevent regressions. All changes, especially those affecting core functionality, should be accompanied by relevant tests.';
            
            // If there are any missing files, append them to the comment
            if (missingFiles.length > 0) {
              body += `\n\n🔍 The following files were modified without corresponding tests:\n${missingFiles.map(file => `- ${file}`).join('\n')}`;
            }
            
            // Log the final body message for debugging
            console.log("Final comment body:", body);
            
            // Post the comment on the PR
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: body
            });

@TahaTesser
Copy link
Collaborator Author

@ulusoyca Excellent. I'll close this since it makes a very tiny change.

@TahaTesser TahaTesser closed this Aug 20, 2024
@TahaTesser TahaTesser deleted the test_check_remove_sync branch August 20, 2024 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants