# https://tox.wiki/en/latest/user_guide.html
# https://tox.wiki/en/latest/config.html
[tox]
minversion = 4.0.0
envlist =
    lint,py3{8,9,10,11},covreport,docs,precommit
toxworkdir={env:TOX_WORK_DIR:{tox_root}/.tox}

[testenv]
passenv =
    DBUS_SESSION_BUS_ADDRESS # This is needed for keyring acccess on Linux.
allowlist_externals = poetry
setenv =
    extensive: BERKELEYDB_DIR = /usr
    COVERAGE_FILE = {env:COVERAGE_FILE:{toxinidir}/.coverage.{envname}}
    MYPY_CACHE_DIR = {envdir}/.mypy_cache
    docs: POETRY_ARGS_docs = --only=docs
    extensive: POETRY_ARGS_extensive = --extras=berkeleydb --extras=networkx --extras=html --extras=orjson
    lxml: POETRY_ARGS_lxml = --extras=lxml
commands_pre =
    py3{8,9,10,11}: python -c 'import os; print("\n".join(f"{key}={value}" for key, value in os.environ.items()))'
    py3{8,9,10,11}: poetry check --lock
    py3{8,9,10,11}: poetry install --no-root --only=main --only=dev --only=lint --only=tests {env:POETRY_ARGS_docs:} {env:POETRY_ARGS_extensive:} {env:POETRY_ARGS_lxml:} {env:POETRY_ARGS:} --sync
commands =
    min: python -c 'import sys; print("min qualifier not supported on this environment"); sys.exit(1);'
    poetry config --list
    poetry env info
    poetry run python -m pip freeze
    {env:TOX_EXTRA_COMMAND:}
    {env:TOX_MYPY_COMMAND:poetry run python -m mypy --show-error-context --show-error-codes --junit-xml=test_reports/{env:TOX_JUNIT_XML_PREFIX:}mypy-junit.xml}
    {posargs:poetry run {env:TOX_TEST_HARNESS:} pytest -ra --tb=native {env:TOX_PYTEST_ARGS:--junit-xml=test_reports/{env:TOX_JUNIT_XML_PREFIX:}pytest-junit.xml --cov --cov-report=} {env:TOX_PYTEST_EXTRA_ARGS:}}
    docs: poetry run sphinx-build -T -W -b html -d {envdir}/doctree docs docs/_build/html

[testenv:covreport]
skip_install = true
parallel_show_output = true
depends = py3{8,9,10,11}{-extensive,}{-docs,}
setenv =
    COVERAGE_FILE=
commands_pre =
    poetry install --no-root --only=tests
commands =
    poetry run python -m coverage combine
    poetry run python -m coverage report

[testenv:lint]
skip_install = true
commands_pre =
    poetry install --no-root --only=lint
commands =
    poetry run ruff check .


[testenv:docs]
passenv =
    TERM
    DBUS_SESSION_BUS_ADDRESS
setenv =
    PYTHONHASHSEED = 0
commands_pre =
    poetry lock --check
    poetry install --only=main --only=docs --extras=html
    poetry env info
commands =
    poetry run sphinx-build -T -W -b html -d {envdir}/doctree docs docs/_build/html

[testenv:py38-extensive-min]
base = void
deps =
    pytest==7.*
    pytest-cov==4.*
setenv =
    BERKELEYDB_DIR = /usr
    COVERAGE_FILE = {env:COVERAGE_FILE:{toxinidir}/.coverage.{envname}}
    PIP_CONSTRAINT = devtools/constraints.min
extras =
    berkeleydb
    networkx
    lxml
    html
    orjson
commands =
    {envpython} --version
    pip freeze
    {posargs:{envpython} -m pytest -ra --tb=native {env:TOX_PYTEST_ARGS:--junit-xml=test_reports/{env:TOX_JUNIT_XML_PREFIX:}pytest-junit.xml --cov --cov-report=}}

[testenv:precommit{,all}]
skip_install = true
deps = pre-commit
passenv =
    HOMEPATH  # needed on Windows
    PRE_COMMIT_HOME # so that users can configure it
    XDG_CACHE_HOME # so that pre-commit won't use the default
commands =
    precommit: pre-commit run
    precommitall: pre-commit run --all-files
