| 1 |
name: Test |
| 2 |
|
| 3 |
on: |
| 4 |
push: |
| 5 |
branches: |
| 6 |
- master |
| 7 |
pull_request: |
| 8 |
schedule: |
| 9 |
# cron every week on monday |
| 10 |
- cron: "0 0 * * 1" |
| 11 |
|
| 12 |
permissions: {} |
| 13 |
|
| 14 |
env: |
| 15 |
UV_NO_SYNC: true |
| 16 |
INLINE_SNAPSHOT_DEFAULT_FLAGS: review |
| 17 |
|
| 18 |
jobs: |
| 19 |
changes: |
| 20 |
runs-on: ubuntu-latest |
| 21 |
# Required permissions |
| 22 |
permissions: |
| 23 |
pull-requests: read |
| 24 |
# Set job outputs to values from filter step |
| 25 |
timeout-minutes: 5 |
| 26 |
outputs: |
| 27 |
src: ${{ steps.filter.outputs.src }} |
| 28 |
steps: |
| 29 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 30 |
with: |
| 31 |
persist-credentials: false |
| 32 |
# For pull requests it's not necessary to checkout the code but for the main branch it is |
| 33 |
- uses: dorny/paths-filter@7b450fff21473bca461d4b92ce414b9d0420d706 # v4.0.2 |
| 34 |
id: filter |
| 35 |
with: |
| 36 |
filters: | |
| 37 |
src: |
| 38 |
- .github/workflows/test.yml |
| 39 |
- docs_src/** |
| 40 |
- fastapi/** |
| 41 |
- scripts/** |
| 42 |
- tests/** |
| 43 |
- .python-version |
| 44 |
- pyproject.toml |
| 45 |
- uv.lock |
| 46 |
|
| 47 |
test: |
| 48 |
needs: |
| 49 |
- changes |
| 50 |
if: needs.changes.outputs.src == 'true' || github.ref == 'refs/heads/master' |
| 51 |
timeout-minutes: 10 |
| 52 |
strategy: |
| 53 |
matrix: |
| 54 |
os: [ windows-latest, macos-latest ] |
| 55 |
python-version: [ "3.14", "3.14t" ] |
| 56 |
deprecated-tests: [ "no-deprecation" ] |
| 57 |
uv-resolution: |
| 58 |
- highest |
| 59 |
starlette-src: |
| 60 |
- starlette-pypi |
| 61 |
- starlette-git |
| 62 |
include: |
| 63 |
- os: macos-latest |
| 64 |
python-version: "3.10" |
| 65 |
coverage: coverage |
| 66 |
uv-resolution: lowest-direct |
| 67 |
deprecated-tests: "no-deprecation" |
| 68 |
- os: windows-latest |
| 69 |
python-version: "3.12" |
| 70 |
coverage: coverage |
| 71 |
uv-resolution: lowest-direct |
| 72 |
deprecated-tests: "no-deprecation" |
| 73 |
- os: ubuntu-latest |
| 74 |
python-version: "3.13" |
| 75 |
coverage: coverage |
| 76 |
uv-resolution: highest |
| 77 |
deprecated-tests: "no-deprecation" |
| 78 |
- os: ubuntu-latest |
| 79 |
python-version: "3.13" |
| 80 |
uv-resolution: highest |
| 81 |
codspeed: codspeed |
| 82 |
deprecated-tests: "no-deprecation" |
| 83 |
- os: ubuntu-latest |
| 84 |
python-version: "3.13" |
| 85 |
uv-resolution: highest |
| 86 |
deprecated-tests: "no-deprecation" |
| 87 |
without-httpx2: true |
| 88 |
- os: ubuntu-latest |
| 89 |
python-version: "3.14" |
| 90 |
coverage: coverage |
| 91 |
uv-resolution: highest |
| 92 |
starlette-src: starlette-git |
| 93 |
deprecated-tests: "test-deprecation" |
| 94 |
- os: ubuntu-latest |
| 95 |
python-version: "3.14t" |
| 96 |
coverage: coverage |
| 97 |
uv-resolution: highest |
| 98 |
deprecated-tests: "no-deprecation" |
| 99 |
fail-fast: false |
| 100 |
runs-on: ${{ matrix.os }} |
| 101 |
env: |
| 102 |
UV_PYTHON: ${{ matrix.python-version }} |
| 103 |
UV_RESOLUTION: ${{ matrix.uv-resolution }} |
| 104 |
STARLETTE_SRC: ${{ matrix.starlette-src }} |
| 105 |
steps: |
| 106 |
- name: Dump GitHub context |
| 107 |
env: |
| 108 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 109 |
run: echo "$GITHUB_CONTEXT" |
| 110 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 111 |
with: |
| 112 |
persist-credentials: false |
| 113 |
- name: Set up Python |
| 114 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 115 |
with: |
| 116 |
python-version: ${{ matrix.python-version }} |
| 117 |
- name: Setup uv |
| 118 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 119 |
with: |
| 120 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 121 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 122 |
version: "0.11.18" |
| 123 |
enable-cache: true |
| 124 |
cache-dependency-glob: | |
| 125 |
pyproject.toml |
| 126 |
uv.lock |
| 127 |
- name: Install Dependencies |
| 128 |
run: uv sync --no-dev --group tests --extra all |
| 129 |
- name: Ensure that we have the lowest supported Pydantic version |
| 130 |
if: matrix.uv-resolution == 'lowest-direct' |
| 131 |
run: uv pip install "pydantic==2.9.0" |
| 132 |
- name: Install Starlette from source |
| 133 |
if: matrix.starlette-src == 'starlette-git' |
| 134 |
run: uv pip install "git+https://github.com/Kludex/starlette@main" |
| 135 |
- name: Install deprecated libraries just for testing |
| 136 |
if: matrix.deprecated-tests == 'test-deprecation' |
| 137 |
run: uv pip install orjson ujson |
| 138 |
- name: Uninstall httpx2 to run tests with httpx |
| 139 |
if: matrix.without-httpx2 == 'true' |
| 140 |
run: uv pip uninstall httpx2 |
| 141 |
- name: Reinstall SQLAlchemy without Cython extensions |
| 142 |
if: matrix.python-version == '3.14t' && matrix.os == 'ubuntu-latest' |
| 143 |
run: "DISABLE_SQLALCHEMY_CEXT=1 uv pip install --force-reinstall --no-binary :all: sqlalchemy" |
| 144 |
- run: mkdir coverage |
| 145 |
- name: Test |
| 146 |
run: uv run --no-sync bash scripts/test-cov.sh $PYTEST_OPTIONS |
| 147 |
env: |
| 148 |
COVERAGE_FILE: coverage/.coverage.${{ runner.os }}-py${{ matrix.python-version }}-${{ matrix.deprecated-tests}} |
| 149 |
CONTEXT: ${{ runner.os }}-py${{ matrix.python-version }}-${{ matrix.deprecated-tests}} |
| 150 |
PYTEST_OPTIONS: ${{ (matrix.without-httpx2 == 'true') && '-W ignore::UserWarning' || '' }} |
| 151 |
# Do not store coverage for all possible combinations to avoid file size max errors in Smokeshow |
| 152 |
- name: Store coverage files |
| 153 |
if: matrix.coverage == 'coverage' |
| 154 |
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 155 |
with: |
| 156 |
name: coverage-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.deprecated-tests}}-${{ hashFiles('**/coverage/.coverage.*') }} |
| 157 |
path: coverage |
| 158 |
include-hidden-files: true |
| 159 |
|
| 160 |
benchmark: |
| 161 |
needs: |
| 162 |
- changes |
| 163 |
if: needs.changes.outputs.src == 'true' || github.ref == 'refs/heads/master' |
| 164 |
runs-on: ubuntu-latest |
| 165 |
timeout-minutes: 5 |
| 166 |
env: |
| 167 |
UV_PYTHON: "3.13" |
| 168 |
UV_RESOLUTION: highest |
| 169 |
steps: |
| 170 |
- name: Dump GitHub context |
| 171 |
env: |
| 172 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 173 |
run: echo "$GITHUB_CONTEXT" |
| 174 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 175 |
with: |
| 176 |
persist-credentials: false |
| 177 |
- name: Set up Python |
| 178 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 179 |
with: |
| 180 |
python-version: "3.13" |
| 181 |
- name: Setup uv |
| 182 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 183 |
with: |
| 184 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 185 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 186 |
version: "0.11.18" |
| 187 |
enable-cache: true |
| 188 |
cache-dependency-glob: | |
| 189 |
pyproject.toml |
| 190 |
uv.lock |
| 191 |
- name: Install Dependencies |
| 192 |
run: uv sync --no-dev --group tests --extra all |
| 193 |
- name: CodSpeed benchmarks |
| 194 |
uses: CodSpeedHQ/action@a4a36bb07c0638b0b4ca52bf1f3dad1b4289e52f # v4.18.1 |
| 195 |
with: |
| 196 |
mode: simulation |
| 197 |
run: uv run --no-sync pytest tests/benchmarks --codspeed |
| 198 |
- name: CodSpeed memory benchmark |
| 199 |
uses: CodSpeedHQ/action@a4a36bb07c0638b0b4ca52bf1f3dad1b4289e52f # v4.18.1 |
| 200 |
with: |
| 201 |
mode: memory |
| 202 |
run: uv run --no-sync pytest tests/memory_benchmarks --codspeed |
| 203 |
|
| 204 |
regression-test: |
| 205 |
runs-on: ubuntu-latest |
| 206 |
timeout-minutes: 10 |
| 207 |
steps: |
| 208 |
- name: Check out the pull request |
| 209 |
if: github.event_name == 'pull_request' |
| 210 |
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 211 |
with: |
| 212 |
ref: ${{ github.sha }} |
| 213 |
path: pr |
| 214 |
persist-credentials: false |
| 215 |
fetch-depth: 0 |
| 216 |
- name: Find changed tests |
| 217 |
if: github.event_name == 'pull_request' |
| 218 |
id: changed-tests |
| 219 |
working-directory: pr |
| 220 |
env: |
| 221 |
BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 222 |
HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 223 |
run: | |
| 224 |
git diff --name-only --diff-filter=AM -z "$BASE_SHA" "$HEAD_SHA" -- tests \ |
| 225 |
| while IFS= read -r -d '' file; do |
| 226 |
case "$(basename "$file")" in |
| 227 |
test_*.py) printf '%s\0' "$file" ;; |
| 228 |
esac |
| 229 |
done > "$RUNNER_TEMP/changed-tests" |
| 230 |
if [ -s "$RUNNER_TEMP/changed-tests" ]; then |
| 231 |
echo "found=true" >> "$GITHUB_OUTPUT" |
| 232 |
git diff --binary "$BASE_SHA" "$HEAD_SHA" -- tests \ |
| 233 |
> "$RUNNER_TEMP/tests.patch" |
| 234 |
else |
| 235 |
echo "found=false" >> "$GITHUB_OUTPUT" |
| 236 |
echo "No added or modified test files; regression proof is not applicable." |
| 237 |
fi |
| 238 |
- name: Check out the base revision |
| 239 |
if: steps.changed-tests.outputs.found == 'true' |
| 240 |
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 241 |
with: |
| 242 |
ref: ${{ github.event.pull_request.base.sha }} |
| 243 |
path: base |
| 244 |
persist-credentials: false |
| 245 |
- name: Set up Python |
| 246 |
if: steps.changed-tests.outputs.found == 'true' |
| 247 |
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 248 |
with: |
| 249 |
python-version-file: "base/.python-version" |
| 250 |
- name: Setup uv |
| 251 |
if: steps.changed-tests.outputs.found == 'true' |
| 252 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 253 |
with: |
| 254 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 255 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 256 |
version: "0.11.18" |
| 257 |
enable-cache: true |
| 258 |
- name: Run the changed tests against the base code |
| 259 |
if: steps.changed-tests.outputs.found == 'true' |
| 260 |
working-directory: base |
| 261 |
run: | |
| 262 |
git apply "$RUNNER_TEMP/tests.patch" |
| 263 |
uv sync --locked --no-dev --group tests --extra all |
| 264 |
set +e |
| 265 |
xargs -0 uv run --no-sync pytest -- < "$RUNNER_TEMP/changed-tests" |
| 266 |
status=$? |
| 267 |
set -e |
| 268 |
if [ "$status" -eq 0 ]; then |
| 269 |
echo "::warning::The changed tests already pass on the base revision. Check whether the fix is still needed." |
| 270 |
echo "### Regression proof: base already passes :warning:" >> "$GITHUB_STEP_SUMMARY" |
| 271 |
echo "The changed tests pass without the pull request's code changes." >> "$GITHUB_STEP_SUMMARY" |
| 272 |
else |
| 273 |
echo "The changed tests fail on the base revision as expected (pytest exit code $status)." |
| 274 |
echo "### Regression proof: base fails as expected :white_check_mark:" >> "$GITHUB_STEP_SUMMARY" |
| 275 |
fi |
| 276 |
coverage-combine: |
| 277 |
needs: |
| 278 |
- test |
| 279 |
runs-on: ubuntu-latest |
| 280 |
timeout-minutes: 5 |
| 281 |
steps: |
| 282 |
- name: Dump GitHub context |
| 283 |
env: |
| 284 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 285 |
run: echo "$GITHUB_CONTEXT" |
| 286 |
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 |
| 287 |
with: |
| 288 |
persist-credentials: false |
| 289 |
- uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 |
| 290 |
with: |
| 291 |
python-version-file: ".python-version" |
| 292 |
- name: Setup uv |
| 293 |
uses: astral-sh/setup-uv@d31148d669074a8d0a63714ba94f3201e7020bc3 # v8.3.0 |
| 294 |
with: |
| 295 |
# Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. |
| 296 |
# See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 |
| 297 |
version: "0.11.18" |
| 298 |
enable-cache: true |
| 299 |
cache-dependency-glob: | |
| 300 |
pyproject.toml |
| 301 |
uv.lock |
| 302 |
- name: Install Dependencies |
| 303 |
run: uv sync --locked --no-dev --group tests --extra all |
| 304 |
- name: Get coverage files |
| 305 |
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 306 |
with: |
| 307 |
pattern: coverage-* |
| 308 |
path: coverage |
| 309 |
merge-multiple: true |
| 310 |
- run: ls -la coverage |
| 311 |
- run: uv run coverage combine coverage |
| 312 |
- run: uv run coverage html --title "Coverage for ${{ github.sha }}" |
| 313 |
- name: Store coverage HTML |
| 314 |
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 315 |
with: |
| 316 |
name: coverage-html |
| 317 |
path: htmlcov |
| 318 |
include-hidden-files: true |
| 319 |
- run: uv run coverage report --fail-under=100 |
| 320 |
|
| 321 |
# https://github.com/marketplace/actions/alls-green#why |
| 322 |
test-alls-green: # This job does nothing and is only used for the branch protection |
| 323 |
if: always() |
| 324 |
needs: |
| 325 |
- test |
| 326 |
- coverage-combine |
| 327 |
- benchmark |
| 328 |
- regression-test |
| 329 |
runs-on: ubuntu-latest |
| 330 |
timeout-minutes: 5 |
| 331 |
steps: |
| 332 |
- name: Dump GitHub context |
| 333 |
env: |
| 334 |
GITHUB_CONTEXT: ${{ toJson(github) }} |
| 335 |
run: echo "$GITHUB_CONTEXT" |
| 336 |
- name: Decide whether the needed jobs succeeded or failed |
| 337 |
uses: re-actors/alls-green@05ac9388f0aebcb5727afa17fcccfecd6f8ec5fe # v1.2.2 |
| 338 |
with: |
| 339 |
jobs: ${{ toJSON(needs) }} |
| 340 |
allowed-skips: coverage-combine,test,benchmark |