From 0cc576c06a5ad2b72bce59bdb2c9fa97f72a28c0 Mon Sep 17 00:00:00 2001 From: Alexander Maretskiy Date: Tue, 27 Oct 2015 17:30:57 +0200 Subject: [PATCH] [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 --- rally/ui/utils.py | 32 +++++++++++++++++++++----------- tests/unit/ui/test_utils.py | 22 +++++++++++++++------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/rally/ui/utils.py b/rally/ui/utils.py index 94f55cf240..4858c77875 100644 --- a/rally/ui/utils.py +++ b/rally/ui/utils.py @@ -19,6 +19,13 @@ import re import sys +HELP_MESSAGE = ( + "Usage:\n\t" + "utils.py render " + "[= = ...]\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 " - "= =\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:])) diff --git a/tests/unit/ui/test_utils.py b/tests/unit/ui/test_utils.py index 69312bc618..12c95a2ee6 100644 --- a/tests/unit/ui/test_utils.py +++ b/tests/unit/ui/test_utils.py @@ -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()