#!/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"

# Install mamba if not available to avoid Conda solver issues
if [[ ! -e "`which mamba`" ]]; then
    echo Installing Mamba
    conda install --yes -n base -c conda-forge mamba
fi

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

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

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