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 StringIO import StringIO
34 from optparse import make_option
36 class TrivialCommand(Command):
37 def __init__(self, **kwargs):
38 Command.__init__(self, "help text", **kwargs)
40 def execute(self, options, args, tool):
44 class CommandTest(unittest.TestCase):
45 def test_name_with_arguments(self):
46 command_with_args = TrivialCommand(argument_names="ARG1 ARG2")
47 self.assertEqual(command_with_args.name_with_arguments("simple"), "simple ARG1 ARG2")
49 command_with_args = TrivialCommand(options=[make_option("--my_option")])
50 self.assertEqual(command_with_args.name_with_arguments("simple"), "simple [options]")
53 class TrivialTool(MultiCommandTool):
54 def __init__(self, commands):
55 MultiCommandTool.__init__(self, commands)
57 def should_show_command_help(self, command):
60 def should_execute_command(self, command):
64 class MultiCommandToolTest(unittest.TestCase):
66 def _capture_stderr(self):
67 self.saved_stderr = sys.stderr
68 sys.stderr = StringIO()
70 def _release_stderr(self):
71 string = sys.stderr.getvalue()
72 sys.stderr = self.saved_stderr
73 self.saved_stderr = None
76 def _assert_split(self, args, expected_split):
77 self.assertEqual(MultiCommandTool._split_args(args), expected_split)
79 def test_split_args(self):
80 # MultiCommandToolTest._split_args returns: (global_args, command, command_args)
81 full_args = ["--global-option", "command", "--option", "arg"]
82 full_args_expected = (["--global-option"], "command", ["--option", "arg"])
83 self._assert_split(full_args, full_args_expected)
86 full_args_expected = ([], None, [])
87 self._assert_split(full_args, full_args_expected)
89 full_args = ["command", "arg"]
90 full_args_expected = ([], "command", ["arg"])
91 self._assert_split(full_args, full_args_expected)
93 def test_command_by_name(self):
94 foo_command = { "name" : "foo_command", "object" : TrivialCommand() }
95 tool = TrivialTool([foo_command])
97 self.assertEqual(tool.command_by_name("foo_command"), foo_command)
98 self.assertEqual(tool.command_by_name("bar"), None)
100 def test_command_help(self):
101 command_with_args = TrivialCommand(options=[make_option("--my_option")])
102 foo_command = { "name" : "foo_command", "object" : command_with_args }
103 tool = TrivialTool([foo_command])
105 self._capture_stderr()
106 exit_code = tool.main(["tool", "help", "foo_command"])
107 help_text = self._release_stderr()
108 expected_subcommand_help = " foo_command [options] help text\nOptions:\n --my_option=MY_OPTION\n\n"
109 self.assertEqual(exit_code, 0)
110 self.assertEqual(help_text, expected_subcommand_help)
113 if __name__ == "__main__":