From 716a5371ff4f211cdfb1398bf167e19562fd17d0 Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Fri, 23 Aug 2024 09:21:08 +0200 Subject: [PATCH] add github actions to lint and run tests and run test coverage --- .github/.coveragerc | 4 + .github/workflows/lint-code.yml | 33 ++++++ .github/workflows/pytest.yml | 180 ++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 .github/.coveragerc create mode 100644 .github/workflows/lint-code.yml create mode 100644 .github/workflows/pytest.yml diff --git a/.github/.coveragerc b/.github/.coveragerc new file mode 100644 index 0000000..8ea7c7c --- /dev/null +++ b/.github/.coveragerc @@ -0,0 +1,4 @@ +[run] +omit = nf_class/*-template/* +source = nf_class +relative_files = True \ No newline at end of file diff --git a/.github/workflows/lint-code.yml b/.github/workflows/lint-code.yml new file mode 100644 index 0000000..4e730a7 --- /dev/null +++ b/.github/workflows/lint-code.yml @@ -0,0 +1,33 @@ +name: Lint code formatting +on: + push: + branches: + - dev + pull_request: + release: + types: [published] + +# Cancel if a newer run is started +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + Pre-commit: + runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4 + + - name: Set up Python 3.12 + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5 + with: + python-version: "3.12" + cache: "pip" + + - name: Install pre-commit + run: pip install pre-commit + + - name: Run pre-commit + run: pre-commit run --all-files diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..ea097bf --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,180 @@ +name: Python tests +# This workflow is triggered on pushes and PRs to the repository. +# Only run if we changed a Python file +on: + push: + branches: + - dev + paths-ignore: + - "CHANGELOG.md" + pull_request: + paths-ignore: + - "CHANGELOG.md" + # ignore github workflows except for the current one + - ".github/**" + - "!.github/workflows/pytest.yml" + release: + types: [published] + workflow_dispatch: + +# Cancel if a newer run with the same workflow name is queued +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +jobs: + setup: + runs-on: "ubuntu-latest" + strategy: + matrix: + python-version: ["3.8", "3.12"] + runner: ["ubuntu-latest"] + include: + - python-version: "3.8" + runner: "ubuntu-20.04" + + steps: + - name: Check conditions + id: conditions + run: echo "run-tests=${{ github.ref == 'refs/heads/master' || (matrix.runner == 'ubuntu-20.04' && matrix.python-version == '3.8') }}" >> "$GITHUB_OUTPUT" + + outputs: + python-version: ${{ matrix.python-version }} + runner: ${{ matrix.runner }} + run-tests: ${{ steps.conditions.outputs.run-tests }} + + # create a test matrix based on all python files in /tests + list_tests: + name: Get test file matrix + runs-on: "ubuntu-latest" + steps: + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4 + name: Check out source-code repository + + - name: List tests + id: list_tests + run: | + echo "tests=$(find tests -type f -name "test_*.py" | tac | sed 's/tests\///g' | jq -R -s -c '{test: (split("\n")[:-1])}')" >> $GITHUB_OUTPUT + outputs: + tests: ${{ steps.list_tests.outputs.tests }} + + test: + name: Run ${{matrix.test}} with Python ${{ needs.setup.outputs.python-version }} on ${{ needs.setup.outputs.runner }} + needs: [setup, list_tests] + if: ${{ needs.setup.outputs.run-tests }} + runs-on: ubuntu-latest + strategy: + matrix: ${{ fromJson(needs.list_tests.outputs.tests) }} + fail-fast: false # run all tests even if one fails + steps: + - name: go to subdirectory and change nextflow workdir + run: | + mkdir -p pytest + cd pytest + export NXF_WORK=$(pwd) + + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4 + name: Check out source-code repository + + - name: Set up Python ${{ needs.setup.outputs.python-version }} + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5 + with: + python-version: ${{ needs.setup.outputs.python-version }} + cache: "pip" + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip -r requirements-dev.txt + pip install -e . + + - name: Install dev version of nf-core + # reinstall the dev version of nf-core until v3.0.0 is released + run: | + pip install --upgrade --force-reinstall git+https://github.com/nf-core/tools.git@dev + + - name: Downgrade git to the Ubuntu official repository's version + if: ${{ needs.setup.outputs.runner == 'ubuntu-20.04' && needs.setup.outputs.python-version == '3.8' }} + run: | + sudo apt update + sudo apt remove -y git git-man + sudo add-apt-repository --remove ppa:git-core/ppa + sudo apt install -y git + + - name: Set up Singularity + if: ${{ matrix.test == 'test_download.py'}} + uses: eWaterCycle/setup-singularity@931d4e31109e875b13309ae1d07c70ca8fbc8537 # v7 + with: + singularity-version: 3.8.3 + + - name: Get current date + id: date + run: echo "date=$(date +'%Y-%m')" >> $GITHUB_ENV + + - name: Install Nextflow + uses: nf-core/setup-nextflow@v2 + + - name: Install nf-test + uses: nf-core/setup-nf-test@v1 + + - name: move coveragerc file up + run: | + mv .github/.coveragerc . + + - name: Test with pytest + run: | + python3 -m pytest tests/${{matrix.test}} --color=yes --cov --durations=0 && exit_code=0|| exit_code=$? + + - name: remove slashes from test name + run: | + test=$(echo ${{ matrix.test }} | sed 's/\//__/g') + echo "test=${test}" >> $GITHUB_ENV + + - name: Upload coverage + uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4 + with: + name: coverage_${{ env.test }} + path: .coverage + + coverage: + needs: test + # use the runner given by the input if it is dispatched manually, run on github if it is a rerun or on self-hosted by default + runs-on: ubuntu-latest + steps: + - name: go to subdirectory + run: | + mkdir -p pytest + cd pytest + + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4 + - name: Set up Python 3.12 + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5 + with: + python-version: "3.12" + cache: "pip" + + - name: Install dependencies + run: | + python -m pip install --upgrade pip -r requirements-dev.txt + pip install -e . + + - name: move coveragerc file up + run: | + mv .github/.coveragerc . + + - name: Download all artifacts + uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4 + - name: Run coverage + run: | + coverage combine --keep coverage*/.coverage* + coverage report + coverage xml + + - uses: codecov/codecov-action@5ecb98a3c6b747ed38dc09f787459979aebb39be # v4 + with: + files: coverage.xml + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}