#!/usr/bin/env bats setup() { set -euo pipefail bats_load_library bats-support bats_load_library bats-assert bats_load_library bats-file DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" PATH="$DIR/../bin:$PATH" mkdir "$BATS_TEST_TMPDIR/repo" cd "$BATS_TEST_TMPDIR/repo" git init -b main git config user.email "john.doe@example.com" git config user.name "John Doe" touch readme git add readme git commit -m "initial" } commit_with_assertion() { git commit --allow-empty -F - <<-EOF test \`\`\`git-check-assertions $1 \`\`\` EOF } @test "should not run any assertion blocks when on main" { commit_with_assertion "touch ../test" run git-check-assertions assert_success assert_file_not_exists ../test } @test "should not run any assertion blocks from main when on a feature branch" { commit_with_assertion "touch ../test" git checkout -b feature git commit --allow-empty -m "feature" run git-check-assertions assert_success assert_file_not_exists ../test } @test "should run all succeeding assertion blocks on the feature branch and finally return to the original branch" { git checkout -b feature commit_with_assertion "touch ../test1" commit_with_assertion "touch ../test2" run git-check-assertions assert_success assert_file_exists ../test1 assert_file_exists ../test2 run git symbolic-ref --short HEAD assert_output "feature" } @test "should stop at the first failing assertion block and return to the original branch" { git checkout -b feature commit_with_assertion "touch ../test1 && exit 3" commit_with_assertion "touch ../test2" run git-check-assertions assert_failure assert_file_exists ../test1 assert_file_not_exists ../test2 run git symbolic-ref --short HEAD assert_output "feature" } @test "should restore worktree when finished" { git checkout -b feature commit_with_assertion 'echo blah >> readme' run git-check-assertions assert_success assert_size_zero readme } @test "should restore worktree between commits" { git checkout -b feature commit_with_assertion 'echo blah >> readme' commit_with_assertion 'cp readme ../test' run git-check-assertions assert_success assert_size_zero ../test } @test "assertions should run against the version of the code inside of the commit" { git checkout -b feature echo commit1 >readme git add readme commit_with_assertion 'grep commit1 readme' echo commit2 >readme git add readme commit_with_assertion 'grep commit2 readme' run git-check-assertions 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." } @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." }