| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2009 Google Inc. All rights reserved. | |
| 2 # | |
| 3 # Redistribution and use in source and binary forms, with or without | |
| 4 # modification, are permitted provided that the following conditions are | |
| 5 # met: | |
| 6 # | |
| 7 # * Redistributions of source code must retain the above copyright | |
| 8 # notice, this list of conditions and the following disclaimer. | |
| 9 # * Redistributions in binary form must reproduce the above | |
| 10 # copyright notice, this list of conditions and the following disclaimer | |
| 11 # in the documentation and/or other materials provided with the | |
| 12 # distribution. | |
| 13 # * Neither the name of Google Inc. nor the names of its | |
| 14 # contributors may be used to endorse or promote products derived from | |
| 15 # this software without specific prior written permission. | |
| 16 # | |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 28 | |
| 29 import unittest | |
| 30 | |
| 31 from optparse import make_option | |
| 32 | |
| 33 from webkitpy.common.system.outputcapture import OutputCapture | |
| 34 from webkitpy.tool.multi_command_tool import MultiCommandTool | |
| 35 from webkitpy.tool.commands.command import Command | |
| 36 | |
| 37 | |
| 38 class TrivialCommand(Command): | |
| 39 name = "trivial" | |
| 40 help_text = "help text" | |
| 41 long_help = "trivial command long help text" | |
| 42 show_in_main_help = True | |
| 43 | |
| 44 def __init__(self, **kwargs): | |
| 45 super(TrivialCommand, self).__init__(**kwargs) | |
| 46 | |
| 47 def execute(self, options, args, tool): | |
| 48 pass | |
| 49 | |
| 50 | |
| 51 class UncommonCommand(TrivialCommand): | |
| 52 name = "uncommon" | |
| 53 show_in_main_help = False | |
| 54 | |
| 55 | |
| 56 class TrivialTool(MultiCommandTool): | |
| 57 | |
| 58 def __init__(self, commands): | |
| 59 MultiCommandTool.__init__(self, commands) | |
| 60 | |
| 61 def name(self): | |
| 62 return 'trivial-tool' | |
| 63 | |
| 64 def path(self): | |
| 65 return __file__ | |
| 66 | |
| 67 def should_execute_command(self, command): | |
| 68 return (True, None) | |
| 69 | |
| 70 | |
| 71 class MultiCommandToolTest(unittest.TestCase): | |
| 72 | |
| 73 def _assert_split(self, args, expected_split): | |
| 74 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), e
xpected_split) | |
| 75 | |
| 76 def test_split_args(self): | |
| 77 # MultiCommandToolTest._split_command_name_from_args returns: (command,
args) | |
| 78 full_args = ["--global-option", "command", "--option", "arg"] | |
| 79 full_args_expected = ("command", ["--global-option", "--option", "arg"]) | |
| 80 self._assert_split(full_args, full_args_expected) | |
| 81 | |
| 82 full_args = [] | |
| 83 full_args_expected = (None, []) | |
| 84 self._assert_split(full_args, full_args_expected) | |
| 85 | |
| 86 full_args = ["command", "arg"] | |
| 87 full_args_expected = ("command", ["arg"]) | |
| 88 self._assert_split(full_args, full_args_expected) | |
| 89 | |
| 90 def test_command_by_name(self): | |
| 91 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) | |
| 92 self.assertEqual(tool.command_by_name("trivial").name, "trivial") | |
| 93 self.assertIsNone(tool.command_by_name("bar")) | |
| 94 | |
| 95 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expect
ed_stderr="", expected_exit_code=0): | |
| 96 exit_code = OutputCapture().assert_outputs( | |
| 97 self, tool.main, [main_args], expected_stdout=expected_stdout, expec
ted_stderr=expected_stderr) | |
| 98 self.assertEqual(exit_code, expected_exit_code) | |
| 99 | |
| 100 def test_global_help(self): | |
| 101 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) | |
| 102 expected_common_commands_help = """Usage: trivial-tool [options] COMMAND
[ARGS] | |
| 103 | |
| 104 Options: | |
| 105 -h, --help show this help message and exit | |
| 106 | |
| 107 Common trivial-tool commands: | |
| 108 trivial help text | |
| 109 | |
| 110 See 'trivial-tool help --all-commands' to list all commands. | |
| 111 See 'trivial-tool help COMMAND' for more information on a specific command. | |
| 112 | |
| 113 """ | |
| 114 self._assert_tool_main_outputs(tool, ["tool"], expected_common_commands_
help) | |
| 115 self._assert_tool_main_outputs(tool, ["tool", "help"], expected_common_c
ommands_help) | |
| 116 expected_all_commands_help = """Usage: trivial-tool [options] COMMAND [A
RGS] | |
| 117 | |
| 118 Options: | |
| 119 -h, --help show this help message and exit | |
| 120 | |
| 121 All trivial-tool commands: | |
| 122 help Display information about this program or its subcommands | |
| 123 trivial help text | |
| 124 uncommon help text | |
| 125 | |
| 126 See 'trivial-tool help --all-commands' to list all commands. | |
| 127 See 'trivial-tool help COMMAND' for more information on a specific command. | |
| 128 | |
| 129 """ | |
| 130 self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"],
expected_all_commands_help) | |
| 131 # Test that arguments can be passed before commands as well | |
| 132 self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"],
expected_all_commands_help) | |
| 133 | |
| 134 def test_command_help(self): | |
| 135 command_with_options = TrivialCommand(options=[make_option("--my_option"
)]) | |
| 136 tool = TrivialTool(commands=[command_with_options]) | |
| 137 expected_subcommand_help = """trivial [options] help text | |
| 138 | |
| 139 trivial command long help text | |
| 140 | |
| 141 Options: | |
| 142 --my_option=MY_OPTION | |
| 143 | |
| 144 """ | |
| 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 |