Skip to content

Commit

Permalink
66 improve error message when coverage is reduced (#67)
Browse files Browse the repository at this point in the history
* testing adding report to github env

* printing coverage report

* trying to fix something

* more tests

* trying to access and print env variable

* trying output vars

* print output vars

* print output vars some more

* fixing output

* reducing complexity

* changing syntax

* trying something

* trying some more

* fix it

* trying some more fixes

* trying some more fixes again

* fixing fixes

* trying it on the real thing

* trying the report

* changing coverage number

* trying something else

* back to last thing

* testing formatting

* testing formatting again

* test output format

* test output format further

* final formatting

* test if in actions

* check name of branch

* fix if condition

* upload coverage only for main
  • Loading branch information
eriktamsen authored Mar 23, 2023
1 parent b2f1adb commit 5432e03
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions .github/workflows/push_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
tests:
runs-on: ubuntu-latest

outputs:
branch_coverage: ${{ steps.pytest.outputs.coverage }}
coverage_report: ${{ steps.pytest.outputs.report }}

steps:
- name: checkout repo content
uses: actions/checkout@v2
Expand All @@ -44,23 +48,34 @@ jobs:
run: mamba env update -n fenicsxconcrete -f environment.yml
if: steps.cache.outputs.cache-hit != 'true'

- name: Test with pytest
- name: pytest
id : pytest
shell: bash -l {0}
run: |
cd $GITHUB_WORKSPACE
# grab the coverage output and also print it to the screen
coverage run -m pytest -s -W error::UserWarning && coverage xml
COVERAGE_REPORT=$(coverage report -m | tee /dev/stderr)
# passing a multiline string to output to use it in next job
echo "report<<EOF" >> $GITHUB_OUTPUT
echo "$COVERAGE_REPORT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# extract the percentage of the total coverage, e.g. `75%`
COVERAGE_PCT=$(echo $COVERAGE_REPORT | grep -oP "TOTAL\s+\d+\s+\d+\s+(\d+%)" | grep -oP "\d+%")
# get only the coverage number without the percentage symbol
COVERAGE_NUM=$(echo $COVERAGE_PCT | grep -oP "\d+")
# active branch name
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/})
# add them to the github env for later usage
echo "COVERAGE_NUM=$(echo $COVERAGE_NUM)" >> $GITHUB_ENV
echo "coverage=$(echo $COVERAGE_NUM)" >> $GITHUB_OUTPUT # to be used in next job
echo "COVERAGE_PCT=$(echo $COVERAGE_PCT)" >> $GITHUB_ENV
# removing all possible slashes in file names, they seem to appear during Pull Requests
echo "BRANCH_NAME=$(echo $BRANCH_NAME | sed "s/\//_/g")" >> $GITHUB_ENV
Expand All @@ -71,6 +86,7 @@ jobs:
make html
- name: Create Coverage Badge
if: contains(env.BRANCH_NAME, 'main')
uses: schneegans/dynamic-badges-action@v1.6.0
with:
auth: ${{ secrets.GIST_TOKEN }}
Expand All @@ -88,10 +104,8 @@ jobs:
steps:
- name: check coverage
run: |
# getting coverage number from gist for this branch
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed "s/\//_/g")
BRANCH_DATA=$(curl -s -X GET "https://gist.githubusercontent.com/eriktamsen/c10a5b6d0714b1fe2344eb60918e92f8/raw/fenicsxconcrete_{$BRANCH_NAME}_coverage.json")
BRANCH_COVERAGE=$(echo $BRANCH_DATA | grep -o -P 'message.{3,6}' | sed -e 's/"//g' | sed -e 's/://g' | sed -e 's/message//g' | sed -e 's/%//g' | sed -e 's/,//g')
# getting coverage number from output of first job for this branch
BRANCH_COVERAGE=${{ needs.tests.outputs.branch_coverage }}
echo "Coverage of current branch: $BRANCH_COVERAGE"
declare -i int_branch_coverage=$BRANCH_COVERAGE
Expand All @@ -104,6 +118,12 @@ jobs:
# compute difference
delta_coverage=$(( int_branch_coverage - int_main_coverage ))
echo "Increase in coverage: $delta_coverage"
echo "Coverage report:"
echo "-----------------------------------------------------------------------------------------------"
echo "${{ needs.tests.outputs.coverage_report }}"
echo "-----------------------------------------------------------------------------------------------"
if [[ $delta_coverage -lt 0 ]]
then
echo "The current branch has lower coverage than the main branch, please improve your code"
Expand Down

0 comments on commit 5432e03

Please sign in to comment.