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

Decouple merging of --python with nox.options from --sessions and --keywords #359

Merged
merged 6 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@
)


def _session_filters_merge_func(
def _sessions_and_keywords_merge_func(
key: str, command_args: argparse.Namespace, noxfile_args: argparse.Namespace
) -> List[str]:
"""Only return the Noxfile value for sessions/pythons/keywords if neither sessions,
pythons or keywords are specified on the command-line.
"""Only return the Noxfile value for sessions/keywords if neither sessions
or keywords are specified on the command-line.

Args:
key (str): This function is used for the "sessions", "pythons" and "keywords"
key (str): This function is used for both the "sessions" and "keywords"
options, this allows using ``funtools.partial`` to pass the
same function for both options.
command_args (_option_set.Namespace): The options specified on the
command-line.
noxfile_args (_option_set.Namespace): The options specified in the
noxfile_Args (_option_set.Namespace): The options specified in the
Noxfile."""
if not any((command_args.sessions, command_args.pythons, command_args.keywords)):
if not command_args.sessions and not command_args.keywords:
return getattr(noxfile_args, key)
return getattr(command_args, key)

Expand Down Expand Up @@ -221,7 +221,7 @@ def _session_completer(
"--session",
group=options.groups["primary"],
noxfile=True,
merge_func=functools.partial(_session_filters_merge_func, "sessions"),
merge_func=functools.partial(_sessions_and_keywords_merge_func, "sessions"),
nargs="*",
default=_sessions_default,
help="Which sessions to run. By default, all sessions will run.",
Expand All @@ -234,7 +234,6 @@ def _session_completer(
"--python",
group=options.groups["primary"],
noxfile=True,
merge_func=functools.partial(_session_filters_merge_func, "pythons"),
nargs="*",
help="Only run sessions that use the given python interpreter versions.",
),
Expand All @@ -244,7 +243,7 @@ def _session_completer(
"--keywords",
group=options.groups["primary"],
noxfile=True,
merge_func=functools.partial(_session_filters_merge_func, "keywords"),
merge_func=functools.partial(_sessions_and_keywords_merge_func, "keywords"),
help="Only run sessions that match the given expression.",
),
_option_set.Option(
Expand Down
28 changes: 28 additions & 0 deletions tests/resources/noxfile_options_pythons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 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.

import nox

nox.options.sessions = ["{default_session}"]
nox.options.pythons = ["{default_python}"]


@nox.session(python=["{default_python}", "{alternate_python}"])
def test(session):
pass


@nox.session(python=["{default_python}", "{alternate_python}"])
def launch_rocket(session):
pass
83 changes: 83 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import sys
from pathlib import Path
from unittest import mock

import contexter
Expand Down Expand Up @@ -444,6 +445,88 @@ def test_main_noxfile_options_sessions(monkeypatch):
assert config.sessions == ["test"]


@pytest.fixture
def generate_noxfile_options_pythons(tmp_path):
"""Generate noxfile.py with test and launch_rocket sessions.

The sessions are defined for both the default and alternate Python versions.
The ``default_session`` and ``default_python`` parameters determine what
goes into ``nox.options.sessions`` and ``nox.options.pythons``, respectively.
"""

def generate_noxfile(default_session, default_python, alternate_python):
path = Path(RESOURCES) / "noxfile_options_pythons.py"
text = path.read_text()
text = text.format(
default_session=default_session,
default_python=default_python,
alternate_python=alternate_python,
)
path = tmp_path / "noxfile.py"
path.write_text(text)
return str(path)

return generate_noxfile


python_current_version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
python_next_version = "{}.{}".format(sys.version_info.major, sys.version_info.minor + 1)


def test_main_noxfile_options_with_pythons_override(
capsys, monkeypatch, generate_noxfile_options_pythons
):
noxfile = generate_noxfile_options_pythons(
default_session="test",
default_python=python_next_version,
alternate_python=python_current_version,
)

monkeypatch.setattr(
sys, "argv", ["nox", "--noxfile", noxfile, "--python", python_current_version]
)

with mock.patch("sys.exit") as sys_exit:
nox.__main__.main()
_, stderr = capsys.readouterr()
sys_exit.assert_called_once_with(0)

for python_version in [python_current_version, python_next_version]:
for session in ["test", "launch_rocket"]:
line = "Running session {}-{}".format(session, python_version)
if session == "test" and python_version == python_current_version:
assert line in stderr
else:
assert line not in stderr


def test_main_noxfile_options_with_sessions_override(
capsys, monkeypatch, generate_noxfile_options_pythons
):
noxfile = generate_noxfile_options_pythons(
default_session="test",
default_python=python_current_version,
alternate_python=python_next_version,
)

monkeypatch.setattr(
sys, "argv", ["nox", "--noxfile", noxfile, "--session", "launch_rocket"]
)

with mock.patch("sys.exit") as sys_exit:
nox.__main__.main()
_, stderr = capsys.readouterr()
sys_exit.assert_called_once_with(0)

for python_version in [python_current_version, python_next_version]:
for session in ["test", "launch_rocket"]:
line = "Running session {}-{}".format(session, python_version)
if session == "launch_rocket" and python_version == python_current_version:
assert line in stderr
else:
assert line not in stderr


@pytest.mark.parametrize(("isatty_value", "expected"), [(True, True), (False, False)])
def test_main_color_from_isatty(monkeypatch, isatty_value, expected):
monkeypatch.setattr(sys, "argv", [sys.executable])
Expand Down