#!/bin/bash

set -e

# defaults if not set
: "${SKIP_LINT:=0}"
: "${COV_REPORT:=xml}"
: "${TEST_ARGS:=-v}"

OUTPUT_DIR="/var/task/output"

# 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...'
    python3 -m yamllint -s docker-compose.yaml
    python3 -m flake8 vector_utils/ tests/
else
    echo 'Skipping linting...'
fi

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

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

python3 -m pytest $TEST_ARGS tests/ $COVERAGE_ARGS --junitxml=pyunit.xml

mkdir -p "$OUTPUT_DIR"
rm -rf "$OUTPUT_DIR/*"
mv pyunit.xml "$OUTPUT_DIR/pyunit.xml"
[ -f coverage.xml ] && mv coverage.xml "$OUTPUT_DIR/coverage.xml"
[ -d htmlcov ] && mv htmlcov "$OUTPUT_DIR/htmlcov"

echo 'Done!'
