#!/bin/sh

# Copyright (C) Dmitry Volyntsev
# Copyright (C) NGINX, Inc.

set -e

. test/options
. test/setup

export NJS_TEST_DIR

for njs_test in $NJS_TESTS; do
    . test/prepare

    njs_log="$NJS_TEST_DIR/${njs_test%.*}.log"

    verbose "$njs_test $njs_log"
    cat << END >> $NJS_TEST_LOG
----------------------------------------
running $njs_test $njs_log
END

    if [ "$NJS_SKIP_LIST" != "${NJS_SKIP_LIST#*$njs_test*}" ]; then
        skip $njs_test
        continue
    fi

    status=0

    NJS_PATH=$njs_paths \
    NJS_EXIT_CODE=$NJS_TEST_EXIT_CODE \
    $NJS_TEST_BINARY $NJS_TEST_DIR/$njs_test > $njs_log 2>&1 || status=$?

    cat $njs_log >> $NJS_TEST_LOG
    njs_out=`cat $njs_log`

    if [ "$status" -eq 0 ]; then
        if [ -n "$njs_negative" ]; then
            failed $njs_test $njs_log

        elif [ $njs_async = yes ]; then
            if [ "$njs_out" != 'Test262:AsyncTestComplete' ]; then
                failed $njs_test $njs_log

            else
                passed $njs_test
            fi

        else
            if [ -n "$njs_out" ]; then
                failed $njs_test $njs_log

            else
                passed $njs_test
            fi
        fi

    else
        if [ -n "$njs_negative" ]; then
            if [ "$status" = "$NJS_TEST_EXIT_CODE" ]; then
                passed $njs_test
            else
                echo "negative test exited with unexpected exit code:$status"
                failed $njs_test $njs_log
            fi

        else
            failed $njs_test $njs_log
        fi
    fi

done

. test/finalize
. test/report
