on master help sign in
Merge pull request #3343 from steshaw/portable-timeout

Simplify basic010 and reg039 with portable `timeout`

commitc8e4651040756a65819d3d92a34e0da1e4ec25f6 authorNiklas Larsson <metaniklas@gmail.com> dateSun 7 Aug 2016, 20:44 (10 years ago) parent120b53c276ba0a
3 files changed+25−67
test/basic010/run+5−36 test/reg039/run+3−31 test/scripts/timeout+17
test/basic010/run+5−36
@@ -1,41 +1,10 @@
11#!/usr/bin/env bash
22
3+set -eu
34
4−declare -a extraargs
5−for arg in "$@"
6−do
7− extraargs=("${extraargs[@]}" "'$arg'")
8−done
5+TIMEOUT=../scripts/timeout
96
10−if (which timeout&>/dev/null); then
11− timeout 10 ${IDRIS:-idris} $@ Main.idr --nocolour --check --warnreach -o basic010
12− timeout 5 ./basic010
13−else
14− # From http://unix.stackexchange.com/questions/43340/how-to-introduce-timeout-for-shell-scripting
15− # Here copied from reg039.
16− #
17− # Executes command with a timeout
18− # Params:
19− # $1 timeout in seconds
20− # $2 command
21− # Returns 1 if timed out 0 otherwise
22− timeout() {
7+$TIMEOUT 10 "${IDRIS:-idris}" "$@" Main.idr --nocolour --check --warnreach -o basic010
8+$TIMEOUT 5 ./basic010
239
24− time=$1
25−
26− # start the command in a subshell to avoid problem with pipes
27− # (spawn accepts one command)
28− command="/bin/sh -c \"$2\""
29−
30− expect -c "set echo \"-noecho\"; set timeout $time; spawn -noecho $command; expect timeout { exit 1 } eof { exit 0 }" | perl -pe 's/\r$//'
31−
32− if [ $? = 1 ] ; then
33− echo "Timeout after ${time} seconds"
34− fi
35−
36− }
37−
38− timeout 10 "${IDRIS:-idris} ${extraargs[*]} Main.idr --nocolour --check --warnreach -o basic010"
39− timeout 5 "./basic010"
40−fi
41−rm -f *.ibc basic010
10+rm -f -- *.ibc basic010
test/reg039/run+3−31
@@ -1,35 +1,7 @@
11#!/usr/bin/env bash
22
3+set -eu
34
4−declare -a extraargs
5−for arg in "$@"
6−do
7− extraargs=("${extraargs[@]}" "'$arg'")
8−done
9−if (which timeout&>/dev/null); then
10− timeout 60 ${IDRIS:-idris} $@ --nocolour reg039.idr --exec go
11−else
12− # From http://unix.stackexchange.com/questions/43340/how-to-introduce-timeout-for-shell-scripting
13− # Executes command with a timeout
14− # Params:
15− # $1 timeout in seconds
16− # $2 command
17− # Returns 1 if timed out 0 otherwise
18− timeout() {
5+TIMEOUT=../scripts/timeout
196
20− time=$1
21−
22− # start the command in a subshell to avoid problem with pipes
23− # (spawn accepts one command)
24− command="/bin/sh -c \"$2\""
25−
26− expect -c "set echo \"-noecho\"; set timeout $time; spawn -noecho $command; expect timeout { exit 1 } eof { exit 0 }" | perl -pe 's/\r$//'
27−
28− if [ $? = 1 ] ; then
29− echo "Timeout after ${time} seconds"
30− fi
31−
32− }
33− timeout 60 "${IDRIS:-idris} ${extraargs[*]} --nocolour reg039.idr --exec go"
34− fi
35− rm -f reg039 *.ibc
7+$TIMEOUT 60 "${IDRIS:-idris}" "$@" --nocolour reg039.idr --exec go
test/scripts/timeoutnew file+17
@@ -0,0 +1,17 @@
1+#!/usr/bin/env perl
2+
3+use strict;
4+use File::Basename;
5+
6+sub Usage {
7+ my $name = basename($0);
8+ print STDERR "usage: $name seconds shell-command...\n";
9+ exit 2;
10+}
11+
12+Usage if (scalar @ARGV) < 2;
13+
14+my $timeout = shift;
15+
16+alarm $timeout;
17+exec @ARGV;