#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

# source cli lib
source /usr/local/clilib

status "Preparing XDG_RUNTIME_DIR"
export XDG_RUNTIME_DIR=/tmp/xdg-runtime-dir
mkdir -p $XDG_RUNTIME_DIR

# setup exit trap
function finish {
	status "Changing ownership of /app directory"
	perms=$(stat -c '%u:%g' /app)
	wraperr chown -R $perms /app
}
trap finish EXIT

status "Loading venv"
source /venv/bin/activate
echo $VIRTUAL_ENV

status "Changing directory to /app"
wraperr cd /app

status "Installing pip depenencies"
wraperr pip install -r requirements.txt

status "Installing dev dependencies"
wraperr pip install -r requirements.dev.txt

status "Preparing data trees for tests..."
wraperr bash -c "cd tests/_support_data; ./gen-data.sh"

status "Running tests itself"
wraperr python3 -m pytest .\
	-v \
	-n auto \
	--ignore=tests/_support_data \
	--color=yes \
	--cov . \
	--cov-config .coveragerc \
	--cov-report term-missing \
	--cov-report html
exit 0