diff --git a/bin/git-check-assertions b/bin/git-check-assertions index cdc14e8..ea221fc 100755 --- a/bin/git-check-assertions +++ b/bin/git-check-assertions @@ -35,7 +35,7 @@ export -f assert_fails # helper functions inspired by bats/bats-assert run() { set +e - ("$@") + output="$("$@" 2>&1)" status=$? set -e return 0 @@ -52,7 +52,14 @@ assert_failure() { exit 1 fi } -export -f run assert_success assert_failure +assert_output() { + if [ "$output" != "$1" ]; then + echo "Expected output to equal: $1" + echo "Actual output: $output" + exit 1 + fi +} +export -f run assert_success assert_failure assert_output # 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 064341f..11f23e7 100755 --- a/test/git-check-assertions.bats +++ b/test/git-check-assertions.bats @@ -150,3 +150,23 @@ commit_with_assertion() { assert_failure assert_output --partial "Expected command to fail, but it succeeded." } + +@test "assert_output should succeed if the output matches the given string" { + git checkout -b feature + commit_with_assertion $'run echo hello\nassert_output hello' + + run git-check-assertions + + assert_success +} + +@test "assert_output should fail if the output does not match the given string" { + git checkout -b feature + commit_with_assertion $'run echo hello\nassert_output goodbye' + + run git-check-assertions + + assert_failure + assert_output --partial "Expected output to equal: goodbye" + assert_output --partial "Actual output: hello" +}