#!/bin/bash

[ -d .venv ] || ./build.sh venv

.venv/bin/python -sc 'import pytest' &>/dev/null || .venv/bin/pip install -I \
    -i https://pypi.yandex-team.ru/simple \
    py pytest==4.6.11 pytest-sugar backports.functools-lru-cache==1.6.4 \
    pytest-xdist==1.34.0 pytest-flakes==4.0.1 pytest-cov==2.12.1 pytest-pep8 \
    pytest-instafail pyjack funcsigs scandir pathlib2 more_itertools==5.0.0 \
    coverage==5.5 packaging==20.9 attrs==20.3.0 importlib-metadata==2.1.1 \
    zipp==1.2.0 contextlib2==0.6.0.post1 configparser==4.0.2

PYTHONPATH=$(pwd)/snapshot/lib:$PYTHONPATH
export PYTHONPATH
export TERMINFO=/lib/terminfo

pytest_args=(
    --instafail
    -s
)

cp tools/pytest.ini .

path_spec=0

while test -n "$1"; do
    case "$1" in
        --nocov)
            NOCOV=1
            shift
            ;;

        --nobox*)
            NOBOXED=1
            shift
            ;;

        --nofork*)
            NOFORK=1
            shift
            ;;

        style|bigbang|code)
            TESTTYPE=$1
            shift
            ;;

        benchmark)
            TESTTYPE=$1
            NOCOV=1
            NOFORK=1
            NOBOXED=1
            shift
            ;;

        *)
            if [ -d "$1" ] || [ -f "$1" ]; then
                path_spec=1
            fi

            pytest_args=( "${pytest_args[@]}" "$1" )
            shift
            ;;
    esac
done

if [ -z "$NOCOV" ]; then
   # base_path=snapshot/lib/skycore
    #for path in "$base_path"/*; do
    #    pytest_args=( "${pytest_args[@]}" --cov=$base_path/$path )
    #done

    pytest_args=(
        "${pytest_args[@]}"
        --cov-report=html
        --cov-config=tools/coverage.conf
        --no-cov-on-fail
        --cov=src
    )
fi

if [ -z "$NOBOXED" ]; then
    boxed_arg="--boxed"
fi

if [ -z "$NOFORK" ]; then
    cpu_count=$(.venv/bin/python -c 'import multiprocessing; print(multiprocessing.cpu_count())')
else
    cpu_count=0
fi

case "$TESTTYPE" in
    style)
        pytest_args=( -m 'pep8 or flakes' "${pytest_args[@]}" )
        shift 1
        ;;
    bigbang)
        pytest_args=( -m 'not pep8 and not flakes' -n $cpu_count $boxed_arg -k 'bigbang and not benchmark' "${pytest_args[@]}" )
        shift 1
        ;;
    code)
        pytest_args=( -m 'not pep8 and not flakes' -n $cpu_count $boxed_arg -k 'not (bigbang or benchmark)' "${pytest_args[@]}" )
        shift 1
        ;;
    benchmark)
        pytest_args=( -m 'not pep8 and not flakes' -n $cpu_count $boxed_arg -k 'benchmark' "${pytest_args[@]}" )
        shift 1
        ;;
    *)
        pytest_args=( -m 'not pep8 and not flakes' -n $cpu_count $boxed_arg -k 'not benchmark' "${pytest_args[@]}" )
        ;;
esac

if [ $path_spec -eq 0 ]; then
    if [ "$TESTTYPE" = style ]; then
        pytest_args=( "${pytest_args[@]}" src tests )
    else
        pytest_args=( "${pytest_args[@]}" tests )
    fi
fi

echo .venv/bin/python -sttm pytest "${pytest_args[@]}"
.venv/bin/python -sttm pytest "${pytest_args[@]}"
rm pytest.ini
