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.
31 from multicommandtool import MultiCommandTool, Command
32 from modules.outputcapture import OutputCapture
34 from optparse import make_option
36 class TrivialCommand(Command):
38 def __init__(self, **kwargs):
39 Command.__init__(self, "help text", **kwargs)
41 def execute(self, options, args, tool):
45 class CommandTest(unittest.TestCase):
46 def test_name_with_arguments(self):
47 command_with_args = TrivialCommand(argument_names="ARG1 ARG2")
48 self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
50 command_with_args = TrivialCommand(options=[make_option("--my_option")])
51 self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
54 class TrivialTool(MultiCommandTool):
55 def __init__(self, commands=None):
56 MultiCommandTool.__init__(self, commands)
61 def should_show_command_help(self, command):
64 def should_execute_command(self, command):
68 class MultiCommandToolTest(unittest.TestCase):
69 def _assert_split(self, args, expected_split):
70 self.assertEqual(MultiCommandTool._split_args(args), expected_split)
72 def test_split_args(self):
73 # MultiCommandToolTest._split_args returns: (global_args, command, command_args)
74 full_args = ["--global-option", "command", "--option", "arg"]
75 full_args_expected = (["--global-option"], "command", ["--option", "arg"])
76 self._assert_split(full_args, full_args_expected)
79 full_args_expected = ([], None, [])
80 self._assert_split(full_args, full_args_expected)
82 full_args = ["command", "arg"]
83 full_args_expected = ([], "command", ["arg"])
84 self._assert_split(full_args, full_args_expected)
86 def test_command_by_name(self):
87 # This also tests Command auto-discovery.
89 self.assertEqual(tool.command_by_name("trivial").name, "trivial")
90 self.assertEqual(tool.command_by_name("bar"), None)
92 def test_command_help(self):
93 command_with_options = TrivialCommand(options=[make_option("--my_option")])
94 tool = TrivialTool(commands=[command_with_options])
96 capture = OutputCapture()
97 capture.capture_output()
98 exit_code = tool.main(["tool", "help", "trivial"])
99 (stdout_string, stderr_string) = capture.restore_output()
100 expected_subcommand_help = "trivial [options] help text\nOptions:\n --my_option=MY_OPTION\n\n"
101 self.assertEqual(exit_code, 0)
102 self.assertEqual(stdout_string, "")
103 self.assertEqual(stderr_string, expected_subcommand_help)
106 if __name__ == "__main__":