#!/bin/sh
set -e

# notice the / at end in order to load this module
PACKAGE='isexe'
SEDCMD="s,(require|resolve)\s*[(]\s*'[.][.]/,\\1('$PACKAGE,g"
SEDERROR="Code[[:space:]]*coverage[ ]*is[ ]*disabled"

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP

cp -r test $tmpdir/test
find test/ -maxdepth 1 -name '*.js' -print0 | \
    xargs -0 -n1  \
	  sh -c 'set -e; echo "TESTING: $3"; sed -E "$1" < "$3" > "$2/$3"' sedtape "$SEDCMD" "$tmpdir"
cd $tmpdir

TAPCMD=""

TAPCMD() {
    set -e;
    tap -R spec test/*.js --branches=100 --statements=100 --functions=100 --lines=100
}

SEDERROR () {
    set -e
    sed -e "/$SEDERROR/w /dev/fd/2" -e "/$SEDERROR/d"
}

TAPCMD 2> error.log
exec 3>&1 ;SEDERROR < error.log 2>&3 >&2 3>&-
