Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Nox with Poetry #347

Closed
danieleades opened this issue Sep 15, 2020 · 13 comments
Closed

Using Nox with Poetry #347

danieleades opened this issue Sep 15, 2020 · 13 comments

Comments

@danieleades
Copy link

cross-posted to python-poetry/poetry#2920

I've been trying to migrate an existing project to Poetry. It is using Tox to test a matrix of different package versions. The difficulties of trying to get Poetry and Tox to play nicely together for matrices of package versions is fairly well-documented (see https://stackoverflow.com/questions/59377071/how-can-i-get-tox-and-poetry-to-work-together-to-support-testing-multiple-versio)

I'm playing around with Nox as an alternative, and it's looking very promising for being easier to integrate with Poetry, but there are still a few caveats.

I have the following Nox script for testing against a couple of different python interpreters, and a couple of different versions of sphinx-

import nox

@nox.session(python = ["3.5", "3.6"]
@nox.parametrize("sphinx", ["^2.4", "^3.0"])
def tests(session, sphinx)
    session.run('poetry', 'add', f'sphinx@{sphinx}', external=True)
    session.run('poetry', 'install', external=True)
    session.run('pytest')

this works perfectly, except that it modifies the pyproject.toml file in my project's root. Is there a better way for me to arrange this?

@theacodes
Copy link
Collaborator

theacodes commented Sep 15, 2020 via email

@danieleades
Copy link
Author

the poetry add step is changing the version of the 'sphinx' package in the pyproject.toml file. Once the script exits, I then need to revert that change and regenerate the lockfile

for example if sphinx version is set to 3.0 in the pyproject.toml file, say, and I use "2.6" in my nox session, the file will have sphinx set to "2.6" when the nox script exits.

@danieleades
Copy link
Author

To be clear, I'm not suggesting that nox is misbehaving!
Im simply wondering if this workflow can be supported today, or if there is interest in implementing it (either on the nox side, or the Poetry side)

@theacodes
Copy link
Collaborator

theacodes commented Sep 15, 2020 via email

@danieleades
Copy link
Author

there are a couple of solutions proposed in python-poetry/poetry#2920

I wonder what a first class API would look like?

  • one solution is to clone the entire working directory to a temporary folder for each run. This is a nice general solution (though it's kind of the nuclear option). I could see Nox having a config flag to perform this step automagically
  • the second proposed solution is to cache and replace the pyproject.toml and poetry.lock files. This is lightweight and solves my problem, but it's very poetry-specific and won't help people that are, for whatever reason, modifying other project files during their sessions

@danieleades
Copy link
Author

@theacodes you can see the approach i'm attempting to use to restore the pyproject.toml and poetry.lock files after execution here - python-poetry/poetry#2920 (comment)

it's not working because wrapping nox in a try/finally block doesn't seem to work.

Is there anyway to cleanup code after a session with Nox? Effectively a 'teardown' function

@layday
Copy link
Contributor

layday commented Sep 20, 2020

it's not working because wrapping nox in a try/finally block doesn't seem to work.

Sure it works - dump gets called on import, before the tests run ;P You have to move the try block inside the session:

@nox.session(python=python_versions)
@nox.parametrize("sphinx", sphinx_versions)
def tests(session, sphinx):
    try:
        session.run('poetry', 'add', f'sphinx@{sphinx}', external=True)
        session.run('poetry', 'install', external=True)
        session.run('nosetests')
    finally:
        dump(project_file)
        dump(lockfile)

@danieleades
Copy link
Author

i'm afraid this still isn't working

given this code-

import nox

@nox.session(python = ["3.5", "3.6"]
@nox.parametrize("sphinx", ["^2.4", "^3.0"])
def tests(session, sphinx)
    session.run('poetry', 'add', f'sphinx@{sphinx}', external=True)
    session.run('poetry', 'install', external=True)
    session.run('pytest')

the python interpreter used is always the one selected by Poetry, in my case 3.8.
I can't use this setup for testing a matrix of interpreter versions.
any ideas?

@layday
Copy link
Contributor

layday commented Oct 5, 2020

Yes, you should install Poetry and pytest with Nox.

@cjolowicz
Copy link
Collaborator

Hi @danieleades would something like this work for you?

import nox

@nox.session(python=["3.5", "3.6"])
@nox.parametrize("sphinx", ["2.4.4", "3.2.1"])
def tests(session, sphinx):
    session.install(".", f"sphinx=={sphinx}", "pytest")
    session.run("pytest")

This will install and test your package with a specific sphinx version, using only the version constraints in the package metadata, not the lock file.

@danieleades
Copy link
Author

I did get this working in the end! With the original solution. I suspect it was working all along ...

@theacodes
Copy link
Collaborator

Is this still a bug? Should I keep this open? It doesn't seem like there's anything left to resolve here, so I'm going to close for now but I'm happy to re-open if there's still more work to do.

@danieleades
Copy link
Author

if anyone stumbles upon this, I've got a working implementation, integrated with a github action here - https://github.com/danieleades/sphinxcontrib-needs/actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants