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 20 matching lines...) Expand all Loading... |
31 | 31 |
32 from optparse import make_option | 32 from optparse import make_option |
33 | 33 |
34 from webkitpy.common.system.outputcapture import OutputCapture | 34 from webkitpy.common.system.outputcapture import OutputCapture |
35 from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain | 35 from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain |
36 | 36 |
37 | 37 |
38 class TrivialCommand(Command): | 38 class TrivialCommand(Command): |
39 name = "trivial" | 39 name = "trivial" |
40 show_in_main_help = True | 40 show_in_main_help = True |
| 41 |
41 def __init__(self, **kwargs): | 42 def __init__(self, **kwargs): |
42 Command.__init__(self, "help text", **kwargs) | 43 Command.__init__(self, "help text", **kwargs) |
43 | 44 |
44 def execute(self, options, args, tool): | 45 def execute(self, options, args, tool): |
45 pass | 46 pass |
46 | 47 |
47 | 48 |
48 class UncommonCommand(TrivialCommand): | 49 class UncommonCommand(TrivialCommand): |
49 name = "uncommon" | 50 name = "uncommon" |
50 show_in_main_help = False | 51 show_in_main_help = False |
51 | 52 |
52 | 53 |
53 class LikesToRetry(Command): | 54 class LikesToRetry(Command): |
54 name = "likes-to-retry" | 55 name = "likes-to-retry" |
55 show_in_main_help = True | 56 show_in_main_help = True |
56 | 57 |
57 def __init__(self, **kwargs): | 58 def __init__(self, **kwargs): |
58 Command.__init__(self, "help text", **kwargs) | 59 Command.__init__(self, "help text", **kwargs) |
59 self.execute_count = 0 | 60 self.execute_count = 0 |
60 | 61 |
61 def execute(self, options, args, tool): | 62 def execute(self, options, args, tool): |
62 self.execute_count += 1 | 63 self.execute_count += 1 |
63 if self.execute_count < 2: | 64 if self.execute_count < 2: |
64 raise TryAgain() | 65 raise TryAgain() |
65 | 66 |
66 | 67 |
67 class CommandTest(unittest.TestCase): | 68 class CommandTest(unittest.TestCase): |
| 69 |
68 def test_name_with_arguments(self): | 70 def test_name_with_arguments(self): |
69 command_with_args = TrivialCommand(argument_names="ARG1 ARG2") | 71 command_with_args = TrivialCommand(argument_names="ARG1 ARG2") |
70 self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1
ARG2") | 72 self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1
ARG2") |
71 | 73 |
72 command_with_args = TrivialCommand(options=[make_option("--my_option")]) | 74 command_with_args = TrivialCommand(options=[make_option("--my_option")]) |
73 self.assertEqual(command_with_args.name_with_arguments(), "trivial [opti
ons]") | 75 self.assertEqual(command_with_args.name_with_arguments(), "trivial [opti
ons]") |
74 | 76 |
75 def test_parse_required_arguments(self): | 77 def test_parse_required_arguments(self): |
76 self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1"
, "ARG2"]) | 78 self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1"
, "ARG2"]) |
77 self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), []) | 79 self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), []) |
78 self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG
2"]) | 80 self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG
2"]) |
79 # Note: We might make our arg parsing smarter in the future and allow th
is type of arguments string. | 81 # Note: We might make our arg parsing smarter in the future and allow th
is type of arguments string. |
80 self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 A
RG2]") | 82 self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 A
RG2]") |
81 | 83 |
82 def test_required_arguments(self): | 84 def test_required_arguments(self): |
83 two_required_arguments = TrivialCommand(argument_names="ARG1 ARG2 [ARG3]
") | 85 two_required_arguments = TrivialCommand(argument_names="ARG1 ARG2 [ARG3]
") |
84 expected_logs = "2 arguments required, 1 argument provided. Provided: '
foo' Required: ARG1 ARG2\nSee 'trivial-tool help trivial' for usage.\n" | 86 expected_logs = "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_logs=expec
ted_logs) | 87 exit_code = OutputCapture().assert_outputs(self, two_required_arguments.
check_arguments_and_execute, |
| 88 [None, ["foo"], TrivialTool()
], expected_logs=expected_logs) |
86 self.assertEqual(exit_code, 1) | 89 self.assertEqual(exit_code, 1) |
87 | 90 |
88 | 91 |
89 class TrivialTool(MultiCommandTool): | 92 class TrivialTool(MultiCommandTool): |
| 93 |
90 def __init__(self, commands=None): | 94 def __init__(self, commands=None): |
91 MultiCommandTool.__init__(self, name="trivial-tool", commands=commands) | 95 MultiCommandTool.__init__(self, name="trivial-tool", commands=commands) |
92 | 96 |
93 def path(self): | 97 def path(self): |
94 return __file__ | 98 return __file__ |
95 | 99 |
96 def should_execute_command(self, command): | 100 def should_execute_command(self, command): |
97 return (True, None) | 101 return (True, None) |
98 | 102 |
99 | 103 |
100 class MultiCommandToolTest(unittest.TestCase): | 104 class MultiCommandToolTest(unittest.TestCase): |
| 105 |
101 def _assert_split(self, args, expected_split): | 106 def _assert_split(self, args, expected_split): |
102 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), e
xpected_split) | 107 self.assertEqual(MultiCommandTool._split_command_name_from_args(args), e
xpected_split) |
103 | 108 |
104 def test_split_args(self): | 109 def test_split_args(self): |
105 # MultiCommandToolTest._split_command_name_from_args returns: (command,
args) | 110 # MultiCommandToolTest._split_command_name_from_args returns: (command,
args) |
106 full_args = ["--global-option", "command", "--option", "arg"] | 111 full_args = ["--global-option", "command", "--option", "arg"] |
107 full_args_expected = ("command", ["--global-option", "--option", "arg"]) | 112 full_args_expected = ("command", ["--global-option", "--option", "arg"]) |
108 self._assert_split(full_args, full_args_expected) | 113 self._assert_split(full_args, full_args_expected) |
109 | 114 |
110 full_args = [] | 115 full_args = [] |
111 full_args_expected = (None, []) | 116 full_args_expected = (None, []) |
112 self._assert_split(full_args, full_args_expected) | 117 self._assert_split(full_args, full_args_expected) |
113 | 118 |
114 full_args = ["command", "arg"] | 119 full_args = ["command", "arg"] |
115 full_args_expected = ("command", ["arg"]) | 120 full_args_expected = ("command", ["arg"]) |
116 self._assert_split(full_args, full_args_expected) | 121 self._assert_split(full_args, full_args_expected) |
117 | 122 |
118 def test_command_by_name(self): | 123 def test_command_by_name(self): |
119 # This also tests Command auto-discovery. | 124 # This also tests Command auto-discovery. |
120 tool = TrivialTool() | 125 tool = TrivialTool() |
121 self.assertEqual(tool.command_by_name("trivial").name, "trivial") | 126 self.assertEqual(tool.command_by_name("trivial").name, "trivial") |
122 self.assertEqual(tool.command_by_name("bar"), None) | 127 self.assertEqual(tool.command_by_name("bar"), None) |
123 | 128 |
124 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expect
ed_stderr = "", expected_exit_code=0): | 129 def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expect
ed_stderr="", expected_exit_code=0): |
125 exit_code = OutputCapture().assert_outputs(self, tool.main, [main_args],
expected_stdout=expected_stdout, expected_stderr=expected_stderr) | 130 exit_code = OutputCapture().assert_outputs( |
| 131 self, tool.main, [main_args], expected_stdout=expected_stdout, expec
ted_stderr=expected_stderr) |
126 self.assertEqual(exit_code, expected_exit_code) | 132 self.assertEqual(exit_code, expected_exit_code) |
127 | 133 |
128 def test_retry(self): | 134 def test_retry(self): |
129 likes_to_retry = LikesToRetry() | 135 likes_to_retry = LikesToRetry() |
130 tool = TrivialTool(commands=[likes_to_retry]) | 136 tool = TrivialTool(commands=[likes_to_retry]) |
131 tool.main(["tool", "likes-to-retry"]) | 137 tool.main(["tool", "likes-to-retry"]) |
132 self.assertEqual(likes_to_retry.execute_count, 2) | 138 self.assertEqual(likes_to_retry.execute_count, 2) |
133 | 139 |
134 def test_global_help(self): | 140 def test_global_help(self): |
135 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) | 141 tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()]) |
(...skipping 22 matching lines...) Expand all Loading... |
158 uncommon help text | 164 uncommon help text |
159 | 165 |
160 See 'trivial-tool help --all-commands' to list all commands. | 166 See 'trivial-tool help --all-commands' to list all commands. |
161 See 'trivial-tool help COMMAND' for more information on a specific command. | 167 See 'trivial-tool help COMMAND' for more information on a specific command. |
162 | 168 |
163 """ | 169 """ |
164 self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"],
expected_all_commands_help) | 170 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 | 171 # 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) | 172 self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"],
expected_all_commands_help) |
167 | 173 |
168 | |
169 def test_command_help(self): | 174 def test_command_help(self): |
170 command_with_options = TrivialCommand(options=[make_option("--my_option"
)], long_help="LONG HELP") | 175 command_with_options = TrivialCommand(options=[make_option("--my_option"
)], long_help="LONG HELP") |
171 tool = TrivialTool(commands=[command_with_options]) | 176 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" | 177 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"], expect
ed_subcommand_help) | 178 self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expect
ed_subcommand_help) |
OLD | NEW |