-
Notifications
You must be signed in to change notification settings - Fork 49
/
test-git-wip.sh
executable file
·235 lines (173 loc) · 3.87 KB
/
test-git-wip.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
set -e
# split the script name into BASE directory, and the name it calls itSELF
BASE=$(realpath ${0%/*})
SELF=${0##*/}
# this tells git where to find our copy of git-wip script
export PATH="$BASE:$PATH"
# keep state for each test in this temporary tree
TEST_TREE=/tmp/git-wip-test-$$
REPO="$TEST_TREE/repo"
CMD="$TEST_TREE/cmd"
OUT="$TEST_TREE/out"
RC="$TEST_TREE/rc"
# ------------------------------------------------------------------------
# some helpful utility functions
die() { echo >&2 "ERROR: $@" ; exit 1 ; }
warn() { echo >&2 "WARNING: $@" ; }
comment() { echo >&2 "# $@" ; }
_RUN() {
comment "$@"
[ `pwd` = "$REPO" ] || die "expecting to be in $REPO, not `pwd`"
set +e
echo "$@" >"$CMD"
eval "$@" >"$OUT" 2>&1
echo "$?" >"$RC"
set -e
}
RUN() {
_RUN "$@"
local rc="`cat $RC`"
[ "$rc" = 0 ] || handle_error
}
EXP_none() {
local out="$(head -n1 "$OUT")"
if [ -n "$out" ] ; then
warn "out: $out"
handle_error
fi
}
EXP_text() {
local exp="$1"
local out="$(head -n1 "$OUT")"
if [ "$out" != "$exp" ] ; then
warn "exp: $exp"
warn "out: $out"
handle_error
fi
}
EXP_grep() {
if ! grep -q "$@" < "$OUT" ; then
warn "grep: $@"
handle_error
fi
}
create_test_tree() {
rm -rf "$REPO"
mkdir -p "$REPO"
cd "$REPO"
RUN git init
}
cleanup_test_tree() {
rm -rf "$TEST_TREE"
}
handle_error() {
set +e
warn "CMD='`cat $CMD`' RC=`cat $RC`"
cat >&1 "$OUT"
cleanup_test_tree
exit 1
}
trap cleanup_test_tree EXIT
trap handle_error INT TERM ERR
# ------------------------------------------------------------------------
# tests
test_general() {
# init
create_test_tree
RUN "echo 1 >README"
RUN git add README
RUN git commit -m README
# run wip w/o changes
_RUN git wip save
EXP_text "no changes"
RUN git wip save --editor
EXP_none
RUN git wip save -e
EXP_none
# expecting a master branch
RUN git branch
EXP_grep "^\* master$"
EXP_grep -v "wip"
# not expecting wip ref at this time
RUN git for-each-ref
EXP_grep -v "commit.refs/wip/master$"
# make changes, store wip
RUN "echo 2 >README"
RUN git wip save --editor
EXP_none
# expecting a wip ref
RUN git for-each-ref
EXP_grep "commit.refs/wip/master$"
# expecting a log entry
RUN git wip log
EXP_grep "^commit "
EXP_grep "^\s\+WIP$"
# there should be no wip branch
RUN git branch
EXP_grep -v "wip"
# make changes, store wip
RUN "echo 3 >README"
RUN git wip save "\"message2\""
EXP_none
# expecting both log entries
RUN git wip log
EXP_grep "^commit "
EXP_grep "^\s\+WIP$"
EXP_grep "^\s\+message2$"
# make a commit
RUN git add -u README
RUN git commit -m README.2
# make changes, store wip
RUN "echo 4 >UNTRACKED"
RUN "echo 4 >README"
RUN git wip save "\"message3\""
EXP_none
# expecting message3, not message2 or original WIP
RUN git wip log
EXP_grep "^commit "
EXP_grep -v "^\s\+WIP$"
EXP_grep -v "^\s\+message2$"
EXP_grep "^\s\+message3$"
# expecting file changes to README, not UNTRACKED
RUN git wip log --stat
EXP_grep "^commit "
EXP_grep "^ README | 2"
EXP_grep -v "UNTRACKED"
# need to be able to extract latest data from git wip branch
RUN git show HEAD:README
EXP_grep '^3$'
EXP_grep -v '^4$'
RUN git show wip/master:README
EXP_grep -v '^3$'
EXP_grep '^4$'
}
test_spaces() {
# init
create_test_tree
RUN "echo 1 >\"s p a c e s\""
RUN git add "\"s p a c e s\""
RUN git commit -m "\"s p a c e s\""
# make changes, store wip
RUN "echo 2 >\"s p a c e s\""
RUN git wip save "\"message with spaces\""
EXP_none
# expecting a wip ref
RUN git for-each-ref
EXP_grep "commit.refs/wip/master$"
# expecting a log entry
RUN git wip log
EXP_grep "^commit "
EXP_grep "^\s\+message with spaces$"
}
# ------------------------------------------------------------------------
# run tests
TESTS=( test_general test_spaces )
for TEST in "${TESTS[@]}" ; do
echo "-- $TEST"
$TEST
echo "OK"
done
trap - INT TERM ERR
cleanup_test_tree
echo "DONE"