#!/usr/bin/env bash

set -euo pipefail
export PYTHONDONTWRITEBYTECODE=1

tests_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
scripts_dir="$(cd "${tests_dir}/.." && pwd)"

clean_python_cache() {
  find "${scripts_dir}" -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete
  find "${scripts_dir}" -depth -type d -name __pycache__ -empty -delete
}

assert_no_python_cache() {
  if find "${scripts_dir}" \( -type d -name __pycache__ -o -type f \( -name '*.pyc' -o -name '*.pyo' \) \) -print -quit |
    grep -q .; then
    printf '%s
' 'Python cache files remain after shared book infra tests.' >&2
    return 1
  fi
}

trap clean_python_cache EXIT
clean_python_cache

python3 "${tests_dir}/test_check.py" -v
"${tests_dir}/bootstrap_probe.sh"
clean_python_cache
assert_no_python_cache
