Skip to content

Commit 26bd63f

Browse files
committed
Finish first half of Bats tests
1 parent 88615ce commit 26bd63f

File tree

6 files changed

+304
-76
lines changed

6 files changed

+304
-76
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

tests/git-abort.bats

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,91 @@
33
source "$BATS_TEST_DIRNAME/test_util.sh"
44

55
setup_file() {
6-
test_util.setup_file
6+
test_util.setup_file
77
}
88

9-
# This is ran before each "@test".
109
setup() {
11-
# cd's to a temporary directory that is unique for each "@test".
12-
test_util.cd_test
13-
14-
# Do initialization (this code is copied from test_git_alias.py)
15-
git init
16-
git config --global alias.globalalias status
17-
git config --global alias.x status
18-
git config --local alias.localalias status
19-
git config --local alias.y status
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
2059
}
2160

22-
@test "list all works" {
23-
run git alias
24-
25-
# This is one way to do it, most similar to the "test_list_all" test in
26-
# test_git_alias.py. It's slightly more accurate because each substring
27-
# must match a particular line of output (rather than matching any part of the output)
28-
assert_line 'globalalias = status'
29-
assert_line 'x = status'
30-
assert_line 'localalias = status'
31-
assert_line 'y = status'
32-
33-
# To test the full output, this is one way:
34-
assert_output 'globalalias = status
35-
localalias = status
36-
x = status
37-
y = status'
38-
39-
# 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.
40-
assert_output - <<-"EOF"
41-
globalalias = status
42-
localalias = status
43-
x = status
44-
y = status
45-
EOF
46-
47-
# 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).
48-
# 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).
49-
assert_success
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
5076
}
5177

52-
@test "list all global works" {
53-
run git alias --global
78+
@test "revert" {
79+
run git revert A
80+
assert_failure
5481

55-
# One debugging technique for bats test I've found useful is to redirect to
56-
# 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.
57-
# echo "$output" >&3
82+
run git status
83+
assert_line -p 'You are currently reverting commit'
84+
assert_line -p 'Unmerged paths:'
85+
assert_success
5886

87+
run git abort
88+
assert_success
5989

60-
assert_output 'globalalias = status
61-
x = status'
62-
assert_success
90+
run git status
91+
assert_line -p 'nothing to commit, working tree clean'
92+
assert_success
6393
}

tests/git-alias.bats

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,104 @@
33
source "$BATS_TEST_DIRNAME/test_util.sh"
44

55
setup_file() {
6-
test_util.setup_file
6+
test_util.setup_file
77
}
88

99
setup() {
10-
test_util.cd_test
10+
test_util.cd_test
1111

12-
# Any extra setup goes here.
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
1317
}
1418

15-
@test "blah 1" {
16-
:
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
1728
}
1829

19-
@test "blah 2" {
20-
:
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
21106
}

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 --local user.name test
15+
git config --local user.email [email protected]
16+
printf '%s\n' 'A' > tmpfile
17+
git add .
18+
git commit -m 'test: add data A'
19+
git config --local user.name testagain
20+
git config --local user.email [email protected]
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+
}

0 commit comments

Comments
 (0)