Skip to content

Commit

Permalink
chore: Configure pre-commit, update the justfile (#890)
Browse files Browse the repository at this point in the history
Setups `pre-commit` instead of the ad-hoc `.github/pre-commit` file, and
cleans up the `just` recipes.

The main benefit of this is to avoid running both codebase tests on each
commit, since `pre-commit` filters the required checks based on the
modified files.

Updates `DEVELOPMENT.md` with the new commands. It should be a bit more
straightforward now.

drive-by: Fixes for the `trailing-whitespace` and `end-of-file-fixer`
hooks.
  • Loading branch information
aborgna-q authored Mar 20, 2024
1 parent 5381831 commit 63afb03
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 121 deletions.
57 changes: 0 additions & 57 deletions .github/pre-commit

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/drop-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ jobs:
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Cleanup
run: |
gh extension install actions/gh-actions-cache
REPO=${{ github.repository }}
BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
echo "Fetching list of cache key"
cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH | cut -f 1 )
## Setting this to not fail the workflow while deleting cache keys.
## Setting this to not fail the workflow while deleting cache keys.
set +e
echo "Deleting caches..."
for cacheKey in $cacheKeysForPR
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Check Conventional Commits format
on:
pull_request_target:
branches:
- main
- main
types:
- opened
- edited
Expand Down Expand Up @@ -69,4 +69,4 @@ jobs:
# labels change, you might want to use the `labeled` and `unlabeled`
# event triggers in your workflow.
ignoreLabels: |
ignore-semantic-pull-request
ignore-semantic-pull-request
2 changes: 1 addition & 1 deletion .github/workflows/unsoundness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Unsoundness checks
on:
push:
branches:
- main
- main
workflow_dispatch: {}

concurrency:
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ proptest-regressions/
.devenv*
devenv.local.nix

# managed by devenv
.pre-commit-config.yaml

# Coverage report
lcov.info

Expand Down
65 changes: 65 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0 # Use the ref you want to point at
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: check-toml
- id: check-vcs-permalinks
- id: check-yaml
- id: detect-private-key
- id: end-of-file-fixer
exclude: ^specification/schema/
- id: trailing-whitespace
- id: fix-byte-order-marker
- id: mixed-line-ending
# Python-specific
- id: check-ast
- id: check-docstring-first
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
- id: mypy
additional_dependencies: [pydantic]

- repo: local
hooks:
- id: cargo-fmt
name: cargo format
description: Format rust code with `cargo fmt`.
entry: cargo fmt --all -- --check
language: system
files: \.rs$
pass_filenames: false
- id: cargo-check
name: cargo check
description: Check rust code with `cargo check`.
entry: cargo check --all --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
- id: cargo-test
name: cargo test
description: Run tests with `cargo test`.
entry: cargo test --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
- id: cargo-clippy
name: cargo clippy
description: Run clippy lints with `cargo clippy`.
entry: cargo clippy --all-features --workspace -- -D warnings
language: system
files: \.rs$
pass_filenames: false
29 changes: 12 additions & 17 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,24 @@ To setup the environment manually you will need:
- Just: https://just.systems/
- Poetry: https://python-poetry.org/

You can use the git hook in [`.github/pre-commit`](.github/pre-commit) to automatically run the test and check formatting before committing.
To install it, run:
Once you have these installed, you can install the required python dependencies and setup pre-commit hooks with:

```bash
ln -s .github/pre-commit .git/hooks/pre-commit
# Or, to check before pushing instead
ln -s .github/pre-commit .git/hooks/pre-push
just setup
```

## 🏃 Running the tests

To compile and test the rust code, run:
To compile and test the code, run:

```bash
# Rust tests
just test # or `cargo test`
# Python tests
just pytest
just test
# or, to test only the rust code or the python code
just test rust
just test python
```

Run the benchmarks with:
Run the rust benchmarks with:

```bash
cargo bench
Expand All @@ -75,7 +72,6 @@ The rustfmt tool is used to enforce a consistent rust coding style. The CI will
To format your code, run:

```bash
# Format rust code
just format
```

Expand All @@ -89,6 +85,9 @@ To quickly fix common issues, run:

```bash
just fix
# or, to fix only the rust code or the python code
just fix rust
just fix python
```

## 📈 Code Coverage
Expand All @@ -98,18 +97,14 @@ line-by-line coverage report on
[codecov](https://app.codecov.io/gh/CQCL/hugr/commits?branch=All%20branches).

To run the coverage checks locally, first install `cargo-llvm-cov`.

```bash
cargo install cargo-llvm-cov
```

Then run the tests:

```bash
# Rust test coverage
just coverage
# Python test
just pycoverage
```

and open it with your favourite coverage viewer. In VSCode, you can use
Expand Down Expand Up @@ -140,4 +135,4 @@ We accept the following contribution types:
- test: Adding missing tests, refactoring tests; no production code change.
- ci: CI related changes. These changes are not published in the changelog.
- chore: Updating build tasks, package manager configs, etc. These changes are not published in the changelog.
- revert: Reverting previous commits.
- revert: Reverting previous commits.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
8 changes: 1 addition & 7 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let
in
{
# https://devenv.sh/packages/
# on macos frameworks have to be explicitly specified
# on macos frameworks have to be explicitly specified
# otherwise a linker error ocurs on rust packages
packages = [
pkgs.just
Expand Down Expand Up @@ -46,12 +46,6 @@ in
components = [ "rustc" "cargo" "clippy" "rustfmt" "rust-analyzer" ];
};

# https://devenv.sh/pre-commit-hooks/
pre-commit.hooks.clippy.enable = true;
pre-commit.tools.clippy = lib.mkForce config.languages.rust.toolchain.clippy;
pre-commit.hooks.rustfmt.enable = true;
pre-commit.tools.rustfmt = lib.mkForce config.languages.rust.toolchain.rustfmt;

# https://devenv.sh/processes/
# processes.ping.exec = "ping example.com";

Expand Down
2 changes: 1 addition & 1 deletion devenv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ inputs:
url: github:nix-community/fenix
inputs:
nixpkgs:
follows: nixpkgs
follows: nixpkgs
84 changes: 57 additions & 27 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,70 @@
help:
@just --list --justfile {{justfile()}}

# Run all the rust tests
test:
cargo test --all-features
# Prepare the environment for development, installing all the dependencies and
# setting up the pre-commit hooks.
setup:
poetry install
poetry run pre-commit install -t pre-commit

# Auto-fix all clippy warnings
fix:
cargo clippy --all-targets --all-features --workspace --fix --allow-staged
poetry run ruff check --fix

# Run the pre-commit checks
# Run the pre-commit checks.
check:
./.github/pre-commit
poetry run pre-commit run --all-files

# Format the code
format:
cargo fmt
poetry run ruff format
# Run all the tests.
test language="[rust|python]": (_run_lang language \
"cargo test --all-features" \
"poetry run pytest"
)

# Generate a test coverage report
coverage:
cargo llvm-cov --lcov > lcov.info
# Run all the benchmarks.
bench language="[rust|python]": (_run_lang language \
"cargo bench" \
"true"
)

# Load a poetry shell with the dependencies installed
pyshell:
poetry shell
# Auto-fix all clippy warnings.
fix language="[rust|python]": (_run_lang language \
"cargo clippy --all-targets --all-features --workspace --fix --allow-staged --allow-dirty" \
"poetry run ruff check --fix"
)

# Format the code.
format language="[rust|python]": (_run_lang language \
"cargo fmt" \
"poetry run ruff format"
)

# Run the python tests
pytest:
poetry run pytest
# Generate a test coverage report.
coverage language="[rust|python]": (_run_lang language \
"cargo llvm-cov --lcov > lcov.info" \
"poetry run pytest --cov=./ --cov-report=html"
)

# Generate a python test coverage report
pycoverage:
poetry run pytest --cov=./ --cov-report=html
# Load a shell with all the dependencies installed
shell:
poetry shell

# Update the HUGR schema
# Update the HUGR schema.
update-schema:
poetry run python scripts/generate_schema.py specification/schema/


# Runs a rust and a python command, depending on the `language` variable.
#
# If `language` is set to `rust` or `python`, only run the command for that language.
# Otherwise, run both commands.
_run_lang language rust_cmd python_cmd:
#!/usr/bin/env bash
set -euo pipefail
if [ "{{ language }}" = "rust" ]; then
set -x
{{ rust_cmd }}
elif [ "{{ language }}" = "python" ]; then
set -x
{{ python_cmd }}
else
set -x
{{ rust_cmd }}
{{ python_cmd }}
fi
Loading

0 comments on commit 63afb03

Please sign in to comment.