Skip to content

Commit

Permalink
Test session requires
Browse files Browse the repository at this point in the history
  • Loading branch information
gschaffner authored and henryiii committed Sep 9, 2024
1 parent 170d995 commit 9d05989
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nox/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def format_dependency(self, dependency: str) -> str:
msg = "Cannot parametrize requires with {python} when python is None or a bool."
raise ValueError(msg)
return formatted
raise TypeError(
raise TypeError( # pragma: no cover
"The requires of a not-yet-parametrized session cannot be parametrized."
)

Expand Down
128 changes: 128 additions & 0 deletions tests/resources/noxfile_requires.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright 2022 Alethea Katherine Flowers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import nox


@nox.session(requires=["c", "b"])
def a(session):
print(session.name)


@nox.session()
def b(session):
print(session.name)


@nox.session()
def c(session):
print(session.name)


@nox.session(requires=["e"])
def d(session):
print(session.name)


@nox.session(requires=["c"])
def e(session):
print(session.name)


@nox.session(requires=["b", "g"])
def f(session):
print(session.name)


@nox.session(requires=["b", "h"])
def g(session):
print(session.name)


@nox.session(requires=["c"])
def h(session):
print(session.name)


#


@nox.session(requires=["j"])
def i(session):
print(session.name)


@nox.session(requires=["i"])
def j(session):
print(session.name)


#


@nox.session(python=["3.9", "3.10"])
def k(session):
print(session.name)


@nox.session(requires=["k"])
def m(session):
print(session.name)


@nox.session(python="3.10", requires=["k-{python}"])
def n(session):
print(session.name)


#


@nox.session(requires=["does_not_exist"])
def o(session):
print(session.name)


#


@nox.session(python=["3.9", "3.10"])
def p(session):
print(session.name)


@nox.session(python=None, requires=["p-{python}"])
def q(session):
print(session.name)


#


@nox.session
def r(session):
print(session.name)
raise Exception("Fail!")


@nox.session(requires=["r"])
def s(session):
print(session.name)


@nox.session(requires=["r"])
def t(session):
print(session.name)
50 changes: 50 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,56 @@ def test_main_with_bad_session_names(run_nox, session):
assert session in stderr


@pytest.mark.parametrize(
("sessions", "expected_order"),
[
(("g", "a", "d"), ("b", "c", "h", "g", "a", "e", "d")),
(("m",), ("k-3.9", "k-3.10", "m")),
(("n",), ("k-3.10", "n")),
],
)
def test_main_requires(run_nox, sessions, expected_order):
noxfile = os.path.join(RESOURCES, "noxfile_requires.py")
returncode, stdout, _ = run_nox(f"--noxfile={noxfile}", "--sessions", *sessions)
assert returncode == 0
assert tuple(stdout.rstrip("\n").split("\n")) == expected_order


def test_main_requires_cycle(run_nox):
noxfile = os.path.join(RESOURCES, "noxfile_requires.py")
returncode, _, stderr = run_nox(f"--noxfile={noxfile}", "--session=i")
assert returncode != 0
# While the exact cycle reported is not unique and is an implementation detail, this
# still serves as a regression test for unexpected changes in the implementation's
# behavior.
assert "Sessions are in a dependency cycle: i -> j -> i" in stderr


def test_main_requires_missing_session(run_nox):
noxfile = os.path.join(RESOURCES, "noxfile_requires.py")
returncode, _, stderr = run_nox(f"--noxfile={noxfile}", "--session=o")
assert returncode != 0
assert "Session not found: does_not_exist" in stderr


def test_main_requires_bad_python_parametrization(run_nox):
noxfile = os.path.join(RESOURCES, "noxfile_requires.py")
with pytest.raises(
ValueError,
match="Cannot parametrize requires",
):
returncode, _, _ = run_nox(f"--noxfile={noxfile}", "--session=q")
assert returncode != 0


@pytest.mark.parametrize("session", ("s", "t"))
def test_main_requires_chain_fail(run_nox, session):
noxfile = os.path.join(RESOURCES, "noxfile_requires.py")
returncode, _, stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}")
assert returncode != 0
assert "Prerequisite session r was not successful" in stderr


def test_main_noxfile_options(monkeypatch, generate_noxfile_options):
noxfile_path = generate_noxfile_options(reuse_existing_virtualenvs=True)
monkeypatch.setattr(
Expand Down

0 comments on commit 9d05989

Please sign in to comment.