#!/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...'
    yamllint -s -d "{extends: relaxed, rules: {line-length: {max: 100}}}"  docker-compose.yml
    flake8 fargate_tools/ tests/
else
    echo 'Skipping linting...'
fi

if [ "$COV_REPORT" != "off" ]; then
    COVERAGE_ARGS="--cov-report $COV_REPORT --cov-report xml:build/coverage.xml --cov=fargate_tools"
fi

echo 'Running tests...'
python3.11 -m pytest $TEST_ARGS tests/ $COVERAGE_ARGS --junitxml=build/pyunit.xml

if [ "$COV_REPORT" = "html" ]; then
    echo "Serving out coverage report on port $COV_PORT..."
    python3.11 -m http.server --directory htmlcov $COV_PORT
fi

echo 'Done!'
