#! /bin/bash

# Test setup: see bitbucket-pipelines.yml

set -exuo pipefail

if [[ $# -gt 1 ]]; then
    echo "usage: ./run-tests [--skip-experiments]"
    exit 2
fi

# We need two environment variables for the doctests and experiments.
: "${DOWNWARD_BENCHMARKS?Please set DOWNWARD_BENCHMARKS}"
: "${DOWNWARD_REPO?Please set DOWNWARD_REPO}"

function cleanup {
    rm -rf examples/data
    rm -rf examples/ff/data
}

# Change into top directory.
cd "$(dirname "$0")"
cd ..

# Make lab available on the PYTHONPATH.
LAB=`realpath .`
echo "Checking lab at $LAB"
export PYTHONPATH=${LAB}
echo "Using PYTHONPATH: $PYTHONPATH"

cleanup

python2 -m py.test tests

# Run doctests.
cd lab
python -m doctest reports/__init__.py experiment.py parser.py tools.py
cd ..
cd downward
python -m doctest experiment.py
cd ..

# Check imports and unused/missing variables.
python -m pyflakes lab/calls lab/reports lab/*.py  # Don't check external dir.
python -m pyflakes downward examples tests setup.py docs/conf.py

./tests/find-dead-code

# Check code formatting.
pycodestyle --max-line-length=90 --exclude=data,external lab downward examples setup.py

# Check if documentation builds.
cd docs
make -B html
echo "Docs: file://$(pwd)/_build/html/index.html"
cd ..

echo "Code and style tests passed"

# Skip experiments if any argument (e.g., --skip-experiments) was passed.
if [[ $# -gt 0 ]]; then
    exit
fi

cd examples

cd ff
./ff.py --all
cd ..

./report-external-results.py --all

./lmcut.py --all

./showcase-options.py --all

echo "Experiment tests passed"
