[tox]
min_version = 4.22
env_list =
    lint,
    mypy-{api,pkg},
    docs-{build,doctests},
    py3{8-15}{,-pyopenssl}{,-oldest}{,-idna},
    coverage-{combine,report}


[testenv]
package = wheel
wheel_build_env = .pkg
dependency_groups = tests
extras =
    idna: idna
deps =
    pyopenssl: pyopenssl
pass_env =
    FORCE_COLOR
    NO_COLOR
set_env =
    oldest: PIP_CONSTRAINT = tests/constraints/oldest-cryptography.txt
    oldest: UV_CONSTRAINT = tests/constraints/oldest-cryptography.txt
    pyopenssl-oldest: PIP_CONSTRAINT = tests/constraints/oldest-pyopenssl.txt
    pyopenssl-oldest: UV_CONSTRAINT = tests/constraints/oldest-pyopenssl.txt
commands =
    coverage run -m pytest {posargs}
    py312-pyopenssl-latest-idna: coverage run -m pytest --doctest-modules --doctest-glob='*.rst' {posargs}


# Split combine/report in 2 to avoid excessive "Combined data file ..." output.
[testenv:coverage-combine]
# keep in-sync with .python-version-default
base_python = py314
depends = py3*
skip_install = true
deps = coverage
commands = coverage combine


[testenv:coverage-report]
# keep in-sync with .python-version-default
base_python = py314
depends = coverage-combine
parallel_show_output = true
skip_install = true
deps = coverage
commands = coverage report


[testenv:lint]
skip_install = true
deps = prek
commands = prek run --all-files {posargs}


[testenv:mypy-api]
dependency_groups = mypy
commands = mypy tests/typing docs/pyopenssl_example.py


[testenv:mypy-pkg]
dependency_groups = mypy
commands = mypy src


[testenv:docs-{build,doctests,linkcheck}]
# Keep base_python in sync with gh-actions and .readthedocs.yaml.
base_python = py314
dependency_groups = docs
commands =
    # N.B. doctests is not a nitpicky as build -- we need to run both in CI!
    build: sphinx-build -n -T -W -b html -d {envtmpdir}/doctrees docs {posargs:docs/_build/}html
    doctests: sphinx-build -n -T -W -b doctest -d {envtmpdir}/doctrees docs {posargs:docs/_build/}html
    linkcheck: sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees docs docs/_build/html

[testenv:docs-watch]
package = editable
base_python = {[testenv:docs-build]base_python}
dependency_groups = {[testenv:docs-build]dependency_groups}
deps = watchfiles
commands =
    watchfiles \
        --ignore-paths docs/_build/ \
        'sphinx-build -W -n --jobs auto -b html -d {envtmpdir}/doctrees docs docs/_build/html' \
        src \
        docs
