-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgit-repo-watcher-tests
executable file
·267 lines (199 loc) · 6.41 KB
/
git-repo-watcher-tests
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
main_script="git-repo-watcher"
shunit_git="https://github.com/kward/shunit2"
# The tests use the temp folder to store the test framework
# and all generated git repositories
temp_dir="/tmp/git-repo-watcher"
stdout="$temp_dir/stdout.txt"
shunit2_dir="$temp_dir/shunit2/"
remote_repo="remote"
remote_repo_clone="remote-clone"
watched_clone="watched-clone"
test_file_name="test-file"
test_commit_message="Test commit message"
# A verbose-flag can be passed ("-v")
[[ "$1" == "-v" ]] && verbose=true && shift
# Redirecting output
if [[ "$verbose" ]]; then
exec 3>&1
exec 4>&2
else
exec 3>/dev/null
exec 4>/dev/null
fi
# Create temp directory
[[ ! -d "$temp_dir" ]] && mkdir "$temp_dir"
# Checkout shunit2 unit test framework
if [[ ! -d "$shunit2_dir" ]]; then
git clone --depth 1 "$shunit_git" "$shunit2_dir"
fi
# Prints a seperator. It helps to make the output more readable.
printSeparator() {
local sep="$1"
[[ -z "$sep" ]] && sep="-"
# shellcheck disable=SC2034
for i in {1..75}; do local x=$x"$sep"; done && echo "$x"
}
# Removes created temp files and directories
cleanupTempFiles() {
[[ -f "$stdout" ]] && rm "$stdout"
[[ -d "$temp_dir/$remote_repo" ]] && rm -rf "${temp_dir:?}/$remote_repo"
[[ -d "$temp_dir/$watched_clone" ]] && rm -rf "${temp_dir:?}/$watched_clone"
[[ -d "$temp_dir/$remote_repo_clone" ]] && rm -rf "${temp_dir:?}/$remote_repo_clone"
# I don't know why, but the following tests seem to start,
# before all temp files are deleted, so we pause a little
sleep 0.05
}
# Will be executed before each test
# https://github.com/kward/shunit2#-setupteardown
setUp() {
cd "$script_dir" || exit 1
# Create a fresh 'remote' git repository
git init --bare "$temp_dir/$remote_repo" 1>&3 2>&4
git clone "$temp_dir/$remote_repo" "$temp_dir/$remote_repo_clone" 1>&3 2>&4
pushOneFile 'README.md'
git clone "$temp_dir/$remote_repo" "$temp_dir/$watched_clone" 1>&3 2>&4
}
# Will be executed after each test
tearDown() {
cleanupTempFiles
printSeparator
return 0
}
# Commiting and pushing file to remote repository
#
# $1 - File name
# $2 - Commit message
pushOneFile() {
local file="$1"
[[ -z "$file" ]] && file="$test_file_name"
local message="$2"
[[ -z "$message" ]] && message="$test_commit_message"
cd "$temp_dir/$remote_repo_clone" || exit 1
touch "$file"
git add . 1>&3 2>&4
git commit -m "$message" 1>&3 2>&4
git push 1>&3 2>&4
}
printBashVersion() {
echo && printSeparator "#"
echo "Testing on bash-version: '$BASH_VERSION'"
printSeparator "#" && echo && printSeparator
}
# Start the main git-repo-watcher-script
#
# $1 - override options
startWatcher() {
local options=" -d $temp_dir/$watched_clone"
[[ -n "$1" ]] && options="$1"
# A short interval is used to guarantee multiple iterations
options="$options -i 0.1" # 0.1 seconds
eval "$script_dir/$main_script $options" &>$stdout &
watcher_pid=$!
}
# $1 - time to collect output
collectOutput() {
local sleep_time=1
[[ -n "$1" ]] && sleep_time="$1"
# We wait one second to collect stdout of multiple
# iterations of the main watch loop.
sleep "$sleep_time"
# Killing the watcher script
disown "$watcher_pid"
kill "$watcher_pid" 2>/dev/null
# Reading the logfile
result=$(cat "$stdout")
[[ "$verbose" ]] && echo "$result"
}
# $1 - branch name
createNewBranch() {
cd "$temp_dir/$remote_repo_clone" || exit 1
git checkout -b "$1" 1>&3 2>&4
git push --set-upstream origin "$1" 1>&3 2>&4
}
# -------------------------------------------------------------------- #
# Should print the help screen, if no arguments given
testHelpScreen() {
startWatcher " " # no arguments
collectOutput 0.1
assertContains "$result" "ERROR: Git directory (-d) not given!"
assertContains "$result" "The following options are available:"
}
# -------------------------------------------------------------------- #
testNewCommitBeforeStart() {
pushOneFile
startWatcher
collectOutput
assertContains "$result" "1 file changed"
}
# -------------------------------------------------------------------- #
testNewCommitAfterStart() {
startWatcher
pushOneFile
collectOutput
assertContains "$result" "1 file changed"
}
# -------------------------------------------------------------------- #
testPulledHook() {
pushOneFile
startWatcher
collectOutput
assertContains "$result" "Changes pulled"
}
# -------------------------------------------------------------------- #
testSpecialCharactersInCommitMessage() {
local message="!$%&/()=?{[]}#<>§|;:_,.-*+~'\"´\`"
pushOneFile "$test_file_name" "$message"
startWatcher
collectOutput
assertContains "$result" "$message"
}
# -------------------------------------------------------------------- #
testNothingChangedHook() {
startWatcher
collectOutput 0.2
assertContains "$result" "Nothing changed"
}
# -------------------------------------------------------------------- #
testStartupHook() {
startWatcher
collectOutput
assertContains "$result" "Watch started"
}
# -------------------------------------------------------------------- #
testBranchChangedHook() {
local branch="new_branch"
createNewBranch "$branch"
startWatcher
sleep 0.5
cd "$temp_dir/$watched_clone" || exit 1
git checkout -t "origin/$branch" 1>&3 2>&4
collectOutput
assertContains "$result" "Branch changed"
assertContains "$result" "$branch"
}
# -------------------------------------------------------------------- #
testNoUpstreamHook() {
startWatcher
sleep 0.5
cd "$temp_dir/$watched_clone" || exit 1
git checkout -b "new_branch" 1>&3 2>&4
collectOutput
assertContains "$result" "Upstream not set"
}
# -------------------------------------------------------------------- #
testRelativePath() {
pushOneFile
# Use a relative path for the git directory
cd "$temp_dir" || exit 1
startWatcher "-d $watched_clone"
collectOutput
assertNotContains "$result" "No such file or directory"
assertContains "$result" "Changes pulled"
}
# -------------------------------------------------------------------- #
printBashVersion
# shellcheck source=/tmp/git-repo-watcher/shunit2
. $shunit2_dir/shunit2
# -------------------------------------------------------------------- #