Skip to content

Commit

Permalink
Fix garbled error message when session not found (#434)
Browse files Browse the repository at this point in the history
* Add Noxfile for testing normalized session names

* Add tests for normalized session names

* Add failing test for regression when session not found

* Fix garbled error message when session not found
  • Loading branch information
cjolowicz authored Jun 3, 2021
1 parent 56c7d56 commit 4ddd055
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
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

0 comments on commit 4ddd055

Please sign in to comment.