; tox configuration file for running tests similar to buildbot builders.
;
; The main trial based environments are defined based on a set of tags. Each
; tag will trigger a different behavior. Multiple tags can be set for the same environment:
;
; * alldeps - install all possible dependencies.
; * nodeps - avoid installing any dependency.
; * withcov - run the tests wrapped using the coverage.
; * nocov - run the tests directly, without using the coverage wrapper.
; * wheel - build the wheel distribution
; * posix - prepare the tests to be run in a Linux/Unix/macOS environment.
; * macos - prepare the tests to be run in a macOS 10.11+ environment (superset of posix)
; * macos1010 - prepare the tests to be run in a macOS 10.10 environment (superset of posix)
; * windows - prepare the tests to be executed under Windows.
;
; There is are non-default `codecov-push` and `coveralls-push` environments
; used to push the coverage results to codecov.io and coveralls.io,
; respectively. They should be called after running both some number of
; `-withcov` environments and also `coverage-prepare`.

; For compatibility with the current buildbot infrastructure, `codecov-publish`
; combines `coverage-prepare` and `codecov-push` into a single step.
;
; A non-default `txchecker-travis` environment is used to run twistedchecker
; on travis in --diff mode
;
[tox]
minversion=2.4
skip_missing_interpreters=True
toxworkdir=build/
envlist=lint,pyflakes,apidocs,narrativedocs,newsfragment,manifest-checker,twine,py27-alldeps-nocov,py36-alldeps-nocov

[testenv]
;; dependencies managed by extras in t.p._setup.py._EXTRAS_REQUIRE
extras =
    alldeps: all_non_platform

    windows: windows_platform

    alldeps-macos: osx_platform

    serial: serial

    ; Documentation needs Twisted install to get the version.
    narrativedocs: dev

;; dependencies that are not specified as extras
deps =
    py27-alldeps-{posix,macos}: pysqlite

    {withcov}: coverage

    {coverage-prepare,codecov-publish}: coverage

    {codecov-push,codecov-publish}: codecov

    coveralls-push: coveralls
    coveralls-push: PyYAML

    wheel: wheel

    ; Code quality checkers
    manifest-checker: check-manifest

    twine: twine

    lint: pyflakes
    lint: twistedchecker>=0.7.1
    lint: diff-cover==0.9.12
    lint: pycodestyle

    wheels: cibuildwheel==0.10.0

; All environment variables are passed.
passenv = *

setenv =
    ; Enable sub-processes coverage reports and store coverage reports in a
    ; known location.
    COVERAGE_PROCESS_START = {toxinidir}/.coveragerc
    COVERAGE_FILE = {toxinidir}/.coverage
    # Help tests know where the base directory is.
    TOX_INI_DIR = {toxinidir}

    ; Skip twistedchecker warnings that are not yet required in the required run
    lint: TWISTEDCHECKER_SKIP_WARNINGS = W9208,C0302,C0103,C9302

    ; Configure cibuildwheel to build wheels for:
    ;       CPython 2.7, 3.5, 3.6, and 3.7.
    ; This includes variants of bit depth, unicode width, etc.
    wheels: CIBW_BUILD=cp27-* cp35-* cp36-* cp37-*

    ; If serial or windows extras, force serial testing
    {windows,serial}: TWISTED_FORCE_SERIAL_TESTS = 1

commands =
    ;
    ; Display information about Python interpreter
    ; which will be used in subsequent steps
    ;
    python {toxinidir}/admin/dump_all_version_info.py

    # We need to define nodeps *somewhere* so tox recognises it
    nodeps: python -c "print('No dependencies installed...')"

    ; Install PyDoctor here so it DOESNT overwrite Twisted
    apidocs: pip install --no-deps epydoc pydoctor

    ; Run tests without wrapping them using coverage.
    nocov: python -m twisted.trial --reactor={env:TWISTED_REACTOR:default} --reporter={env:TRIAL_REPORTER:verbose}  {posargs:twisted}

    ; Run the tests wrapped using coverage.
    withcov: python {toxinidir}/admin/_copy.py {toxinidir}/admin/zz_coverage.pth {envsitepackagesdir}/zz_coverage.pth
    withcov: coverage erase
    withcov: coverage run -p --rcfile={toxinidir}/.coveragerc -m twisted.trial --reactor={env:TWISTED_REACTOR:default} --reporter={env:TRIAL_REPORTER:verbose} {posargs:twisted}

    ; Prepare coverage reports for publication.
    {coverage-prepare,codecov-publish}: coverage combine
    {coverage-prepare,codecov-publish}: coverage xml -o coverage.xml -i

    ; Publish coverage reports to codecov.
    {codecov-push,codecov-publish}: codecov {env:CODECOV_OPTIONS:} -X search -X gcov -f coverage.xml

    ; Publish coverage reports to coveralls.
    coveralls-push: coveralls

    ; Build in a root folder so that buildbot can pick the result
    wheel: python setup.py bdist_wheel --dist-dir {toxinidir}/dist

    lint: /bin/sh -c "pyflakes $(find src/twisted admin bin \! -name "compat.py" \! -name "test_compat.py" \! -name "dict.py"  -name '*.py' -o -name '*.3only')"
    lint: {toxinidir}/.travis/twistedchecker-trunk-diff.sh {posargs:twisted}
    lint: /bin/sh -c "git diff trunk | python {toxinidir}/admin/pycodestyle-twisted.py --diff"

    apidocs: {toxinidir}/bin/admin/build-apidocs {toxinidir}/src/ apidocs
    narrativedocs: sphinx-build -aW -b html -d {toxinidir}/docs/_build {toxinidir}/docs {toxinidir}/docs/_build/

    newsfragment: python {toxinidir}/bin/admin/check-newsfragment "{toxinidir}"

    manifest-checker: check-manifest --ignore "docs/_build*,docs/historic/*,admin*,bin/admin*,twisted/topfiles/*.Old"

    twine: twine check {distdir}/*.*

    wheels: cibuildwheel --output-dir {toxinidir}/wheelhouse {toxinidir}
    wheels: python setup.py sdist --formats=gztar,zip --dist-dir={toxinidir}/wheelhouse
    wheels: ls -l {toxinidir}/wheelhouse

[testenv:apidocs]
# Epydoc and pydoctor currently on work on Python 2
basepython=python2.7

[testenv:manifest-checker]
skip_install = true

# TwistedChecker needs to be 3.5
[testenv:lint]
basepython=python3.5

[testenv:coverage-prepare]
skip_install=True

[testenv:codecov-publish]
skip_install=True

[testenv:codecov-push]
skip_install=True

[testenv:coveralls-push]
skip_install=True
