|
| 1 | +# This is the main workflow for testing the code, notebooks and package. |
| 2 | +# It is divided into three jobs: |
| 3 | +# 1. Code-quality: |
| 4 | +# - Check the validity of pyproject.toml |
| 5 | +# - Check code linting |
| 6 | +# - Check code formatting |
| 7 | +# - Check Jupyter notebooks formatting |
| 8 | +# - Check formatting of Markdown, YAML, TOML, etc. files |
| 9 | +# 2. Test-code: |
| 10 | +# - Test the code base and upload coverage to Codecov |
| 11 | +# - Create the Python package |
| 12 | +# - Upload the Python package for the next job |
| 13 | +# 3. Test-package: |
| 14 | +# - Download the Python package from the previous job |
| 15 | +# - Install the downloaded Python package |
| 16 | +# - Test the code base |
| 17 | +# - Check if Jupiter Notebooks run without errors |
| 18 | + |
| 19 | +name: Test code, notebooks and package |
| 20 | + |
| 21 | +on: |
| 22 | + # Trigger the workflow on push |
| 23 | + push: |
| 24 | + # Every branch |
| 25 | + branches: |
| 26 | + - '**' |
| 27 | + # But do not run this workflow on creating a new tag starting with 'v', e.g. 'v1.0.3' (see pypi-publish.yml) |
| 28 | + tags-ignore: |
| 29 | + - 'v*' |
| 30 | + # Trigger the workflow on pull request |
| 31 | + pull_request: |
| 32 | + branches: |
| 33 | + - '**' |
| 34 | + # Allows you to run this workflow manually from the Actions tab |
| 35 | + workflow_dispatch: |
| 36 | + |
| 37 | +# Allow only one concurrent workflow, skipping runs queued between the run in-progress and latest queued. |
| 38 | +# And cancel in-progress runs. |
| 39 | +concurrency: |
| 40 | + group: |
| 41 | + ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} |
| 42 | + cancel-in-progress: true |
| 43 | + |
| 44 | +jobs: |
| 45 | + # Job 1: Check code quality and consistency |
| 46 | + code-quality: |
| 47 | + strategy: |
| 48 | + matrix: |
| 49 | + os: [ubuntu-latest] |
| 50 | + python-version: ['3.12'] |
| 51 | + |
| 52 | + runs-on: ${{ matrix.os }} |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + fetch-depth: '0' # full history with tags to get the version number by versioningit |
| 59 | + |
| 60 | + - name: Set up Python ${{ matrix.python-version }} |
| 61 | + uses: actions/setup-python@v5 |
| 62 | + with: |
| 63 | + python-version: ${{ matrix.python-version }} |
| 64 | + |
| 65 | + - name: Install Python dependencies |
| 66 | + # Install 'validate-pyproject' for checking pyproject.toml |
| 67 | + # Install 'ruff' for code linting and formatting |
| 68 | + # Install 'nbqa' for quality assurance of Jupyter notebooks |
| 69 | + run: pip install 'validate-pyproject[all]' ruff nbqa |
| 70 | + |
| 71 | + - name: Install npm dependencies |
| 72 | + # Install 'prettier' for code formatting of Markdown, YAML, etc. files |
| 73 | + # Install 'prettier-plugin-toml' plugin for code formatting of TOML files |
| 74 | + run: npm install prettier prettier-plugin-toml --save-dev --save-exact |
| 75 | + |
| 76 | + # Check the validity of pyproject.toml |
| 77 | + - name: Check validity of pyproject.toml |
| 78 | + id: check_pyproject |
| 79 | + continue-on-error: true |
| 80 | + run: validate-pyproject pyproject.toml |
| 81 | + |
| 82 | + # Check code linting with Ruff in the project root |
| 83 | + - name: Check code linting |
| 84 | + id: check_code_linting |
| 85 | + continue-on-error: true |
| 86 | + run: ruff check . |
| 87 | + |
| 88 | + - name: Suggestion to fix code linting issues (for *.py files) |
| 89 | + if: steps.check_code_linting.outcome == 'failure' |
| 90 | + run: |
| 91 | + echo "In project root run 'ruff check . --fix' and commit changes to |
| 92 | + fix issues." |
| 93 | + |
| 94 | + # Check code formatting with Ruff in the project root |
| 95 | + - name: Check code formatting |
| 96 | + id: check_code_formatting |
| 97 | + continue-on-error: true |
| 98 | + run: ruff format . --check |
| 99 | + |
| 100 | + - name: Suggestion to fix code formatting issues (for *.py files) |
| 101 | + if: steps.check_code_formatting.outcome == 'failure' |
| 102 | + run: |
| 103 | + echo "In project root run 'ruff format .' and commit changes to fix |
| 104 | + issues." |
| 105 | + |
| 106 | + # Check Jupyter notebooks with nbQA in the sample directory |
| 107 | + - name: Check Jupyter notebooks formatting |
| 108 | + id: check_notebooks |
| 109 | + continue-on-error: true |
| 110 | + run: nbqa ruff examples/ |
| 111 | + |
| 112 | + - name: Suggestion to fix notebook formatting issues (for *.ipynb files) |
| 113 | + if: steps.check_notebooks.outcome == 'failure' |
| 114 | + run: |
| 115 | + echo "In project root run 'nbqa ruff examples/ --fix' and commit |
| 116 | + changes to fix issues." |
| 117 | + |
| 118 | + # Check formatting of Markdown, YAML, TOML, etc. files with Prettier in the project root |
| 119 | + - name: Check formatting of Markdown, YAML, TOML, etc. files |
| 120 | + id: check_others_formatting |
| 121 | + continue-on-error: true |
| 122 | + run: npx prettier . --check --config=prettierrc.toml |
| 123 | + |
| 124 | + - name: Suggestion to fix non-code formatting issues (for *.md, etc.) |
| 125 | + if: steps.check_others_formatting.outcome == 'failure' |
| 126 | + run: |
| 127 | + echo "In project root run 'npx prettier . --write |
| 128 | + --config=prettierrc.toml' and commit changes to fix issues." |
| 129 | + |
| 130 | + - name: Force fail if any of the previous steps failed |
| 131 | + if: | |
| 132 | + steps.check_pyproject.outcome == 'failure' || |
| 133 | + steps.check_code_linting.outcome == 'failure' || |
| 134 | + steps.check_code_formatting.outcome == 'failure' || |
| 135 | + steps.check_notebooks.outcome == 'failure' || |
| 136 | + steps.check_others_formatting.outcome == 'failure' |
| 137 | + run: exit 1 |
| 138 | + |
| 139 | + # Job 2: Test code and upload coverage to Codecov. |
| 140 | + test-code: |
| 141 | + needs: code-quality # previous job 'code-quality' need to be finished first |
| 142 | + |
| 143 | + # current job matrix. if modified, remember to UPDATE the strategy in the next job |
| 144 | + strategy: |
| 145 | + fail-fast: false |
| 146 | + matrix: |
| 147 | + os: [ubuntu-24.04, windows-2022, macos-13, macos-14] |
| 148 | + python-version: ['3.10', '3.11', '3.12'] |
| 149 | + |
| 150 | + runs-on: ${{ matrix.os }} |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Checkout repository |
| 154 | + uses: actions/checkout@v4 |
| 155 | + with: |
| 156 | + fetch-depth: '0' # full history with tags to get the version number by versioningit |
| 157 | + |
| 158 | + - name: Set up Python ${{ matrix.python-version }} |
| 159 | + uses: actions/setup-python@v5 |
| 160 | + with: |
| 161 | + python-version: ${{ matrix.python-version }} |
| 162 | + |
| 163 | + - name: Upgrade package installer for Python |
| 164 | + run: python -m pip install --upgrade pip |
| 165 | + |
| 166 | + - name: Install Python dependencies |
| 167 | + run: pip install '.[dev,charts]' |
| 168 | + |
| 169 | + - name: Run Python tests and create coverage report |
| 170 | + run: > |
| 171 | + pytest tests/ --cov=./ --cov-report=xml:coverage/coverage.xml |
| 172 | + --junitxml=./coverage/junit.xml --color=yes -n auto |
| 173 | +
|
| 174 | + #- name: Upload test results to Codecov |
| 175 | + # if: ${{ !cancelled() }} |
| 176 | + # uses: codecov/test-results-action@v1 |
| 177 | + # with: |
| 178 | + # files: ./coverage/junit.xml |
| 179 | + # fail_ci_if_error: true # optional (default = false) |
| 180 | + # name: Pytest results |
| 181 | + # token: ${{ secrets.CODECOV_TOKEN }} |
| 182 | + |
| 183 | + #- name: Upload coverage report to Codecov |
| 184 | + # uses: codecov/codecov-action@v4 |
| 185 | + # with: |
| 186 | + # files: ./coverage/coverage.xml |
| 187 | + # env_vars: OS,PYTHON |
| 188 | + # fail_ci_if_error: true # optional (default = false) |
| 189 | + # name: Pytest coverage |
| 190 | + # token: ${{ secrets.CODECOV_TOKEN }} |
| 191 | + # env: |
| 192 | + # OS: ${{ matrix.os }} |
| 193 | + # PYTHON: ${{ matrix.python-version }} |
| 194 | + |
| 195 | + - name: Create Python package |
| 196 | + run: python -m build --wheel --outdir dist |
| 197 | + |
| 198 | + - name: |
| 199 | + Upload zipped Python package (with tests and examples) for next job |
| 200 | + uses: actions/upload-artifact@v4 |
| 201 | + with: |
| 202 | + name: |
| 203 | + EasyDiffractionLib_py${{ matrix.python-version }}_${{ matrix.os |
| 204 | + }}_${{ runner.arch }} |
| 205 | + path: | |
| 206 | + dist/*.whl |
| 207 | + tests/ |
| 208 | + examples/ |
| 209 | + if-no-files-found: 'error' |
| 210 | + compression-level: 0 |
| 211 | + |
| 212 | + # Job 3: Test the package |
| 213 | + test-package: |
| 214 | + needs: test-code # the previous job needs to be finished first |
| 215 | + |
| 216 | + strategy: |
| 217 | + fail-fast: false |
| 218 | + matrix: |
| 219 | + os: [ubuntu-24.04, windows-2022, macos-13, macos-14] |
| 220 | + python-version: ['3.10', '3.11', '3.12'] |
| 221 | + |
| 222 | + runs-on: ${{ matrix.os }} |
| 223 | + |
| 224 | + steps: |
| 225 | + - name: Set up Python ${{ matrix.python-version }} |
| 226 | + uses: actions/setup-python@v5 |
| 227 | + with: |
| 228 | + python-version: ${{ matrix.python-version }} |
| 229 | + |
| 230 | + - name: Upgrade package installer for Python |
| 231 | + run: python -m pip install --upgrade pip |
| 232 | + |
| 233 | + - name: |
| 234 | + Download zipped Python package (with tests and examples) from previous |
| 235 | + job |
| 236 | + uses: actions/download-artifact@v4 |
| 237 | + with: # name or path are taken from the upload step of the previous job |
| 238 | + name: |
| 239 | + EasyDiffractionLib_py${{ matrix.python-version }}_${{ matrix.os |
| 240 | + }}_${{ runner.arch }} |
| 241 | + path: . # directory to extract downloaded zipped artifacts |
| 242 | + |
| 243 | + # The local version must be higher than the PyPI version for pip to |
| 244 | + # prefer the local version. So, after a new release and a new tag, |
| 245 | + # remember to merge the master branch with the develop branch, |
| 246 | + # and then create a new feature branch from the develop branch. |
| 247 | + - name: Install Python package from previous job with 'dev' extras |
| 248 | + run: pip install 'easydiffraction[dev]' --find-links=dist |
| 249 | + |
| 250 | + - name: Run Python tests |
| 251 | + run: > |
| 252 | + pytest tests/ --color=yes -n auto |
| 253 | +
|
| 254 | + - name: Check if Jupiter Notebooks run without errors |
| 255 | + shell: bash |
| 256 | + run: > |
| 257 | + pytest --nbmake examples/ --ignore-glob='examples/*emcee*' |
| 258 | + --nbmake-timeout=300 --color=yes -n=auto |
0 commit comments