[Reports] Fix tracebacks from rally.ui.utils run via CLI
This also makes CLI returning exit code to shell Change-Id: I9f43fe25719ec59546d17d8553bbc2ac78085830 Closes-bug: #1510537
This commit is contained in:
parent
355bb27044
commit
0cc576c06a
@ -19,6 +19,13 @@ import re
|
||||
import sys
|
||||
|
||||
|
||||
HELP_MESSAGE = (
|
||||
"Usage:\n\t"
|
||||
"utils.py render <lookup/path/to/template.mako>"
|
||||
"[<key-1>=<value-1> <key-2>=<value-2> ...]\n\n\t"
|
||||
"Where key-1,value-1 and key-2,value-2 are key pairs of template.")
|
||||
|
||||
|
||||
def get_template(template_path):
|
||||
import mako.lookup
|
||||
|
||||
@ -30,23 +37,26 @@ def get_template(template_path):
|
||||
]
|
||||
|
||||
lookup = mako.lookup.TemplateLookup(directories=lookup_dirs)
|
||||
|
||||
return lookup.get_template(template_path)
|
||||
try:
|
||||
return lookup.get_template(template_path)
|
||||
except mako.exceptions.TopLevelLookupException as e:
|
||||
raise ValueError(e)
|
||||
|
||||
|
||||
def main(*args):
|
||||
if (len(args) < 2 or args[0] != "render"
|
||||
or not all(re.match("^[^=]+=[^=]+$", arg) for arg in args[2:])):
|
||||
raise ValueError(
|
||||
"Usage: \n\t"
|
||||
"utils.py render <lookup/path/to/template.mako> "
|
||||
"<key-1>=<value-1> <key-2>=<value-2>\n\n\t"
|
||||
"Where key-1,value-1 and key-2,value-2 are key pairs of template"
|
||||
)
|
||||
print(HELP_MESSAGE, file=sys.stderr)
|
||||
return 1
|
||||
|
||||
render_kwargs = dict([arg.split("=") for arg in args[2:]])
|
||||
print(get_template(args[1]).render(**render_kwargs))
|
||||
try:
|
||||
render_kwargs = dict([arg.split("=") for arg in args[2:]])
|
||||
print(get_template(args[1]).render(**render_kwargs))
|
||||
except ValueError as e:
|
||||
print(str(e), file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(*sys.argv[1:])
|
||||
sys.exit(main(*sys.argv[1:]))
|
||||
|
@ -20,23 +20,31 @@ from rally.ui import utils
|
||||
from tests.unit import test
|
||||
|
||||
|
||||
class PlotTestCase(test.TestCase):
|
||||
class ModuleTestCase(test.TestCase):
|
||||
|
||||
def test_get_template(self):
|
||||
self.assertIsInstance(utils.get_template("task/report.mako"),
|
||||
mako.template.Template)
|
||||
|
||||
def test_get_template_raises(self):
|
||||
self.assertRaises(ValueError, utils.get_template, "absent_template")
|
||||
|
||||
@mock.patch("rally.ui.utils.get_template")
|
||||
def test_main(self, mock_get_template):
|
||||
utils.main("render", "somepath", "a=1", "b=2")
|
||||
self.assertEqual(0, utils.main("render", "somepath", "a=1", "b=2"))
|
||||
|
||||
mock_get_template.assert_called_once_with("somepath")
|
||||
mock_get_template.return_value.render.assert_called_once_with(
|
||||
a="1", b="2"
|
||||
)
|
||||
|
||||
def test_main_bad_input(self):
|
||||
self.assertRaises(ValueError, utils.main)
|
||||
self.assertRaises(ValueError, utils.main, "not_a_render")
|
||||
self.assertRaises(ValueError, utils.main, "render")
|
||||
self.assertRaises(ValueError, utils.main, "render", "path", "a 1")
|
||||
@mock.patch("rally.ui.utils.print", create=True)
|
||||
@mock.patch("rally.ui.utils.sys.stderr")
|
||||
def test_main_bad_input(self, mock_stderr, mock_print):
|
||||
self.assertTrue(utils.HELP_MESSAGE.startswith("Usage:"))
|
||||
for args in ([], ["not_a_render"], ["render"],
|
||||
["render", "expected_arg", "unexpected_arg"]):
|
||||
self.assertEqual(1, utils.main(*args))
|
||||
mock_print.assert_called_once_with(utils.HELP_MESSAGE,
|
||||
file=mock_stderr)
|
||||
mock_print.reset_mock()
|
||||
|
Loading…
Reference in New Issue
Block a user