Skip to content

Commit

Permalink
timeout(): Replace with a simple implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Oct 28, 2020
1 parent cdf6463 commit b24fede
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
16 changes: 7 additions & 9 deletions lib/libexec.sh
Expand Up @@ -27,7 +27,7 @@ load() {
unixtime() {
IFS=" $IFS"
#shellcheck disable=SC2046
set -- $(date -u +'%Y %m %d %H %M %S') "$1"
set -- $("$SHELLSPEC_DATE" -u +'%Y %m %d %H %M %S') "$1"
IFS=${IFS# }
set -- "$1" "${2#0}" "${3#0}" "${4#0}" "${5#0}" "${6#0}" "$7"
[ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" "$7"
Expand Down Expand Up @@ -140,14 +140,12 @@ if kill -s 0 $$ 2>/dev/null; then
fi

timeout() {
{
( shift; "$@" ) &
set -- "$1" "$!"
( sleep "$1"; signal KILL "$2" ) &
wait "$2" ||:
signal KILL $! ||:
wait $! ||:
} 2>/dev/null &&:
set -- "$(t=0; unixtime t; echo "$t")" "$@"
while [ "$(t=0; unixtime t; echo "$t")" -le "$(($1 + $2))" ]; do
( shift 2; "$@" ) && return 0
nap
done
return 0
}

read_quickfile() {
Expand Down
2 changes: 1 addition & 1 deletion libexec/shellspec-reporter.sh
Expand Up @@ -141,7 +141,7 @@ reporter_callback() {

tssv_parse "field" reporter_callback ||:

timeout 1 sleep_wait_until read_time_log "time" "$SHELLSPEC_TIME_LOG" ||:
timeout 1 read_time_log "time" "$SHELLSPEC_TIME_LOG" ||:
read_time_log "time" "$SHELLSPEC_TIME_LOG" ||:

output_formatters end
Expand Down
2 changes: 2 additions & 0 deletions shellspec
Expand Up @@ -130,6 +130,7 @@ export SHELLSPEC_XTRACEFD_VAR=''
export SHELLSPEC_CLONE_TYPE=''
export SHELLSPEC_MV="mv"
export SHELLSPEC_CHMOD="chmod"
export SHELLSPEC_DATE="date"
export SHELLSPEC_RM="rm"
export SHELLSPEC_DOCKER_IMAGE=""

Expand Down Expand Up @@ -623,6 +624,7 @@ fi
command_path SHELLSPEC_ENV "env" ||:
command_path SHELLSPEC_MV "mv" ||:
command_path SHELLSPEC_CHMOD "chmod" ||:
command_path SHELLSPEC_DATE "date" ||:
command_path SHELLSPEC_RM "rm" ||:
command_path SHELLSPEC_SLEEP "sleep" ||:

Expand Down
4 changes: 3 additions & 1 deletion spec/libexec_spec.sh
Expand Up @@ -8,13 +8,15 @@ Describe 'libexec.sh'
Include "$SHELLSPEC_LIB/libexec.sh"

Describe 'unixtime()'
Before "SHELLSPEC_DATE=fake_date"

Parameters
"2019 08 18 08 17 44" 1566116264
"2020 01 01 01 23 45" 1577841825
End

_unixtime() {
eval "date() { echo \"$2\"; }"
eval "fake_date() { echo \"$2\"; }"
unixtime "$1"
}

Expand Down

0 comments on commit b24fede

Please sign in to comment.