#!/usr/bin/env bash
set -e

# Usage: run-tests [database]

database="$DB_NAME"
if [[ -n "$1" ]]; then
    database="$1"
fi

if [[ -n "$TEST_INCLUDE" ]]; then
    test_modules=$(manifestoo --select-include "$TEST_INCLUDE" --select-exclude "$TEST_EXCLUDE" list --separator=,)
else
    test_modules=$(manifestoo --select-addons-dir "$TEST_ADDONS_DIR" --select-exclude "$TEST_EXCLUDE" list --separator=,)
fi

source set-addons-path

log-entrypoint "Run test for these modules: $test_modules"
log-entrypoint "Test coverage includes: $TEST_ADDONS_DIR/*"
without_demo=${WITHOUT_DEMO:=True}
odoo_init_param=""
if [[ "$without_demo" = "True" ]]; then
    odoo_init_param="--without-demo=all"
fi
unbuffer coverage run --include "$TEST_ADDONS_DIR/*" --branch \
    /opt/odoo-venv/bin/odoo \
    -d "$database" \
    -i "$test_modules" \
    --workers 0 \
    --test-enable \
    --stop-after-init \
    --http-port=8068 \
    --gevent-port=8071 \
    $odoo_init_param
coverage xml
