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 <[email protected]>
  • Loading branch information
henryiii committed Mar 4, 2023
1 parent f79ce9c commit ebad9ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
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
54 changes: 54 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,60 @@ 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,
)
]
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 ebad9ec

Please sign in to comment.