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

Provide a handy CLI function and/or method on Func objects, to list all session names for a given Func, for easier Github Actions integration #407

Closed
smarie opened this issue Mar 22, 2021 · 6 comments

Comments

@smarie
Copy link
Contributor

smarie commented Mar 22, 2021

Let's consider the following prototypical noxfile.py :

import nox

@nox.session(python=["3.7", "3.8"])
def tests(session):
    print("Interesting stuff going on here")

@nox.session(python=["3.7", "3.8"])
@nox.parametrize("a", [-1, 1])
@nox.parametrize("b", [False, True])
def tests_with_params(session, a, b):
    print("Interesting stuff going on here")

@nox.session(python="3.7")
def other(session):
    print("another session")

When integrating such a nox-based project into github actions, the basic way to do it is to list the sessions:

# (in .github/workflows/base.yml)
jobs:
  run_all_tests:
    strategy:
      fail-fast: false
      matrix:
        # Here we manually list two nox sessions
        nox_session: ["tests-3.7", "tests-3.8"]
    name: Run nox session ${{ matrix.nox_session }}
    runs-on: ubuntu-latest
    steps:
      # (other steps before this: checkout code, install python and nox...)
      - run: nox -s "${{ matrix.nox_session }}"

It would be handy to not have to redefine the list of test sessions in the github actions build matrix.

I found a way to solve this issue, and have the GHA build workflow dynamically create a build matrix from nox sessions : see this post.

However it would be even better to not have to create a dedicated session for this. In other words if

> nox --list <base_session_name>

was a valid CLI command, the example demonstrated in the above stackoverflow post would be simplified greatly.

As a companion, I think that providing a method on the Func object to list all generated session names could also be handy.

Note that all of this may need to evolve slightly when #404 is merged

@FollowTheProcess
Copy link
Collaborator

I may me missing something but could you not just do something similar to nox's own ci.yml i.e:

# noxfile.py
@nox.session(python=ALL_SUPPORTED_VERSIONS_HERE)
def test(session: nox.Session) -> None:
   """
   Runs all the tests for all python versions.
   """
   <some_good_stuff>
   ...
# CI.yml
name: CI

on: [push, pull_request]

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest]
        python-version: ["3.8", "3.9", "3.10.0-rc.2"]

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install Dependencies
        run: |
          python3 -m pip install --upgrade pip setuptools wheel
          python3 -m pip install nox

      - name: Run Tests and Coverage
        run: nox --non-interactive --session "test-${{ matrix.python-version }}"

Here we've not had to specify explicit nox session, albeit we have had to specify supported python versions in both places, but there's an open issue suggesting parsing of python_requires to set this, which would be great! #384

@smarie
Copy link
Contributor Author

smarie commented Sep 16, 2021

@FollowTheProcess : sure we can do as you suggest, but it only works for a single session, non-parametrized, and you have to put all versions in the yaml.

My point is more general-purpose: consider a random project with a random number of sessions, possibly with various python versions and/or parameters. Plus, consider that only some sessions are relevant for gh actions.

See for example this project: https://github.com/smarie/python-pytest-cases/actions

@FollowTheProcess
Copy link
Collaborator

@FollowTheProcess : sure we can do as you suggest, but it only works for a single session, non-parametrized, and you have to put all versions in the yaml.

My point is more general-purpose: consider a random project with a random number of sessions, possibly with various python versions and/or parameters. Plus, consider that only some sessions are relevant for gh actions.

See for example this project: https://github.com/smarie/python-pytest-cases/actions

Ahh yeah I get it now! I was imagining a simpler case. I'm on board with this idea 👍🏻

@Wirg
Copy link

Wirg commented Jan 2, 2023

I might have implemented something that could be easily added to nox and solve this issue before running into it.
It use a "hack" on the base nox's workflow and directly takes the sessions from the manifest.
What do you think about it ? @FollowTheProcess
See : https://github.com/Wirg/stqdm/blob/main/commands/list_nox_manifest.py#L83-L136'

Usage :

$ python commands/list_nox_manifest.py --sessions tests
> ["tests(python='3.7', streamlit_version='~=0.66', tqdm_version='~=4.50')", "tests(python='3.8', streamlit_version='~=0.66', tqdm_version='~=4.50')"]
$ python commands/list_nox_manifest.py --return-base-sessions
> ["isort", "lint", "coverage", "tests", "black"]
$ python commands/list_nox_manifest.py --list-python-versions
> ["3.10", "3.7", "3.8", "3.9"]

EDIT : seems related to #658

@FollowTheProcess
Copy link
Collaborator

FollowTheProcess commented Jan 7, 2023

It looks like this use case would now be addressed by #658 (PR #665)?

@henryiii
Copy link
Collaborator

henryiii commented Apr 6, 2024

Should be addressed by json output, I think!

@henryiii henryiii closed this as completed Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants