#!/bin/bash
set -evu

# Usage:
#
# build-test [mypy|nomypy]
#
# Arguments:
# - mypy: include mypy check ("mypy" or "nomypy")
#
# Environment variables used:
# - GITHUB_WORKSPACE: workspace directory
#
# WARNING: running this locally will delete any local files that
# aren't strictly part of the git tree, including gitignored files!

MODULE=quantinuum_qircheck

MYPY=$1

PLAT=`python -c 'import platform; print(platform.system())'`

PYVER=`python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))'`

git clean -dfx

echo "Module to test: ${MODULE}"

MODULEDIR="${GITHUB_WORKSPACE}"

ARTIFACTSDIR=${GITHUB_WORKSPACE}/wheelhouse

rm -rf ${ARTIFACTSDIR} && mkdir ${ARTIFACTSDIR}

python -m pip install --upgrade pip wheel build

# Generate and install the package
python -m build
for w in dist/*.whl ; do
    python -m pip install "$w[test]"
    cp $w ${ARTIFACTSDIR}
done

# Test and mypy:
if [[ "${MYPY}" = "mypy" ]]
then
    python -m pip install --upgrade mypy
fi

cd ${GITHUB_WORKSPACE}/tests

pytest --doctest-modules

cd ..

if [[ "${MYPY}" = "mypy" ]]
then
    ${GITHUB_WORKSPACE}/mypy-check ${GITHUB_WORKSPACE}}
fi
