forked from extern/nushell
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:
parent
b9808c8598
commit
4da7bbbb59
5
.githooks/pre-commit
Executable file
5
.githooks/pre-commit
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
use ../toolkit.nu fmt
|
||||
|
||||
fmt --check --verbose
|
6
.githooks/pre-push
Executable file
6
.githooks/pre-push
Executable file
@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env nu
|
||||
|
||||
use ../toolkit.nu [fmt, clippy]
|
||||
|
||||
fmt --check --verbose
|
||||
clippy --verbose
|
@ -61,12 +61,22 @@ The most comprehensive test suite we have is the `nu-test-support` crate. For te
|
||||
```shell
|
||||
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:
|
||||
|
||||
```shell
|
||||
cargo test --workspace
|
||||
```
|
||||
or via the `toolkit.nu` command:
|
||||
```shell
|
||||
use toolkit.nu test
|
||||
test
|
||||
```
|
||||
|
||||
- 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
|
||||
cargo fmt --all -- --check
|
||||
```
|
||||
or via the `toolkit.nu` command:
|
||||
```shell
|
||||
use toolkit.nu fmt
|
||||
fmt --check
|
||||
```
|
||||
|
||||
- Format the code in the project
|
||||
|
||||
```shell
|
||||
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
|
||||
|
||||
|
51
toolkit.nu
51
toolkit.nu
@ -9,9 +9,18 @@
|
||||
# check standard code formatting and apply the changes
|
||||
export def fmt [
|
||||
--check: bool # do not apply the format changes, only check the syntax
|
||||
--verbose: bool # print extra information about the command's progress
|
||||
] {
|
||||
if ($check) {
|
||||
cargo fmt --all -- --check
|
||||
if $verbose {
|
||||
print $"running ('toolkit fmt' | pretty-print-command)"
|
||||
}
|
||||
|
||||
if $check {
|
||||
try {
|
||||
cargo fmt --all -- --check
|
||||
} catch {
|
||||
error make -u { msg: $"\nplease run ('toolkit fmt' | pretty-print-command) to fix formatting!" }
|
||||
}
|
||||
} else {
|
||||
cargo fmt --all
|
||||
}
|
||||
@ -20,8 +29,18 @@ export def fmt [
|
||||
# check that you're using the standard code style
|
||||
#
|
||||
# > it is important to make `clippy` happy :relieved:
|
||||
export def clippy [] {
|
||||
cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect
|
||||
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
|
||||
} catch {
|
||||
error make -u { msg: $"\nplease fix the above ('clippy' | pretty-print-command) errors before continuing!" }
|
||||
}
|
||||
}
|
||||
|
||||
# check that all the tests pass
|
||||
@ -180,17 +199,14 @@ def report [
|
||||
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))
|
||||
] {
|
||||
print $"running ('toolkit fmt' | pretty-print-command)"
|
||||
try {
|
||||
fmt --check
|
||||
fmt --check --verbose
|
||||
} catch {
|
||||
print $"\nplease run (ansi default_dimmed)(ansi default_italic)toolkit fmt(ansi reset) to fix the formatting"
|
||||
return (report --fail-fmt)
|
||||
}
|
||||
|
||||
print $"running ('toolkit clippy' | pretty-print-command)"
|
||||
try {
|
||||
clippy
|
||||
clippy --verbose
|
||||
} catch {
|
||||
return (report --fail-clippy)
|
||||
}
|
||||
@ -211,3 +227,20 @@ export def "check pr" [
|
||||
|
||||
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)"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user