diff --git a/bin/git-check-assertions b/bin/git-check-assertions index f7fe936..60de125 100755 --- a/bin/git-check-assertions +++ b/bin/git-check-assertions @@ -32,6 +32,23 @@ assert_fails() { } export -f assert_fails +# helper functions inspired by bats/bats-assert +run() { + set +e + ("$@") + status=$? + set -e + return 0 +} +assert_success() { + if [ "$status" -ne 0 ]; then + echo "Expected command to succeed, but it failed." + exit 1 + fi +} +export -f run assert_success + +# main flow orig_ref="$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)" base="$(git merge-base main HEAD)" mapfile -t commits < <(git rev-list --reverse "${base}..HEAD") diff --git a/test/git-check-assertions.bats b/test/git-check-assertions.bats index f58c3ea..271d0d0 100755 --- a/test/git-check-assertions.bats +++ b/test/git-check-assertions.bats @@ -112,3 +112,22 @@ commit_with_assertion() { assert_success } + +@test "assert_success should succeed if the executed command succeeded" { + git checkout -b feature + commit_with_assertion $'run exit 0\nassert_success' + + run git-check-assertions + + assert_success +} + +@test "assert_success should fail if the executed command failed" { + git checkout -b feature + commit_with_assertion $'run exit 1\nassert_success' + + run git-check-assertions + + assert_failure + assert_output --partial "Expected command to succeed, but it failed." +}