From f9f5fb116401e7f5204e9c9c0b03c70d4fddc63d Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Sun, 26 Aug 2018 02:21:56 -0500 Subject: [PATCH 1/6] Check for ``NOXSESSION`` env var --- nox/tasks.py | 11 +++++++++-- tests/test_tasks.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nox/tasks.py b/nox/tasks.py index 40dbb4d0..8e121491 100644 --- a/nox/tasks.py +++ b/nox/tasks.py @@ -75,6 +75,8 @@ def discover_manifest(module, global_config): def filter_manifest(manifest, global_config): """Filter the manifest according to the provided configuration. + The global configuration takes precedence over the ``NOXSESSION`` env var. + Args: manifest (~.Manifest): The manifest of sessions to be run. global_config (~nox.main.GlobalConfig): The global configuration. @@ -84,12 +86,17 @@ def filter_manifest(manifest, global_config): the manifest otherwise (to be sent to the next task). """ + + nox_env = os.environ.get("NOXSESSION") + env_sessions = nox_env.split(",") if nox_env else [] + sessions = global_config.sessions or env_sessions + # Filter by the name of any explicit sessions. # This can raise KeyError if a specified session does not exist; # log this if it happens. - if global_config.sessions: + if sessions: try: - manifest.filter_by_name(global_config.sessions) + manifest.filter_by_name(sessions) except KeyError as exc: logger.error("Error while collecting sessions.") logger.error(exc.args[0]) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 3f0c7bdf..4e9dbd45 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -99,6 +99,22 @@ def test_filter_manifest_keywords(): assert len(manifest) == 2 +@pytest.mark.parametrize( + "env,sessions", [("foo", ["foo"]), ("foo,bar", ["foo", "bar"])] +) +def test_filter_manifest_nox_env(env, sessions): + config = argparse.Namespace(sessions=(), keywords=()) + os.environ["NOXSESSION"] = env + manifest = Manifest( + {"foo": session_func, "bar": session_func, "foobar": session_func}, config + ) + return_value = tasks.filter_manifest(manifest, config) + assert return_value is manifest + assert len(manifest) == len(sessions) + for session in sessions: + assert session in manifest + + def test_honor_list_request_noop(): config = argparse.Namespace(list_sessions=False) manifest = {"thing": mock.sentinel.THING} From 7cf3f63c5b9bad01c89134f3009dc5b0b5ccd47a Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Sun, 26 Aug 2018 02:22:44 -0500 Subject: [PATCH 2/6] Docs --- docs/usage.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/usage.rst b/docs/usage.rst index a4054fdc..5160b09b 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -56,6 +56,11 @@ By default nox will run all sessions defined in the noxfile. However, you can ch nox -s lint tests nox -e lint +You can also use the ``NOXSESSION`` environment variable:: + + NOXSESSION=lint nox + NOXSESSION=lint,tests nox + Nox will run these sessions in the same order they are specified. You can also use `pytest-style keywords`_ to filter test sessions:: From e90ed2f0f63139eb2369de871b99698c8b3adb79 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 29 Aug 2018 00:08:40 -0500 Subject: [PATCH 3/6] Check env var in argparser --- nox/__main__.py | 7 +++++++ nox/tasks.py | 10 ++-------- tests/test_main.py | 23 +++++++++++++++++++++++ tests/test_tasks.py | 16 ---------------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/nox/__main__.py b/nox/__main__.py index 1362774d..37578b83 100644 --- a/nox/__main__.py +++ b/nox/__main__.py @@ -68,6 +68,7 @@ def main(): "-e", "--sessions", nargs="*", + default=_get_default_sessions(), help="Which sessions to run, by default, all sessions will run.", ) parser.add_argument( @@ -136,5 +137,11 @@ def main(): sys.exit(exit_code) +def _get_default_sessions(): + nox_env = os.environ.get("NOXSESSION") + env_sessions = nox_env.split(",") if nox_env else None + return env_sessions + + if __name__ == "__main__": # pragma: no cover main() diff --git a/nox/tasks.py b/nox/tasks.py index 8e121491..013fb7ee 100644 --- a/nox/tasks.py +++ b/nox/tasks.py @@ -75,8 +75,6 @@ def discover_manifest(module, global_config): def filter_manifest(manifest, global_config): """Filter the manifest according to the provided configuration. - The global configuration takes precedence over the ``NOXSESSION`` env var. - Args: manifest (~.Manifest): The manifest of sessions to be run. global_config (~nox.main.GlobalConfig): The global configuration. @@ -87,16 +85,12 @@ def filter_manifest(manifest, global_config): """ - nox_env = os.environ.get("NOXSESSION") - env_sessions = nox_env.split(",") if nox_env else [] - sessions = global_config.sessions or env_sessions - # Filter by the name of any explicit sessions. # This can raise KeyError if a specified session does not exist; # log this if it happens. - if sessions: + if global_config.sessions: try: - manifest.filter_by_name(sessions) + manifest.filter_by_name(global_config.sessions) except KeyError as exc: logger.error("Error while collecting sessions.") logger.error(exc.args[0]) diff --git a/tests/test_main.py b/tests/test_main.py index 598ee30f..77d42bfc 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -19,6 +19,7 @@ import contexter import pkg_resources +import pytest import nox import nox.__main__ @@ -146,6 +147,28 @@ def test_main_explicit_sessions(): assert config.sessions == ["1", "2"] +@pytest.mark.parametrize( + "env,sessions", [("foo", ["foo"]), ("foo,bar", ["foo", "bar"])] +) +def test_main_session_from_nox_env_var(env, sessions): + os.environ["NOXSESSION"] = env + sys.argv = [sys.executable] + with mock.patch("nox.workflow.execute") as execute: + execute.return_value = 0 + + # Call the main function. + with mock.patch.object(sys, "exit") as exit: + nox.__main__.main() + exit.assert_called_once_with(0) + assert execute.called + + # Verify that the sessions from the env var are listed in the config. + config = execute.call_args[1]["global_config"] + assert len(config.sessions) == len(sessions) + for session in sessions: + assert session in config.sessions + + def test_main_positional_args(): sys.argv = [sys.executable, "1", "2", "3"] with mock.patch("nox.workflow.execute") as execute: diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 4e9dbd45..3f0c7bdf 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -99,22 +99,6 @@ def test_filter_manifest_keywords(): assert len(manifest) == 2 -@pytest.mark.parametrize( - "env,sessions", [("foo", ["foo"]), ("foo,bar", ["foo", "bar"])] -) -def test_filter_manifest_nox_env(env, sessions): - config = argparse.Namespace(sessions=(), keywords=()) - os.environ["NOXSESSION"] = env - manifest = Manifest( - {"foo": session_func, "bar": session_func, "foobar": session_func}, config - ) - return_value = tasks.filter_manifest(manifest, config) - assert return_value is manifest - assert len(manifest) == len(sessions) - for session in sessions: - assert session in manifest - - def test_honor_list_request_noop(): config = argparse.Namespace(list_sessions=False) manifest = {"thing": mock.sentinel.THING} From 87ed8211c44e72f1130911ea3793dfbdeed41c5a Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 29 Aug 2018 00:52:10 -0500 Subject: [PATCH 4/6] Whitespace --- nox/tasks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nox/tasks.py b/nox/tasks.py index 013fb7ee..40dbb4d0 100644 --- a/nox/tasks.py +++ b/nox/tasks.py @@ -84,7 +84,6 @@ def filter_manifest(manifest, global_config): the manifest otherwise (to be sent to the next task). """ - # Filter by the name of any explicit sessions. # This can raise KeyError if a specified session does not exist; # log this if it happens. From 39fe3a7c0b38f8b945880ecb6ea02f4534015adf Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 29 Aug 2018 12:56:07 -0500 Subject: [PATCH 5/6] Fix test --- tests/test_main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index 77d42bfc..1fb9e2ce 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -61,6 +61,8 @@ def test_global_config_constructor(): def test_main_no_args(): + # Prevents any interference from outside + os.environ.pop("NOXSESSION", None) sys.argv = [sys.executable] with mock.patch("nox.workflow.execute") as execute: execute.return_value = 0 From 35ff95eb8cf958e190b4e3a09f35b6ac3789bda1 Mon Sep 17 00:00:00 2001 From: Thea Flowers Date: Fri, 14 Sep 2018 12:51:06 -0700 Subject: [PATCH 6/6] Use monkeypatch --- tests/test_main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 1fb9e2ce..97081d28 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -60,9 +60,9 @@ def test_global_config_constructor(): assert config.posargs == ["a", "b", "c"] -def test_main_no_args(): +def test_main_no_args(monkeypatch): # Prevents any interference from outside - os.environ.pop("NOXSESSION", None) + monkeypatch.delenv("NOXSESSION", raising=False) sys.argv = [sys.executable] with mock.patch("nox.workflow.execute") as execute: execute.return_value = 0 @@ -152,8 +152,8 @@ def test_main_explicit_sessions(): @pytest.mark.parametrize( "env,sessions", [("foo", ["foo"]), ("foo,bar", ["foo", "bar"])] ) -def test_main_session_from_nox_env_var(env, sessions): - os.environ["NOXSESSION"] = env +def test_main_session_from_nox_env_var(monkeypatch, env, sessions): + monkeypatch.setenv("NOXSESSION", env) sys.argv = [sys.executable] with mock.patch("nox.workflow.execute") as execute: execute.return_value = 0