mirror of
https://github.com/tj/git-extras.git
synced 2026-09-09 23:16:17 -04:00
Implement half of tests in Bats (#1187)
* Initial scaffold
* Finish first half of Bats tests
* Add Bats to CI
* Fix CI
* Fix CI
* Fix configuration to set git name and email
* Fix `GIT_CONFIG_{KEY,VALUE}` indexing
* Fix Git config override precedence
* Remove unused and untested function
This commit is contained in:
parent
aadf5f874b
commit
18ecffef5e
|
|
@ -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
|
||||
|
|
|
|||
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
|
|
@ -70,22 +70,33 @@ jobs:
|
|||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Install poetry
|
||||
run: pip install poetry
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
cache: 'poetry'
|
||||
cache-dependency-path: "tests/pyproject.toml"
|
||||
python-version-file: "tests/pyproject.toml"
|
||||
- name: Install dependencies
|
||||
- name: Install Python Dependencies
|
||||
run: |
|
||||
cd tests || exit
|
||||
poetry install --only test
|
||||
- name: Unit test
|
||||
- name: Setup Bats
|
||||
id: setup-bats
|
||||
uses: bats-core/bats-action@3.0.0
|
||||
- name: Test with Pytest
|
||||
run: |
|
||||
cd tests
|
||||
poetry run pytest
|
||||
- name: Test with Bats
|
||||
env:
|
||||
BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}
|
||||
TERM: xterm
|
||||
run: bats ./tests
|
||||
|
||||
build:
|
||||
strategy:
|
||||
|
|
|
|||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "vendor/bats-all"]
|
||||
path = vendor/bats-all
|
||||
url = https://github.com/hyperupcall/bats-all
|
||||
93
tests/git-abort.bats
Normal file
93
tests/git-abort.bats
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
# shellcheck shell=bash
|
||||
|
||||
source "$BATS_TEST_DIRNAME/test_util.sh"
|
||||
|
||||
setup_file() {
|
||||
test_util.setup_file
|
||||
}
|
||||
|
||||
setup() {
|
||||
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 "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 "revert" {
|
||||
run git revert A
|
||||
assert_failure
|
||||
|
||||
run git status
|
||||
assert_line -p 'You are currently reverting 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
|
||||
}
|
||||
106
tests/git-alias.bats
Normal file
106
tests/git-alias.bats
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
# 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 --global alias.globalalias status
|
||||
git config --global alias.x status
|
||||
git config --local alias.localalias status
|
||||
git config --local alias.y status
|
||||
}
|
||||
|
||||
@test "list all works" {
|
||||
run git alias
|
||||
assert_output - <<-'EOF'
|
||||
globalalias = status
|
||||
localalias = status
|
||||
x = status
|
||||
y = status
|
||||
EOF
|
||||
assert_success
|
||||
}
|
||||
|
||||
@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
|
||||
}
|
||||
66
tests/git-archive-file.bats
Normal file
66
tests/git-archive-file.bats
Normal file
|
|
@ -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
|
||||
}
|
||||
52
tests/git-authors.bats
Normal file
52
tests/git-authors.bats
Normal file
|
|
@ -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_VALUE_0='test@example.com'
|
||||
GIT_CONFIG_VALUE_1='test'
|
||||
printf '%s\n' 'A' > tmpfile
|
||||
git add .
|
||||
git commit -m 'test: add data A'
|
||||
GIT_CONFIG_VALUE_0='testagain@example.com'
|
||||
GIT_CONFIG_VALUE_1='testagain'
|
||||
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=$(<AUTHORS)
|
||||
assert_equal "$content" $'test <test@example.com>\ntestagain <testagain@example.com>'
|
||||
}
|
||||
|
||||
@test "list authors has email defaultly" {
|
||||
run git authors --list
|
||||
assert_output $'test <test@example.com>\ntestagain <testagain@example.com>'
|
||||
assert_success
|
||||
|
||||
run git authors -l
|
||||
assert_output $'test <test@example.com>\ntestagain <testagain@example.com>'
|
||||
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
|
||||
}
|
||||
26
tests/test_util.sh
Normal file
26
tests/test_util.sh
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# shellcheck shell=bash
|
||||
|
||||
source "$BATS_TEST_DIRNAME/../vendor/bats-all/load.bash"
|
||||
|
||||
test_util.setup_file() {
|
||||
cd "$BATS_FILE_TMPDIR"
|
||||
|
||||
export GIT_CONFIG_NOSYSTEM=1
|
||||
export GIT_CONFIG_GLOBAL="$PWD/git_config"
|
||||
export GIT_CONFIG_COUNT=3
|
||||
export GIT_CONFIG_KEY_0="user.email"
|
||||
export GIT_CONFIG_VALUE_0="name@example.com"
|
||||
export GIT_CONFIG_KEY_1="user.name"
|
||||
export GIT_CONFIG_VALUE_1="Name"
|
||||
# This removes default warning about default "master" branch on some Git versions.
|
||||
export GIT_CONFIG_KEY_2="init.defaultBranch"
|
||||
export GIT_CONFIG_VALUE_2="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"
|
||||
}
|
||||
|
||||
test_util.cd_test() {
|
||||
cd "$BATS_TEST_TMPDIR"
|
||||
}
|
||||
1
vendor/bats-all
vendored
Submodule
1
vendor/bats-all
vendored
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit a16a4a2cfa744878992a5f5d3f206319dba54158
|
||||
Loading…
Reference in a new issue