-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests
executable file
·404 lines (353 loc) · 10.2 KB
/
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/env bash
#
# Copyright © 2014, David McIntosh <[email protected]>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided this permission
# notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
# To run these tests, shunit2 should be in a directory included in your
# executable PATH.
# shunit2 is available from:
# http://code.google.com/p/shunit2/
set -u
. ./et
typeset -a tempdirs
typeset old_dir
# If we are running an OS which doesn't include mktemp, use shunit2's internal
# replacement
mkdtemp()
{
local dir ind
if which mktemp > /dev/null; then
dir=$(mktemp -d)
else
dir=$(_shunit_mktempDir)
fi
echo "${dir}"
}
rmdir_later()
{
local ind
ind=0
[[ -v tempdirs ]] && ind=${#tempdirs[@]}
tempdirs[${ind}]=$1
unset ind
}
setUp()
{
old_dir=$(pwd)
}
test_is_id()
{
is_id a
assertFalse "Called with non-id 1" $?
is_id a12
assertFalse "Called with non-id 2" $?
is_id " 1"
assertFalse "Called with non-id 3" $?
is_id 3216v
assertFalse "Called with non-id 4" $?
is_id 1234567890123
assertFalse "Called with non-id 5" $?
is_id 123
assertTrue "Called with id 1" $?
is_id 000000123456
assertTrue "Called with id 2" $?
}
test_filename_or_id()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
touch "${dir}/post-000000123456.md"
touch "${dir}/post-000000123457-fiction.md"
cd "${dir}"
actual=$(filename_or_id "123456")
assertTrue "Called with present id 1" $?
assertEquals "post-000000123456.md" "${actual}"
actual=$(filename_or_id "123457")
assertTrue "Called with present id 2" $?
assertEquals "post-000000123457-fiction.md" "${actual}"
actual=$(filename_or_id "000000123456")
assertTrue "Called with present padded id 1" $?
assertEquals "post-000000123456.md" "${actual}"
actual=$(filename_or_id "000000123457")
assertTrue "Called with present padded id 2" $?
assertEquals "post-000000123457-fiction.md" "${actual}"
actual=$(filename_or_id "123459")
assertFalse "Called with absent id" $?
actual=$(filename_or_id "000000123459")
assertFalse "Called with absent padded id" $?
actual=$(filename_or_id "post-000000123456.md")
assertTrue "Called with present file 1" $?
assertEquals "post-000000123456.md" "${actual}"
actual=$(filename_or_id "post-000000123457-fiction.md")
assertTrue "Called with present file 2" $?
assertEquals "post-000000123457-fiction.md" "${actual}"
actual=$(filename_or_id "post-000000123459.md")
assertFalse "Called with absent file 1" $?
}
test_has_repo()
{
local dir repo_type
dir=$(mkdtemp)
rmdir_later "${dir}"
mkdir "${dir}/.hg"
cd ${dir}
repo_type=$(has_repo)
assertTrue "Called in directory with hg repository" $?
assertEquals "hg" "${repo_type}"
dir=$(mkdtemp)
rmdir_later "${dir}"
mkdir "${dir}/.git"
cd ${dir}
repo_type=$(has_repo)
assertTrue "Called in directory with git repository" $?
assertEquals "git" "${repo_type}"
dir=$(mkdtemp)
rmdir_later "${dir}"
cd ${dir}
repo_type=$(has_repo)
assertFalse "Called in directory with no repository" $?
}
test_has_uncommitted_changes()
{
files=1
hg()
{
(( files )) || return
if [[ "$1" == "status" ]]; then
echo "M foo"
fi
}
git()
{
(( files )) || return
if [[ "$1" == "status" ]] && [[ "$2" == "-s" ]]; then
echo " M foo"
fi
}
has_uncommitted_changes hg
assertTrue $?
has_uncommitted_changes git
assertTrue $?
files=0
has_uncommitted_changes hg
assertFalse $?
has_uncommitted_changes git
assertFalse $?
unset -f hg
unset -f git
}
test_get_conf()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cat > "${dir}/.et" << EOF
source = https://ello.co/wtf.json
EOF
actual=$(get_conf "${dir}/.et" 'foo')
assertFalse 'Called with invalid key' $?
actual=$(get_conf "${dir}/.et" 'source')
assertTrue 'Called with valid key' $?
assertEquals "https://ello.co/wtf.json" "${actual}"
}
test_write_conf()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cat > "${dir}/expected" << EOF
source = https://ello.co/wtf.json
EOF
write_conf "${dir}/actual" "source" "https://ello.co/wtf.json"
cmp "${dir}/expected" "${dir}/actual"
assertTrue "Expected and actual files should match" $?
}
test_get_filename()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
while read i
do
touch "${dir}/$i"
done << EOF
post-000001141677.md
post-000002587443.md
post-000002871989.md
post-000002840628.md
post-000002127388.md
EOF
cd "${dir}"
actual=$(get_filename '1141677')
assertTrue 'Called with valid id 1' $?
assertEquals "post-000001141677.md" "${actual}"
actual=$(get_filename '2840628')
assertTrue 'Called with valid id 2' $?
assertEquals "post-000002840628.md" "${actual}"
actual=$(get_filename '546541251')
assertFalse 'Called with invalid id' $?
}
test_in_array()
{
arr=(foo bar qux)
in_array bar "${arr[@]}"
assertTrue 'Called with element present' $?
in_array baz "${arr[@]}"
assertFalse 'Called with element not present' $?
}
test_file_assets()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cat > "${dir}/post-000000123456.md" << EOF
2014-09-26T15:18:28.027Z
Duis vitae tortor eu magna tincidunt semper sit amet nec odio. Aliquam tincidunt nibh purus, ac consequat erat maximus nec. Ut ultricies pulvinar nibh id ornare. Praesent leo massa, mattis eu ornare non, tincidunt eget est.
{
"url": "//d324imu86q1bqn.cloudfront.net/uploads/asset/attachment/90210/optimized.jpg",
"via": "direct",
"alt": "laserdiscs.jpg",
"asset_id": 90210
}
Cras pretium porta erat id accumsan. Mauris sodales lectus in tortor eleifend egestas. Cras molestie ac ipsum ut tristique. Vestibulum ac euismod eros. Fusce eget consectetur magna.
EOF
expected="90210|//d324imu86q1bqn.cloudfront.net/uploads/asset/attachment/90210/optimized.jpg|laserdiscs.jpg"
actual=$(file_assets "${dir}/post-000000123456.md")
assertEquals "${expected}" "${actual}"
}
test_retrieve_asset()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cd "${dir}"
function curl
{
assertEquals "$1" "-o"
assertEquals "$2" "assets/000000123456/000000090210-laserdisc.jpg"
assertEquals "$3" "http://example.com/assets/90210/optimized.jpg"
return 0
}
retrieve_asset 000000123456 90210 "http://example.com/assets/90210/optimized.jpg" "laserdisc.jpg"
unset -f curl
function curl
{
return 1
}
retrieve_asset 000000123456 90210 "http://example.com/assets/90210/optimized.jpg" "laserdisc.jpg" 2> /dev/null
assertEquals 1 $?
unset -f curl
touch "assets/000000123456/000000090210-laserdisc.jpg"
function curl
{
assertTrue "curl should not be called for existing file" 1
return 0
}
retrieve_asset 000000123456 90210 "http://example.com/assets/90210/optimized.jpg" "laserdisc.jpg"
unset -f curl
}
test_each_asset()
{
input="\
a|b|c
d|e|f"
actual=$(echo "${input}" | each_asset echo)
expected="\
a b c
d e f"
assertEquals "${expected}" "${actual}"
}
test_first_in_glob()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cd "${dir}"
patt="foo*.txt"
actual="$(first_in_glob ${patt})"
rc=$?
assertEquals "" "${actual}"
assertFalse "Called with non existent glob" ${rc}
touch "foo1.txt"
touch "foo2.txt"
touch "foo3.txt"
actual="$(first_in_glob ${patt})"
rc=$?
assertEquals "foo1.txt" "${actual}"
assertTrue "Called with existent glob" ${rc}
}
test_get_cookie_table()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cd "${dir}"
touch ./foo.sqlite
called=1
function sqlite3
{
local expected_filename="./foo.sqlite"
assertEquals "${expected_filename}" "$1"
local expected_query="SELECT name, value FROM moz_cookies WHERE baseDomain = 'ello.co'"
assertEquals "${expected_query}" "$2"
called=0
}
cookie_file_globs[firefork]="./foo.sqlite"
local -a output
local n=0
get_cookie_table "firefork" "ello.co"
assertTrue "Sqlite3 not called" ${called}
}
test_get_cookie_string()
{
dir=$(mkdtemp)
rmdir_later "${dir}"
cd "${dir}"
touch ./foo.sqlite
function sqlite3
{
local expected_filename="./foo.sqlite"
assertEquals "${expected_filename}" "$1"
local expected_query="SELECT name, value FROM moz_cookies WHERE baseDomain = 'ello.co'"
assertEquals "${expected_query}" "$2"
cat << EOF
__cfduid|f39f49de8b5b40dbe98ce503f40e592960ef558205ae83
D_SID|54.53.12.99:FnAOoEqArtfsUpuKio8xolMC/mRfcqnW4tmL8mc140i
remember_user_token|PIq8Y4V41QslOiO3fHjhcN5FX04eAGzkNNG6TWU4VF75sEPSMfVJPy5%3D--688bcff12a86e019c976ffcf29c313e695077538
EOF
}
expected="\
__cfduid=f39f49de8b5b40dbe98ce503f40e592960ef558205ae83;D_SID=54.53.12.99:FnAOoEqArtfsUpuKio8xolMC/mRfcqnW4tmL8mc140i;remember_user_token=PIq8Y4V41QslOiO3fHjhcN5FX04eAGzkNNG6TWU4VF75sEPSMfVJPy5%3D--688bcff12a86e019c976ffcf29c313e695077538"
cookie_file_globs[firefork]="./foo.sqlite"
actual=$(get_cookie_string "firefork" "ello.co")
assertEquals "${expected}" "${actual}"
}
tearDown()
{
cd "${old_dir}"
unset -f sqlite3
if [[ -v cookie_file_globs[firefork] ]]; then
unset cookie_file_globs[firefork]
fi
}
oneTimeTearDown()
{
if [[ -v verbose ]]; then
echo
echo "Removing directories:"
echo
opts=-rvf
else
opts=-rf
fi
if [[ -v tempdirs ]]; then
for dir in "${tempdirs[@]}"
do
rm ${opts} -- "${dir}"
done
fi
}
. $(which shunit2)