#!/bin/bash

set -e

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

# convert to all lowercase
COV_REPORT=$(echo $COV_REPORT | tr '[:upper:]' '[:lower:]')

# run linting of python and yaml files
if [ "$SKIP_LINT" -ne 1 ]; then
    echo 'Running linting...'
    yamllint -s docker-compose.yml
    flake8 territories/ tests/
else
    echo 'Skipping linting...'
fi

# set flags for pytest-cov
COVERAGE_ARGS=""
if [ "$COV_REPORT" != "off" ]; then
    COVERAGE_ARGS="--cov-report $COV_REPORT --cov=territories"
fi

# run pytest with args set
echo 'Running tests...'
PYTHONDONTWRITEBYTECODE=1 python -m pytest $TEST_ARGS tests/ \
    -p no:cacheprovider \
    $COVERAGE_ARGS  \
    --cov-report xml:coverage.xml \
    --junitxml=pyunit.xml

echo 'Done!'
