2009-11-21 Eric Seidel <eric@webkit.org>
[WebKit-https.git] / WebKitTools / Scripts / modules / multicommandtool_unittest.py
index 997e11aadb8ae330202e375e04d55a97144143b6..dbae909fe3370bd7136b905e6d5260a3134d5c56 100644 (file)
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import sys
 import unittest
 from multicommandtool import MultiCommandTool, Command
+from StringIO import StringIO
 
 from optparse import make_option
 
 class TrivialCommand(Command):
+    name = "trivial"
     def __init__(self, **kwargs):
         Command.__init__(self, "help text", **kwargs)
 
@@ -42,16 +45,19 @@ class TrivialCommand(Command):
 class CommandTest(unittest.TestCase):
     def test_name_with_arguments(self):
         command_with_args = TrivialCommand(argument_names="ARG1 ARG2")
-        self.assertEqual(command_with_args.name_with_arguments("simple"), "simple ARG1 ARG2")
+        self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
 
         command_with_args = TrivialCommand(options=[make_option("--my_option")])
-        self.assertEqual(command_with_args.name_with_arguments("simple"), "simple [options]")
+        self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
 
 
 class TrivialTool(MultiCommandTool):
-    def __init__(self, commands):
+    def __init__(self, commands=None):
         MultiCommandTool.__init__(self, commands)
 
+    def path():
+        return __file__
+
     def should_show_command_help(self, command):
         return True
 
@@ -60,6 +66,16 @@ class TrivialTool(MultiCommandTool):
 
 
 class MultiCommandToolTest(unittest.TestCase):
+    def _capture_stderr(self):
+        self.saved_stderr = sys.stderr
+        sys.stderr = StringIO()
+
+    def _release_stderr(self):
+        string = sys.stderr.getvalue()
+        sys.stderr = self.saved_stderr
+        self.saved_stderr = None
+        return string
+
     def _assert_split(self, args, expected_split):
         self.assertEqual(MultiCommandTool._split_args(args), expected_split)
 
@@ -78,12 +94,22 @@ class MultiCommandToolTest(unittest.TestCase):
         self._assert_split(full_args, full_args_expected)
 
     def test_command_by_name(self):
-        foo_command = { "name" : "foo_command", "object" :  TrivialCommand() }
-        tool = TrivialTool([foo_command])
-
-        self.assertEqual(tool.command_by_name("foo_command"), foo_command)
+        # This also tests Command auto-discovery.
+        tool = TrivialTool()
+        self.assertEqual(tool.command_by_name("trivial").name, "trivial")
         self.assertEqual(tool.command_by_name("bar"), None)
 
+    def test_command_help(self):
+        command_with_options = TrivialCommand(options=[make_option("--my_option")])
+        tool = TrivialTool(commands=[command_with_options])
+
+        self._capture_stderr()
+        exit_code = tool.main(["tool", "help", "trivial"])
+        help_text = self._release_stderr()
+        expected_subcommand_help = "  trivial [options]   help text\nOptions:\n  --my_option=MY_OPTION\n\n"
+        self.assertEqual(exit_code, 0)
+        self.assertEqual(help_text, expected_subcommand_help)
+
 
 if __name__ == "__main__":
     unittest.main()