| OLD | NEW |
| 1 # Copyright (c) 2009 Google Inc. All rights reserved. | 1 # Copyright (c) 2009 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 import unittest | 29 import unittest |
| 30 | 30 |
| 31 from optparse import make_option | 31 from optparse import make_option |
| 32 | 32 |
| 33 from webkitpy.common.system.outputcapture import OutputCapture | 33 from webkitpy.common.system.outputcapture import OutputCapture |
| 34 from webkitpy.tool.multi_command_tool import MultiCommandTool | 34 from webkitpy.tool.multi_command_tool import MultiCommandTool |
| 35 from webkitpy.tool.commands import Command | 35 from webkitpy.tool.commands.command import Command |
| 36 | 36 |
| 37 | 37 |
| 38 class TrivialCommand(Command): | 38 class TrivialCommand(Command): |
| 39 name = "trivial" | 39 name = "trivial" |
| 40 help_text = "help text" | 40 help_text = "help text" |
| 41 long_help = "trivial command long help text" | 41 long_help = "trivial command long help text" |
| 42 show_in_main_help = True | 42 show_in_main_help = True |
| 43 | 43 |
| 44 def __init__(self, **kwargs): | 44 def __init__(self, **kwargs): |
| 45 super(TrivialCommand, self).__init__(**kwargs) | 45 super(TrivialCommand, self).__init__(**kwargs) |
| 46 | 46 |
| 47 def execute(self, options, args, tool): | 47 def execute(self, options, args, tool): |
| 48 pass | 48 pass |
| 49 | 49 |
| 50 | 50 |
| 51 class UncommonCommand(TrivialCommand): | 51 class UncommonCommand(TrivialCommand): |
| 52 name = "uncommon" | 52 name = "uncommon" |
| 53 show_in_main_help = False | 53 show_in_main_help = False |
| 54 | 54 |
| 55 | 55 |
| 56 class TrivialTool(MultiCommandTool): | 56 class TrivialTool(MultiCommandTool): |
| 57 | 57 |
| 58 def __init__(self, commands=None): | 58 def __init__(self, commands): |
| 59 MultiCommandTool.__init__(self, name="trivial-tool", commands=commands) | 59 MultiCommandTool.__init__(self, commands) |
| 60 |
| 61 def name(self): |
| 62 return 'trivial-tool' |
| 60 | 63 |
| 61 def path(self): | 64 def path(self): |
| 62 return __file__ | 65 return __file__ |
| 63 | 66 |
| 64 def should_execute_command(self, command): | 67 def should_execute_command(self, command): |
| 65 return (True, None) | 68 return (True, None) |
| 66 | 69 |
| 67 | 70 |
| 68 class MultiCommandToolTest(unittest.TestCase): | 71 class MultiCommandToolTest(unittest.TestCase): |
| 69 | 72 |
| 70 def _assert_split(self, args, expected_split): | 73 def _assert_split(self, args, expected_split): |
| 71 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), e
xpected_split) | 74 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), e
xpected_split) |
| 72 | 75 |
| 73 def test_split_args(self): | 76 def test_split_args(self): |
| 74 # MultiCommandToolTest._split_command_name_from_args returns: (command,
args) | 77 # MultiCommandToolTest._split_command_name_from_args returns: (command,
args) |
| 75 full_args = ["--global-option", "command", "--option", "arg"] | 78 full_args = ["--global-option", "command", "--option", "arg"] |
| 76 full_args_expected = ("command", ["--global-option", "--option", "arg"]) | 79 full_args_expected = ("command", ["--global-option", "--option", "arg"]) |
| 77 self._assert_split(full_args, full_args_expected) | 80 self._assert_split(full_args, full_args_expected) |
| 78 | 81 |
| 79 full_args = [] | 82 full_args = [] |
| 80 full_args_expected = (None, []) | 83 full_args_expected = (None, []) |
| 81 self._assert_split(full_args, full_args_expected) | 84 self._assert_split(full_args, full_args_expected) |
| 82 | 85 |
| 83 full_args = ["command", "arg"] | 86 full_args = ["command", "arg"] |
| 84 full_args_expected = ("command", ["arg"]) | 87 full_args_expected = ("command", ["arg"]) |
| 85 self._assert_split(full_args, full_args_expected) | 88 self._assert_split(full_args, full_args_expected) |
| 86 | 89 |
| 87 def test_command_by_name(self): | 90 def test_command_by_name(self): |
| 88 # This also tests Command auto-discovery. | 91 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) |
| 89 tool = TrivialTool() | |
| 90 self.assertEqual(tool.command_by_name("trivial").name, "trivial") | 92 self.assertEqual(tool.command_by_name("trivial").name, "trivial") |
| 91 self.assertIsNone(tool.command_by_name("bar")) | 93 self.assertIsNone(tool.command_by_name("bar")) |
| 92 | 94 |
| 93 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expect
ed_stderr="", expected_exit_code=0): | 95 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expect
ed_stderr="", expected_exit_code=0): |
| 94 exit_code = OutputCapture().assert_outputs( | 96 exit_code = OutputCapture().assert_outputs( |
| 95 self, tool.main, [main_args], expected_stdout=expected_stdout, expec
ted_stderr=expected_stderr) | 97 self, tool.main, [main_args], expected_stdout=expected_stdout, expec
ted_stderr=expected_stderr) |
| 96 self.assertEqual(exit_code, expected_exit_code) | 98 self.assertEqual(exit_code, expected_exit_code) |
| 97 | 99 |
| 98 def test_global_help(self): | 100 def test_global_help(self): |
| 99 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) | 101 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 123 | 125 |
| 124 See 'trivial-tool help --all-commands' to list all commands. | 126 See 'trivial-tool help --all-commands' to list all commands. |
| 125 See 'trivial-tool help COMMAND' for more information on a specific command. | 127 See 'trivial-tool help COMMAND' for more information on a specific command. |
| 126 | 128 |
| 127 """ | 129 """ |
| 128 self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"],
expected_all_commands_help) | 130 self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"],
expected_all_commands_help) |
| 129 # Test that arguments can be passed before commands as well | 131 # Test that arguments can be passed before commands as well |
| 130 self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"],
expected_all_commands_help) | 132 self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"],
expected_all_commands_help) |
| 131 | 133 |
| 132 def test_command_help(self): | 134 def test_command_help(self): |
| 133 command_with_options = TrivialCommand(options=[make_option("--my_option"
)], ) | 135 command_with_options = TrivialCommand(options=[make_option("--my_option"
)]) |
| 134 tool = TrivialTool(commands=[command_with_options]) | 136 tool = TrivialTool(commands=[command_with_options]) |
| 135 expected_subcommand_help = """trivial [options] help text | 137 expected_subcommand_help = """trivial [options] help text |
| 136 | 138 |
| 137 trivial command long help text | 139 trivial command long help text |
| 138 | 140 |
| 139 Options: | 141 Options: |
| 140 --my_option=MY_OPTION | 142 --my_option=MY_OPTION |
| 141 | 143 |
| 142 """ | 144 """ |
| 143 self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expect
ed_subcommand_help) | 145 self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expect
ed_subcommand_help) |
| 146 |
| 147 def test_constructor_calls_bind_to_tool(self): |
| 148 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) |
| 149 self.assertEqual(tool.commands[0]._tool, tool) |
| 150 self.assertEqual(tool.commands[1]._tool, tool) |
| OLD | NEW |