From f9d66966871685cd88eb08642a981fb87fc20d5f Mon Sep 17 00:00:00 2001 From: Sven van Heugten Date: Tue, 3 Mar 2026 20:23:39 +0100 Subject: [PATCH] Introduce assert_failure --- bin/git-check-assertions | 8 +++++++- test/git-check-assertions.bats | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bin/git-check-assertions b/bin/git-check-assertions index 60de125..cdc14e8 100755 --- a/bin/git-check-assertions +++ b/bin/git-check-assertions @@ -46,7 +46,13 @@ assert_success() { exit 1 fi } -export -f run assert_success +assert_failure() { + if [ "$status" -eq 0 ]; then + echo "Expected command to fail, but it succeeded." + exit 1 + fi +} +export -f run assert_success assert_failure # main flow orig_ref="$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)" diff --git a/test/git-check-assertions.bats b/test/git-check-assertions.bats index 271d0d0..064341f 100755 --- a/test/git-check-assertions.bats +++ b/test/git-check-assertions.bats @@ -131,3 +131,22 @@ commit_with_assertion() { assert_failure assert_output --partial "Expected command to succeed, but it failed." } + +@test "assert_failure should succeed if the executed command failed" { + git checkout -b feature + commit_with_assertion $'run exit 1\nassert_failure' + + run git-check-assertions + + assert_success +} + +@test "assert_failure should fail if the executed command succeeded" { + git checkout -b feature + commit_with_assertion $'run exit 0\nassert_failure' + + run git-check-assertions + + assert_failure + assert_output --partial "Expected command to fail, but it succeeded." +}