Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Further updates to Contributing.md re testing, linting etc. #4115

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
80 changes: 80 additions & 0 deletions .github/workflows/ci-quick.yml
@@ -0,0 +1,80 @@
name: Quick CI

# A short version of the CI, designed to run in seconds (<200s) on every push.
# Runs lints & basic tests on ALL branches EXCEPT main.

on:
push:
branches-ignore:
- main

env:
CARGO_TERM_COLOR: always

jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: python -m pip install --upgrade pip && pip install nox
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: Check python formatting and lints (ruff)
run: nox -s ruff
- name: Check rust formatting (rustfmt)
run: nox -s rustfmt

semver-checks:
if: github.ref != 'refs/heads/main'
needs: [fmt]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: obi1kenobi/cargo-semver-checks-action@v2

clippy:
needs: [fmt]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy,rust-src
- uses: actions/setup-python@v5
- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.event_name != 'merge_group' }}
- run: python -m pip install --upgrade pip && pip install nox
- run: nox -s clippy

tests:
name: test rust skip-full
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.event_name != 'merge_group' }}
- uses: dtolnay/rust-toolchain@stable
with:
components: rust-src
- run: python -m pip install --upgrade pip && pip install nox
- run: nox -s test-rust -- skip-full

check-guide:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Setup mdBook & lychee
uses: taiki-e/install-action@v2
with:
tool: mdbook,lychee
- name: Check the docs & links
run: |
python -m pip install --upgrade pip && pip install nox
nox -s check-guide
79 changes: 51 additions & 28 deletions Contributing.md
Expand Up @@ -18,6 +18,7 @@ The following sections also contain specific ideas on where to start contributin
## Setting up a development environment

To work and develop PyO3, you need Python & Rust installed on your system.

* We encourage the use of [rustup](https://rustup.rs/) to be able to select and choose specific toolchains based on the project.
* [Pyenv](https://github.com/pyenv/pyenv) is also highly recommended for being able to choose a specific Python version.
* [virtualenv](https://virtualenv.pypa.io/en/latest/) can also be used with or without Pyenv to use specific installed Python versions.
Expand All @@ -27,6 +28,40 @@ To work and develop PyO3, you need Python & Rust installed on your system.

* When using pyenv on macOS, installing a Python version using `--enable-shared` is required to make it work. i.e `env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.12`

### Testing, linting, etc. with nox

[`Nox`][nox] is used to automate many of our CI tasks and can be used locally to handle verfication tasks as you code. We recommend running these actions via nox to make use of our prefered configuration options. You can install nox into your global python with pip: `pip install nox` or (recommended) with [`pipx`][pipx] `pip install pipx`, `pipx install nox`

The main nox commands we have implemented are:

* `nox -s test` will run the full suite of recommended rust and python tests (>10 minutes)
* `nox -s test-rust -- skip-full` will run a short suite of rust tests (2-3 minutes)
* `nox -s ruff` will check python linting and apply standard formatting rules
* `nox -s rustfmt` will check basic rust linting and apply standard formatting rules
* `nox -s clippy` will run clippy to make recommendations on rust style
* `nox -s bench` will benchmark your rust code
* `nox -s codspeed` will run our suite of rust and python performance tests
* `nox -s coverage` will analyse test coverage and output `coverage.json` (alternatively: `nox -s coverage lcov` outputs `lcov.info`)
* `nox -s check-guide` will use [`lychee`][lychee] to check all the links in the guide and doc comments.

Use `nox -l` to list the full set of subcommands you can run.

#### UI Tests

PyO3 uses [`trybuild`][trybuild] to develop UI tests to capture error messages from the Rust compiler for some of the macro functionality.

Because there are several feature combinations for these UI tests, when updating them all (e.g. for a new Rust compiler version) it may be helpful to use the `update-ui-tests` nox session:

```bash
nox -s update-ui-tests
```

### Github workflow

In addition to the larger CI workflow which runs on each Pull Request, we provide a short CI workflow which runs on every push to github. This workflow runs the nox sessions: `rustfmt`, `ruff`, `clippy` and `test-rust -- skip-full` and is designed to run in under 200s to provide feedback within a reasonable timeframe and not use all your github actions minutes.

## Ways to help

### Help users identify bugs

The [PyO3 Discord server](https://discord.gg/33kcChzH7f) is very active with users who are new to PyO3, and often completely new to Rust. Helping them debug is a great way to get experience with the PyO3 codebase.
Expand Down Expand Up @@ -98,36 +133,22 @@ Tests run with all supported Python versions with the latest stable Rust compile

If you are adding a new feature, you should add it to the `full` feature in our *Cargo.toml** so that it is tested in CI.

You can run these tests yourself with
`nox`. Use `nox -l` to list the full set of subcommands you can run.

#### Linting Python code
`nox -s ruff`

#### Linting Rust code
`nox -s rustfmt`

#### Semver checks
`cargo semver-checks check-release`

#### Clippy
`nox -s clippy-all`

#### Tests
`cargo test --features full`

#### Check all conditional compilation
`nox -s check-feature-powerset`
You can run these tests yourself with `nox`. The full set of actions run in CI is:

#### UI Tests

PyO3 uses [`trybuild`][trybuild] to develop UI tests to capture error messages from the Rust compiler for some of the macro functionality.

Because there are several feature combinations for these UI tests, when updating them all (e.g. for a new Rust compiler version) it may be helpful to use the `update-ui-tests` nox session:
1. `nox -s rustfmt` - everything will abort if fmt has not been run on the code before pushing
1. `cargo semver-checks check-release` (checks no semver violations based on currently released version of Pyo3)
1. `nox -s set-minimal-package-versions` & `nox -s check-all`
1. `nox -s clippy-all` on a matrix of different OS, python and rust architectures
1. build and test against a matrix of different OS, python and rust architectures
1. test under valgrind to identify memory leakages
1. `nox -s coverage` for latest windows, macos and ubuntu
1. `nox -s test` with a debug build of python
1. `nox -s test-version-limits`
1. `nox -s check-feature-powerset` to check conditional compilation
1. cross-compilation tests
1. `nox -s check-guide` to build the guide and doc-comments docummentation and use [`lychee`][lychee] to validate all links

```bash
nox -s update-ui-tests
```
If you wish to validate your code against this suite before raising a PR, then you can manually trigger the CI in github actions.

### Documenting changes

Expand Down Expand Up @@ -242,3 +263,5 @@ In the meanwhile, some of our maintainers have personal GitHub sponsorship pages
[mdbook]: https://rust-lang.github.io/mdBook/cli/index.html
[lychee]: https://github.com/lycheeverse/lychee
[nox]: https://github.com/theacodes/nox
[pipx]: https://pipx.pypa.io/stable/
[trybuild]: https://github.com/dtolnay/trybuild