2009-11-21 Eric Seidel <eric@webkit.org>
[WebKit-https.git] / WebKitTools / Scripts / modules / 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 from multicommandtool import MultiCommandTool, Command
32 from StringIO import StringIO
33
34 from optparse import make_option
35
36 class TrivialCommand(Command):
37     name = "trivial"
38     def __init__(self, **kwargs):
39         Command.__init__(self, "help text", **kwargs)
40
41     def execute(self, options, args, tool):
42         pass
43
44
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")
49
50         command_with_args = TrivialCommand(options=[make_option("--my_option")])
51         self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
52
53
54 class TrivialTool(MultiCommandTool):
55     def __init__(self, commands=None):
56         MultiCommandTool.__init__(self, commands)
57
58     def path():
59         return __file__
60
61     def should_show_command_help(self, command):
62         return True
63
64     def should_execute_command(self, command):
65         return True
66
67
68 class MultiCommandToolTest(unittest.TestCase):
69     def _capture_stderr(self):
70         self.saved_stderr = sys.stderr
71         sys.stderr = StringIO()
72
73     def _release_stderr(self):
74         string = sys.stderr.getvalue()
75         sys.stderr = self.saved_stderr
76         self.saved_stderr = None
77         return string
78
79     def _assert_split(self, args, expected_split):
80         self.assertEqual(MultiCommandTool._split_args(args), expected_split)
81
82     def test_split_args(self):
83         # MultiCommandToolTest._split_args returns: (global_args, command, command_args)
84         full_args = ["--global-option", "command", "--option", "arg"]
85         full_args_expected = (["--global-option"], "command", ["--option", "arg"])
86         self._assert_split(full_args, full_args_expected)
87
88         full_args = []
89         full_args_expected = ([], None, [])
90         self._assert_split(full_args, full_args_expected)
91
92         full_args = ["command", "arg"]
93         full_args_expected = ([], "command", ["arg"])
94         self._assert_split(full_args, full_args_expected)
95
96     def test_command_by_name(self):
97         # This also tests Command auto-discovery.
98         tool = TrivialTool()
99         self.assertEqual(tool.command_by_name("trivial").name, "trivial")
100         self.assertEqual(tool.command_by_name("bar"), None)
101
102     def test_command_help(self):
103         command_with_options = TrivialCommand(options=[make_option("--my_option")])
104         tool = TrivialTool(commands=[command_with_options])
105
106         self._capture_stderr()
107         exit_code = tool.main(["tool", "help", "trivial"])
108         help_text = self._release_stderr()
109         expected_subcommand_help = "  trivial [options]   help text\nOptions:\n  --my_option=MY_OPTION\n\n"
110         self.assertEqual(exit_code, 0)
111         self.assertEqual(help_text, expected_subcommand_help)
112
113
114 if __name__ == "__main__":
115     unittest.main()