Skip to content

Commit

Permalink
Allow --force-python on unparametrized sessions (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysle authored Feb 20, 2024
1 parent a6cfa2e commit a75578a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ def _session_completer(
),
_option_set.Option(
"force_pythons",
"-P",
"--force-pythons",
"--force-python",
group=options.groups["python"],
Expand All @@ -441,6 +442,7 @@ def _session_completer(
help=(
"Run sessions with the given interpreters instead of those listed in the"
" Noxfile. This is a shorthand for ``--python=X.Y --extra-python=X.Y``."
" It will also work on sessions that don't have any interpreter parametrized."
),
finalizer_func=_force_pythons_finalizer,
),
Expand Down
4 changes: 4 additions & 0 deletions nox/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ def make_session(
# Otherwise, add the extra specified python.
assert isinstance(func.python, str)
func.python = _unique_list(func.python, *extra_pythons)
elif not func.python and self._config.force_pythons:
# If a python is forced by the user, but the underlying function
# has no version parametrised, add it as sole occupant to func.python
func.python = _unique_list(*extra_pythons)

# If the func has the python attribute set to a list, we'll need
# to expand them.
Expand Down
36 changes: 36 additions & 0 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def create_mock_config():
cfg.force_venv_backend = None
cfg.default_venv_backend = None
cfg.extra_pythons = None
cfg.force_pythons = None
cfg.posargs = []
return cfg

Expand Down Expand Up @@ -287,6 +288,41 @@ def session_func():
assert expected == [session.func.python for session in manifest._all_sessions]


@pytest.mark.parametrize(
"python,force_pythons,expected",
[
(None, [], [None]),
(None, ["3.8"], ["3.8"]),
(None, ["3.8", "3.9"], ["3.8", "3.9"]),
(False, [], [False]),
(False, ["3.8"], ["3.8"]),
(False, ["3.8", "3.9"], ["3.8", "3.9"]),
("3.5", [], ["3.5"]),
("3.5", ["3.8"], ["3.5", "3.8"]),
("3.5", ["3.8", "3.9"], ["3.5", "3.8", "3.9"]),
(["3.5", "3.9"], [], ["3.5", "3.9"]),
(["3.5", "3.9"], ["3.8"], ["3.5", "3.9", "3.8"]),
(["3.5", "3.9"], ["3.8", "3.4"], ["3.5", "3.9", "3.8", "3.4"]),
(["3.5", "3.9"], ["3.5", "3.9"], ["3.5", "3.9"]),
],
)
def test_force_pythons(python, force_pythons, expected):
cfg = create_mock_config()
cfg.force_pythons = force_pythons
cfg.extra_pythons = force_pythons

manifest = Manifest({}, cfg)

def session_func():
pass

func = Func(session_func, python=python)
for session in manifest.make_session("my_session", func):
manifest.add_session(session)

assert expected == [session.func.python for session in manifest._all_sessions]


def test_add_session_parametrized():
manifest = Manifest({}, create_mock_config())

Expand Down

0 comments on commit a75578a

Please sign in to comment.