Closed TahaTesser closed 3 months ago
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/github-script@v7.0.1
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
});
@ulusoyca Excellent. I'll close this since it makes a very tiny change.
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.///
).melos run analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?