diff --git a/test/git-check-assertions.bats b/test/git-check-assertions.bats index 4ad07e9..0b0b0e1 100755 --- a/test/git-check-assertions.bats +++ b/test/git-check-assertions.bats @@ -1,19 +1,114 @@ #!/usr/bin/env bats setup() { + set -euo pipefail + load 'test_helper/bats-support/load' load 'test_helper/bats-assert/load' + load 'test_helper/bats-file/load' 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" } -@test "can run" { - git commit --allow-empty -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 }