[common]
python_matrix = {36,37,38,39,310,311}
postgres_matrix =
    pg11: PG_MAJOR = 11
    pg12: PG_MAJOR = 12
    pg13: PG_MAJOR = 13
    pg14: PG_MAJOR = 14
    pg15: PG_MAJOR = 15
psycopg_deps =
    py{37,38,39,310,311}-{lin,win}: psycopg[binary]
    mac: psycopg2-binary
    py36: psycopg2-binary
platforms =
    lin: linux
    mac: darwin
    win: win32

[tox]
min_version = 4.0
requires =
    tox>4
env_list =
    dep
    lint
    py{[common]python_matrix}-test-{lin,mac,win}
    docs
skipsdist = True
toxworkdir = {env:TOX_WORK_DIR:.tox}
skip_missing_interpreters = True

[testenv]
setenv =
    PYTHONDONTWRITEBYTECODE = 1
    mac: OPEN_CMD = {env:OPEN_CMD:open}
    lin: OPEN_CMD = {env:OPEN_CMD:xdg-open}
passenv =
    BROWSER
    DISPLAY

[testenv:lint]
description = Lint code with flake8
commands = flake8 {posargs:patroni tests setup.py}
deps =
    flake8

[testenv:py{36,37,38,39,310,311}-test-{lin,win,mac}]
description = Run unit tests with pytest
labels =
    test
commands_pre =
    - {tty:rm -f "{toxworkdir}{/}cov_report_{env_name}_html{/}index.html":true}
    - {tty:rm -f "{toxworkdir}{/}pytest_report_{env_name}.html":true}
commands =
    pytest \
    -p no:cacheprovider \
    --verbose \
    --doctest-modules \
    --capture=fd \
    --cov=patroni \
    --cov-report=term-missing \
    --cov-append \
    {tty::--cov-report="xml\:{toxworkdir}{/}cov_report.{env_name}.xml"} \
    {tty:--cov-report="html\:{toxworkdir}{/}cov_report_{env_name}_html":} \
    {tty:--html="{toxworkdir}{/}pytest_report_{env_name}.html":} \
    {posargs:tests patroni}
commands_post =
    - {tty:{env:OPEN_CMD} "{toxworkdir}{/}cov_report_{env_name}_html{/}index.html":true}
    - {tty:{env:OPEN_CMD} "{toxworkdir}{/}pytest_report_{env_name}.html":true}
deps =
    -r requirements.txt
    mock>=2.0.0
    pytest
    pytest-cov
    pytest-html
    {[common]psycopg_deps}
platform =
    {[common]platforms}
allowlist_externals =
    rm
    {env:OPEN_CMD}

[testenv:dep]
description = Check package dependency problems
commands = pipdeptree -w fail
deps =
    -r requirements.txt
    pipdeptree
    {[common]psycopg_deps}

[testenv:py{37,38,39,310,311}-type-{lin,mac,win}]
description = Run static type checking with pyright
labels =
    type
deps =
    -r requirements.txt
    pyright
    psycopg2-binary
    psycopg[binary]
commands = pyright --venv-path {toxworkdir}{/}{envname} {posargs:patroni}
platform =
    {[common]platforms}

[testenv:black]
description = Reformat code with black
deps = black
commands = black {posargs:patroni tests}

[testenv:pg{12,13,14,15}-docker-build]
description = Build docker containers needed for testing
labels =
    behave
    docker-build
setenv =
    {[common]postgres_matrix}
    DOCKER_BUILDKIT = 1
passenv =
    BASE_IMAGE
commands =
    docker build . \
        --tag patroni-dev:{env:PG_MAJOR} \
        --build-arg PG_MAJOR \
        --build-arg BASE_IMAGE={env:BASE_IMAGE:postgres} \
        --file features/Dockerfile
allowlist_externals = docker

[testenv:pg{12,13,14,15}-docker-behave-{etcd}-{lin,mac}]
description = Run behaviour tests in patroni-dev docker container
setenv =
    etcd: DCS=etcd
    {[common]postgres_matrix}
    CONTAINER_NAME = tox-{env_name}-{env:PYTHONHASHSEED}
labels =
    behave
depends =
    pg{11,12,13,14,15}-docker-build

# There's a bug which affects calling multiple envs on the command line
# This should be a valid command: tox -e 'py{36,37,38,39,310,311}-behave-{env:DCS}-lin'
# Replaced with workaround, see https://github.com/tox-dev/tox/issues/2850
commands =
    docker run \
        --volume {tox_root}:/src \
        --env DCS={env:DCS} \
        --hostname {env:CONTAINER_NAME} \
        --name {env:CONTAINER_NAME} \
        --rm \
        --tty \
        {env:PATRONI_DEV_IMAGE:patroni-dev:{env:PG_MAJOR}} \
        tox run -x 'tox.env_list=py{[common]python_matrix}-behave-{env:DCS}-lin' \
        -- --format plain {posargs}

allowlist_externals =
    docker
    find
platform =
    lin: linux
;    win: win32
    mac: darwin

[testenv:py{36,38,39,310,311}-behave-{etcd}-{lin,win,mac}]
description = Run behaviour tests (locally with tox)
deps =
    -r requirements.txt
    behave
    coverage
    {[common]psycopg_deps}
setenv =
    DCS = {env:DCS:etcd}
passenv =
    ETCD_UNSUPPORTED_ARCH
commands =
    python3 -m behave {posargs}
platform =
    {[common]platforms}

[testenv:docs-{lin,mac,win}]
description = Build Sphinx documentation
labels:
    docs
deps =
    sphinx>=4
    sphinx_rtd_theme
commands =
    sphinx-build \
    -d "{envtmpdir}{/}doctree" docs "{toxworkdir}{/}docs_out" \
    --color -b html \
    {posargs}
commands_post =
    - {tty:{env:OPEN_CMD} "{toxworkdir}{/}docs_out{/}index.html":true:}
allowlist_externals =
    {env:OPEN_CMD}
platform =
    {[common]platforms}

[flake8]
max-line-length = 120
ignore = D401,W503
