multicommandtool_unittest.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. import sys
  29. import unittest2 as unittest
  30. from optparse import make_option
  31. from webkitpy.common.system.outputcapture import OutputCapture
  32. from webkitpy.tool.multicommandtool import MultiCommandTool, Command, TryAgain
  33. class TrivialCommand(Command):
  34. name = "trivial"
  35. show_in_main_help = True
  36. help_text = "help text"
  37. def __init__(self, **kwargs):
  38. Command.__init__(self, **kwargs)
  39. def execute(self, options, args, tool):
  40. pass
  41. class UncommonCommand(TrivialCommand):
  42. name = "uncommon"
  43. show_in_main_help = False
  44. class LikesToRetry(Command):
  45. name = "likes-to-retry"
  46. show_in_main_help = True
  47. help_text = "help text"
  48. def __init__(self, **kwargs):
  49. Command.__init__(self, **kwargs)
  50. self.execute_count = 0
  51. def execute(self, options, args, tool):
  52. self.execute_count += 1
  53. if self.execute_count < 2:
  54. raise TryAgain()
  55. class CommandTest(unittest.TestCase):
  56. def test_name_with_arguments(self):
  57. TrivialCommand.argument_names = "ARG1 ARG2"
  58. command_with_args = TrivialCommand()
  59. self.assertEqual(command_with_args.name_with_arguments(), "trivial ARG1 ARG2")
  60. TrivialCommand.argument_names = None
  61. command_with_args = TrivialCommand(options=[make_option("--my_option")])
  62. self.assertEqual(command_with_args.name_with_arguments(), "trivial [options]")
  63. def test_parse_required_arguments(self):
  64. self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1", "ARG2"])
  65. self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), [])
  66. self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG2"])
  67. # Note: We might make our arg parsing smarter in the future and allow this type of arguments string.
  68. self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 ARG2]")
  69. def test_required_arguments(self):
  70. TrivialCommand.argument_names = "ARG1 ARG2 [ARG3]"
  71. two_required_arguments = TrivialCommand()
  72. expected_logs = "2 arguments required, 1 argument provided. Provided: 'foo' Required: ARG1 ARG2\nSee 'trivial-tool help trivial' for usage.\n"
  73. exit_code = OutputCapture().assert_outputs(self, two_required_arguments.check_arguments_and_execute, [None, ["foo"], TrivialTool()], expected_logs=expected_logs)
  74. self.assertEqual(exit_code, 1)
  75. TrivialCommand.argument_names = None
  76. class TrivialTool(MultiCommandTool):
  77. def __init__(self, commands=None):
  78. MultiCommandTool.__init__(self, name="trivial-tool", commands=commands)
  79. def path(self):
  80. return __file__
  81. def should_execute_command(self, command):
  82. return (True, None)
  83. class MultiCommandToolTest(unittest.TestCase):
  84. def _assert_split(self, args, expected_split):
  85. self.assertEqual(MultiCommandTool._split_command_name_from_args(args), expected_split)
  86. def test_split_args(self):
  87. # MultiCommandToolTest._split_command_name_from_args returns: (command, args)
  88. full_args = ["--global-option", "command", "--option", "arg"]
  89. full_args_expected = ("command", ["--global-option", "--option", "arg"])
  90. self._assert_split(full_args, full_args_expected)
  91. full_args = []
  92. full_args_expected = (None, [])
  93. self._assert_split(full_args, full_args_expected)
  94. full_args = ["command", "arg"]
  95. full_args_expected = ("command", ["arg"])
  96. self._assert_split(full_args, full_args_expected)
  97. def test_command_by_name(self):
  98. # This also tests Command auto-discovery.
  99. tool = TrivialTool()
  100. self.assertEqual(tool.command_by_name("trivial").name, "trivial")
  101. self.assertEqual(tool.command_by_name("bar"), None)
  102. def _assert_tool_main_outputs(self, tool, main_args, expected_stdout, expected_stderr = "", expected_exit_code=0):
  103. exit_code = OutputCapture().assert_outputs(self, tool.main, [main_args], expected_stdout=expected_stdout, expected_stderr=expected_stderr)
  104. self.assertEqual(exit_code, expected_exit_code)
  105. def test_retry(self):
  106. likes_to_retry = LikesToRetry()
  107. tool = TrivialTool(commands=[likes_to_retry])
  108. tool.main(["tool", "likes-to-retry"])
  109. self.assertEqual(likes_to_retry.execute_count, 2)
  110. def test_global_help(self):
  111. tool = TrivialTool(commands=[TrivialCommand(), UncommonCommand()])
  112. expected_common_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
  113. Options:
  114. -h, --help show this help message and exit
  115. Common trivial-tool commands:
  116. trivial help text
  117. See 'trivial-tool help --all-commands' to list all commands.
  118. See 'trivial-tool help COMMAND' for more information on a specific command.
  119. """
  120. self._assert_tool_main_outputs(tool, ["tool"], expected_common_commands_help)
  121. self._assert_tool_main_outputs(tool, ["tool", "help"], expected_common_commands_help)
  122. expected_all_commands_help = """Usage: trivial-tool [options] COMMAND [ARGS]
  123. Options:
  124. -h, --help show this help message and exit
  125. All trivial-tool commands:
  126. help Display information about this program or its subcommands
  127. trivial help text
  128. uncommon help text
  129. See 'trivial-tool help --all-commands' to list all commands.
  130. See 'trivial-tool help COMMAND' for more information on a specific command.
  131. """
  132. self._assert_tool_main_outputs(tool, ["tool", "help", "--all-commands"], expected_all_commands_help)
  133. # Test that arguments can be passed before commands as well
  134. self._assert_tool_main_outputs(tool, ["tool", "--all-commands", "help"], expected_all_commands_help)
  135. def test_command_help(self):
  136. TrivialCommand.long_help = "LONG HELP"
  137. command_with_options = TrivialCommand(options=[make_option("--my_option")])
  138. tool = TrivialTool(commands=[command_with_options])
  139. expected_subcommand_help = "trivial [options] help text\n\nLONG HELP\n\nOptions:\n --my_option=MY_OPTION\n\n"
  140. self._assert_tool_main_outputs(tool, ["tool", "help", "trivial"], expected_subcommand_help)