Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / Tools / Scripts / webkitpy / common / system / executive_unittest.py
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
30 import os
31 import signal
32 import subprocess
33 import sys
34 import unittest
35
36 from webkitpy.common.system.executive import Executive, run_command, ScriptError
37 from webkitpy.test import cat, echo
38
39
40 def never_ending_command():
41     """Arguments for a command that will never end (useful for testing process
42     killing). It should be a process that is unlikely to already be running
43     because all instances will be killed."""
44     if sys.platform == 'win32':
45         return ['wmic']
46     return ['yes']
47
48
49 class ExecutiveTest(unittest.TestCase):
50
51     def test_run_command_with_bad_command(self):
52         def run_bad_command():
53             run_command(["foo_bar_command_blah"], error_handler=Executive.ignore_error, return_exit_code=True)
54         self.failUnlessRaises(OSError, run_bad_command)
55
56     def test_run_command_args_type(self):
57         executive = Executive()
58         self.assertRaises(AssertionError, executive.run_command, "echo")
59         self.assertRaises(AssertionError, executive.run_command, u"echo")
60         executive.run_command(echo.command_arguments('foo'))
61         executive.run_command(tuple(echo.command_arguments('foo')))
62
63     def test_run_command_with_unicode(self):
64         """Validate that it is safe to pass unicode() objects
65         to Executive.run* methods, and they will return unicode()
66         objects by default unless decode_output=False"""
67         unicode_tor_input = u"WebKit \u2661 Tor Arne Vestb\u00F8!"
68         if sys.platform == 'win32':
69             encoding = 'mbcs'
70         else:
71             encoding = 'utf-8'
72         encoded_tor = unicode_tor_input.encode(encoding)
73         # On Windows, we expect the unicode->mbcs->unicode roundtrip to be
74         # lossy. On other platforms, we expect a lossless roundtrip.
75         if sys.platform == 'win32':
76             unicode_tor_output = encoded_tor.decode(encoding)
77         else:
78             unicode_tor_output = unicode_tor_input
79
80         executive = Executive()
81
82         output = executive.run_command(cat.command_arguments(), input=unicode_tor_input)
83         self.assertEquals(output, unicode_tor_output)
84
85         output = executive.run_command(echo.command_arguments("-n", unicode_tor_input))
86         self.assertEquals(output, unicode_tor_output)
87
88         output = executive.run_command(echo.command_arguments("-n", unicode_tor_input), decode_output=False)
89         self.assertEquals(output, encoded_tor)
90
91         # Make sure that str() input also works.
92         output = executive.run_command(cat.command_arguments(), input=encoded_tor, decode_output=False)
93         self.assertEquals(output, encoded_tor)
94
95         # FIXME: We should only have one run* method to test
96         output = executive.run_and_throw_if_fail(echo.command_arguments("-n", unicode_tor_input), quiet=True)
97         self.assertEquals(output, unicode_tor_output)
98
99         output = executive.run_and_throw_if_fail(echo.command_arguments("-n", unicode_tor_input), quiet=True, decode_output=False)
100         self.assertEquals(output, encoded_tor)
101
102     def test_kill_process(self):
103         executive = Executive()
104         process = subprocess.Popen(never_ending_command(), stdout=subprocess.PIPE)
105         self.assertEqual(process.poll(), None)  # Process is running
106         executive.kill_process(process.pid)
107         # Note: Can't use a ternary since signal.SIGKILL is undefined for sys.platform == "win32"
108         if sys.platform == "win32":
109             expected_exit_code = 1
110         else:
111             expected_exit_code = -signal.SIGKILL
112         self.assertEqual(process.wait(), expected_exit_code)
113         # Killing again should fail silently.
114         executive.kill_process(process.pid)
115
116     def _assert_windows_image_name(self, name, expected_windows_name):
117         executive = Executive()
118         windows_name = executive._windows_image_name(name)
119         self.assertEqual(windows_name, expected_windows_name)
120
121     def test_windows_image_name(self):
122         self._assert_windows_image_name("foo", "foo.exe")
123         self._assert_windows_image_name("foo.exe", "foo.exe")
124         self._assert_windows_image_name("foo.com", "foo.com")
125         # If the name looks like an extension, even if it isn't
126         # supposed to, we have no choice but to return the original name.
127         self._assert_windows_image_name("foo.baz", "foo.baz")
128         self._assert_windows_image_name("foo.baz.exe", "foo.baz.exe")
129
130     def test_kill_all(self):
131         executive = Executive()
132         # We use "yes" because it loops forever.
133         process = subprocess.Popen(never_ending_command(), stdout=subprocess.PIPE)
134         self.assertEqual(process.poll(), None)  # Process is running
135         executive.kill_all(never_ending_command()[0])
136         # Note: Can't use a ternary since signal.SIGTERM is undefined for sys.platform == "win32"
137         if sys.platform == "cygwin":
138             expected_exit_code = 0  # os.kill results in exit(0) for this process.
139         elif sys.platform == "win32":
140             expected_exit_code = 1
141         else:
142             expected_exit_code = -signal.SIGTERM
143         self.assertEqual(process.wait(), expected_exit_code)
144         # Killing again should fail silently.
145         executive.kill_all(never_ending_command()[0])
146
147     def test_check_running_pid(self):
148         executive = Executive()
149         self.assertTrue(executive.check_running_pid(os.getpid()))
150         # Maximum pid number on Linux is 32768 by default
151         self.assertFalse(executive.check_running_pid(100000))