1 # Copyright (c) 2009 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
32 from optparse import make_option
34 from webkitpy.common.system.outputcapture import OutputCapture
35 from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain
38 class TrivialCommand(Command):
40 show_in_main_help = True
41 def __init__(self, **kwargs):
42 Command.__init__(self, "help text", **kwargs)
44 def execute(self, options, args, tool):
48 class UncommonCommand(TrivialCommand):
50 show_in_main_help = False
53 class LikesToRetry(Command):
54 name = "likes-to-retry"
55 show_in_main_help = True
57 def __init__(self, **kwargs):
58 Command.__init__(self, "help text", **kwargs)
59 self.execute_count = 0
61 def execute(self, options, args, tool):
62 self.execute_count += 1
63 if self.execute_count < 2:
67 class CommandTest(unittest.TestCase):
68 def test_name_with_arguments(self):
69 command_with_args = TrivialCommand(argument_names="ARG1 ARG2")
70 self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
72 command_with_args = TrivialCommand(options=[make_option("--my_option")])
73 self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
75 def test_parse_required_arguments(self):
76 self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1", "ARG2"])
77 self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), [])
78 self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG2"])
79 # Note: We might make our arg parsing smarter in the future and allow this type of arguments string.
80 self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 ARG2]")
82 def test_required_arguments(self):
83 two_required_arguments = TrivialCommand(argument_names="ARG1 ARG2 [ARG3]")
84 expected_missing_args_error = "2 arguments required, 1 argument provided. Provided: 'foo' Required: ARG1 ARG2\nSee 'trivial-tool help trivial' for usage.\n"
85 exit_code = OutputCapture().assert_outputs(self, two_required_arguments.check_arguments_and_execute, [None, ["foo"], TrivialTool()], expected_stderr=expected_missing_args_error)
86 self.assertEqual(exit_code, 1)
89 class TrivialTool(MultiCommandTool):
90 def __init__(self, commands=None):
91 MultiCommandTool.__init__(self, name="trivial-tool", commands=commands)
96 def should_execute_command(self, command):
100 class MultiCommandToolTest(unittest.TestCase):
101 def _assert_split(self, args, expected_split):
102 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), expected_split)
104 def test_split_args(self):
105 # MultiCommandToolTest._split_command_name_from_args returns: (command, args)
106 full_args = ["--global-option", "command", "--option", "arg"]
107 full_args_expected = ("command", ["--global-option", "--option", "arg"])
108 self._assert_split(full_args, full_args_expected)
111 full_args_expected = (None, [])
112 self._assert_split(full_args, full_args_expected)
114 full_args = ["command", "arg"]
115 full_args_expected = ("command", ["arg"])
116 self._assert_split(full_args, full_args_expected)
118 def test_command_by_name(self):
119 # This also tests Command auto-discovery.
121 self.assertEqual(tool.command_by_name("trivial").name, "trivial")
122 self.assertEqual(tool.command_by_name("bar"), None)
124 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expected_stderr = "", expected_exit_code=0):
125 exit_code = OutputCapture().assert_outputs(self, tool.main, [main_args], expected_stdout=expected_stdout, expected_stderr=expected_stderr)
126 self.assertEqual(exit_code, expected_exit_code)
128 def test_retry(self):
129 likes_to_retry = LikesToRetry()
130 tool = TrivialTool(commands=[likes_to_retry])
131 tool.main(["tool", "likes-to-retry"])
132 self.assertEqual(likes_to_retry.execute_count, 2)
134 def test_global_help(self):
135 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()])
136 expected_common_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
139 -h, --help show this help message and exit
141 Common trivial-tool commands:
144 See 'trivial-tool help --all-commands' to list all commands.
145 See 'trivial-tool help COMMAND' for more information on a specific command.
148 self._assert_tool_main_outputs(tool, ["tool"], expected_common_commands_help)
149 self._assert_tool_main_outputs(tool, ["tool", "help"], expected_common_commands_help)
150 expected_all_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
153 -h, --help show this help message and exit
155 All trivial-tool commands:
156 help Display information about this program or its subcommands
160 See 'trivial-tool help --all-commands' to list all commands.
161 See 'trivial-tool help COMMAND' for more information on a specific command.
164 self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"], expected_all_commands_help)
165 # Test that arguments can be passed before commands as well
166 self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"], expected_all_commands_help)
169 def test_command_help(self):
170 command_with_options = TrivialCommand(options=[make_option("--my_option")], long_help="LONG HELP")
171 tool = TrivialTool(commands=[command_with_options])
172 expected_subcommand_help = "trivial [options] help text\n\nLONG HELP\n\nOptions:\n --my_option=MY_OPTION\n\n"
173 self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expected_subcommand_help)
176 if __name__ == "__main__":