|
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 signal |
|
31 import subprocess |
|
32 import sys |
|
33 import unittest |
|
34 |
|
35 from webkitpy.common.system.executive import Executive, run_command, ScriptError |
|
36 |
|
37 |
|
38 class ExecutiveTest(unittest.TestCase): |
|
39 |
|
40 def test_run_command_with_bad_command(self): |
|
41 def run_bad_command(): |
|
42 run_command(["foo_bar_command_blah"], error_handler=Executive.ignore_error, return_exit_code=True) |
|
43 self.failUnlessRaises(OSError, run_bad_command) |
|
44 |
|
45 def test_run_command_args_type(self): |
|
46 executive = Executive() |
|
47 self.assertRaises(AssertionError, executive.run_command, "echo") |
|
48 self.assertRaises(AssertionError, executive.run_command, u"echo") |
|
49 executive.run_command(["echo", "foo"]) |
|
50 executive.run_command(("echo", "foo")) |
|
51 |
|
52 def test_run_command_with_unicode(self): |
|
53 """Validate that it is safe to pass unicode() objects |
|
54 to Executive.run* methods, and they will return unicode() |
|
55 objects by default unless decode_output=False""" |
|
56 executive = Executive() |
|
57 unicode_tor = u"WebKit \u2661 Tor Arne Vestb\u00F8!" |
|
58 utf8_tor = unicode_tor.encode("utf-8") |
|
59 |
|
60 output = executive.run_command(["cat"], input=unicode_tor) |
|
61 self.assertEquals(output, unicode_tor) |
|
62 |
|
63 output = executive.run_command(["echo", "-n", unicode_tor]) |
|
64 self.assertEquals(output, unicode_tor) |
|
65 |
|
66 output = executive.run_command(["echo", "-n", unicode_tor], decode_output=False) |
|
67 self.assertEquals(output, utf8_tor) |
|
68 |
|
69 # Make sure that str() input also works. |
|
70 output = executive.run_command(["cat"], input=utf8_tor, decode_output=False) |
|
71 self.assertEquals(output, utf8_tor) |
|
72 |
|
73 # FIXME: We should only have one run* method to test |
|
74 output = executive.run_and_throw_if_fail(["echo", "-n", unicode_tor], quiet=True) |
|
75 self.assertEquals(output, unicode_tor) |
|
76 |
|
77 output = executive.run_and_throw_if_fail(["echo", "-n", unicode_tor], quiet=True, decode_output=False) |
|
78 self.assertEquals(output, utf8_tor) |
|
79 |
|
80 def test_kill_process(self): |
|
81 executive = Executive() |
|
82 # We use "yes" because it loops forever. |
|
83 process = subprocess.Popen(["yes"], stdout=subprocess.PIPE) |
|
84 self.assertEqual(process.poll(), None) # Process is running |
|
85 executive.kill_process(process.pid) |
|
86 # Note: Can't use a ternary since signal.SIGKILL is undefined for sys.platform == "win32" |
|
87 if sys.platform == "win32": |
|
88 expected_exit_code = 0 # taskkill.exe results in exit(0) |
|
89 else: |
|
90 expected_exit_code = -signal.SIGKILL |
|
91 self.assertEqual(process.wait(), expected_exit_code) |
|
92 # Killing again should fail silently. |
|
93 executive.kill_process(process.pid) |
|
94 |
|
95 def _assert_windows_image_name(self, name, expected_windows_name): |
|
96 executive = Executive() |
|
97 windows_name = executive._windows_image_name(name) |
|
98 self.assertEqual(windows_name, expected_windows_name) |
|
99 |
|
100 def test_windows_image_name(self): |
|
101 self._assert_windows_image_name("foo", "foo.exe") |
|
102 self._assert_windows_image_name("foo.exe", "foo.exe") |
|
103 self._assert_windows_image_name("foo.com", "foo.com") |
|
104 # If the name looks like an extension, even if it isn't |
|
105 # supposed to, we have no choice but to return the original name. |
|
106 self._assert_windows_image_name("foo.baz", "foo.baz") |
|
107 self._assert_windows_image_name("foo.baz.exe", "foo.baz.exe") |
|
108 |
|
109 def test_kill_all(self): |
|
110 executive = Executive() |
|
111 # We use "yes" because it loops forever. |
|
112 process = subprocess.Popen(["yes"], stdout=subprocess.PIPE) |
|
113 self.assertEqual(process.poll(), None) # Process is running |
|
114 executive.kill_all("yes") |
|
115 # Note: Can't use a ternary since signal.SIGTERM is undefined for sys.platform == "win32" |
|
116 if sys.platform in ("win32", "cygwin"): |
|
117 expected_exit_code = 0 # taskkill.exe results in exit(0) |
|
118 else: |
|
119 expected_exit_code = -signal.SIGTERM |
|
120 self.assertEqual(process.wait(), expected_exit_code) |
|
121 # Killing again should fail silently. |
|
122 executive.kill_all("yes") |