From b880bbf24d8b1c547c562119a0530a699bd3e209 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Mon, 27 Dec 2021 10:14:28 -0500 Subject: [PATCH] refactor: simpler impl --- nox/tasks.py | 23 +++++++---------------- tests/test_tasks.py | 10 +++++----- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/nox/tasks.py b/nox/tasks.py index 49f3e78f..a33f3138 100644 --- a/nox/tasks.py +++ b/nox/tasks.py @@ -174,14 +174,19 @@ def filter_manifest( # 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: + # log this if it happens. The sessions does not come from the noxfile + # if keywords is not empty. + if global_config.sessions is not None: try: manifest.filter_by_name(global_config.sessions) except KeyError as exc: logger.error("Error while collecting sessions.") logger.error(exc.args[0]) return 3 + if not manifest and not global_config.list_sessions: + print("No sessions selected. Please select a session with -s .\n") + _produce_listing(manifest, global_config) + return 0 # Filter by python interpreter versions. if global_config.pythons: @@ -208,20 +213,6 @@ def filter_manifest( logger.error("No sessions selected after filtering by keyword.") return 3 - # If the global_config is set to an empty list, and a list was not - # requested, and no filtering was done, - if ( - global_config.sessions is not None - and not global_config.sessions - and not global_config.list_sessions - and not global_config.keywords - and not global_config.pythons - ): - manifest.filter_by_name(global_config.sessions) - print("No sessions selected. Please select a session with -s .\n") - _produce_listing(manifest, global_config) - return 0 - # Return the modified manifest. return manifest diff --git a/tests/test_tasks.py b/tests/test_tasks.py index afc79f9b..50f7f86b 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -228,7 +228,7 @@ def test_filter_manifest_not_found(): def test_filter_manifest_pythons(): config = _options.options.namespace( - sessions=(), pythons=("3.8",), keywords=(), posargs=[] + sessions=None, pythons=("3.8",), keywords=(), posargs=[] ) manifest = Manifest( {"foo": session_func_with_python, "bar": session_func, "baz": session_func}, @@ -241,7 +241,7 @@ def test_filter_manifest_pythons(): def test_filter_manifest_pythons_not_found(caplog): config = _options.options.namespace( - sessions=(), pythons=("1.2",), keywords=(), posargs=[] + sessions=None, pythons=("1.2",), keywords=(), posargs=[] ) manifest = Manifest( {"foo": session_func_with_python, "bar": session_func, "baz": session_func}, @@ -254,7 +254,7 @@ def test_filter_manifest_pythons_not_found(caplog): def test_filter_manifest_keywords(): config = _options.options.namespace( - sessions=(), pythons=(), keywords="foo or bar", posargs=[] + sessions=None, pythons=(), keywords="foo or bar", posargs=[] ) manifest = Manifest( {"foo": session_func, "bar": session_func, "baz": session_func}, config @@ -266,7 +266,7 @@ def test_filter_manifest_keywords(): def test_filter_manifest_keywords_not_found(caplog): config = _options.options.namespace( - sessions=(), pythons=(), keywords="mouse or python", posargs=[] + sessions=None, pythons=(), keywords="mouse or python", posargs=[] ) manifest = Manifest( {"foo": session_func, "bar": session_func, "baz": session_func}, config @@ -278,7 +278,7 @@ def test_filter_manifest_keywords_not_found(caplog): def test_filter_manifest_keywords_syntax_error(): config = _options.options.namespace( - sessions=(), pythons=(), keywords="foo:bar", posargs=[] + sessions=None, pythons=(), keywords="foo:bar", posargs=[] ) manifest = Manifest({"foo_bar": session_func, "foo_baz": session_func}, config) return_value = tasks.filter_manifest(manifest, config)