-
Notifications
You must be signed in to change notification settings - Fork 107
/
system_test.sh
executable file
·573 lines (505 loc) · 16.1 KB
/
system_test.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/bin/sh
trap '' ERR 2> /dev/null || exec bash $0 "$@"
# test runner
function try { let tests+=1; this="$1"; }
function assert {
[[ "$1" == "$2" ]] && { printf "."; return; }
printf "\nFAIL: $this\n'$1' != '$2'\n"
echo "*************************************"
echo "System test directory contents:"
: | head -n 50 $(find $tmp -type f ! -perm -111)
echo "*************************************"
exit 1
}
function skip { printf "s"; }
function zz { sleep 0.25; }
function setup {
rm -f $tmp/*
touch $tmp/file1 $tmp/file2
unset ENTR_STATUS_SCRIPT
}
tmp=$(cd $(mktemp -d ${TMPDIR:-/tmp}/entr-system-test-XXXXXX); pwd -P)
tsession=$(basename $tmp)
clear_tty='test -t 0 && stty echo icanon'
clear_tmux='tmux kill-session -t $tsession 2>/dev/null || true'
clear_tmp='rm -rf $tmp'
trap "$clear_tty; $clear_tmux; $clear_tmp" EXIT
trap 'printf "\nTerminated by SIGINT at line $LINENO\n"; exit 1' INT
# required utilities
utils="file pgrep git vim tmux"
for util in $utils; do
p=$(command -pv $util) || {
echo "ERROR: could not locate the '$util' utility" >&2
echo "System tests depend on the following: $utils" >&2
exit 1
}
done
if [ $(uname) == 'Linux' ]; then
date | awk -S '{}' 2> /dev/null || {
echo "ERROR: gawk required on Linux"
exit 1
}
fi
# local binary, non-interactive by default
alias entr='./entr -n'
alias entr_tty='./entr'
# alias expansion is not enabled by default in non-interactive Bourne Again Shell (bash) sessions
command -v shopt > /dev/null && shopt -s expand_aliases
# fast tests
try "no arguments"
entr 2> /dev/null || code=$?
assert $code 1
try "no input"
entr echo "vroom" 2> /dev/null || code=$?
assert $code 1
try "reload and clear options with no utility to run"
entr -r -c 2> /dev/null || code=$?
assert $code 1
try "empty input"
echo "" | entr echo 2> /dev/null || code=$?
assert $code 1
try "no regular files provided as input"
mkdir $tmp/dir1
ls $tmp | entr echo 2> /dev/null || code=$?
rmdir $tmp/dir1
assert $code 1
# status message tests
try "install default status script"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
ls $tmp/* | entr -zx true >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
wait $bgpid
sed -i -e "s,'.*.awk',$1 /.../status.awk," $tmp/exec.out
assert "$(cat $tmp/exec.err)" ""
assert "$(cat $tmp/exec.out)" "$(printf 'entr: created /.../status.awk\ntrue returned exit code 0\n')"
try "status script not compatible with restart option"
setup
ls $tmp/* | entr -zrx true >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
wait $bgpid; assert "$?" "1"
try "block unsafe status script"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
cat > $ENTR_STATUS_SCRIPT <<-EOF
{ system("date") }
EOF
ls $tmp/* | entr -zx true >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
sed -i -e "s,.*awk: .*,awk: system is unsafe ... status.awk," $tmp/exec.err
assert "$(head -n1 $tmp/exec.err)" "awk: system is unsafe ... status.awk"
assert "$(head -n1 $tmp/exec.out)" ""
try "allow unsafe status script"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
cat > $ENTR_STATUS_SCRIPT <<-EOF
{ system("date") }
EOF
ls $tmp/* | entr -zxx true >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
assert "$(head -n1 $tmp/exec.err)" ""
try "use custom status script"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
cat > $ENTR_STATUS_SCRIPT <<-'EOF'
{
print "=", $1, $2, "="
}
EOF
ls $tmp/* | entr -zx false >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
assert "$(cat $tmp/exec.err)" ""
assert "$(cat $tmp/exec.out)" "$(printf '= exit 1 =')"
try "use custom status script with shell option and kill"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
cat > $ENTR_STATUS_SCRIPT <<-'EOF'
{
print "=", $1, $2, "="
}
EOF
ls $tmp/* | entr -zx -s 'kill -9 $$' >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
assert "$(cat $tmp/exec.err)" ""
assert "$(cat $tmp/exec.out)" "$(printf '= signal 9 =')"
try "abort if status script terminates"
setup
export ENTR_STATUS_SCRIPT="$tmp/status.awk"
cat > $ENTR_STATUS_SCRIPT <<-'EOF'
{ exit; }
EOF
ls $tmp/* | entr -x true >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
assert "$(cat $tmp/exec.err)" "entr: status process terminated"
assert "$(cat $tmp/exec.out)" ""
# terminal tests
unset TMUX
try "spacebar triggers utility"
setup
env SHELL=/bin/sh tmux new-session -s $tsession -d
echo "waiting" > $tmp/file1
echo "finished" > $tmp/file2
tmux send-keys -t $tsession:0 \
"ls $tmp/file2 | ./entr -p cp $tmp/file2 $tmp/file1" C-m ; zz
assert "$(cat $tmp/file1)" "waiting"
tmux send-keys -t $tsession:0 "xyz" C-m ; zz
assert "$(cat $tmp/file1)" "waiting"
tmux send-keys -t $tsession:0 " " ; zz
assert "$(cat $tmp/file1)" "finished"
tmux send-keys -t $tsession:0 "q" ; zz
tmux kill-session -t $tsession
# file system tests
try "exec a command using one-shot option"
setup
ls $tmp/file2 | entr -zp cat $tmp/file2 >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
echo 456 >> $tmp/file2 ; zz
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.err)" ""
assert "$(head -n1 $tmp/exec.out)" "$(printf '456\n')"
try "exec a command using one-shot option and return signal number"
setup
ls $tmp/file2 | entr -z sh -c 'kill -9 $$' >$tmp/exec.out 2>$tmp/exec.err
assert "$?" "137"
assert "$(cat $tmp/exec.err)" ""
assert "$(cat $tmp/exec.out)" ""
try "exec a command using one-shot and shell options and return signal"
setup
ls $tmp/file2 | entr -z -s 'kill -9 $$' >$tmp/exec.out 2>$tmp/exec.err
assert "$?" "137"
assert "$(tail -c23 $tmp/exec.out)" ""
try "fail to exec a command using one-shot option"
setup
ls $tmp/file* | entr -z /usr/bin/false_X 2>$tmp/exec.err
assert "$?" "1"
try "exec a command using one-shot option exit code from child"
setup
ls $tmp/file* | entr -z sh -c 'exit 4' &
bgpid=$! ; zz
wait $bgpid; assert "$?" "4"
try "restart a server when a file is modified using one-shot option"
setup
if [ $(uname) == 'Linux' ]; then
skip "GNU nc spins while retrying SELECT(2); busybox does not support domain sockets"
else
ls $tmp/file2 | entr -rz nc -l -U $tmp/nc.s >> $tmp/exec.out &
bgpid=$! ; zz
echo "123" | nc -NU $tmp/nc.s 2> /dev/null || {
echo "123" | nc -U $tmp/nc.s
} ; zz
echo 456 >> $tmp/file2 ; zz
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "123"
fi
try "exec a command in non-intertive mode"
setup
ls $tmp/file* | entr tty >$tmp/exec.out &
bgpid=$! ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "not a tty"
try "exec a command as a background task and ensure stdin is closed"
setup
ls $tmp/file* | entr -r sh -c 'test -t 0; echo $?; kill $$' >$tmp/exec.out &
bgpid=$! ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "1"
try "exec a command as a background task, and verify that read from stdin doesn't complain"
setup
ls $tmp/file* | entr -r sh -c 'read X' 2>$tmp/exec.err &
bgpid=$! ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.err)" ""
try "exec single shell utility and exit when a file is added to an implicit watch path"
setup
ls $tmp/file* | entr -dp sh -c 'echo ping' >$tmp/exec.out 2>$tmp/exec.err \
|| true &
bgpid=$! ; zz
touch $tmp/newfile
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "ping"
assert "$(cat $tmp/exec.err)" "entr: directory altered"
try "exec single shell utility and exit when a subdirectory is added"
setup
ls -d $tmp | entr -dp sh -c 'echo ping' >$tmp/exec.out 2>$tmp/exec.err \
|| true &
bgpid=$! ; zz
mkdir $tmp/newdir
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "ping"
assert "$(cat $tmp/exec.err)" "entr: directory altered"
rmdir $tmp/newdir
try "exec single shell utility and exit when a hidden subdirectory is added"
setup
ls -d $tmp | entr -ddp sh -c 'echo ping' >$tmp/exec.out 2>$tmp/exec.err \
|| true &
bgpid=$! ; zz
mkdir $tmp/.newdir
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "ping"
assert "$(cat $tmp/exec.err)" "entr: directory altered"
rmdir $tmp/.newdir
try "exec single shell utility and exit when a file is added to a specific path"
setup
ls -d $tmp | entr -dp sh -c 'echo ping' >$tmp/exec.out 2>$tmp/exec.err \
|| true &
bgpid=$! ; zz
touch $tmp/newfile
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "ping"
assert "$(cat $tmp/exec.err)" "entr: directory altered"
try "do nothing when a file not monitored is changed in directory watch mode"
setup
ls $tmp/file2 | entr -dp echo "changed" >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
echo "123" > $tmp/file1
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" ""
assert "$(cat $tmp/exec.err)" ""
try "exec utility when a file is written by Vim in directory watch mode"
setup
ls $tmp/file* | entr -dp echo "changed" >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
vim -e -s -u NONE -N \
-c ":r!date" \
-c ":wq" $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "changed"
assert "$(cat $tmp/exec.err)" ""
try "exec utility when a file is opened for write and then closed"
setup
echo "---" > $tmp/file1
ls $tmp/file* | entr -p echo "changed" > $tmp/exec.out &
bgpid=$! ; zz
: > $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
if [ $(uname | grep -E 'Darwin|FreeBSD|DragonFly') ]; then
skip "NOTE_TRUNCATE not supported"
else
assert "$(cat $tmp/exec.out)" "changed"
fi
try "exec single utility when an entire stash of files is reverted"
if [ ! -d /usr/include ]; then
skip "Operating system does not include files in a standard location"
else
setup
cp /usr/include/*.h $tmp/
cd $tmp
git init -q
git config --local user.email [email protected]
git add *.h
git commit -m "initial checkin" -q
for f in `ls *.h | head`; do
chmod 644 $f
echo "" >> $f
done
cd - > /dev/null ; zz
ls $tmp/*.h | entr -p echo "changed" > $tmp/exec.out &
bgpid=$! ; zz
cd $tmp
git checkout *.h -q
cd - > /dev/null ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
rm -rf $tmp/.git
assert "$(cat $tmp/exec.out)" "changed"
fi
try "exec utility when a file is written by Vim"
setup
ls $tmp/file* | entr -p echo "changed" > $tmp/exec.out &
bgpid=$! ; zz
vim -e -s -u NONE -N \
-c ":r!date" \
-c ":wq" $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "changed"
try "exec shell utility when a file is written by Vim with 'backup'"
setup
ls $tmp/file* | entr -p echo "changed" > $tmp/exec.out &
bgpid=$! ; zz
vim -e -s -u NONE -N \
-c ":set backup" \
-c ":r!date" \
-c ":wq" $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "changed"
try "exec shell utility when a file is written by Vim with 'nowritebackup'"
setup
ls $tmp/file* | entr -p echo "changed" > $tmp/exec.out &
bgpid=$! ; zz
vim -e -s -u NONE -N \
-c ":set nowritebackup" \
-c ":r!date" \
-c ":wq" $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "changed"
try "restart a server when a file is modified"
setup
echo "started." > $tmp/file1
ls $tmp/file2 | entr -r tail -f $tmp/file1 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
assert "$(cat $tmp/exec.out)" "started."
echo 456 >> $tmp/file2 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "$(printf 'started.\nstarted.')"
try "ensure that all shell subprocesses are terminated in restart mode"
setup
cat <<-SCRIPT > $tmp/go.sh
#!/bin/sh
trap 'echo "caught signal"; exit' TERM
echo "running"; sleep 10
SCRIPT
chmod +x $tmp/go.sh
ls $tmp/file2 | entr -r sh -c "$tmp/go.sh" 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
kill -INT $bgpid ; zz
assert "$(cat $tmp/exec.out)" "$(printf 'running\ncaught signal')"
try "ensure that all shell subprocesses are terminated when terminal is closed"
setup
cat <<-SCRIPT > $tmp/go.sh
#!/bin/sh
trap 'echo "caught signal"; exit' TERM
echo "running"; sleep 10
SCRIPT
chmod +x $tmp/go.sh
ls $tmp/file2 | entr -r sh -c "$tmp/go.sh" 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
kill -HUP $bgpid ; zz
assert "$(cat $tmp/exec.out)" "$(printf 'running\ncaught signal')"
try "exit with no action when restart and dirwatch flags are combined"
setup
echo "started." > $tmp/file1
ls $tmp/file* | entr -rd tail -f $tmp/file1 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
assert "$(cat $tmp/exec.out)" "started."
touch $tmp/newfile
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "$(printf 'started.')"
try "exec single shell utility when two files change simultaneously"
setup
ln $tmp/file1 $tmp/file3
ls $tmp/file* | entr -p sh -c 'echo ping' > $tmp/exec.out &
bgpid=$! ; zz
echo 456 >> $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "ping"
try "exec single shell utility on startup and when a file is changed"
setup
ls $tmp/file* | entr sh -c 'printf ping' > $tmp/exec.out &
bgpid=$! ; zz
echo 456 >> $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "pingping"
try "exec a command if a file is made executable"
setup
ls $tmp/file* | entr -p echo /_ > $tmp/exec.out &
bgpid=$! ; zz
chmod +x $tmp/file2 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "$tmp/file2"
try "ensure watches operate on a running executable"
setup
cp /bin/sleep $tmp/
ls $tmp/sleep | entr -rs "echo 'vroom'; $tmp/sleep 30" \
> $tmp/exec.out 2> /dev/null &
bgpid=$! ; zz
cp -f /bin/sleep $tmp/ ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
rm -f $tmp/sleep
assert "$(cat $tmp/exec.out)" "$(printf 'vroom\nvroom\n')"
try "exec a command using the first file to change"
setup
ls $tmp/file* | entr -p cat /_ > $tmp/exec.out &
bgpid=$! ; zz
echo 456 > $tmp/file1 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "456"
try "exec single shell utility using utility substitution"
setup
ls $tmp/file1 $tmp/file2 | entr -p file /_ > $tmp/exec.out &
bgpid=$! ; zz
echo 456 >> $tmp/file2; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "$tmp/file2: ASCII text"
try "watch and exec a program that is overwritten"
setup
touch $tmp/script; chmod 755 $tmp/script
echo $tmp/script | entr -p $tmp/script $tmp/file1 > $tmp/exec.out &
bgpid=$! ; zz
cat > $tmp/script <<-EOF
#!/bin/sh
echo vroom
EOF
zz ; kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "vroom"
try "exec an interactive utility when a file changes"
setup
if ! test -t 0 ; then
skip "A TTY is not available"
else
ls $tmp/file* | entr_tty -p sh -c 'tty | cut -c1-9' 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
echo 456 >> $tmp/file2 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out | tr '/pts' '/tty')" "/dev/tty"
fi
try "exec a command using shell option"
setup
ls $tmp/file* | entr -ps 'file $0; exit 2' >$tmp/exec.out 2>$tmp/exec.err &
bgpid=$! ; zz
echo 456 >> $tmp/file2 ; zz
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.err)" ""
assert "$(head -n1 $tmp/exec.out)" "$(printf ${tmp}'/file2: ASCII text')"
try "exec a command as a background task"
setup
(ls $tmp/file* | entr -ps 'echo terminating; kill $$' >$tmp/exec.out 2>$tmp/exec.err &)
zz
echo 456 >> $tmp/file2 ; zz
assert "$(cat $tmp/exec.err)" ""
assert "$(head -n1 $tmp/exec.out)" "terminating"
# extra slow tests that rely on timeouts
try "respond to events that occur while the utility is running"
setup
ls $tmp/file* | entr -a sh -c 'echo "vroom"; sleep 0.5' > $tmp/exec.out &
bgpid=$! ; zz
echo "123" > $tmp/file1
sleep 1
kill -INT $bgpid
wait $bgpid; assert "$?" "0"
assert "$(cat $tmp/exec.out)" "$(printf 'vroom\nvroom\n')"
try "ensure that all subprocesses are terminated in restart mode when a file is removed"
setup
cat <<-SCRIPT > $tmp/go.sh
#!/bin/sh
trap 'echo "caught signal"; exit' TERM
echo "running"; sleep 10
SCRIPT
chmod +x $tmp/go.sh
ls $tmp/file2 | entr -r sh -c "$tmp/go.sh" 2> /dev/null > $tmp/exec.out &
bgpid=$! ; zz
rm $tmp/file2; sleep 2
pgrep -P $bgpid > /dev/null || assert "$?" "1"
assert "$(cat $tmp/exec.out)" "$(printf 'running\ncaught signal')"
this="exit 0"
echo
echo "$tests tests PASSED"