From 08cc13299413a6398e52ebc5ab5316265a111080 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 26 Oct 2024 00:41:11 -0400 Subject: [PATCH] tests: add test for modern parametrize Signed-off-by: Henry Schreiner --- docs/tutorial.rst | 35 +++++++++++++++++++++++++++++++++++ nox/_resolver.py | 3 +-- tests/test_main.py | 9 +++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 51dfe916..82e61880 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -386,6 +386,41 @@ You can also pass the notified session positional arguments: Note that this will only have the desired effect if selecting sessions to run via the ``--session/-s`` flag. If you simply run ``nox``, all selected sessions will be run. +Requiring sessions +------------------ + +You can also request sessions be run before your session runs. This is done with the ``needs=`` keyword: + + +.. code-block:: python + + @nox.session + def tests(session): + session.install("pytest") + session.run("pytest") + + @nox.session(needs=["tests"]) + def coverage(session): + session.install("coverage") + session.run("coverage") + +The required sessions will be stably topologically sorted and run. Parametrized +sessions are supported. You can also get the current Python version with +``{python}``, though arbitrary parametrizations are not supported. + + +.. code-block:: python + + @nox.session(python=["3.10", "3.13"]) + def tests(session): + session.install("pytest") + session.run("pytest") + + @nox.session(python=["3.10", "3.13"], needs=["tests-{python}"]) + def coverage(session): + session.install("coverage") + session.run("coverage") + Testing against different and multiple Pythons ---------------------------------------------- diff --git a/nox/_resolver.py b/nox/_resolver.py index e903cd35..c5c2a8e5 100644 --- a/nox/_resolver.py +++ b/nox/_resolver.py @@ -192,8 +192,7 @@ def extend_walk( walk_list = list(walk) cycle = walk_list[walk_list.index(node) :] + [node] raise CycleError("Nodes are in a dependency cycle", tuple(cycle)) - else: - walk[node] = None + walk[node] = None return walk sort = prepended_by_dependencies(root) diff --git a/tests/test_main.py b/tests/test_main.py index 47efe8de..cacafdb6 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -495,6 +495,8 @@ def test_main_with_bad_session_names(run_nox, session): (("g", "a", "d"), ("b", "c", "h", "g", "a", "e", "d")), (("m",), ("k-3.9", "k-3.10", "m")), (("n",), ("k-3.10", "n")), + (("v",), ("u(django='1.9')", "u(django='2.0')", "v")), + (("w",), ("u(django='1.9')", "u(django='2.0')", "w")), ], ) def test_main_requires(run_nox, sessions, expected_order): @@ -539,6 +541,13 @@ def test_main_requires_chain_fail(run_nox, session): assert "Prerequisite session r was not successful" in stderr +@pytest.mark.parametrize("session", ("w", "u")) +def test_main_requries_modern_param(run_nox, session): + noxfile = os.path.join(RESOURCES, "noxfile_requires.py") + returncode, _, stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}") + assert returncode == 0 + + def test_main_noxfile_options(monkeypatch, generate_noxfile_options): noxfile_path = generate_noxfile_options(reuse_existing_virtualenvs=True) monkeypatch.setattr(