#!/bin/bash

set -eu

# defaults if not set
: "${SKIP_LINT:=0}"
: "${SKIP_TYPE_CHECKS:=0}"

# run linting of python files
if [ "$SKIP_LINT" -ne 1 ]; then
    yamllint -s docker-compose.yml docker-compose.unit-lint.yml
    echo 'Running linting...'
    poetry run ruff check assets tests application.py dev.py
    echo 'Running ruff formatter check...'
    poetry run ruff format assets tests application.py dev.py --diff --no-cache
else
    echo 'Skipping linting...'
fi

# type checking
if [ "$SKIP_TYPE_CHECKS" -ne 1 ]; then
    echo 'Running mypy...'
    poetry run mypy --cache-dir=/dev/null assets tests
else
    echo 'Skipping type checks...'
fi

# run pytest with args set
echo 'Running tests...'
PYTHONDONTWRITEBYTECODE=1 COVERAGE_FILE=.coverage \
    pytest tests/ $TEST_ARGS \
    -p no:cacheprovider \
    --ignore=tests/integration/ \
    --ignore=tests/load/ \
    --ignore=tests/testutils/ \
    --cov assets \
    --cov-report xml:coverage.xml \
    --cov-report term \
    --cov-report html:coverage \
    --junitxml=pyunit.xml

echo 'Done!'
