[tox]
# to be portable, all environment names must start with py<number>-. This will
# make tox chose a specific python version. If we omit the py<number> factor,
# the result depends on the current state of the OS people are using.
# If available, tox runs tests for Python 3.6, 3.7, 3.8 and pypy3. It will skip
# any unavailable, i.e. not installed, version.
envlist =
    py{36,37,38,py3}
    py3-{setup,lint,coverage}
skip_missing_interpreters=true
requires = setuptools > 42.0.0

## One can use blocks as the two examples below, to provide tox with paths to
## different Python installations.
#
#[testenv:py36]
#basepython={env:HOME}/.local/python/3.6.10/bin/python3.6
#
#[testenv:py38]
#basepython={env:HOME}/.local/python/3.8.1/bin/python3.8
#

[envvars]
setenv =
    JOBS={env:JOBS:8}
    DB_TYPE={env:DB_TYPE:postgres}
    DB_USER={env:DB_USER:askbot}
    DB_PASS={env:DB_PASS:askB0T!}
    DB_HOST={env:DB_HOST:localhost}
    DB_PORT={env:DB_PORT:5432}
    DB_NAME={env:DB_NAME:askbotfortox}
    DATABASE_URL = {env:DATABASE_URL:{env:DB_TYPE}://{env:DB_USER}:{env:DB_PASS}@{env:DB_HOST}:{env:DB_PORT}/{env:DB_NAME}}
    TOX_ENVSITEPACKAGESDIR = {envsitepackagesdir}

[unittests]
commands =
    coverage run --rcfile ../.coveragerc manage.py test --parallel {env:JOBS} askbot.tests askbot.deps.django_authopenid.tests

[coverage]
commands =
    coverage html --rcfile ../.coveragerc

[lint]
commands =
    pylint -j {env:JOBS} --exit-zero --rcfile={toxinidir}/.pylintrc {toxinidir}/{posargs:askbot}

[setup]
whitelist_externals =
    mkdir
    rm
    psql
commands_pre =
    mkdir -p {toxinidir}/deploy_askbot
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "DROP DATABASE IF EXISTS {env:DB_NAME}"
    -psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "DROP ROLE IF EXISTS {env:DB_USER}"
    -psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "CREATE USER {env:DB_USER} WITH PASSWORD '{env:DB_PASS}'"
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "ALTER ROLE {env:DB_USER} SET client_encoding TO 'utf8'"
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "ALTER ROLE {env:DB_USER} SET default_transaction_isolation TO 'read committed'"
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "ALTER ROLE {env:DB_USER} SET timezone TO 'UTC'"
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "ALTER USER {env:DB_USER} CREATEDB"
    psql -h {env:DB_HOST} -p {env:DB_PORT} -U postgres -c "CREATE DATABASE {env:DB_NAME} OWNER='{env:DB_USER}'"
commands = python -c ""
commands_post =
    rm -rf {toxinidir}/deploy_askbot
    rm -rf {toxinidir}/static

[testenv]
depends =
    py3: py{36,37,38,py3}
deps =
    py{36,37,38,py3}: -raskbot_requirements.txt
    -rrequirements-tests.txt
    coverage: coverage
    lint: pylint
skipsdist=
    coverage: true
    lint: true
skip_install=
    coverage: true
    lint: true
changedir = {toxinidir}/testproject/
setenv =
    {[envvars]setenv}
commands =
    py{36,37,38,py3}: {[unittests]commands}
    coverage: {[coverage]commands}
    lint: {[lint]commands}
    setup: {[setup]commands}
whitelist_externals =
    setup: {[setup]whitelist_externals}
commands_pre =
    setup: {[setup]commands_pre}
commands_post =
    setup: {[setup]commands_post}
