7

我想在我用诗歌管理的项目中使用nox

不顺利的是在 nox 会话中安装 dev 依赖项。

我有noxfile.py如下所示:

import nox
from nox.sessions import Session
from pathlib import Path

__dir__ = Path(__file__).parent.absolute()


@nox.session(python=PYTHON)
def test(session: Session):
    session.install(str(__dir__))  # I want to use dev dependency here
    session.run("pytest")

如何在 nox 会话中安装 dev 依赖项?

4

3 回答 3

2

目前,session.install不支持poetryinstall只是在 shell 中运行 pip 。poetry您可以使用更通用的方法激活session.run

例子:

@nox.session(python=False)
def tests(session):
    session.run('poetry', 'shell')
    session.run('poetry', 'install')
    session.run('pytest')

当您设置会话时,您可以通过禁用创建 python virtualenv( python=False) 并poetry使用poetry shell.

于 2020-01-24T22:01:59.800 回答
2

经过一些试验和错误,与我在@Yann 的回答中评论的相反,似乎poetry忽略了VIRTUAL_ENV传递的变量nox.

受 Claudio Jolowicz 的精彩系列Hypermodern Python的启发,我解决了以下问题:

@nox.session(python=PYTHON)
def test(session: Session) -> None:
    """
    Run unit tests.

    Arguments:
        session: The Session object.
    """
    args = session.posargs or ["--cov"]
    session.install(".")
    install_with_constraints(
        session,
        "coverage[toml]",
        "pytest",
        "pytest-cov",
        "pytest-mock",
        "pytest-flask",
    )
    session.run("pytest", *args)

在这里,我只是pip用来安装一个 PEP517 包。

不幸的是,PEP517 通过pip不支持可编辑(“-e”)开关安装。

仅供参考:install_with_constraints是我从 Claudio 那里借来的功能,经过编辑后可以在 Windows 上运行:

def install_with_constraints(
    session: Session, *args: str, **kwargs: Any
) -> None:
    """
    Install packages constrained by Poetry's lock file.

    This function is a wrapper for nox.sessions.Session.install. It
    invokes pip to install packages inside of the session's virtualenv.
    Additionally, pip is passed a constraints file generated from
    Poetry's lock file, to ensure that the packages are pinned to the
    versions specified in poetry.lock. This allows you to manage the
    packages as Poetry development dependencies.

    Arguments:
        session: The Session object.
        args: Command-line arguments for pip.
        kwargs: Additional keyword arguments for Session.install.
    """
    req_path = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
    session.run(
        "poetry",
        "export",
        "--dev",
        "--format=requirements.txt",
        f"--output={req_path}",
        external=True,
    )
    session.install(f"--constraint={req_path}", *args, **kwargs)
    os.unlink(req_path)
于 2020-06-18T15:32:35.570 回答
0

我想在没有诗歌的情况下使用 nox 会话,所以我用诗歌来生成这样的论点session.install()

@nox.session
def tests(session: nox.sessions.Sessio0n):
    """ Run all tests """
    session.install(*requirements_txt, '.', 'pytest')
    session.run('pytest', 'tests/')


# Get requirements.txt from poetry
import tempfile, subprocess
with tempfile.NamedTemporaryFile('w+') as f:
    subprocess.run(f'poetry export --no-interaction --dev --format requirements.txt --without-hashes --output={f.name}', shell=True, check=True)
    f.seek(0)
    requirements_txt = f.readlines()
于 2021-09-05T00:32:35.423 回答