Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/pavilion/commands/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import errno
import pprint
import json

from pavilion import output
from pavilion import cmd_utils
Expand Down Expand Up @@ -53,6 +54,9 @@ def _setup_arguments(self, parser):
'-f', '--file', dest='files', action='append', default=[],
help='Add tests listed in the given file, as per the "pav run" command',
)
parser.add_argument(
'-j', '--json', action='store_true',
help='Output the resolved test config as JSON')
parser.add_argument(
'tests', action='store', nargs='*',
help='The name of the test to view. Should be in the format '
Expand Down Expand Up @@ -96,5 +100,9 @@ def run(self, pav_cfg, args):
return errno.EINVAL

configs = {pt.config['name']: pt.config for pt in proto_tests}
pprint.pprint(configs, stream=self.outfile) # ext-print: ignore

if args.json:
json.dump(configs, self.outfile, indent=2)
else:
pprint.pprint(configs, stream=self.outfile) # ext-print: ignore
return 0
30 changes: 30 additions & 0 deletions test/tests/view_cmd_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Exercise the view command."""
import json

from pavilion import arguments
from pavilion import commands
from pavilion.unittest import PavTestCase
Expand All @@ -18,3 +20,31 @@ def test_view(self):
])

self.assertEqual(view_cmd.run(self.pav_cfg, args), 0)

def test_view_json(self):
"""Test that the --json flag outputs valid JSON."""

view_cmd = commands.get_command('view')
view_cmd.silence()

arg_parser = arguments.get_parser()

args = arg_parser.parse_args([
'view', '--json', 'hello_world'
])

self.assertEqual(view_cmd.run(self.pav_cfg, args), 0)

# Verify output is valid JSON
output_str = view_cmd.outfile.getvalue()

try:
json_obj = json.loads(output_str)
except json.JSONDecodeError as e:
self.fail(f"Output is not valid JSON: {e}")

print(json_obj)

# Ensure it's a dict with expected structure (non-empty)
self.assertIsInstance(json_obj, dict)
self.assertTrue(json_obj != {})
Loading