#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

#
# Tox global configuration
#
[tox]
envlist = py{310,311,312,313,314}
skip_missing_interpreters = true
isolated_build = true

# Configuration variables to share across environments
[config]
BST_PLUGINS_COMMUNITY_VERSION = 637e2d95e656b9d46575da5737b65fd0cc25dce2 # 2.1.0
BST_PLUGINS_VERSION = 79649529cffb695d0d22195ed9a4910c80ca6907 # 2.5.0

#
# Defaults for all environments
#
# Anything specified here is inherited by the sections
#
[testenv]
usedevelop =
    # This is required by Cython in order to get coverage for cython files.
    py{310,311,312,313,314}-!nocover: True

commands =
    # Running with coverage reporting enabled
    py{310,311,312,313,314}-!plugins-!nocover: pytest --basetemp {envtmpdir} --cov=buildstream --cov-config .coveragerc {posargs}
    # Running with coverage reporting disabled
    py{310,311,312,313,314}-!plugins-nocover: pytest --basetemp {envtmpdir} {posargs}
    # Running external plugins tests with coverage reporting enabled
    py{310,311,312,313,314}-plugins-!nocover: pytest --basetemp {envtmpdir} --cov=buildstream --cov-config .coveragerc --plugins {posargs}
    # Running external plugins tests with coverage disabled
    py{310,311,312,313,314}-plugins-nocover: pytest --basetemp {envtmpdir} --plugins {posargs}
commands_post:
    py{310,311,312,313,314}-!nocover: mkdir -p .coverage-reports
    py{310,311,312,313,314}-!nocover: mv {envtmpdir}/.coverage {toxinidir}/.coverage-reports/.coverage.{env:COVERAGE_PREFIX:}{envname}
deps =
    py{310,311,312,313,314}: -rrequirements/requirements.txt
    py{310,311,312,313,314}: -rrequirements/dev-requirements.txt
    py{310,311,312,313,314}: git+https://github.com/apache/buildstream-plugins.git@{env:BST_PLUGINS_VERSION:{[config]BST_PLUGINS_VERSION}}

    # Install local sample plugins for testing pip plugin origins
    py{310,311,312,313,314}: {toxinidir}/tests/plugins/sample-plugins

    # Install external plugins for plugin tests
    py{310,311,312,313,314}-plugins: git+https://gitlab.com/buildstream/buildstream-plugins-community.git@{env:BST_PLUGINS_COMMUNITY_VERSION:{[config]BST_PLUGINS_COMMUNITY_VERSION}}#egg=bst_plugins_community[deb]

    # Only require coverage and pytest-cov when using it
    !nocover: -rrequirements/cov-requirements.txt

    # Install pytest-random-order for '-randomized'
    randomized: pytest-random-order
passenv =
    ARTIFACT_CACHE_SERVICE
    ARTIFACT_INDEX_SERVICE
    ARTIFACT_STORAGE_SERVICE
    BST_CAS_STAGING_ROOT
    GI_TYPELIB_PATH
    INTEGRATION_CACHE
    http_proxy
    HTTP_PROXY
    https_proxy
    HTTPS_PROXY
    no_proxy
    NO_PROXY
    REMOTE_EXECUTION_SERVICE
    SOURCE_CACHE_SERVICE
    SSL_CERT_FILE
    BST_PLUGINS_COMMUNITY_VERSION
#
# These keys are not inherited by any other sections
#
setenv =
    py{310,311,312,313,314}: COVERAGE_FILE = {envtmpdir}/.coverage
    py{310,311,312,313,314}: BST_TEST_HOME = {envtmpdir}
    py{310,311,312,313,314}: BST_TEST_XDG_CACHE_HOME = {envtmpdir}/cache
    py{310,311,312,313,314}: BST_TEST_XDG_CONFIG_HOME = {envtmpdir}/config
    py{310,311,312,313,314}: BST_TEST_XDG_DATA_HOME = {envtmpdir}/share

    # This is required to get coverage for Cython
    py{310,311,312,313,314}-!nocover: BST_CYTHON_TRACE = 1
    randomized: PYTEST_ADDOPTS="--random-order-bucket=global"

allowlist_externals =
    py{310,311,312,313,314}:
        mv
        mkdir

#
# Environment for native windows
#
[testenv:win32]
commands =
    bst help
    cmd /C md testdir
    bst init testdir --project-name="test"
deps =
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt
    colorama
    windows-curses
    cython
    .
allowlist_externals =
    bst
    cmd

#
# Coverage reporting
#
[testenv:coverage]
# This is required by Cython in order to get coverage for cython files.
usedevelop = True

commands =
    coverage combine --rcfile={toxinidir}/.coveragerc {toxinidir}/.coverage-reports/
    coverage html --rcfile={toxinidir}/.coveragerc --directory={toxinidir}/.coverage-reports/
    coverage report --rcfile={toxinidir}/.coveragerc --show-missing
deps =
    -rrequirements/cov-requirements.txt
setenv =
    COVERAGE_FILE = {toxinidir}/.coverage-reports/.coverage

#
# Code formatters
#
[testenv:format]
skip_install = True
deps =
    black~=26.3.0
commands =
    black {posargs: src tests doc/source/conf.py setup.py}

#
# Code format checkers
#
[testenv:format-check]
skip_install = True
deps =
    black~=26.3.0
commands =
    black --check --diff {posargs: src tests doc/source/conf.py setup.py}

#
# Running linters
#
[testenv:lint]
commands_pre =
    # Build C extensions to allow Pylint to analyse them
    {envpython} setup.py build_ext --inplace

commands =
    pylint {posargs: buildstream tests doc/source/conf.py setup.py}
deps =
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt
    setuptools
    Cython

#
# Running static type checkers
#
[testenv:mypy]
skip_install = True
commands =
    mypy --namespace-packages {posargs}
deps =
    mypy==1.13.0
    types-protobuf==5.28.3.20241030
    types-ujson==5.10.0.20240515
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt


#
# Building documentation
#
[testenv:docs]
commands =
    make -C doc
# sphinx_rtd_theme < 0.4.2 breaks search functionality for Sphinx >= 1.8
deps =
    sphinx == 8.1.3
    sphinx-click == 6.0.0
    sphinx_rtd_theme == 3.0.2
    pytest
    -rrequirements/requirements.txt
    git+https://github.com/apache/buildstream-plugins.git@{env:BST_PLUGINS_VERSION:{[config]BST_PLUGINS_VERSION}}
passenv =
    BST_FORCE_SESSION_REBUILD
    BST_SOURCE_CACHE
    HOME
    LANG
    LC_ALL
allowlist_externals =
    make


#
# (re-)Generating man pages
#
[testenv:man]
commands =
    # We set the version to 0, because click-man would now
    # otherwise want to pull a hardcoded version from pyproject.toml
    # that somehow gets magically transported to the EntryPoint.version
    click-man --man-version {posargs} --target man bst
deps =
    click-man >= 0.5.0
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt
    Cython


#
# Usefull for running arbitrary scripts in a BuildStream virtual env
#
[testenv:venv]
commands = {posargs}
deps =
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt
allowlist_externals = *


#
# Convenience environment for running individual tests from the
# battery of templated source tests.
#
# You should pass this the part of a test node's id after "::". For
# example, to run the test
# buildstream/testing/_sourcetests/fetch.py::test_fetch_cross_junction[git-inline]
# you would do tox -e sourcetests -- test_fetch_cross_junction[git-inline]
#
# This does rely on the fact that none of the tests in
# buildstream.testing have the same name.
#
[testenv:sourcetests]
commands = pytest --basetemp {envtmpdir} --ignore tests -k "{posargs}"
deps =
    -rrequirements/requirements.txt
    -rrequirements/dev-requirements.txt

# When building using PEP518 and 517, we don't want default dependencies
# installed by the base environment.
[testenv:.package]
deps =


#
# Regenerate protobuf and grpc code
#
[testenv:build-grpc]
skip_install = True
commands = python setup.py build_grpc
deps =
    grpcio-tools==1.69.0  # Requires protobuf >= 5.29
    Cython
