Skip to content

Merged test adnd coverage actions. Update badge only when there is ch… #1

Merged test adnd coverage actions. Update badge only when there is ch…

Merged test adnd coverage actions. Update badge only when there is ch… #1

name: Tests & Coverage
on:
push:
branches:
- main
permissions:
contents: write
jobs:
test-and-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y make gcc build-essential gcovr
- name: Build and run unit tests
id: runtest
run: |
cd Lab1
make test_checksum
OUTPUT=$(./Lab1 2>&1)
echo "$OUTPUT"
# Parse Unity output: "X Tests Y Failures Z Ignored"
TESTS=$(echo "$OUTPUT" | grep -oP '(?<=\-\-\-\-\-\s)\d+(?=\sTests)' || echo "0")
FAILURES=$(echo "$OUTPUT" | grep -oP '\d+(?= Failures)' || echo "0")
PASSED=$((TESTS - FAILURES))
echo "tests=$TESTS" >> $GITHUB_OUTPUT
echo "passed=$PASSED" >> $GITHUB_OUTPUT
echo "failed=$FAILURES" >> $GITHUB_OUTPUT
- name: Run coverage analysis
id: runcov
run: |
cd Lab1
make coverage
PCT=$(grep 'Lines executed' coverage_checksum.txt | head -1 | sed -E 's/.*:([0-9.]+)%.*$/\1/')
echo "coverage=$PCT" >> $GITHUB_OUTPUT
- name: Update README badges
if: success()
run: |
TESTS=${{ steps.runtest.outputs.tests }}
PASSED=${{ steps.runtest.outputs.passed }}
FAILED=${{ steps.runtest.outputs.failed }}
PCT=${{ steps.runcov.outputs.coverage }}
# Determine test badge color
if [ "$FAILED" -eq 0 ] && [ "$PASSED" -gt 0 ]; then TEST_COLOR="brightgreen"; else TEST_COLOR="red"; fi
# Determine coverage badge color based on percentage
if (( $(echo "$PCT >= 80" | bc -l) )); then COVERAGE_COLOR="brightgreen"; elif (( $(echo "$PCT >= 60" | bc -l) )); then COVERAGE_COLOR="yellowgreen"; elif (( $(echo "$PCT >= 40" | bc -l) )); then COVERAGE_COLOR="orange"; else COVERAGE_COLOR="red"; fi
# Update both badges in README
sed -i "s|badge/tests-[^-]*-[a-z]*|badge/tests-${PASSED}/${TESTS}-${TEST_COLOR}|" README.md
sed -i "s|badge/coverage-[^-]*-[a-z]*|badge/coverage-${PCT}%25-${COVERAGE_COLOR}|" README.md
# Check if README actually changed
if git diff --quiet README.md; then
echo "Badges unchanged, skipping commit and push"
else
echo "Badges updated, committing changes"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add README.md
git commit -m "Update test (${PASSED}/${TESTS}) and coverage (${PCT}%) badges"
git push
fi