Introduce a DSL to replace the plain bash scripts
It's really important for `git-check-assertions` blocks to be 'obviously correct', so that we don't need to... test the tests that test the tests. Let's introduce a little DSL that is less error-prone than a plain bash script.
This commit is contained in:
parent
ad10b2cc4c
commit
4704cc088d
3 changed files with 130 additions and 127 deletions
63
README.md
63
README.md
|
|
@ -7,11 +7,11 @@ I recently wrote two blog posts arguing that there might be some value in writin
|
|||
|
||||
This is a simple verifier for such assertions.
|
||||
|
||||
You include a small bash script inside your commit messages, and `git-check-assertions` will then check out every commit (from the point that your branch diverged from `main`), and verify that the script in the commit message runs successfully.
|
||||
You write assertions in your commit messages, and `git-check-assertions` will then check out every commit (from the point that your branch diverged from `main`), and verify that the assertions in the commit message hold for the version of the code that is in the commit.
|
||||
|
||||
For a real-world example, check out the commits in [this pull request](https://codeberg.org/svenvanheugten/git-check-assertions/pulls/8), where `git-check-assertions` is used on itself.
|
||||
|
||||
⚠️ Only run this on repositories and branches that you trust, since the `bash` scripts in the commit messages can do whatever they want.
|
||||
⚠️ Only run this on repositories and branches that you trust, since the commands in the commit messages can do whatever they want.
|
||||
|
||||
## Installation
|
||||
|
||||
|
|
@ -56,26 +56,22 @@ If you use Nix with flakes, you can simply add it to your program's devshell ins
|
|||
|
||||
## What do you put in your commit messages?
|
||||
|
||||
Simply add a bash script enclosed in a `git-check-assertions` block to a commit message, e.g.:
|
||||
Simply add a `git-check-assertions` block to a commit message, e.g.:
|
||||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
dotnet build
|
||||
dotnet test --no-build
|
||||
[success] dotnet build
|
||||
[success] dotnet test --no-build
|
||||
```
|
||||
~~~
|
||||
|
||||
This script will be run with `set -euo pipefail` on the version of the code that is in the commit, and the commit will be considered correct if the script exits successfully.
|
||||
Each block is parsed line-by-line:
|
||||
|
||||
You can technically assert that a command fails by writing `! ... || exit 1`, or write assertions about a command's output by piping it to `grep`, but doing so won't lead to very useful error messages when things go wrong. To make those things easier, there are some helper functions included, which are inspired by [bats](https://github.com/bats-core/bats-core) and [bats-assert](https://github.com/bats-core/bats-assert):
|
||||
* A line starting with `[success] ` runs the rest of the line as a shell command and asserts that it exits with status `0`.
|
||||
* A line starting with `[failure] ` runs the rest of the line as a shell command and asserts that it exits with a non-zero status.
|
||||
* Any following non-empty line belongs to the most recent command and asserts that the combined stdout/stderr from that command contains that string.
|
||||
|
||||
* `run <command>`: run a command, capturing its exit status in `status` and combined stdout/stderr in `output`.
|
||||
* `assert_success`: succeed if `run` produced a zero exit status.
|
||||
* `assert_failure`: succeed if `run` produced a non-zero exit status.
|
||||
* `assert_output <string>`: succeed if `output` exactly matches the string.
|
||||
* `assert_output --partial <string>`: succeed if `output` contains the string.
|
||||
|
||||
I'm considering taking `bats-assert` as a dependency, but for now, this very minimal set of functions with a similar interface should get you on your way.
|
||||
Blank lines are ignored. A new `[success]` or `[failure]` line starts a new command block.
|
||||
|
||||
## Multiple blocks in one commit message
|
||||
|
||||
|
|
@ -93,7 +89,7 @@ Assert that a commit builds:
|
|||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
dotnet build
|
||||
[success] dotnet build
|
||||
```
|
||||
~~~
|
||||
|
||||
|
|
@ -101,9 +97,8 @@ Assert that a commit builds and that the tests succeed:
|
|||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
dotnet build
|
||||
run dotnet test --no-build
|
||||
assert_success
|
||||
[success] dotnet build
|
||||
[success] dotnet test --no-build
|
||||
```
|
||||
~~~
|
||||
|
||||
|
|
@ -111,20 +106,18 @@ Assert that a commit builds, but that the tests do not succeed:
|
|||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
dotnet build
|
||||
run dotnet test --no-build
|
||||
assert_failure
|
||||
[success] dotnet build
|
||||
[failure] dotnet test --no-build
|
||||
```
|
||||
~~~
|
||||
|
||||
Assert that a commit builds, and that the tests fail with exactly the error that you expect:
|
||||
Assert that a commit builds, and that the tests fail with the error that you expect:
|
||||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
dotnet build
|
||||
run dotnet test --no-build
|
||||
assert_failure
|
||||
assert_output --partial "Invalid URL"
|
||||
[success] dotnet build
|
||||
[failure] dotnet test --no-build
|
||||
Invalid URL
|
||||
```
|
||||
~~~
|
||||
|
||||
|
|
@ -132,12 +125,10 @@ Assert that a specific change breaks the tests (as discussed [here](https://sven
|
|||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
run dotnet test
|
||||
assert_success
|
||||
[success] dotnet test
|
||||
|
||||
sed -i '/crucial code/d' Main.fs
|
||||
run dotnet test
|
||||
assert_failure
|
||||
[success] sed -i '/crucial code/d' Main.fs
|
||||
[failure] dotnet test
|
||||
```
|
||||
~~~
|
||||
|
||||
|
|
@ -145,12 +136,10 @@ Assert that a specific change in a commit is necessary for the tests to succeed:
|
|||
|
||||
~~~
|
||||
```git-check-assertions
|
||||
run dotnet test
|
||||
assert_success
|
||||
[success] dotnet test
|
||||
|
||||
git checkout HEAD~ Main.fs
|
||||
run dotnet test
|
||||
assert_failure
|
||||
assert_output --partial "Invalid URL"
|
||||
[success] git checkout HEAD~ Main.fs
|
||||
[failure] dotnet test
|
||||
Invalid URL
|
||||
```
|
||||
~~~
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue