Skip to content

Commit 8e4c7a7

Browse files
committed
added unit testing using bats
1 parent de423ed commit 8e4c7a7

File tree

5 files changed

+193
-2
lines changed

5 files changed

+193
-2
lines changed

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "test/lib/bats-support"]
2+
path = test/lib/bats-support
3+
url = https://github.com/ztombol/bats-support
4+
[submodule "test/lib/bats-assert"]
5+
path = test/lib/bats-assert
6+
url = https://github.com/ztombol/bats-assert

bin/gg

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ err_missing_param() {
6161
$(green "gg") $(yellow "$2") $(red "<$1>")
6262
6363
EOF
64-
exit 0
64+
exit 1
6565
}
6666

6767
unknown_command() {
@@ -70,7 +70,7 @@ unknown_command() {
7070
$(red "Unknown command: '$1'")
7171
7272
EOF
73-
exit 0
73+
exit 1
7474
}
7575

7676
# Commands
@@ -87,6 +87,7 @@ checkout() {
8787
# TODO: Success message
8888
if [ -z "$1" ]; then
8989
err_missing_param "thing to checkout" "ch"
90+
exit 1
9091
else
9192
checkoutItem="$1"
9293
git checkout $checkoutItem

test/gg.bats

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#!/usr/bin/env bats
2+
3+
load 'lib/bats-support/load'
4+
load 'lib/bats-assert/load'
5+
6+
TMP_DIRECTORY=$(mktemp -d)
7+
8+
# Setup git repo in temp dir to test functions against
9+
# --------------------------------------------------------------------
10+
11+
setup() {
12+
cd $TMP_DIRECTORY
13+
echo 'setting up'
14+
git init
15+
git config user.email "[email protected]"
16+
git config user.name "gg test"
17+
echo "test file" > test_file
18+
git add test_file
19+
run git commit -m "test commit"
20+
}
21+
22+
# If tests pass delete temp git repo
23+
# --------------------------------------------------------------------
24+
25+
teardown() {
26+
if [ $BATS_TEST_COMPLETED ]; then
27+
echo "Deleting $TMP_DIRECTORY"
28+
rm -rf $TMP_DIRECTORY
29+
else
30+
echo "** Did not delete $TMP_DIRECTORY, as test failed **"
31+
fi
32+
cd $BATS_TEST_DIRNAME
33+
}
34+
35+
# The tests
36+
# --------------------------------------------------------------------
37+
38+
@test "Invoke: no param prints help" {
39+
run gg
40+
assert_success
41+
assert_line --partial "simple git aliases"
42+
}
43+
44+
@test "Status" {
45+
run gg s
46+
assert_success
47+
assert_line --partial "On branch master"
48+
}
49+
50+
@test "Checkout: no param" {
51+
run gg ch
52+
assert_failure
53+
assert_line --partial "Missing parameter: thing to checkout"
54+
}
55+
56+
@test "Checkout: branch" {
57+
git branch test
58+
run gg ch test
59+
assert_line --partial "Switched to branch 'test'"
60+
}
61+
62+
@test "Pull" {
63+
run gg pl
64+
assert_success
65+
assert_line --partial "git-pull"
66+
}
67+
68+
# TODO: Learn how to test interactive input
69+
# @test "Checkout Pull Rebase" {
70+
# # Setup
71+
# gg b test
72+
# gg ch master
73+
# echo "test file 2" > test_file2
74+
# git add test_file2
75+
# git commit -m "test commit 2"
76+
# gg ch test
77+
# # Assert
78+
# run gg cpr
79+
# assert_success
80+
# }
81+
82+
@test "Push" {
83+
run gg p
84+
assert_success
85+
assert_line --partial "Could not read from remote repository."
86+
}
87+
88+
@test "Force Push" {
89+
run gg pf
90+
assert_success
91+
assert_line --partial "Strap in, we're force pushing..."
92+
}
93+
94+
@test "Log" {
95+
run gg l
96+
assert_success
97+
assert_line --partial "test commit"
98+
}
99+
100+
@test "Latest commit message" {
101+
run gg lc
102+
assert_success
103+
assert_line --partial "test commit"
104+
}
105+
106+
# TODO: Learn how to test interactive input
107+
# @test "Rebase" {
108+
# run gg r
109+
# assert_failure
110+
# assert_line --partial "Missing parameter: thing to checkout"
111+
# }
112+
113+
# @test "Rebase: no param" {
114+
# run gg r
115+
# assert_failure
116+
# assert_line --partial "Missing parameter: thing to checkout"
117+
# }
118+
119+
@test "Stash" {
120+
echo "test file 2" > test_file2
121+
run gg st
122+
assert_success
123+
assert_line --partial "Saved working directory"
124+
}
125+
126+
@test "Stash Pop" {
127+
echo "test file 2" > test_file2
128+
gg st
129+
run gg stp
130+
assert_success
131+
assert_line --partial "new file: test_file2"
132+
}
133+
134+
@test "Branch" {
135+
run gg b test
136+
assert_success
137+
assert_line --partial "Switched to a new branch 'test'"
138+
}
139+
140+
@test "Branch: no param" {
141+
run gg b
142+
assert_success
143+
assert_line --partial "All Branches:"
144+
}
145+
146+
@test "Branch delete" {
147+
git branch test
148+
run gg bd test
149+
assert_success
150+
assert_line --partial "Deleted branch test"
151+
}
152+
153+
@test "Branch delete: no param" {
154+
run gg bd
155+
assert_failure
156+
assert_line --partial "Missing parameter: branch name to delete"
157+
}
158+
159+
@test "Pull Request" {
160+
git remote add origin https://github.com/csi-lk/gg
161+
run gg pr
162+
assert_success
163+
assert_line --partial "Opening pull request for branch: master"
164+
}
165+
166+
@test "Display Help" {
167+
run gg -h
168+
assert_success
169+
assert_line --partial "simple git aliases"
170+
}
171+
172+
@test "Unknown Command" {
173+
run gg test
174+
assert_failure
175+
assert_line --partial "Unknown command: 'test'"
176+
}
177+
178+
@test "Clean" {
179+
run gg clean
180+
assert_success
181+
assert_line --partial "All Branches:"
182+
}

test/lib/bats-assert

Submodule bats-assert added at 9f88b42

test/lib/bats-support

Submodule bats-support added at 004e707

0 commit comments

Comments
 (0)