diff --git a/.editorconfig b/.editorconfig index bc1a4e7c..b715678d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -20,3 +20,11 @@ trim_trailing_whitespace = false [*.py] indent_size = 4 + +[*.bats] +indent_style = tab +indent_size = 4 + +[tests/*.sh] +indent_style = tab +indent_size = 4 diff --git a/tests/git-abort.bats b/tests/git-abort.bats index 15b120d6..94f9f8d6 100644 --- a/tests/git-abort.bats +++ b/tests/git-abort.bats @@ -3,61 +3,91 @@ source "$BATS_TEST_DIRNAME/test_util.sh" setup_file() { - test_util.setup_file + test_util.setup_file } -# This is ran before each "@test". setup() { - # cd's to a temporary directory that is unique for each "@test". - test_util.cd_test - - # Do initialization (this code is copied from test_git_alias.py) - git init - git config --global alias.globalalias status - git config --global alias.x status - git config --local alias.localalias status - git config --local alias.y status + test_util.cd_test + + git init + git commit --allow-empty -m "Initial commit" + git branch A + git branch B + git checkout A + printf '%s\n' 'a' > tmpfile + git add . + git commit -m A + git checkout B + printf '%s\n' 'b' > tmpfile + git add . + git commit -m B + git status +} + +@test "cherry pick" { + run git cherry-pick A + assert_failure + + run git status + assert_line -p 'You are currently cherry-picking commit' + assert_line -p 'Unmerged paths:' + assert_success + + run git abort + assert_success + + run git status + assert_line -p 'nothing to commit, working tree clean' + assert_success +} + +@test "merge" { + run git merge A + assert_failure + + run git status + assert_line -p 'You have unmerged paths' + assert_line -p 'Unmerged paths:' + assert_success + + run git abort + assert_success + + run git status + assert_line -p 'nothing to commit, working tree clean' + assert_success } -@test "list all works" { - run git alias - - # This is one way to do it, most similar to the "test_list_all" test in - # test_git_alias.py. It's slightly more accurate because each substring - # must match a particular line of output (rather than matching any part of the output) - assert_line 'globalalias = status' - assert_line 'x = status' - assert_line 'localalias = status' - assert_line 'y = status' - - # To test the full output, this is one way: - assert_output 'globalalias = status -localalias = status -x = status -y = status' - - # One can use heredocs to also achieve this, which makes the test look a little nicer. Note that the indentation MUST BE DONE WITH TABS. - assert_output - <<-"EOF" - globalalias = status - localalias = status - x = status - y = status -EOF - - # I tend to put 'assert_success' at the bottom. This usually makes debugging easier because an "assert_output" written above will usuall fail first. This will cause the the output of the run command to be printed (instead of just the exit code). - # The downside to this is that sometimes there is a bug: An error "lines: parameter not set" will be shown instead (which is a Bats issue). - assert_success +@test "rebase" { + run git rebase A + assert_failure + + run git status + assert_line -p 'You are currently rebasing branch' + assert_line -p 'Unmerged paths:' + assert_success + + run git abort + assert_success + + run git status + assert_line -p 'nothing to commit, working tree clean' + assert_success } -@test "list all global works" { - run git alias --global +@test "revert" { + run git revert A + assert_failure - # One debugging technique for bats test I've found useful is to redirect to - # fd3. The variable "$output" is set by Bats, and it is the output of the previous "run" command (in this case, `git alias --global`). Here, we are printing it to fd3 for debugging. Naturally, be sure to run `exec 3>&1` (assuming you are running Bash or Zsh shell) in the terminal before running Bats. -# echo "$output" >&3 + run git status + assert_line -p 'You are currently reverting commit' + assert_line -p 'Unmerged paths:' + assert_success + run git abort + assert_success - assert_output 'globalalias = status -x = status' - assert_success + run git status + assert_line -p 'nothing to commit, working tree clean' + assert_success } diff --git a/tests/git-alias.bats b/tests/git-alias.bats index d6d57ef8..2da2d3c1 100644 --- a/tests/git-alias.bats +++ b/tests/git-alias.bats @@ -3,19 +3,104 @@ source "$BATS_TEST_DIRNAME/test_util.sh" setup_file() { - test_util.setup_file + test_util.setup_file } setup() { - test_util.cd_test + test_util.cd_test - # Any extra setup goes here. + git init + git config --global alias.globalalias status + git config --global alias.x status + git config --local alias.localalias status + git config --local alias.y status } -@test "blah 1" { - : +@test "list all works" { + run git alias + assert_output - <<-'EOF' + globalalias = status + localalias = status + x = status + y = status +EOF + assert_success } -@test "blah 2" { - : +@test "list all globally works" { + run git alias --global + assert_output - <<-'EOF' + globalalias = status + x = status +EOF + assert_success +} + +@test "list all locally works" { + run git alias --local + assert_output - <<-'EOF' + localalias = status + y = status +EOF + assert_success +} + +@test "search globally works" { + run git alias --global global + assert_output - <<-'EOF' + globalalias = status +EOF + assert_success + + run git alias --global local + assert_output '' + assert_failure +} + +@test "search locally works" { + run git alias --local local + assert_output - <<-'EOF' + localalias = status +EOF + assert_success + + run git alias --local global + assert_output '' + assert_failure +} + +@test "get alias globally and defaultly" { + run git alias globalalias + assert_output - <<-'EOF' + globalalias = status +EOF + assert_success +} + +@test "set alias globally and defaultly" { + git alias globalalias diff + run git alias diff + assert_output - <<-'EOF' + globalalias = diff +EOF + assert_success +} + +@test "get alias locally" { + run git alias --local localalias + assert_output - <<-'EOF' + localalias = status +EOF + assert_success +} + +@test "set alias locally" { + git alias --local localalias diff + run git alias + assert_output - <<-'EOF' + globalalias = status + localalias = diff + x = status + y = status +EOF } diff --git a/tests/git-archive-file.bats b/tests/git-archive-file.bats new file mode 100644 index 00000000..920035a9 --- /dev/null +++ b/tests/git-archive-file.bats @@ -0,0 +1,66 @@ +# shellcheck shell=bash + +source "$BATS_TEST_DIRNAME/test_util.sh" + +setup_file() { + test_util.setup_file +} + +setup() { + test_util.cd_test + + git init + printf '%s\n' 'data' > tmpfile + git add . + git commit -m 'test: add data' + git tag 0.1.0 -m 'bump: 0.1.0' +} + +@test "archive file on tags branch" { + git checkout -b tags0.1.0 + run git archive-file + assert_success + + local describe_output= + describe_output=$(git describe) + assert_file_exists "${PWD##*/}.$describe_output.zip" +} + +@test "archive file on any not tags branch without default branch" { + git checkout -b not-tags-branch + run git archive-file + assert_success + + local describe_output= + describe_output=$(git describe --always --long) + assert_file_exists "${PWD##*/}.$describe_output.not-tags-branch.zip" +} + +@test "archive file on any not tags branch with default branch" { + skip "Not working as expected" + + run git archive-file + assert_success + + local describe_output= + describe_output=$(git describe --always --long) + assert_file_exists "${PWD##*/}.$describe_output.zip" +} + +@test "archive file on branch name has slash" { + git checkout -b feature/slash + run git archive-file + assert_success + + local describe_output= + describe_output=$(git describe --always --long) + assert_file_exists "${PWD##*/}.$describe_output.feature-slash.zip" +} + +@test "archive file on dirname has backslash" { + skip +} + +@test "archive file on tag name has slash" { + skip +} diff --git a/tests/git-authors.bats b/tests/git-authors.bats new file mode 100644 index 00000000..bc85edb9 --- /dev/null +++ b/tests/git-authors.bats @@ -0,0 +1,52 @@ +# shellcheck shell=bash + +source "$BATS_TEST_DIRNAME/test_util.sh" + + +setup_file() { + test_util.setup_file +} + +setup() { + test_util.cd_test + + git init + git config --local user.name test + git config --local user.email test@example.com + printf '%s\n' 'A' > tmpfile + git add . + git commit -m 'test: add data A' + git config --local user.name testagain + git config --local user.email testagain@example.com + printf '%s\n' 'B' > tmpfile + git add . + git commit -m 'test: add data B' +} + +@test "output authors has email without any parameter" { + run git authors + assert_success + + local content=$(\ntestagain ' +} + +@test "list authors has email defaultly" { + run git authors --list + assert_output $'test \ntestagain ' + assert_success + + run git authors -l + assert_output $'test \ntestagain ' + assert_success +} + +@test "list authors has no email" { + run git authors --list --no-email + assert_output $'test\ntestagain' + assert_success + + run git authors -l --no-email + assert_output $'test\ntestagain' + assert_success +} diff --git a/tests/test_util.sh b/tests/test_util.sh index 4b2fed75..a3405434 100644 --- a/tests/test_util.sh +++ b/tests/test_util.sh @@ -1,33 +1,20 @@ # shellcheck shell=bash -# Bats has some very (official) nifty utility libraries to make testing easier. -# For example, `bats-support` adds `fail()` -# For example, `bats-assert` adds `assert()`, `assert_success`, `assert_output`. -# These helper functions: -# - _Significantly_ improve error messages (this is the main reason why I like to use these) -# - Make the test more semantic and readable (ex. assert_success reads better than [ $? == 0 ]) -# I created a repository that combines each of the three Bats utility libraries. This makes testing easier, but separate submodules for each Bats utility library can be crated (ex. vendor/bats-all, vendor/bats-support, vendor/bats-assert) -# This `load.bash` sources all Bats utility libraries source "$BATS_TEST_DIRNAME/../vendor/bats-all/load.bash" -# Various helper functions. I always prefix them with "test_util" so their intent and -# location of definition (this file) is obvious wherever they are used. - test_util.setup_file() { - # Bats automatically sets 'set -e', so we don't have to do any "|| exit 1" stuff - cd "$BATS_FILE_TMPDIR" + cd "$BATS_FILE_TMPDIR" - # Export some variables so Git neither reads nor mutates the users' Git config. - export GIT_CONFIG_NOSYSTEM=1 - export GIT_CONFIG_GLOBAL="$PWD/git_config" - # This removes default warning about default "master" branch on some Git versions. - git config --global init.defaultBranch main + export GIT_CONFIG_NOSYSTEM=1 + export GIT_CONFIG_GLOBAL="$PWD/git_config" + # This removes default warning about default "master" branch on some Git versions. + git config --global init.defaultBranch main - # Append to path so that we can access all commands included from git-extras - # TODO: This currently breaks with commands that are included in "not_needed_git_repo" etc. - PATH="$BATS_TEST_DIRNAME/../bin:$PATH" + # Append to path so that we can access all commands included from git-extras + # TODO: This currently breaks with commands that are included in "not_needed_git_repo" etc. + PATH="$BATS_TEST_DIRNAME/../bin:$PATH" } test_util.cd_test() { - cd "$BATS_TEST_TMPDIR" + cd "$BATS_TEST_TMPDIR" }