Add log when experimental network loader is used
[WebKit-https.git] / Tools / Scripts / webkitpy / tool / multicommandtool_unittest.py
1 # Copyright (c) 2009 Google Inc. All rights reserved.
2 # Copyright (C) 2020 Apple Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import logging
31 import unittest
32
33 from optparse import make_option
34 from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain
35
36 from webkitcorepy import OutputCapture
37
38
39 class TrivialCommand(Command):
40     name = "trivial"
41     show_in_main_help = True
42     help_text = "help text"
43
44     def __init__(self, **kwargs):
45         Command.__init__(self, **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 LikesToRetry(Command):
57     name = "likes-to-retry"
58     show_in_main_help = True
59     help_text = "help text"
60
61     def __init__(self, **kwargs):
62         Command.__init__(self, **kwargs)
63         self.execute_count = 0
64
65     def execute(self, options, args, tool):
66         self.execute_count += 1
67         if self.execute_count < 2:
68             raise TryAgain()
69
70
71 class CommandTest(unittest.TestCase):
72     def test_name_with_arguments(self):
73         TrivialCommand.argument_names = "ARG1 ARG2"
74         command_with_args = TrivialCommand()
75         self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
76
77         TrivialCommand.argument_names = None
78         command_with_args = TrivialCommand(options=[make_option("--my_option")])
79         self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
80
81     def test_parse_required_arguments(self):
82         self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1", "ARG2"])
83         self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), [])
84         self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG2"])
85         # Note: We might make our arg parsing smarter in the future and allow this type of arguments string.
86         self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 ARG2]")
87
88     def test_required_arguments(self):
89         TrivialCommand.argument_names = "ARG1 ARG2 [ARG3]"
90         two_required_arguments = TrivialCommand()
91
92         with OutputCapture(level=logging.INFO) as captured:
93             exit_code = two_required_arguments.check_arguments_and_execute(None, ["foo"], TrivialTool())
94         self.assertEqual(
95             captured.root.log.getvalue(),
96             "2 arguments required, 1 argument provided.  Provided: 'foo'  Required: ARG1 ARG2\nSee 'trivial-tool help trivial' for usage.\n",
97         )
98
99         self.assertEqual(exit_code, 1)
100         TrivialCommand.argument_names = None
101
102
103 class TrivialTool(MultiCommandTool):
104     def __init__(self, commands=None):
105         MultiCommandTool.__init__(self, name="trivial-tool", commands=commands)
106
107     def path(self):
108         return __file__
109
110     def should_execute_command(self, command):
111         return (True, None)
112
113
114 class MultiCommandToolTest(unittest.TestCase):
115     def _assert_split(self, args, expected_split):
116         self.assertEqual(MultiCommandTool._split_command_name_from_args(args), expected_split)
117
118     def test_split_args(self):
119         # MultiCommandToolTest._split_command_name_from_args returns: (command, args)
120         full_args = ["--global-option", "command", "--option", "arg"]
121         full_args_expected = ("command", ["--global-option", "--option", "arg"])
122         self._assert_split(full_args, full_args_expected)
123
124         full_args = []
125         full_args_expected = (None, [])
126         self._assert_split(full_args, full_args_expected)
127
128         full_args = ["command", "arg"]
129         full_args_expected = ("command", ["arg"])
130         self._assert_split(full_args, full_args_expected)
131
132     def test_command_by_name(self):
133         # This also tests Command auto-discovery.
134         tool = TrivialTool()
135         self.assertEqual(tool.command_by_name("trivial").name, "trivial")
136         self.assertEqual(tool.command_by_name("bar"), None)
137
138     def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expected_stderr="", expected_exit_code=0):
139         with OutputCapture() as captured:
140             exit_code = tool.main(main_args)
141         self.assertEqual(captured.stdout.getvalue(), expected_stdout)
142         self.assertEqual(captured.stderr.getvalue(), expected_stderr)
143         self.assertEqual(exit_code, expected_exit_code)
144
145     def test_retry(self):
146         likes_to_retry = LikesToRetry()
147         tool = TrivialTool(commands=[likes_to_retry])
148         tool.main(["tool", "likes-to-retry"])
149         self.assertEqual(likes_to_retry.execute_count, 2)
150
151     def test_global_help(self):
152         tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()])
153         expected_common_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
154
155 Options:
156   -h, --help  show this help message and exit
157
158 Common trivial-tool commands:
159    trivial   help text
160
161 See 'trivial-tool help --all-commands' to list all commands.
162 See 'trivial-tool help COMMAND' for more information on a specific command.
163
164 """
165         self._assert_tool_main_outputs(tool, ["tool"], expected_common_commands_help)
166         self._assert_tool_main_outputs(tool, ["tool", "help"], expected_common_commands_help)
167         expected_all_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
168
169 Options:
170   -h, --help  show this help message and exit
171
172 All trivial-tool commands:
173    help       Display information about this program or its subcommands
174    trivial    help text
175    uncommon   help text
176
177 See 'trivial-tool help --all-commands' to list all commands.
178 See 'trivial-tool help COMMAND' for more information on a specific command.
179
180 """
181         self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"], expected_all_commands_help)
182         # Test that arguments can be passed before commands as well
183         self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"], expected_all_commands_help)
184
185     def test_command_help(self):
186         TrivialCommand.long_help = "LONG HELP"
187         command_with_options = TrivialCommand(options=[make_option("--my_option")])
188         tool = TrivialTool(commands=[command_with_options])
189         expected_subcommand_help = "trivial [options]   help text\n\nLONG HELP\n\nOptions:\n  --my_option=MY_OPTION\n\n"
190         self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expected_subcommand_help)