Add git hooks for formatting and running clippy (#8820)

# Description

As another life improvement (and to avoid those `run cargo fmt` commits
😉), this PR adds a command to the toolkit for formatting and running
`clippy` when committing and pushing.

Thanks to @amtoine for the idea!

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Maria José Solano 2023-04-13 05:34:23 -07:00 committed by GitHub
parent b9808c8598
commit 4da7bbbb59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 9 deletions

5
.githooks/pre-commit Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env nu
use ../toolkit.nu fmt
fmt --check --verbose

6
.githooks/pre-push Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env nu
use ../toolkit.nu [fmt, clippy]
fmt --check --verbose
clippy --verbose

View File

@ -61,12 +61,22 @@ The most comprehensive test suite we have is the `nu-test-support` crate. For te
```shell ```shell
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect
``` ```
or via the `toolkit.nu` command:
```shell
use toolkit.nu clippy
clippy
```
- Run all tests: - Run all tests:
```shell ```shell
cargo test --workspace cargo test --workspace
``` ```
or via the `toolkit.nu` command:
```shell
use toolkit.nu test
test
```
- Run all tests for a specific command - Run all tests for a specific command
@ -79,12 +89,30 @@ The most comprehensive test suite we have is the `nu-test-support` crate. For te
```shell ```shell
cargo fmt --all -- --check cargo fmt --all -- --check
``` ```
or via the `toolkit.nu` command:
```shell
use toolkit.nu fmt
fmt --check
```
- Format the code in the project - Format the code in the project
```shell ```shell
cargo fmt --all cargo fmt --all
``` ```
or via the `toolkit.nu` command:
```shell
use toolkit.nu fmt
fmt
```
- Set up `git` hooks to check formatting and run `clippy` before committing and pushing:
```shell
use toolkit.nu setup-git-hooks
setup-git-hooks
```
_Unfortunately, this hook isn't available on Windows._
### Debugging Tips ### Debugging Tips

View File

@ -9,9 +9,18 @@
# check standard code formatting and apply the changes # check standard code formatting and apply the changes
export def fmt [ export def fmt [
--check: bool # do not apply the format changes, only check the syntax --check: bool # do not apply the format changes, only check the syntax
--verbose: bool # print extra information about the command's progress
] { ] {
if ($check) { if $verbose {
print $"running ('toolkit fmt' | pretty-print-command)"
}
if $check {
try {
cargo fmt --all -- --check cargo fmt --all -- --check
} catch {
error make -u { msg: $"\nplease run ('toolkit fmt' | pretty-print-command) to fix formatting!" }
}
} else { } else {
cargo fmt --all cargo fmt --all
} }
@ -20,8 +29,18 @@ export def fmt [
# check that you're using the standard code style # check that you're using the standard code style
# #
# > it is important to make `clippy` happy :relieved: # > it is important to make `clippy` happy :relieved:
export def clippy [] { export def clippy [
--verbose: bool # print extra information about the command's progress
] {
if $verbose {
print $"running ('toolkit clippy' | pretty-print-command)"
}
try {
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect
} catch {
error make -u { msg: $"\nplease fix the above ('clippy' | pretty-print-command) errors before continuing!" }
}
} }
# check that all the tests pass # check that all the tests pass
@ -180,17 +199,14 @@ def report [
export def "check pr" [ export def "check pr" [
--fast: bool # use the "nextext" `cargo` subcommand to speed up the tests (see [`cargo-nextest`](https://nexte.st/) and [`nextest-rs/nextest`](https://github.com/nextest-rs/nextest)) --fast: bool # use the "nextext" `cargo` subcommand to speed up the tests (see [`cargo-nextest`](https://nexte.st/) and [`nextest-rs/nextest`](https://github.com/nextest-rs/nextest))
] { ] {
print $"running ('toolkit fmt' | pretty-print-command)"
try { try {
fmt --check fmt --check --verbose
} catch { } catch {
print $"\nplease run (ansi default_dimmed)(ansi default_italic)toolkit fmt(ansi reset) to fix the formatting"
return (report --fail-fmt) return (report --fail-fmt)
} }
print $"running ('toolkit clippy' | pretty-print-command)"
try { try {
clippy clippy --verbose
} catch { } catch {
return (report --fail-clippy) return (report --fail-clippy)
} }
@ -211,3 +227,20 @@ export def "check pr" [
report --no-fail report --no-fail
} }
# set up git hooks to run:
# - `toolkit fmt --check --verbose` on `git commit`
# - `toolkit fmt --check --verbose` and `toolkit clippy --verbose` on `git push`
export def set-git-hooks [] {
if $nu.os-info.name == windows {
return (print "This git hook isn't available on Windows. Sorry!")
}
print "This command will change your local git configuration and hence modify your development workflow. Are you sure you want to continue? [y]"
if (input) == "y" {
print $"running ('toolkit set-git-hooks' | pretty-print-command)"
git config --local core.hooksPath .githooks
} else {
print $"aborting ('toolkit set-git-hooks' | pretty-print-command)"
}
}