#!/usr/bin/env bash

# Default Python Versions
# shellcheck source=bin/default_pythons
source "bin/default_pythons"

testPythonVersionUnspecified() {
  compile "python_version_unspecified"
  assertCaptured "Installing ${DEFAULT_PYTHON_VERSION}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython2_7() {
  # Python 2.7 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python2"
  assertCaptured "Installing ${LATEST_27}"
  assertCaptured "python-2-7-eol-faq";
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 44.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython2_7_warn() {
  # Python 2.7 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python2_warn"
  assertCaptured "Installing python-2.7.15"
  assertCaptured "python-2-7-eol-faq";
  assertCaptured "Only the latest version"
  assertCaptured "${LATEST_27}"
  assertCapturedSuccess
}

testPython3_4() {
  # Python 3.4 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python3_4"
  assertCaptured "Installing ${LATEST_34}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 19.1.1, setuptools 43.0.0 and wheel 0.33.6"
  assertCaptured "Installing SQLite3"
  # Can't use `assertCapturedSuccess` since Pip outputs a Python 3.4 EOL warning to stderr,
  # and the newest Pip that works on Python 3.4 doesn't support `PIP_NO_PYTHON_VERSION_WARNING`.
  assertCapturedSuccessWithStdErr
}

testPython3_4_warn() {
  # Python 3.4 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python3_4_warn"
  assertCaptured "Installing python-3.4.9"
  assertCaptured "security update!"
  assertCaptured "${LATEST_34}"
  # Can't use `assertCapturedSuccess` since Pip outputs a Python 3.4 EOL warning to stderr,
  # and the newest Pip that works on Python 3.4 doesn't support `PIP_NO_PYTHON_VERSION_WARNING`.
  assertCapturedSuccessWithStdErr
}

testPython3_5() {
  # Python 3.5 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python3_5"
  assertCaptured "Installing ${LATEST_35}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython3_5_warn() {
  # Python 3.5 is EOL, so it has not been built for Heroku-20.
  if [[ $STACK == "heroku-20" ]]; then
    return
  fi
  compile "python3_5_warn"
  assertCaptured "Installing python-3.5.6"
  assertCaptured "security update!"
  assertCaptured "${LATEST_35}"
  assertCapturedSuccess
}

testPython3_6() {
  compile "python3_6"
  assertCaptured "Installing ${LATEST_36}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython3_6_warn() {
  compile "python3_6_warn"
  assertCaptured "Installing python-3.6.11"
  assertCaptured "security update!"
  assertCaptured "${LATEST_36}"
  assertCapturedSuccess
}

testPython3_7() {
  compile "python3_7"
  assertCaptured "Installing ${LATEST_37}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython3_7_warn() {
  compile "python3_7_warn"
  assertCaptured "Installing python-3.7.8"
  assertCaptured "security update!"
  assertCaptured "${LATEST_37}"
  assertCapturedSuccess
}

testPython3_8() {
  compile "python3_8"
  assertCaptured "Installing ${LATEST_38}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython3_8_warn() {
  compile "python3_8_warn"
  assertCaptured "Installing python-3.8.5"
  assertCaptured "security update!"
  assertCaptured "${LATEST_38}"
  assertCapturedSuccess
}

testPython3_9() {
  compile "python3_9"
  assertCaptured "Installing ${LATEST_39}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCaptured "Installing SQLite3"
  assertCapturedSuccess
}

testPython3_9_warn() {
  # Can't test the version warning until there is at least one old version of Python 3.9.
  if [[ "${LATEST_39}" = "python-3.9.0" ]]; then
    return
  fi
  compile "python3_9_warn"
  assertCaptured "Installing python-3.9.0"
  assertCaptured "security update!"
  assertCaptured "${LATEST_39}"
  assertCapturedSuccess
}

testPythonVersionInvalid() {
  compile "python_version_invalid"
  assertCaptured "Requested runtime (python-3.8.99) is not available for this stack"
  assertCaptured "Aborting"
  assertCapturedError
}

testPypy3_6() {
  compile "pypy3_6"
  assertCaptured "Installing ${LATEST_PYPY_36}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2"
  assertCapturedSuccess
}

testPypy3_6_warn() {
  compile "pypy3_6_warn"
  assertCaptured "Installing pypy3.6-7.3.1"
  assertCaptured "security update!"
  assertCaptured "${LATEST_PYPY_36}"
  assertCapturedSuccess
}

testPypy2_7() {
  compile "pypy2_7"
  assertCaptured "Installing ${LATEST_PYPY_27}"
  assertNotCaptured "security update"
  assertCaptured "Installing pip 20.1.1, setuptools 44.1.1 and wheel 0.34.2"
  assertCapturedSuccess
}

testPypy2_7_warn() {
  compile "pypy2_7_warn"
  assertCaptured "Installing pypy2.7-7.3.1"
  assertCaptured "security update!"
  assertCaptured "${LATEST_PYPY_27}"
  assertCapturedSuccess
}

testStickyPythonVersion() {
  local cache_dir="$(mktmpdir)"
  compile "python3_6_warn" "$cache_dir"
  assertCaptured "Installing python-3.6.11"
  assertCapturedSuccess
  compile "python_version_unspecified" "$cache_dir"
  assertNotCaptured "Installing python"
  assertCaptured "security update!"
  assertCapturedSuccess
  # Whilst this file seems like an implementation detail (so something that should
  # not be tested), we must guarantee the filename remains consistent for backwards
  # compatibility across buildpack versions for already-built apps.
  assertFile "python-3.6.11" ".heroku/python-version"
}

testPythonVersionChange() {
  local cache_dir="$(mktmpdir)"
  compile "python3_6_warn" "$cache_dir"
  assertCaptured "Installing python-3.6.11"
  assertCapturedSuccess
  compile "python3_6" "$cache_dir"
  assertCaptured "Found python-3.6.11, removing"
  assertCapturedSuccess
}

pushd $(dirname 0) >/dev/null
popd >/dev/null

source $(pwd)/test/utils
source $(pwd)/test/shunit2
