Skip to content

tests

tests #920

Workflow file for this run

name: tests
on:
push:
branches-ignore:
pull_request:
branches: [ main ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Runs the workflow once per day at 3:15am
schedule:
- cron: '3 16 * * *'
env:
CACHE_NUMBER: 1 # increase to reset cache manually
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
- name: install-extra-deps
uses: ./.github/actions/install-extra-deps
- name: Setup Mambaforge
uses: conda-incubator/setup-miniconda@v2
with:
miniforge-variant: Mambaforge
miniforge-version: latest
activate-environment: fenicsxconcrete
use-mamba: true
- name: Set cache date
run: echo "DATE=$(date +'%Y%m%d')" >> $GITHUB_ENV
- uses: actions/cache@v2
with:
path: "/usr/share/miniconda3/envs/fenicsxconcrete"
key: conda-${{ hashFiles('environment.yml') }}-${{ env.DATE }}-${{ env.CACHE_NUMBER }}
id: cache
- name: Update environment
run: mamba env update -n fenicsxconcrete -f environment.yml
if: steps.cache.outputs.cache-hit != 'true'
- 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
- name: execute example scripts
shell: bash -l {0}
run: |
for file in docs/examples/*.py
do
python $file
done
- name: test docs
shell: bash -l {0}
run: |
cd docs
make html
- name: Create Coverage Badge
if: contains(env.BRANCH_NAME, 'main')
uses: schneegans/[email protected]
with:
auth: ${{ secrets.GIST_TOKEN }}
gistID: c10a5b6d0714b1fe2344eb60918e92f8
filename: fenicsxconcrete_${{ env.BRANCH_NAME }}_coverage.json
label: coverage
message: ${{ env.COVERAGE_PCT }}
valColorRange: ${{ env.COVERAGE_PCT }}
maxColorRange: 95 # value where ligth green starts
minColorRange: 50 # value where red starts
check_coverage:
needs: tests
runs-on: ubuntu-latest
steps:
- name: check coverage
run: |
# 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
# getting coverage number from gist for main branch
MAIN_DATA=$(curl -s -X GET "https://gist.githubusercontent.com/eriktamsen/c10a5b6d0714b1fe2344eb60918e92f8/raw/fenicsxconcrete_main_coverage.json")
MAIN_COVERAGE=$(echo $MAIN_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')
echo "Coverage of main branch: $MAIN_COVERAGE"
declare -i int_main_coverage=$MAIN_COVERAGE
# 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"
exit 1
fi
echo "Passing!!!"