Skip to content

Commit 18ecffe

Browse files
authored
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
1 parent aadf5f8 commit 18ecffe

File tree

9 files changed

+368
-2
lines changed

9 files changed

+368
-2
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ trim_trailing_whitespace = false
2020

2121
[*.py]
2222
indent_size = 4
23+
24+
[*.bats]
25+
indent_style = tab
26+
indent_size = 4
27+
28+
[tests/*.sh]
29+
indent_style = tab
30+
indent_size = 4

.github/workflows/ci.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,33 @@ jobs:
7070

7171
steps:
7272
- uses: actions/checkout@v4
73+
with:
74+
submodules: recursive
7375
- name: Install poetry
7476
run: pip install poetry
7577
- name: Set up Python
7678
uses: actions/setup-python@v5
7779
with:
80+
python-version: '3.12'
7881
cache: 'poetry'
7982
cache-dependency-path: "tests/pyproject.toml"
8083
python-version-file: "tests/pyproject.toml"
81-
- name: Install dependencies
84+
- name: Install Python Dependencies
8285
run: |
8386
cd tests || exit
8487
poetry install --only test
85-
- name: Unit test
88+
- name: Setup Bats
89+
id: setup-bats
90+
uses: bats-core/[email protected]
91+
- name: Test with Pytest
8692
run: |
8793
cd tests
8894
poetry run pytest
95+
- name: Test with Bats
96+
env:
97+
BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}
98+
TERM: xterm
99+
run: bats ./tests
89100

90101
build:
91102
strategy:

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "vendor/bats-all"]
2+
path = vendor/bats-all
3+
url = https://github.com/hyperupcall/bats-all

tests/git-abort.bats

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# shellcheck shell=bash
2+
3+
source "$BATS_TEST_DIRNAME/test_util.sh"
4+
5+
setup_file() {
6+
test_util.setup_file
7+
}
8+
9+
setup() {
10+
test_util.cd_test
11+
12+
git init
13+
git commit --allow-empty -m "Initial commit"
14+
git branch A
15+
git branch B
16+
git checkout A
17+
printf '%s\n' 'a' > tmpfile
18+
git add .
19+
git commit -m A
20+
git checkout B
21+
printf '%s\n' 'b' > tmpfile
22+
git add .
23+
git commit -m B
24+
git status
25+
}
26+
27+
@test "cherry pick" {
28+
run git cherry-pick A
29+
assert_failure
30+
31+
run git status
32+
assert_line -p 'You are currently cherry-picking commit'
33+
assert_line -p 'Unmerged paths:'
34+
assert_success
35+
36+
run git abort
37+
assert_success
38+
39+
run git status
40+
assert_line -p 'nothing to commit, working tree clean'
41+
assert_success
42+
}
43+
44+
@test "merge" {
45+
run git merge A
46+
assert_failure
47+
48+
run git status
49+
assert_line -p 'You have unmerged paths'
50+
assert_line -p 'Unmerged paths:'
51+
assert_success
52+
53+
run git abort
54+
assert_success
55+
56+
run git status
57+
assert_line -p 'nothing to commit, working tree clean'
58+
assert_success
59+
}
60+
61+
@test "rebase" {
62+
run git rebase A
63+
assert_failure
64+
65+
run git status
66+
assert_line -p 'You are currently rebasing branch'
67+
assert_line -p 'Unmerged paths:'
68+
assert_success
69+
70+
run git abort
71+
assert_success
72+
73+
run git status
74+
assert_line -p 'nothing to commit, working tree clean'
75+
assert_success
76+
}
77+
78+
@test "revert" {
79+
run git revert A
80+
assert_failure
81+
82+
run git status
83+
assert_line -p 'You are currently reverting commit'
84+
assert_line -p 'Unmerged paths:'
85+
assert_success
86+
87+
run git abort
88+
assert_success
89+
90+
run git status
91+
assert_line -p 'nothing to commit, working tree clean'
92+
assert_success
93+
}

tests/git-alias.bats

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# shellcheck shell=bash
2+
3+
source "$BATS_TEST_DIRNAME/test_util.sh"
4+
5+
setup_file() {
6+
test_util.setup_file
7+
}
8+
9+
setup() {
10+
test_util.cd_test
11+
12+
git init
13+
git config --global alias.globalalias status
14+
git config --global alias.x status
15+
git config --local alias.localalias status
16+
git config --local alias.y status
17+
}
18+
19+
@test "list all works" {
20+
run git alias
21+
assert_output - <<-'EOF'
22+
globalalias = status
23+
localalias = status
24+
x = status
25+
y = status
26+
EOF
27+
assert_success
28+
}
29+
30+
@test "list all globally works" {
31+
run git alias --global
32+
assert_output - <<-'EOF'
33+
globalalias = status
34+
x = status
35+
EOF
36+
assert_success
37+
}
38+
39+
@test "list all locally works" {
40+
run git alias --local
41+
assert_output - <<-'EOF'
42+
localalias = status
43+
y = status
44+
EOF
45+
assert_success
46+
}
47+
48+
@test "search globally works" {
49+
run git alias --global global
50+
assert_output - <<-'EOF'
51+
globalalias = status
52+
EOF
53+
assert_success
54+
55+
run git alias --global local
56+
assert_output ''
57+
assert_failure
58+
}
59+
60+
@test "search locally works" {
61+
run git alias --local local
62+
assert_output - <<-'EOF'
63+
localalias = status
64+
EOF
65+
assert_success
66+
67+
run git alias --local global
68+
assert_output ''
69+
assert_failure
70+
}
71+
72+
@test "get alias globally and defaultly" {
73+
run git alias globalalias
74+
assert_output - <<-'EOF'
75+
globalalias = status
76+
EOF
77+
assert_success
78+
}
79+
80+
@test "set alias globally and defaultly" {
81+
git alias globalalias diff
82+
run git alias diff
83+
assert_output - <<-'EOF'
84+
globalalias = diff
85+
EOF
86+
assert_success
87+
}
88+
89+
@test "get alias locally" {
90+
run git alias --local localalias
91+
assert_output - <<-'EOF'
92+
localalias = status
93+
EOF
94+
assert_success
95+
}
96+
97+
@test "set alias locally" {
98+
git alias --local localalias diff
99+
run git alias
100+
assert_output - <<-'EOF'
101+
globalalias = status
102+
localalias = diff
103+
x = status
104+
y = status
105+
EOF
106+
}

tests/git-archive-file.bats

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# shellcheck shell=bash
2+
3+
source "$BATS_TEST_DIRNAME/test_util.sh"
4+
5+
setup_file() {
6+
test_util.setup_file
7+
}
8+
9+
setup() {
10+
test_util.cd_test
11+
12+
git init
13+
printf '%s\n' 'data' > tmpfile
14+
git add .
15+
git commit -m 'test: add data'
16+
git tag 0.1.0 -m 'bump: 0.1.0'
17+
}
18+
19+
@test "archive file on tags branch" {
20+
git checkout -b tags0.1.0
21+
run git archive-file
22+
assert_success
23+
24+
local describe_output=
25+
describe_output=$(git describe)
26+
assert_file_exists "${PWD##*/}.$describe_output.zip"
27+
}
28+
29+
@test "archive file on any not tags branch without default branch" {
30+
git checkout -b not-tags-branch
31+
run git archive-file
32+
assert_success
33+
34+
local describe_output=
35+
describe_output=$(git describe --always --long)
36+
assert_file_exists "${PWD##*/}.$describe_output.not-tags-branch.zip"
37+
}
38+
39+
@test "archive file on any not tags branch with default branch" {
40+
skip "Not working as expected"
41+
42+
run git archive-file
43+
assert_success
44+
45+
local describe_output=
46+
describe_output=$(git describe --always --long)
47+
assert_file_exists "${PWD##*/}.$describe_output.zip"
48+
}
49+
50+
@test "archive file on branch name has slash" {
51+
git checkout -b feature/slash
52+
run git archive-file
53+
assert_success
54+
55+
local describe_output=
56+
describe_output=$(git describe --always --long)
57+
assert_file_exists "${PWD##*/}.$describe_output.feature-slash.zip"
58+
}
59+
60+
@test "archive file on dirname has backslash" {
61+
skip
62+
}
63+
64+
@test "archive file on tag name has slash" {
65+
skip
66+
}

tests/git-authors.bats

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# shellcheck shell=bash
2+
3+
source "$BATS_TEST_DIRNAME/test_util.sh"
4+
5+
6+
setup_file() {
7+
test_util.setup_file
8+
}
9+
10+
setup() {
11+
test_util.cd_test
12+
13+
git init
14+
GIT_CONFIG_VALUE_0='[email protected]'
15+
GIT_CONFIG_VALUE_1='test'
16+
printf '%s\n' 'A' > tmpfile
17+
git add .
18+
git commit -m 'test: add data A'
19+
GIT_CONFIG_VALUE_0='[email protected]'
20+
GIT_CONFIG_VALUE_1='testagain'
21+
printf '%s\n' 'B' > tmpfile
22+
git add .
23+
git commit -m 'test: add data B'
24+
}
25+
26+
@test "output authors has email without any parameter" {
27+
run git authors
28+
assert_success
29+
30+
local content=$(<AUTHORS)
31+
assert_equal "$content" $'test <[email protected]>\ntestagain <[email protected]>'
32+
}
33+
34+
@test "list authors has email defaultly" {
35+
run git authors --list
36+
assert_output $'test <[email protected]>\ntestagain <[email protected]>'
37+
assert_success
38+
39+
run git authors -l
40+
assert_output $'test <[email protected]>\ntestagain <[email protected]>'
41+
assert_success
42+
}
43+
44+
@test "list authors has no email" {
45+
run git authors --list --no-email
46+
assert_output $'test\ntestagain'
47+
assert_success
48+
49+
run git authors -l --no-email
50+
assert_output $'test\ntestagain'
51+
assert_success
52+
}

tests/test_util.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# shellcheck shell=bash
2+
3+
source "$BATS_TEST_DIRNAME/../vendor/bats-all/load.bash"
4+
5+
test_util.setup_file() {
6+
cd "$BATS_FILE_TMPDIR"
7+
8+
export GIT_CONFIG_NOSYSTEM=1
9+
export GIT_CONFIG_GLOBAL="$PWD/git_config"
10+
export GIT_CONFIG_COUNT=3
11+
export GIT_CONFIG_KEY_0="user.email"
12+
export GIT_CONFIG_VALUE_0="[email protected]"
13+
export GIT_CONFIG_KEY_1="user.name"
14+
export GIT_CONFIG_VALUE_1="Name"
15+
# This removes default warning about default "master" branch on some Git versions.
16+
export GIT_CONFIG_KEY_2="init.defaultBranch"
17+
export GIT_CONFIG_VALUE_2="main"
18+
19+
# Append to path so that we can access all commands included from git-extras
20+
# TODO: This currently breaks with commands that are included in "not_needed_git_repo" etc.
21+
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
22+
}
23+
24+
test_util.cd_test() {
25+
cd "$BATS_TEST_TMPDIR"
26+
}

0 commit comments

Comments
 (0)