[tox]
envlist = packaging, py36, py37, py38, py39, check_codestyle, check_isort

# we require tox>=2.3.2 for the fix to https://github.com/tox-dev/tox/issues/208
minversion = 2.3.2

[base]
deps =
    python-subunit
    junitxml
    coverage

    # this is pinned since it's a bit of an obscure package.
    coverage-enable-subprocess==1.0

    # cyptography 2.2 requires setuptools >= 18.5
    #
    # older versions of virtualenv (?) give us a virtualenv with the same
    # version of setuptools as is installed on the system python (and tox runs
    # virtualenv under python3, so we get the version of setuptools that is
    # installed on that).
    #
    # anyway, make sure that we have a recent enough setuptools.
    setuptools>=18.5

    # we also need a semi-recent version of pip, because old ones fail to
    # install the "enum34" dependency of cryptography.
    pip>=10

# directories/files we run the linters on.
# if you update this list, make sure to do the same in scripts-dev/lint.sh
lint_targets =
    setup.py
    synapse
    tests
    scripts
    # annoyingly, black doesn't find these so we have to list them
    scripts/export_signing_key
    scripts/generate_config
    scripts/generate_log_config
    scripts/hash_password
    scripts/register_new_matrix_user
    scripts/synapse_port_db
    scripts-dev
    scripts-dev/build_debian_packages
    scripts-dev/sign_json
    scripts-dev/update_database
    stubs
    contrib
    synctl
    synmark
    .buildkite
    docker

# default settings for all tox environments
[testenv]
deps =
    {[base]deps}
extras =
    # install the optional dependendencies for tox environments without
    # '-noextras' in their name
    # (this requires tox 3)
    !noextras: all
    test

setenv =
    # use a postgres db for tox environments with "-postgres" in the name
    # (see https://tox.readthedocs.io/en/3.20.1/config.html#factors-and-factor-conditional-settings)
    postgres: SYNAPSE_POSTGRES = 1

    # this is used by .coveragerc to refer to the top of our tree.
    TOP={toxinidir}

passenv = *

commands =
    # the "env" invocation enables coverage checking for sub-processes. This is
    # particularly important when running trial with `-j`, since that will make
    # it run tests in a subprocess, whose coverage would otherwise not be
    # tracked.  (It also makes an explicit `coverage run` command redundant.)
    #
    # (See https://coverage.readthedocs.io/en/coverage-5.3/subprocess.html.
    # Note that the `coverage.process_startup()` call is done by
    # `coverage-enable-subprocess`.)
    #
    # we use "env" rather than putting a value in `setenv` so that it is not
    # inherited by other tox environments.
    #
    /usr/bin/env COVERAGE_PROCESS_START={toxinidir}/.coveragerc "{envbindir}/trial" {env:TRIAL_FLAGS:} {posargs:tests} {env:TOXSUFFIX:}

# As of twisted 16.4, trial tries to import the tests as a package (previously
# it loaded the files explicitly), which means they need to be on the
# pythonpath. Our sdist doesn't include the 'tests' package, so normally it
# doesn't work within the tox virtualenv.
#
# As a workaround, we tell tox to do install with 'pip -e', which just
# creates a symlink to the project directory instead of unpacking the sdist.
#
# (An alternative to this would be to set PYTHONPATH to include the project
# directory. Note two problems with this:
#
#   - if you set it via `setenv`, then it is also set during the 'install'
#     phase, which inhibits unpacking the sdist, so the virtualenv isn't
#     useful for anything else without setting PYTHONPATH similarly.
#
#   - `synapse` is also loaded from PYTHONPATH so even if you only set
#     PYTHONPATH for the test phase, we're still running the tests against
#     the working copy rather than the contents of the sdist. So frankly
#     you might as well use -e in the first place.
#
# )
usedevelop=true

# A test suite for the oldest supported versions of Python libraries, to catch
# any uses of APIs not available in them.
[testenv:py3-old]
skip_install = true
usedevelop = false
deps =
    # Old automat version for Twisted
    Automat == 0.3.0
    lxml
    {[base]deps}

commands =
    # Make all greater-thans equals so we test the oldest version of our direct
    # dependencies, but make the pyopenssl 17.0, which can work against an
    # OpenSSL 1.1 compiled cryptography (as older ones don't compile on Travis).
    /bin/sh -c 'python -m synapse.python_dependencies | sed -e "s/>=/==/g" -e "/psycopg2/d" -e "s/pyopenssl==16.0.0/pyopenssl==17.0.0/" | xargs -d"\n" pip install'

    # Install Synapse itself. This won't update any libraries.
    pip install -e ".[test]"

    {[testenv]commands}

[testenv:benchmark]
deps =
    {[base]deps}
    pyperf
setenv =
    SYNAPSE_POSTGRES = 1
commands =
    python -m synmark {posargs:}

[testenv:packaging]
skip_install = true
usedevelop = false
deps =
    check-manifest
commands =
    check-manifest

[testenv:check_codestyle]
extras = lint
commands =
    python -m black --check --diff {[base]lint_targets}
    flake8 {[base]lint_targets} {env:PEP8SUFFIX:}
    {toxinidir}/scripts-dev/config-lint.sh

[testenv:check_isort]
extras = lint
commands = isort -c --df --sp setup.cfg {[base]lint_targets}

[testenv:check-newsfragment]
skip_install = true
usedevelop = false
deps = towncrier>=18.6.0rc1
commands =
   python -m towncrier.check --compare-with=origin/develop

[testenv:check-sampleconfig]
commands = {toxinidir}/scripts-dev/generate_sample_config --check

[testenv:combine]
skip_install = true
usedevelop = false
deps =
    coverage
    pip>=10
commands=
    coverage combine
    coverage report

[testenv:cov-erase]
skip_install = true
usedevelop = false
deps =
    coverage
commands=
    coverage erase

[testenv:cov-html]
skip_install = true
usedevelop = false
deps =
    coverage
commands=
    coverage html

[testenv:mypy]
deps =
    {[base]deps}
extras = all,mypy
commands = mypy