Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rez config --json FIELD #1064

Merged
Merged
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: 8 additions & 2 deletions src/rez/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
'''
from __future__ import print_function

import json


def setup_parser(parser, completions=False):
parser.add_argument(
"--json", dest="json", action="store_true",
help="Output dict/list field values as JSON. Useful for setting "
"REZ_*_JSON environment variables. Ignored if FIELD not given")
parser.add_argument(
"--search-list", dest="search_list", action="store_true",
help="list the config files searched")
Expand Down Expand Up @@ -46,8 +52,8 @@ def command(opts, parser, extra_arg_groups=None):
raise ValueError("no such setting: %r" % opts.FIELD)

if isinstance(data, (dict, list)):
txt = dump_yaml(data).strip()
print(txt)
txt = json.dumps(data) if opts.json else dump_yaml(data)
print(txt.strip())
else:
print(data)

Expand Down
49 changes: 48 additions & 1 deletion src/rez/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from rez.system import system
from rez.utils.data_utils import RO_AttrDictWrapper
from rez.packages import get_developer_package
from rez.vendor.six import six
import os
import os.path
import subprocess


class TestConfig(TestBase):
Expand Down Expand Up @@ -237,8 +239,53 @@ def test_7(self):

self.assertEqual(c.packages_path, packages_path)

def test_8(self):
"""Test CLI dict/list value JSON round trip."""
import json
import rez.cli._main

if __name__ == '__main__':
cli_file = rez.cli._main.__file__

# python /path/to/rez/cli/_main.py config ...
config_args = [os.sys.executable, cli_file, "config"]

c = Config([self.root_config_file], locked=True)
env = {
key: value
for key, value in os.environ.items()
if not key.startswith("REZ_")
}

test_configs = {
"packages_path": ["/foo bar/baz", "/foo bar/baz hey", "/home/foo bar/baz"],
"platform_map": {"foo": {"bar": "baz"}},
}
for config_key, test_value in test_configs.items():
try:
# Test fetching default value
stdout = subprocess.check_output(
config_args + ["--json", config_key],
env=env,
)
self.assertEqual(
six.ensure_str(stdout).strip(),
six.ensure_str(json.dumps(getattr(c, config_key))),
)

# Test setting via env var and fetching custom value
test_json_value = six.ensure_str(json.dumps(test_value))
env["REZ_%s_JSON" % config_key.upper()] = test_json_value
stdout = subprocess.check_output(
config_args + ["--json", config_key],
env=env,
)
self.assertEqual(stdout.decode().strip(), test_json_value)
except subprocess.CalledProcessError as error:
print(error.stdout)
raise


if __name__ == "__main__":
unittest.main()


Expand Down