# Scapy tox configuration file
# Copyright (C) 2020 Guillaume Valadon <guillaume@valadon.net>


[tox]
envlist = py{27,34,35,36,37,38,39,py,py3}-{linux,bsd}_{non_root,root},
          py{27,34,25,36,37,38,39,py,py3}-windows,
skip_missing_interpreters = true
minversion = 2.9

# Main tests

[testenv]
description = "Scapy unit tests"
whitelist_externals = sudo
passenv = PATH PWD PROGRAMFILES WINDIR SYSTEMROOT OPENSSL_CONF
          # Used by scapy
          SCAPY_USE_LIBPCAP
deps = mock
       # cryptography requirements
       setuptools>=18.5
       cryptography
       coverage
       python-can
       # disabled on windows because they require c++ dependencies
       brotli ; sys_platform != 'win32'
       zstandard ; sys_platform != 'win32'
platform =
  linux_non_root,linux_root: linux
  bsd_non_root,bsd_root: darwin|freebsd|openbsd|netbsd
  windows: win32
commands =
  linux_non_root: {envpython} {env:SCAPY_PY_OPTS:-m coverage run} -m scapy.tools.UTscapy -c ./test/configs/linux.utsc -N {posargs}
  linux_root: sudo -E {envpython} {env:SCAPY_PY_OPTS:-m coverage run} -m scapy.tools.UTscapy -c ./test/configs/linux.utsc {posargs}
  bsd_non_root: {envpython} {env:SCAPY_PY_OPTS:-m coverage run} -m scapy.tools.UTscapy -c test/configs/bsd.utsc -K manufdb -K tshark -N {posargs}
  bsd_root: sudo -E {envpython} {env:SCAPY_PY_OPTS:-m coverage run} -m scapy.tools.UTscapy -c test/configs/bsd.utsc -K manufdb -K tshark {posargs}
  windows: {envpython} {env:SCAPY_PY_OPTS:-m coverage run} -m scapy.tools.UTscapy -c test/configs/windows.utsc {posargs}
  coverage combine

# Variants of the main tests

[testenv:py38-isotp_kernel_module]
description = "Scapy unit tests - ISOTP Linux kernel module"
whitelist_externals = sudo
                      git
                      bash
                      lsmod
                      modprobe
passenv = PATH PWD PROGRAMFILES WINDIR SYSTEMROOT
deps = {[testenv]deps}
commands =
  sudo -E modprobe can
  git clone --depth=1 https://github.com/linux-can/can-utils.git /tmp/can-utils
  bash -c "cd /tmp/can-utils; ./autogen.sh; ./configure; make; sudo make install"
  git clone --depth=1 https://github.com/hartkopp/can-isotp.git /tmp/can-isotp
  bash -c "cd /tmp/can-isotp; make; sudo make modules_install; sudo modprobe can_isotp || sudo insmod ./net/can/can-isotp.ko" 
  bash -c "rm -rf /tmp/can-utils /tmp/can-isotp"
  lsmod
  sudo -E {envpython} -m coverage run -m scapy.tools.UTscapy -c ./test/configs/linux.utsc {posargs}
  coverage combine

# Specific functions or tests

[testenv:codecov]
description = "Upload coverage results to codecov"
passenv = TOXENV CI TRAVIS TRAVIS_* APPVEYOR APPVEYOR_*
deps = codecov
commands = codecov -e TOXENV


# The files listed past the first argument of the sphinx-apidoc command are ignored
[testenv:apitree]
description = "Regenerates the API reference doc tree"
skip_install = true
changedir = doc/scapy
deps = sphinx
commands =
  sphinx-apidoc -f --no-toc -d 1 --separate --module-first --templatedir=_templates --output-dir api ../../scapy ../../scapy/modules/ ../../scapy/libs/ ../../scapy/tools/ ../../scapy/arch/ ../../scapy/contrib/scada/* ../../scapy/all.py ../../scapy/layers/all.py ../../scapy/compat.py


[testenv:mypy]
description = "Check Scapy compliance against static typing"
skip_install = true
deps = mypy==0.790
       typing
commands = python .config/mypy/mypy_check.py


[testenv:docs]
description = "Build the docs"
skip_install = true
changedir = doc/scapy
deps = sphinx>=2.4.2
       sphinx_rtd_theme
commands =
  sphinx-build -W --keep-going -b html . _build/html


# Debug mode
[testenv:docs2]
description = "Build the docs without rebuilding the API tree"
skip_install = true
changedir = doc/scapy
deps = {[testenv:docs]deps}
setenv =
  SCAPY_APITREE = 0
commands =
  sphinx-build -W --keep-going -b html . _build/html


[testenv:spell]
description = "Check code for Grammar mistakes"
skip_install = true
deps = codespell
# inet6, dhcp6 and the ipynb files contains french: ignore them
commands = codespell --ignore-words=.config/codespell_ignore.txt --skip="*.pyc,*.png,*.jpg,*.ods,*.raw,*.pdf,*.pcap,*.js,*.html,*.der,*_build*,*inet6.py,*dhcp6.py,*.ipynb,*.svg,*.gif,*.obs,*.gz" scapy/ doc/ test/ .github/


[testenv:twine]
description = "Check Scapy code distribution"
skip_install = true
deps = twine
       setuptools>=38.6.0
       cmarkgfm
commands = python setup.py --quiet sdist
           twine check dist/*


[testenv:flake8]
description = "Check Scapy code style & quality"
skip_install = true
deps = flake8
commands = flake8 scapy/


# flake8 configuration
[flake8]
ignore = E731, W504
per-file-ignores =
    scapy/all.py:F403,F401
    scapy/contrib/automotive/obd/obd.py:F405,F403
    scapy/contrib/automotive/obd/pid/pids.py:F405,F403
    scapy/contrib/automotive/obd/scanner.py:F405,F403,E501
    scapy/contrib/automotive/volkswagen/definitions.py:E501
    scapy/contrib/eigrp.py:E501
    scapy/contrib/geneve.py:E501
    scapy/contrib/http2.py:F821
    scapy/contrib/igmp.py:E501
    scapy/contrib/scada/iec104/__init__.py:F405
    scapy/layers/tls/all.py:F403
    scapy/layers/tls/crypto/all.py:F403
    scapy/libs/winpcapy.py:F405,F403,E501
    scapy/tools/UTscapy.py:E501
exclude = scapy/modules/six.py,
          scapy/libs/ethertypes.py
