Diary 2019-04-18

By Max Woerner Chase

Following some extended iterations, I got fed up with the flow of nox recreating the virtual environments every time, and Poetry doing a build for every environment, so I tried a bunch of things until I found something that seemed to work. I'm not sure I can recommend the result, precisely, but I figured I'd explain what I did.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import shutil

import nox

nox.options.reuse_existing_virtualenvs = True


BUILD_LOCATION = None


def install(session, extras=None):
    if BUILD_LOCATION is None:
        session.error("Need to run build first")
    install_string = BUILD_LOCATION
    if extras is not None:
        install_string += f"[{extras}]"
    session.install(install_string)


@nox.session
def build(session):
    shutil.rmtree("dist", ignore_errors=True)
    session.run("poetry", "build", "-vvv", external=True)
    global BUILD_LOCATION
    for filename in os.listdir("dist"):
        if filename.endswith(".whl"):
            BUILD_LOCATION = os.path.join("dist", filename)


@nox.session
def clean(session):
    install(session, "coverage_only")
    session.run("coverage", "erase")

This is the beginning of my noxfile. I start off by importing some stdlib modules into the noxfile, which isn't a thing you can't do, but I don't know if you're not supposed to. Then I import nox as normal, and set the reuse option to true because making the virtualenvs every time was pretty slow. Then, I define a global variable that gets written to inside one of the sessions, which is, again, something that I can't tell whether it's a bad idea, but I can tell that it's possible and easy. I then define a helper function to require it to be set before installing both the wheel and any extras. Next, the session that has to come first, the session that does a release build of the library, which turns out to be pretty quick on its own. It then scans the filesystem to determine what to tell pip to install later. Then, one of the sessions that now rely on the wheel. Installing the wheel is fast, I think because it doesn't need to hit the build system. Doing it like this should be fine, I think.

Anyway, the point of all this is, now running nox (in a virtualenv because Poetry seems to interact oddly with nox's virtualenvs if I don't; not sure what's going on there) gets me results as I watch, instead of leaving me hanging for what feels like minutes. Now that I've souped all this up, I can get back to implementing addition.

Hm. Something weird. I got my coverage all the way to full, and tried to do a refactor, and somehow this seems to have removed a random bit of branch coverage.

(Time passes)

Well, I recovered the coverage, even if I'm not sure why I lost it.

Next up, multiplication. I am... not exactly looking forward to formulating tests for this stuff. Good night.