forked from bleis-tift/Git-Hooks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_common.sh
executable file
·120 lines (94 loc) · 3.09 KB
/
test_common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /bin/sh
. ./common.sh
test_getGitBranchName()
{
git checkout master >/dev/null 2>&1
assertEquals "master" "$(getGitBranchName)"
git checkout -b hoge >/dev/null 2>&1
assertEquals "hoge" "$(getGitBranchName)"
git checkout master >/dev/null 2>&1
git branch -D hoge >/dev/null 2>&1
assertEquals "master" "$(getGitBranchName)"
}
test_isOnMasterBranch()
{
git checkout master >/dev/null 2>&1
isOnMasterBranch
assertEquals 0 $?
git checkout -b hoge >/dev/null 2>&1
isOnMasterBranch
assertEquals 1 $?
git checkout master >/dev/null 2>&1
git branch -D hoge >/dev/null 2>&1
}
test_appendMsgTo1stLine()
{
# 2行のパターン
cat << EOF > ./fortest.txt
first line
second line
EOF
appendMsgTo1stLine fortest.txt "hoge"
assertEquals "first line hoge" "$(cat ./fortest.txt | head -1)"
assertEquals "second line" "$(cat ./fortest.txt | head -2 | tail -1)"
# 1行のパターン
echo "first line" > ./fortest.txt
appendMsgTo1stLine fortest.txt "piyo"
assertEquals "first line piyo" "$(cat ./fortest.txt)"
# 空のパターン
rm ./fortest.txt
touch ./fortest.txt
appendMsgTo1stLine fortest.txt "foo"
assertEquals "foo" "$(cat ./fortest.txt)"
rm ./fortest.txt
}
test_extractTicketId()
{
git checkout master >/dev/null 2>&1
assertEquals "" "$(extractTicketId)"
git checkout -b id/42 >/dev/null 2>&1
assertEquals "refs 42" "$(extractTicketId)"
# 前は一階層のみ階層化可能
git checkout -b bug/id/10 >/dev/null 2>&1
assertEquals "refs 10" "$(extractTicketId)"
# 後ろはどれだけついてもOK
git checkout -b id/9/some/description >/dev/null 2>&1
assertEquals "refs 9" "$(extractTicketId)"
# 両方ついてもOK
git checkout -b issue/id/8/some/description >/dev/null 2>&1
assertEquals "refs 8" "$(extractTicketId)"
git checkout master >/dev/null 2>&1
git branch -D id/42 >/dev/null 2>&1
git branch -D bug/id/10 >/dev/null 2>&1
git branch -D id/9/some/description >/dev/null 2>&1
git branch -D issue/id/8/some/description >/dev/null 2>&1
}
test_hasTicketId()
{
git checkout -b "test/for/hasTicketId" >/dev/null 2>&1
touch test4hasticketid1
git add .
git commit -m "without ticket id." >/dev/null 2>&1
hash="$(git log -1 | head -1)"
assertEquals "false" "$(hasTicketId ${hash##commit })"
touch test4hasticketid2
git add .
git commit -m "with ticket id. refs 42" >/dev/null 2>&1
hash="$(git log -1 | head -1)"
assertEquals "true" "$(hasTicketId ${hash##commit })"
git checkout master >/dev/null 2>&1
git branch -D "test/for/hasTicketId" >/dev/null 2>&1
}
test_extractParents()
{
git checkout -b "test/for/extractParents" >/dev/null 2>&1
old="$(git log -1 | head -1)"
touch test4extractparents
git add .
git commit -m "test" >/dev/null 2>&1
new="$(git log -1 | head -1)"
assertEquals ${old##commit } "$(extractParents ${new##commit })"
git checkout master >/dev/null 2>&1
git branch -D "test/for/extractParents" >/dev/null 2>&1
}
. ./shunit2/src/shell/shunit2