PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/external/webkit/Tools/Scripts/webkitpy/common/system/executive_unittest.py

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
Python | 201 lines | 134 code | 20 blank | 47 comment | 9 complexity | a19a6890b2b8111b1c08754d30f5af1b MD5 | raw file
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. # Copyright (C) 2009 Daniel Bates (dbates@intudata.com). All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. import os
  30. import signal
  31. import subprocess
  32. import sys
  33. import unittest
  34. from webkitpy.common.system.executive import Executive, run_command, ScriptError
  35. from webkitpy.common.system.filesystem_mock import MockFileSystem
  36. from webkitpy.test import cat, echo
  37. class ScriptErrorTest(unittest.TestCase):
  38. def test_string_from_args(self):
  39. error = ScriptError()
  40. self.assertEquals(error._string_from_args(None), 'None')
  41. self.assertEquals(error._string_from_args([]), '[]')
  42. self.assertEquals(error._string_from_args(map(str, range(30))), "['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17'...")
  43. def test_message_with_output(self):
  44. error = ScriptError('My custom message!', '', -1)
  45. self.assertEquals(error.message_with_output(), 'My custom message!')
  46. error = ScriptError('My custom message!', '', -1, 'My output.')
  47. self.assertEquals(error.message_with_output(), 'My custom message!\n\nMy output.')
  48. error = ScriptError('', 'my_command!', -1, 'My output.', '/Users/username/blah')
  49. self.assertEquals(error.message_with_output(), 'Failed to run "my_command!" exit_code: -1 cwd: /Users/username/blah\n\nMy output.')
  50. error = ScriptError('', 'my_command!', -1, 'ab' + '1' * 499)
  51. self.assertEquals(error.message_with_output(), 'Failed to run "my_command!" exit_code: -1\n\nLast 500 characters of output:\nb' + '1' * 499)
  52. def never_ending_command():
  53. """Arguments for a command that will never end (useful for testing process
  54. killing). It should be a process that is unlikely to already be running
  55. because all instances will be killed."""
  56. if sys.platform == 'win32':
  57. return ['wmic']
  58. return ['yes']
  59. class ExecutiveTest(unittest.TestCase):
  60. def assert_interpreter_for_content(self, intepreter, content):
  61. fs = MockFileSystem()
  62. file_path = None
  63. file_interpreter = None
  64. tempfile, temp_name = fs.open_binary_tempfile('')
  65. tempfile.write(content)
  66. tempfile.close()
  67. file_interpreter = Executive.interpreter_for_script(temp_name, fs)
  68. self.assertEqual(file_interpreter, intepreter)
  69. def test_interpreter_for_script(self):
  70. self.assert_interpreter_for_content(None, '')
  71. self.assert_interpreter_for_content(None, 'abcd\nefgh\nijklm')
  72. self.assert_interpreter_for_content(None, '##/usr/bin/perl')
  73. self.assert_interpreter_for_content('perl', '#!/usr/bin/env perl')
  74. self.assert_interpreter_for_content('perl', '#!/usr/bin/env perl\nfirst\nsecond')
  75. self.assert_interpreter_for_content('perl', '#!/usr/bin/perl')
  76. self.assert_interpreter_for_content('perl', '#!/usr/bin/perl -w')
  77. self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/env python')
  78. self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/env python\nfirst\nsecond')
  79. self.assert_interpreter_for_content(sys.executable, '#!/usr/bin/python')
  80. self.assert_interpreter_for_content('ruby', '#!/usr/bin/env ruby')
  81. self.assert_interpreter_for_content('ruby', '#!/usr/bin/env ruby\nfirst\nsecond')
  82. self.assert_interpreter_for_content('ruby', '#!/usr/bin/ruby')
  83. def test_run_command_with_bad_command(self):
  84. def run_bad_command():
  85. run_command(["foo_bar_command_blah"], error_handler=Executive.ignore_error, return_exit_code=True)
  86. self.failUnlessRaises(OSError, run_bad_command)
  87. def test_run_command_args_type(self):
  88. executive = Executive()
  89. self.assertRaises(AssertionError, executive.run_command, "echo")
  90. self.assertRaises(AssertionError, executive.run_command, u"echo")
  91. executive.run_command(echo.command_arguments('foo'))
  92. executive.run_command(tuple(echo.command_arguments('foo')))
  93. def test_run_command_with_unicode(self):
  94. """Validate that it is safe to pass unicode() objects
  95. to Executive.run* methods, and they will return unicode()
  96. objects by default unless decode_output=False"""
  97. unicode_tor_input = u"WebKit \u2661 Tor Arne Vestb\u00F8!"
  98. if sys.platform == 'win32':
  99. encoding = 'mbcs'
  100. else:
  101. encoding = 'utf-8'
  102. encoded_tor = unicode_tor_input.encode(encoding)
  103. # On Windows, we expect the unicode->mbcs->unicode roundtrip to be
  104. # lossy. On other platforms, we expect a lossless roundtrip.
  105. if sys.platform == 'win32':
  106. unicode_tor_output = encoded_tor.decode(encoding)
  107. else:
  108. unicode_tor_output = unicode_tor_input
  109. executive = Executive()
  110. output = executive.run_command(cat.command_arguments(), input=unicode_tor_input)
  111. self.assertEquals(output, unicode_tor_output)
  112. output = executive.run_command(echo.command_arguments("-n", unicode_tor_input))
  113. self.assertEquals(output, unicode_tor_output)
  114. output = executive.run_command(echo.command_arguments("-n", unicode_tor_input), decode_output=False)
  115. self.assertEquals(output, encoded_tor)
  116. # Make sure that str() input also works.
  117. output = executive.run_command(cat.command_arguments(), input=encoded_tor, decode_output=False)
  118. self.assertEquals(output, encoded_tor)
  119. # FIXME: We should only have one run* method to test
  120. output = executive.run_and_throw_if_fail(echo.command_arguments("-n", unicode_tor_input), quiet=True)
  121. self.assertEquals(output, unicode_tor_output)
  122. output = executive.run_and_throw_if_fail(echo.command_arguments("-n", unicode_tor_input), quiet=True, decode_output=False)
  123. self.assertEquals(output, encoded_tor)
  124. def test_kill_process(self):
  125. executive = Executive()
  126. process = subprocess.Popen(never_ending_command(), stdout=subprocess.PIPE)
  127. self.assertEqual(process.poll(), None) # Process is running
  128. executive.kill_process(process.pid)
  129. # Note: Can't use a ternary since signal.SIGKILL is undefined for sys.platform == "win32"
  130. if sys.platform == "win32":
  131. # FIXME: https://bugs.webkit.org/show_bug.cgi?id=54790
  132. # We seem to get either 0 or 1 here for some reason.
  133. self.assertTrue(process.wait() in (0, 1))
  134. else:
  135. expected_exit_code = -signal.SIGKILL
  136. self.assertEqual(process.wait(), expected_exit_code)
  137. # Killing again should fail silently.
  138. executive.kill_process(process.pid)
  139. def _assert_windows_image_name(self, name, expected_windows_name):
  140. executive = Executive()
  141. windows_name = executive._windows_image_name(name)
  142. self.assertEqual(windows_name, expected_windows_name)
  143. def test_windows_image_name(self):
  144. self._assert_windows_image_name("foo", "foo.exe")
  145. self._assert_windows_image_name("foo.exe", "foo.exe")
  146. self._assert_windows_image_name("foo.com", "foo.com")
  147. # If the name looks like an extension, even if it isn't
  148. # supposed to, we have no choice but to return the original name.
  149. self._assert_windows_image_name("foo.baz", "foo.baz")
  150. self._assert_windows_image_name("foo.baz.exe", "foo.baz.exe")
  151. def test_kill_all(self):
  152. executive = Executive()
  153. # We use "yes" because it loops forever.
  154. process = subprocess.Popen(never_ending_command(), stdout=subprocess.PIPE)
  155. self.assertEqual(process.poll(), None) # Process is running
  156. executive.kill_all(never_ending_command()[0])
  157. # Note: Can't use a ternary since signal.SIGTERM is undefined for sys.platform == "win32"
  158. if sys.platform == "cygwin":
  159. expected_exit_code = 0 # os.kill results in exit(0) for this process.
  160. self.assertEqual(process.wait(), expected_exit_code)
  161. elif sys.platform == "win32":
  162. # FIXME: https://bugs.webkit.org/show_bug.cgi?id=54790
  163. # We seem to get either 0 or 1 here for some reason.
  164. self.assertTrue(process.wait() in (0, 1))
  165. else:
  166. expected_exit_code = -signal.SIGTERM
  167. self.assertEqual(process.wait(), expected_exit_code)
  168. # Killing again should fail silently.
  169. executive.kill_all(never_ending_command()[0])
  170. def test_check_running_pid(self):
  171. executive = Executive()
  172. self.assertTrue(executive.check_running_pid(os.getpid()))
  173. # Maximum pid number on Linux is 32768 by default
  174. self.assertFalse(executive.check_running_pid(100000))