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

Fix garbled error message when session not found #434

Merged
merged 4 commits into from
Jun 3, 2021
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
7 changes: 4 additions & 3 deletions nox/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ def filter_by_name(self, specified_sessions: Iterable[str]) -> None:
),
)
)
normalized_specified_sessions = [
_normalize_arg(session_name) for session_name in specified_sessions
missing_sessions = [
session_name
for session_name in specified_sessions
if _normalize_arg(session_name) not in all_sessions
]
missing_sessions = set(normalized_specified_sessions) - all_sessions
if missing_sessions:
raise KeyError("Sessions not found: {}".format(", ".join(missing_sessions)))

Expand Down
16 changes: 16 additions & 0 deletions tests/resources/noxfile_normalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import datetime

import nox


class Foo:
pass


@nox.session
@nox.parametrize(
"arg",
["Jane", "Joe's", '"hello world"', datetime.datetime(1980, 1, 1), [42], Foo()],
)
def test(session, arg):
pass
72 changes: 72 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,78 @@ def test_main_session_with_names(capsys, monkeypatch):
sys_exit.assert_called_once_with(0)


@pytest.fixture
def run_nox(capsys, monkeypatch):
def _run_nox(*args):
monkeypatch.setattr(sys, "argv", ["nox", *args])

with mock.patch("sys.exit") as sys_exit:
nox.__main__.main()
stdout, stderr = capsys.readouterr()
returncode = sys_exit.call_args[0][0]

return returncode, stdout, stderr

return _run_nox


@pytest.mark.parametrize(
("normalized_name", "session"),
[
("test(arg='Jane')", "test(arg='Jane')"),
("test(arg='Jane')", 'test(arg="Jane")'),
("test(arg='Jane')", 'test(arg = "Jane")'),
("test(arg='Jane')", 'test ( arg = "Jane" )'),
('test(arg="Joe\'s")', 'test(arg="Joe\'s")'),
('test(arg="Joe\'s")', "test(arg='Joe\\'s')"),
("test(arg='\"hello world\"')", "test(arg='\"hello world\"')"),
("test(arg='\"hello world\"')", 'test(arg="\\"hello world\\"")'),
("test(arg=[42])", "test(arg=[42])"),
("test(arg=[42])", "test(arg=[42,])"),
("test(arg=[42])", "test(arg=[ 42 ])"),
("test(arg=[42])", "test(arg=[0x2a])"),
(
"test(arg=datetime.datetime(1980, 1, 1, 0, 0))",
"test(arg=datetime.datetime(1980, 1, 1, 0, 0))",
),
(
"test(arg=datetime.datetime(1980, 1, 1, 0, 0))",
"test(arg=datetime.datetime(1980,1,1,0,0))",
),
(
"test(arg=datetime.datetime(1980, 1, 1, 0, 0))",
"test(arg=datetime.datetime(1980, 1, 1, 0, 0x0))",
),
],
)
def test_main_with_normalized_session_names(run_nox, normalized_name, session):
noxfile = os.path.join(RESOURCES, "noxfile_normalization.py")
returncode, _, stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}")
assert returncode == 0
assert normalized_name in stderr


@pytest.mark.parametrize(
"session",
[
"syntax error",
"test(arg=Jane)",
"test(arg='Jane ')",
"_test(arg='Jane')",
"test(arg=42)",
"test(arg=[42.0])",
"test(arg=[43])",
"test(arg=<user_nox_module.Foo object at 0x123456789abc>)",
"test(arg=datetime.datetime(1980, 1, 1))",
],
)
def test_main_with_bad_session_names(run_nox, session):
noxfile = os.path.join(RESOURCES, "noxfile_normalization.py")
returncode, _, stderr = run_nox(f"--noxfile={noxfile}", f"--session={session}")
assert returncode != 0
assert session in stderr


def test_main_noxfile_options(monkeypatch):
monkeypatch.setattr(
sys,
Expand Down