我如何才能使诗歌和诗歌协同工作以支持测试Python依赖项的多个版本? [英] How can I get tox and poetry to work together to support testing multiple versions of a Python dependency?

查看:87
本文介绍了我如何才能使诗歌和诗歌协同工作以支持测试Python依赖项的多个版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个当前使用 pipenv 的项目转换为诗歌的项目,以测试它们之间的区别。该项目是一个简单的可重新分发的Django应用。它支持Python 3.6-8,Django 2.2和3.0。我有一个 tox.ini 文件,该文件涵盖了Python和Django的所有组合,因此:

I am switching a project that currently uses pipenv to poetry as a test to see what the differences are. The project is a simple, redistributable, Django app. It supports Python 3.6-8, and Django 2.2 and 3.0. I have a tox.ini file that covers all combinations of Python and Django thus:

[tox]
envlist = py{36,37,38}-django{22,30}

[testenv]
whitelist_externals = poetry
skip_install = true

deps =
    django22: Django==2.2
    django30: Django==3.0

commands =
    poetry install -vvv
    poetry run pytest --cov=my_app tests/
    poetry run coverage report -m

我遇到的问题(在 pipenv 世界中不存在)是诗歌安装语句将始终用 poetry.lock 文件中的内容覆盖 deps 部分中的内容(自动生成(如果不存在)。这意味着测试矩阵将永远不会针对Django 2.2进行测试-默认情况下,每个tox virtualenv都会安装Django 3.0。

The problem that I am having (which does not exist in the pipenv world) is that the poetry install statement will always overwrite whatever is in the deps section with whatever is in the poetry.lock file (which will be auto-generated if it does not exist). This means that the test matrix will never test against Django 2.2 - as each tox virtualenv gets Django 3.0 installed by default.

我不知道这应该如何工作-使用诗歌安装依赖项是否应该尊重安装它的现有环境?

I don't understand how this is supposed to work - should installing dependencies using poetry respect the existing environment into which it is being installed, or not?

所以-我的问题是-如何建立以诗歌为依存关系管理器的多版本tox(或travis)测试矩阵?

So - my question is - how do I set up a multi-version tox (or travis) test matrix, with poetry as the dependency manager?

我的 pyproject.toml 将Python / Django版本定义为:

My pyproject.toml defines Python / Django versions as:

[tool.poetry.dependencies]
python = "^3.6"
django = "^2.2 || ^3.0"

生成的 poetry.lock 文件(未提交)具有以下Django版本信息:

The generated poetry.lock file (not committed) has this Django version information:

[[package]]
category = "main"
description = "A high-level Python Web framework that encourages rapid development and clean, pragmatic design."
name = "django"
optional = false
python-versions = ">=3.6"
version = "3.0"






更新:包括干净的Tox输出


UPDATE: include clean tox output

这是删除锁定文件并重新创建Tox环境时的结果。如您所见,tox将 Django == 2.2 作为依赖项安装在virtualenv中,但是 poetry 然后将其更新为 3.0 安装时。

This is the result when I delete the lock file, and recreate the tox environment. As you can see, tox installs Django==2.2 as a dependency in the virtualenv, but poetry then updates this to 3.0 when it installs.

我需要一种运行诗歌安装的解决方案,同时尊重现有软件包的安装。例如,如果 pyproject.toml 声明 Django = ^ 2.2 || ^ 3.0 ,并且已经安装了2.2,则固定到该版本-不要尝试升级。

I need a solution that runs the poetry install, respecting existing package installs. i.e. if pyproject.toml states Django = "^2.2 || ^3.0", and 2.2 is already installed, then pin to that version - don't attempt to upgrade.

my-app$ tox -r -e py36-django22
py36-django22 recreate: .tox/py36-django22
py36-django22 installdeps: Django==2.2
py36-django22 installed: Django==2.2,my-app==0.1.0,pytz==2019.3,sqlparse==0.3.0
py36-django22 run-test: commands[0] | poetry install -vvv
Using virtualenv: .tox/py36-django22
Updating dependencies
Resolving dependencies...
   1: derived: django (^2.2 || ^3.0)
   ...
PyPI: 10 packages found for django >=2.2,<4.0
   ...
   1: Version solving took 3.330 seconds.
   1: Tried 1 solutions.

Writing lock file

Package operations: 52 installs, 1 update, 0 removals, 3 skipped

  - ...
  - Updating django (2.2 -> 3.0)
  - ...

UPDATE 2

按照以下sinoroc的说明进行操作-我已经更新了tox文件,删除了 skip_dist 并包含了 isolated_build 。这行得通。 tox会编译并安装软件包-但仅是非dev版本,其中不包括 pytest coverage 以及稍后将要包含的大量整理工具。即我想通过毒理进行处理的工具在诗歌中被指定为开发依赖性。这里有一个解决方案,将所有这些都包含在tox文件中-但这似乎是自欺欺人的-因为那时我的诗歌和tox都声明了依赖关系。

Following instructions from sinoroc below - I have updated the tox file to remove skip_dist and include isolated_build. This works, sort of. tox builds the package, and installs it - but only the non-dev version, which does not include pytest, coverage and a host of linting tools that I'd like to include at a later point. i.e. the tools I want to run through tox are specified as dev-dependencies in poetry. There is a solution here, to include all of these inside the tox file - but that seems self-defeating - as then I have poetry and tox both declaring dependencies.

[tool.poetry.dependencies]
python = "^3.6"
django = "^2.2 || ^3.0"

[tool.poetry.dev-dependencies]
pytest = "^3.0"
pytest-cov = "^2.8"
pytest-django = "^3.7"
coverage = "^4.5"
pylint = "^2.4"
pylint-django = "^2.0"
flake8 = "^3.7"
flake8-bandit = "^2.1"
flake8-docstrings = "^1.5"
isort = "^4.3"
mypy = "^0.750.0"
pre-commit = "^1.20"
black = "=19.3b0"






更新3:解决方案


UPDATE 3: solution

[tox]
isolated_build = True
envlist = lint, mypy, py{36,37,38}-django{22,30}

[travis]
python =
    3.6: lint, mypy, py36
    3.7: lint, mypy, py37
    3.8: lint, mypy, py38

[testenv]
deps =
    pytest
    pytest-cov
    pytest-django
    coverage
    django22: Django==2.2
    django30: Django==3.0

commands =
    django-admin --version
    pytest --cov=my_app tests/

[testenv:lint]
deps =
    pylint
    pylint-django
    flake8
    flake8-bandit
    flake8-docstrings
    isort
    black

commands =
    isort --recursive my_app
    black my_app
    pylint my_app
    flake8 my_app

[testenv:mypy]
deps =
    mypy

commands =
    mypy my_app


推荐答案

还没有对其进行全面测试,但我相信这样的方法应该可以工作:

Haven't thoroughly tested it, but I believe something like this should work:

[tox]
envlist = py{36,37,38}-django{22,30}
isolated_build = True

[testenv]
deps =
    django22: Django==2.2
    django30: Django==3.0
    # plus the dev dependencies
    pytest
    coverage

commands =
    pytest --cov=my_app tests/
    coverage report -m

请参阅" 诗歌" 包装部分中的 tox 文档的第二章。

See the "poetry" section in the "packaging" chapter of the tox documentation.

为了避免重复 dev 依赖项,您可以根据 extras 尝试以下变体功能

In order to avoid the repetition of the dev dependencies, one could try the following variation based on the extras feature:

tox.ini

[tox]
# ...

[testenv]
# ...
deps =
    django22: Django==2.2
    django30: Django==3.0
extras =
    test

pyproject.toml

[tool.poetry]
# ...

[tool.poetry.dependencies]
python = "^3.6"
django = "^2.2 || ^3.0"
#
pytest = { version = "^5.2", optional = true }

[tool.poetry.extras]
test = ["pytest"]

[build-system]
# ...

这篇关于我如何才能使诗歌和诗歌协同工作以支持测试Python依赖项的多个版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆