#!/bin/bash

set -e

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

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

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

# run pytest with args set
if [ "$SKIP_TESTS" -ne 1 ]; then
    echo 'Running tests...'
    PYTHONDONTWRITEBYTECODE=1 COVERAGE_FILE=.coverage pytest tests/unit/ \
      $TEST_ARGS \
      -p no:cacheprovider \
      --cov delivery_metadata \
      --cov-report xml:coverage.xml \
      --cov-report html:coverage \
      --cov-report term \
      --junitxml=pyunit.xml
else
    echo 'Skipping tests...'
fi
