#!/bin/bash
#
# Runs tests in a sandboxed environment that loosely replicates the GitHub Actions
# environment.
set -euo pipefail

# Conda functions are not exported into a new bash environment by default, so we
# need to source the conda profile script from the current conda installation.
# This script expects PS1 to be defined, so we set it to an empty value.
export PS1=
conda_dir=$(conda info --base)
source "${conda_dir}/etc/profile.d/conda.sh"

ENV_NAME="test-augur"

if [[ -e "`which mamba`" ]]
then
    CONDA="mamba"
else
    CONDA="conda"

    echo Install Mamba with `conda install -n base -c conda-forge mamba` for a faster test experience.
fi

# Install Augur from the current directory into a new conda environment.
echo Setting up Conda test environment with ${CONDA}
${CONDA} create --yes --quiet -n ${ENV_NAME} -c conda-forge -c bioconda augur
conda activate ${ENV_NAME}
python3 -m pip install -e .[dev]

# Run unit and functional tests.
echo Running tests
./run_tests.sh || true
bash tests/builds/runner.sh || true

# Clean up the temporary environment.
echo Cleaning up
conda deactivate
conda env remove -n ${ENV_NAME}
