#!/usr/bin/env bash
# Script for running Python package tests against (potentially) different Python versions
# @copyright Highly Confidential and Proprietary Zoox Information. Copyright (c) Zoox.
#
# Date Created: January 14, 2020
# Author: sdaleo
#

# -e Exit script on any error
# -o pipefail Do not propagate errors silently thru pipes
set -eo pipefail

# These environment variables should be present on the Docker container:
# VENVS=/workdir/VirtualEnvs
# PYENV_ROOT=/workdir/.pyenv

# Clear any packages that may be present
rm -f dist/* 2> /dev/null

# Activate Python 3.7.3 to use a default Python version to build the package
. $VENVS/python373/bin/activate

# Generate new Python package in dist/
python setup.py sdist 2> /dev/null

# Deactivate Python 3.7.3
deactivate

# Adjust this to test against different versions
for py_version in 3.6.8 3.7.3
do
    # Strip out '.' from py_version
    triple="${py_version//./}"

    # Create new virtualenv for testing
    VENV=${VENVS}/test_python$triple
    virtualenv -p $PYENV_ROOT/versions/$py_version/bin/python $VENV

    # Activate the virtual environment
    . $VENV/bin/activate

    # Install the requirements
    pip install -r requirements.txt --index-url ${ZOOX_INDEX} --extra-index-url ${PYPI_INDEX}

    # Install test requirements (needed for CI)
    pip install -r test_requirements.txt --index-url ${ZOOX_INDEX} --extra-index-url ${PYPI_INDEX}

    # Install the package into the virtualenv
    pip install --force-reinstall dist/*

    # Run the tests
    echo "Running tests against Python $py_version"
    pytest -v tests/ -W ignore::DeprecationWarning

    # Deactivate virtual environment
    deactivate
done

