Fix formatting issues reported by check-webkit-style
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / multicommandtool_unittest.py
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 sys
30 import unittest
31
32 from optparse import make_option
33
34 from webkitpy.common.system.outputcapture import OutputCapture
35 from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain
36
37
38 class TrivialCommand(Command):
39     name = "trivial"
40     show_in_main_help = True
41     help_text = "help text"
42
43     def __init__(self, **kwargs):
44         Command.__init__(self, **kwargs)
45
46     def execute(self, options, args, tool):
47         pass
48
49
50 class UncommonCommand(TrivialCommand):
51     name = "uncommon"
52     show_in_main_help = False
53
54
55 class LikesToRetry(Command):
56     name = "likes-to-retry"
57     show_in_main_help = True
58     help_text = "help text"
59
60     def __init__(self, **kwargs):
61         Command.__init__(self, **kwargs)
62         self.execute_count = 0
63
64     def execute(self, options, args, tool):
65         self.execute_count += 1
66         if self.execute_count < 2:
67             raise TryAgain()
68
69
70 class CommandTest(unittest.TestCase):
71     def test_name_with_arguments(self):
72         TrivialCommand.argument_names = "ARG1 ARG2"
73         command_with_args = TrivialCommand()
74         self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
75
76         TrivialCommand.argument_names = None
77         command_with_args = TrivialCommand(options=[make_option("--my_option")])
78         self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
79
80     def test_parse_required_arguments(self):
81         self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1", "ARG2"])
82         self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), [])
83         self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG2"])
84         # Note: We might make our arg parsing smarter in the future and allow this type of arguments string.
85         self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 ARG2]")
86
87     def test_required_arguments(self):
88         TrivialCommand.argument_names = "ARG1 ARG2 [ARG3]"
89         two_required_arguments = TrivialCommand()
90         expected_logs = "2 arguments required, 1 argument provided.  Provided: 'foo'  Required: ARG1 ARG2\nSee 'trivial-tool help trivial' for usage.\n"
91         exit_code = OutputCapture().assert_outputs(self, two_required_arguments.check_arguments_and_execute, [None, ["foo"], TrivialTool()], expected_logs=expected_logs)
92         self.assertEqual(exit_code, 1)
93         TrivialCommand.argument_names = None
94
95
96 class TrivialTool(MultiCommandTool):
97     def __init__(self, commands=None):
98         MultiCommandTool.__init__(self, name="trivial-tool", commands=commands)
99
100     def path(self):
101         return __file__
102
103     def should_execute_command(self, command):
104         return (True, None)
105
106
107 class MultiCommandToolTest(unittest.TestCase):
108     def _assert_split(self, args, expected_split):
109         self.assertEqual(MultiCommandTool._split_command_name_from_args(args), expected_split)
110
111     def test_split_args(self):
112         # MultiCommandToolTest._split_command_name_from_args returns: (command, args)
113         full_args = ["--global-option", "command", "--option", "arg"]
114         full_args_expected = ("command", ["--global-option", "--option", "arg"])
115         self._assert_split(full_args, full_args_expected)
116
117         full_args = []
118         full_args_expected = (None, [])
119         self._assert_split(full_args, full_args_expected)
120
121         full_args = ["command", "arg"]
122         full_args_expected = ("command", ["arg"])
123         self._assert_split(full_args, full_args_expected)
124
125     def test_command_by_name(self):
126         # This also tests Command auto-discovery.
127         tool = TrivialTool()
128         self.assertEqual(tool.command_by_name("trivial").name, "trivial")
129         self.assertEqual(tool.command_by_name("bar"), None)
130
131     def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expected_stderr="", expected_exit_code=0):
132         exit_code = OutputCapture().assert_outputs(self, tool.main, [main_args], expected_stdout=expected_stdout, expected_stderr=expected_stderr)
133         self.assertEqual(exit_code, expected_exit_code)
134
135     def test_retry(self):
136         likes_to_retry = LikesToRetry()
137         tool = TrivialTool(commands=[likes_to_retry])
138         tool.main(["tool", "likes-to-retry"])
139         self.assertEqual(likes_to_retry.execute_count, 2)
140
141     def test_global_help(self):
142         tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()])
143         expected_common_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
144
145 Options:
146   -h, --help  show this help message and exit
147
148 Common trivial-tool commands:
149    trivial   help text
150
151 See 'trivial-tool help --all-commands' to list all commands.
152 See 'trivial-tool help COMMAND' for more information on a specific command.
153
154 """
155         self._assert_tool_main_outputs(tool, ["tool"], expected_common_commands_help)
156         self._assert_tool_main_outputs(tool, ["tool", "help"], expected_common_commands_help)
157         expected_all_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
158
159 Options:
160   -h, --help  show this help message and exit
161
162 All trivial-tool commands:
163    help       Display information about this program or its subcommands
164    trivial    help text
165    uncommon   help text
166
167 See 'trivial-tool help --all-commands' to list all commands.
168 See 'trivial-tool help COMMAND' for more information on a specific command.
169
170 """
171         self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"], expected_all_commands_help)
172         # Test that arguments can be passed before commands as well
173         self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"], expected_all_commands_help)
174
175     def test_command_help(self):
176         TrivialCommand.long_help = "LONG HELP"
177         command_with_options = TrivialCommand(options=[make_option("--my_option")])
178         tool = TrivialTool(commands=[command_with_options])
179         expected_subcommand_help = "trivial [options]   help text\n\nLONG HELP\n\nOptions:\n  --my_option=MY_OPTION\n\n"
180         self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expected_subcommand_help)