Search code examples
pythonautomationtox

tox build artifact location


I just started using tox for testing my python project. I was able to configure build and test automation with tox, and I've been able to integrate that automation with a GitHub action to build and test my package when I push commits or a tag. In addition to having this GitHub action build and test, I would like the workflow to upload the python package artifact (sdist) that is built. My problem is: I can't find an easy way to extract the full path to the artifact. I am aware of the distdir config item and its default value of {toxworkdir}/dist. This value does not include the filename of the package itself. I am also aware of TOX_PACKAGE, but it looks like that package file is ephemeral and disappears once the tox run has finished. I'm also aware of the JSON output which includes the package filename that can be returned via the --result-json command line arg.

Right now the only way I can see to get the full path to the artifact is to assemble a string from the pieces I listed above. I don't like this approach because 1: tox already knows the location of the built artifact and it should be able to give it to me, 2: any code I write might break due to an edge case one day or an upstream change. I also don't want to write an explicit build step because tox is already doing the build for me; why should I duplicate functionality?

So my primary question is: can I get the full path to the built artifact from tox? A secondary question I have is: why doesn't anyone seem to use tox for this purpose? I've seen a lot of python modules on github that use tox to test but have some extra set of steps for creating releases. Why not release the artifact that was tested by your CI system? Thanks!


Solution

  • can I get the full path to the built artifact from tox?

    I am pretty sure you can't, at least not easily, but you usually do not need that path.

    Both for uploading a package to e.g. PyPI or using the mentioned GitHub action you usually do not use the full path, but either a path to a directory or even just a glob pattern.

    From the mentioned GitHub action:

    - uses: actions/upload-artifact@v3
      with:
        name: my-artifact
        path: path/to/artifact/ # or path/to/artifact
    

    and

    - uses: actions/upload-artifact@v3
      with:
        name: my-artifact
        path: path/**/[abc]rtifac?/*
    

    or an example for PyPI:

    $ twine upload -r pypi --sign dist/Flask*
    

    Your observation is correct - TOX_PACKAGE is ephemeral, see https://tox.wiki/en/latest/config.html#injected-environment-variables

    Why not release the artifact that was tested by your CI system?

    You could. See above.

    I usually prefer to use https://github.com/pypa/build as I usally want to create both the sdist and a wheel.

    P.S.: I am one of the maintainers of tox.