# Tox (http://tox.testrun.org/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

# Map Python versions in GitHub Actions to tox environments to run, for use by
# the tox-gh-actions package.
[gh-actions]
python =
    3.8: py38-coverage
    3.9: py39-coverage
    3.10: py310-coverage
    3.11: py311-coverage
    3.12: py312-coverage
    pypy-3.8: pypy38
    pypy-3.9: pypy39

[pytest]
twisted = 1

[tox]
envlist = typechecks,codechecks,py{38,39,310,311,312}-{coverage},pypy27,pypy38,pypy39,integration
minversion = 4

[testenv]
# Install code the real way, for maximum realism.
usedevelop = False

passenv = TAHOE_LAFS_*,PIP_*,SUBUNITREPORTER_*,USERPROFILE,HOMEDRIVE,HOMEPATH,COLUMNS
deps =
    # We pull in certify *here* to avoid bug #2913. Basically if a
    # `setup_requires=...` causes a package to be installed (with setuptools)
    # then it'll fail on certain platforms (travis's OX-X 10.12, Slackware
    # 14.2) because PyPI's TLS requirements (TLS >= 1.2) are incompatible with
    # the old TLS clients available to those systems.  Installing it ahead of
    # time (with pip) avoids this problem.
    #
    # We don't pin an exact version of it because it contains CA certificates
    # which necessarily change over time.  Pinning this is guaranteed to cause
    # things to break eventually as old certificates expire and as new ones
    # are used in the wild that aren't present in whatever version we pin.
    # Hopefully there won't be functionality regressions in new releases of
    # this package that cause us the kind of suffering we're trying to avoid
    # with the above pins.
    certifi

extras =
       # Get general testing environment dependencies so we can run the tests
       # how we like.
       testenv

       # And get all of the test suite's actual direct Python dependencies.
       test

setenv =
       # Define TEST_SUITE in the environment as an aid to constructing the
       # correct test command below.
       TEST_SUITE = allmydata
       COLUMNS = 80

commands =
         # As an aid to debugging, dump all of the Python packages and their
         # versions that are installed in the test environment.  This is
         # particularly useful to get from CI runs - though hopefully the
         # version pinning we do limits the variability of this output
         pip freeze

         tahoe --version

         python -c "import sys; print('sys.stdout.encoding:', sys.stdout.encoding)"

         # Run tests with -b to catch bugs like `"%s" % (some_bytes,)`. -b makes
         # Python emit BytesWarnings, and warnings configuration in
         # src/allmydata/tests/__init__.py turns allmydata's BytesWarnings into
         # exceptions.
         !coverage: python -b -m twisted.trial {env:TAHOE_LAFS_TRIAL_ARGS:--rterrors} {posargs:{env:TEST_SUITE}}

         # measuring coverage is somewhat slower than not measuring coverage
         # so only do it on request.
         coverage: python -b -m coverage run -m twisted.trial {env:TAHOE_LAFS_TRIAL_ARGS:--rterrors --reporter=timing} {posargs:{env:TEST_SUITE}}
         coverage: coverage combine
         coverage: coverage xml

[testenv:integration]
usedevelop = False
basepython = python3
platform = mylinux: linux
           mymacos: darwin
           mywindows: win32
setenv =
         COVERAGE_PROCESS_START=.coveragerc
commands =
         # NOTE: 'run with "py.test --keep-tempdir -s -v integration/" to debug failures'
         py.test --timeout=1800 --coverage -s -v {posargs:integration}
         coverage combine


[testenv:codechecks]
basepython = python3
skip_install = true
deps =
     # Pin a specific version so we get consistent outcomes; update this
     # occasionally:
     ruff == 0.1.6
     towncrier
# On macOS, git inside of towncrier needs $HOME.
passenv = HOME
setenv =
	 # If no positional arguments are given, try to run the checks on the
	 # entire codebase, including various pieces of supporting code.
	 DEFAULT_FILES=src integration benchmarks static misc setup.py
commands =
         ruff check {posargs:{env:DEFAULT_FILES}}
         python misc/coding_tools/check-umids.py {posargs:{env:DEFAULT_FILES}}
         python misc/coding_tools/check-debugging.py {posargs:{env:DEFAULT_FILES}}

         # If towncrier.check fails, you forgot to add a towncrier news
         # fragment explaining the change in this branch.  Create one at
         # `newsfragments/<ticket>.<change type>` with some text for the news
         # file.  See towncrier.toml for legal <change type> values.
         python -m towncrier.check --config towncrier.toml


[testenv:typechecks]
basepython = python3
deps =
    mypy==1.8.0
    mypy-zope
    types-mock
    types-six
    types-PyYAML
    types-setuptools
    types-pyOpenSSL
    foolscap
    # Upgrade when new releases come out:
    Twisted==23.10.0
commands =
    # Different versions of Python have a different standard library, and we
    # want to be compatible with all the variations. For speed's sake we only do
    # the earliest and latest versions.
    mypy --python-version=3.8 src
    mypy --python-version=3.12 src


[testenv:draftnews]
passenv = TAHOE_LAFS_*,PIP_*,SUBUNITREPORTER_*,USERPROFILE,HOMEDRIVE,HOMEPATH,COLUMNS
deps =
    # see comment in [testenv] about "certifi"
    certifi
    towncrier==23.11.0
commands =
    python -m towncrier --draft --config towncrier.toml

[testenv:news]
# On macOS, git invoked from Tox needs $HOME.
passenv = TAHOE_LAFS_*,PIP_*,SUBUNITREPORTER_*,USERPROFILE,HOMEDRIVE,HOMEPATH,COLUMNS
whitelist_externals =
    git
deps =
    # see comment in [testenv] about "certifi"
    certifi
    towncrier==23.11.0
commands =
    python -m towncrier --yes --config towncrier.toml
    # commit the changes
    git commit -m "update NEWS.txt for release"

[testenv:deprecations]
commands =
         python misc/build_helpers/run-deprecations.py --package allmydata --warnings={env:TAHOE_LAFS_WARNINGS_LOG:_trial_temp/deprecation-warnings.log} trial {env:TAHOE_LAFS_TRIAL_ARGS:--rterrors} {posargs:allmydata}

[testenv:upcoming-deprecations]
deps =
     # Take the base deps as well!
     {[testenv]deps}
     git+https://github.com/warner/foolscap
commands =
         flogtool --version
         python misc/build_helpers/run-deprecations.py --package allmydata --warnings={env:TAHOE_LAFS_WARNINGS_LOG:_trial_temp/deprecation-warnings.log} trial {env:TAHOE_LAFS_TRIAL_ARGS:--rterrors} {posargs:allmydata}

# Use 'tox -e docs' to check formatting and cross-references in docs .rst
# files. The published docs are built by code run over at readthedocs.org,
# which does not use this target (but does something similar).
#
# If you have "sphinx" installed in your virtualenv, you can just do "make -C
# docs html", or "cd docs; make html".
#
# You can also open docs/_build/html/index.html to see the rendered docs in
# your web browser.

[testenv:docs]
deps =
     -r docs/requirements.txt
# normal install is not needed for docs, and slows things down
skip_install = True
commands =
         sphinx-build -W -b html -d {toxinidir}/docs/_build/doctrees {toxinidir}/docs {toxinidir}/docs/_build/html

[testenv:pyinstaller]
extras =
deps =
    {[testenv]deps}
    packaging
    pyinstaller
    pefile ; platform_system == "Windows"
# Setting PYTHONHASHSEED to a known value assists with reproducible builds.
# See https://pyinstaller.readthedocs.io/en/stable/advanced-topics.html#creating-a-reproducible-build
setenv=PYTHONHASHSEED=1
commands=
    pip freeze
    pyinstaller -y --clean pyinstaller.spec

[testenv:tarballs]
basepython = python3
deps =
commands =
         python setup.py update_version
         python setup.py sdist --formats=gztar bdist_wheel --universal