运行后如何清理毒物环境?

2024-04-19 11:56:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下tox.ini文件:

[tox]
envlist = flake8,py{35,36,37,38}{,-keyring}

[testenv]
usedevelop = True
install_command = pip install -U {opts} {packages}
deps =
    .[test]
    keyring: .[keyring]
setenv =
    COVERAGE_FILE = .coverage.{envname}
commands=
    pytest {toxinidir}/tests -n 4 {posargs}

[testenv:flake8]
basepython = python3
deps = flake8
commands=
    flake8 src tests

[flake8]
ignore: F401,E402,E501,W605,W503

当我运行tox命令时,它会为tox.ini[tox]部分中指定的每个环境创建一个.tox文件夹。你知道吗

当运行tox时,我希望在测试成功后自动读取这些特定文件夹,而不必手动运行rm -rf .tox/NAME_OF_THE_ENV。我查了毒检文件,但什么也没找到。你知道吗

有可能吗?如果是,怎么做?你知道吗


Tags: install文件depspy文件夹truetoxflake8
2条回答

我找到了一种方法,制造了一个毒钩。在env内部运行测试之后,这个钩子运行shutil.rmtree命令。你知道吗

tox_clean_env.py文件中:

import shutil
from tox import hookimpl

@hookimpl
def tox_runtest_post(venv):
    try:
        shutil.rmtree(venv.path)
    except Exception as e:
        print("An exception occurred while removing '{}':".format(venv.path))
        print(e)

我围绕这段代码创建了一个包,只需要使用pip安装它。你知道吗

在我的setup.py中,在setup函数中:

    entry_points={"tox": ["clean_env = tox_clean_env"]},

tox里没有办法。原因是tox将这些环境保留为缓存:下次运行tox时,将重用这些环境,从而节省时间。你知道吗

在用rm -rf .tox运行tox之后,可以立即删除它们。你知道吗

相关问题 更多 >