#!/usr/bin/env bash
# Run the functional tests in a clean virtualenv. This way the tests will fail
# if all required dependencies are not installed by the package.
set -euo pipefail

ENV_DIR=.functestenv

log_progress() {
    local cstart="\e[35m"
    local cend="\e[0m"
    echo -e "${cstart}run-functional-tests: $*${cend}"
}

build_wheel() {
    log_progress "Building wheel"
    # Remove existing .whl files to make sure there is only one .whl. This way we
    # can use a glob to refer to it.
    rm -rf dist/*.whl
    uv build --wheel
}

create_venv() {
    log_progress "Creating venv"
    rm -rf $ENV_DIR
    python -m venv $ENV_DIR

    # Activate it
    if [ -e $ENV_DIR/bin/activate ] ; then
        # Linux, macOS
        . $ENV_DIR/bin/activate
    else
        # Windows
        . $ENV_DIR/Scripts/activate
    fi
}

install_packages() {
    log_progress "Installing packages"

    # Get the path to the generated .whl. We don't know its exact name,
    # so use `ls` to find it. There should be only one file since we
    # remove all the existing .whl files in `build_wheel()`.
    local whl_path
    whl_path="$(ls dist/*.whl)"

    # Install ggshield and test dependencies
    pip install "$whl_path" pytest pytest-xdist pytest-voluptuous jsonschema pytest-rerunfailures
}

run_tests() {
    log_progress "Running tests"
    pytest --disable-pytest-warnings -n auto --reruns 2 --reruns-delay 5 tests/functional "$@"
}

build_wheel
create_venv
install_packages
run_tests "$@"
