From 00b44b18b59c784dc8c61469cb2d33de2dd237f9 Mon Sep 17 00:00:00 2001 From: Claudio Jolowicz Date: Thu, 3 Jun 2021 17:11:49 +0200 Subject: [PATCH 1/4] Add Noxfile for testing normalized session names --- tests/resources/noxfile_normalization.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/resources/noxfile_normalization.py diff --git a/tests/resources/noxfile_normalization.py b/tests/resources/noxfile_normalization.py new file mode 100644 index 00000000..d1a07fe7 --- /dev/null +++ b/tests/resources/noxfile_normalization.py @@ -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 From b9f002bad37ca0c42d7d5f9ac57c5ca914f893e8 Mon Sep 17 00:00:00 2001 From: Claudio Jolowicz Date: Thu, 3 Jun 2021 17:11:27 +0200 Subject: [PATCH 2/4] Add tests for normalized session names --- tests/test_main.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index 35395819..df652695 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -370,6 +370,77 @@ 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=)", + "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 + + def test_main_noxfile_options(monkeypatch): monkeypatch.setattr( sys, From f9cf9fa07d26f081a9c452cf4f6e8c6eddbd52eb Mon Sep 17 00:00:00 2001 From: Claudio Jolowicz Date: Thu, 3 Jun 2021 17:13:19 +0200 Subject: [PATCH 3/4] Add failing test for regression when session not found --- tests/test_main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_main.py b/tests/test_main.py index df652695..9f41ac5d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -439,6 +439,7 @@ 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): From 88bcec36b5548a744775d5673582d6f13e11973d Mon Sep 17 00:00:00 2001 From: Claudio Jolowicz Date: Thu, 3 Jun 2021 17:25:48 +0200 Subject: [PATCH 4/4] Fix garbled error message when session not found --- nox/manifest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nox/manifest.py b/nox/manifest.py index 8265de2d..dbc3304e 100644 --- a/nox/manifest.py +++ b/nox/manifest.py @@ -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)))