#!/bin/bash

set -e

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

# 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...'
  flake8 deliveryhistory/ 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=deliveryhistory"
fi

# run pytest with args set
echo 'Running tests...'

PYTHONDONTWRITEBYTECODE=1 COVERAGE_FILE=build/.coverage python -m pytest -v tests/ \
    --ignore=tests/integration/ \
    -p no:cacheprovider \
    --cov deliveryhistory \
    --cov-report xml:build/coverage.xml \
    --cov-report term \
    --junitxml=build/pyunit.xml
