Skip to content

Commit

Permalink
tests: add test for json output
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Mar 4, 2023
1 parent f79ce9c commit 3d1809c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ To list all available sessions, including parametrized sessions:
nox --list
nox --list-sessions
If you'd like to use the output in later processing, you can add ``--json`` to
get json output for the selected session. Fields include ``session`` (pretty
name), ``name``, ``description``, ``python`` (null if not specified), ``tags``,
and ``call_spec`` (for parametrized sessions).


.. _session_execution_order:

Expand Down
11 changes: 6 additions & 5 deletions nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,12 @@ def filter_manifest(manifest: Manifest, global_config: Namespace) -> Manifest |
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 <session name>.\n")
_produce_listing(manifest, global_config)
return 0

# JSON output requires list sessions also be specified
if global_config.json and not global_config.list_sessions:
logger.error("Must specify --list-sessions with --json")
return 3

# Filter by python interpreter versions.
if global_config.pythons:
manifest.filter_by_python_interpreter(global_config.pythons)
Expand Down Expand Up @@ -300,6 +296,11 @@ def honor_list_request(manifest: Manifest, global_config: Namespace) -> Manifest
if not (global_config.list_sessions or global_config.json):
return manifest

# JSON output requires list sessions also be specified
if global_config.json and not global_config.list_sessions:
logger.error("Must specify --list-sessions with --json")
return 3

if global_config.json:
_produce_json_listing(manifest, global_config)
else:
Expand Down
58 changes: 58 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,64 @@ def test_honor_list_request_doesnt_print_docstring_if_not_present(capsys):
assert "Hello I'm a docstring" not in out


def test_honor_list_json_request(capsys):
config = _options.options.namespace(
list_sessions=True, noxfile="noxfile.py", json=True
)
manifest = mock.create_autospec(Manifest)
manifest.list_all_sessions.return_value = [
(
argparse.Namespace(
name="bar",
friendly_name="foo",
description="simple",
func=argparse.Namespace(python="123"),
tags=[],
),
True,
),
(
argparse.Namespace(),
False,
),
]
return_value = tasks.honor_list_request(manifest, global_config=config)
assert return_value == 0
assert json.loads(capsys.readouterr().out) == [
{
"session": "foo",
"name": "bar",
"description": "simple",
"python": "123",
"tags": [],
"call_spec": {},
}
]


def test_refuse_json_nolist_request(caplog):
config = _options.options.namespace(
list_sessions=False, noxfile="noxfile.py", json=True
)
manifest = mock.create_autospec(Manifest)
manifest.list_all_sessions.return_value = [
(
argparse.Namespace(
name="bar",
friendly_name="foo",
description="simple",
func=argparse.Namespace(python="123"),
tags=[],
),
True,
)
]
return_value = tasks.honor_list_request(manifest, global_config=config)
assert return_value == 3
(record,) = caplog.records
assert record.message == "Must specify --list-sessions with --json"


def test_empty_session_list_in_noxfile(capsys):
config = _options.options.namespace(noxfile="noxfile.py", sessions=(), posargs=[])
manifest = Manifest({"session": session_func}, config)
Expand Down

0 comments on commit 3d1809c

Please sign in to comment.