PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/external/webkit/Tools/Scripts/webkitpy/tool/multicommandtool_unittest.py

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