##// END OF EJS Templates
Fix the signal handler that was breaking the test
Itamar Turner-Trauring -
Show More
@@ -1,189 +1,195 b''
1 1 # encoding: utf-8
2 2 """
3 3 Tests for platutils.py
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import sys
18 import signal
18 19 import os
19 20 import time
20 21 from _thread import interrupt_main # Py 3
21 22 import threading
22 23 from unittest import SkipTest
23 24
24 25 import nose.tools as nt
25 26
26 27 from IPython.utils.process import (find_cmd, FindCmdError, arg_split,
27 28 system, getoutput, getoutputerror,
28 29 get_output_error_code)
29 30 from IPython.utils.capture import capture_output
30 31 from IPython.testing import decorators as dec
31 32 from IPython.testing import tools as tt
32 33
33 34 python = os.path.basename(sys.executable)
34 35
35 36 #-----------------------------------------------------------------------------
36 37 # Tests
37 38 #-----------------------------------------------------------------------------
38 39
39 40
40 41 @dec.skip_win32
41 42 def test_find_cmd_ls():
42 43 """Make sure we can find the full path to ls."""
43 44 path = find_cmd('ls')
44 45 nt.assert_true(path.endswith('ls'))
45 46
46 47
47 48 def has_pywin32():
48 49 try:
49 50 import win32api
50 51 except ImportError:
51 52 return False
52 53 return True
53 54
54 55
55 56 @dec.onlyif(has_pywin32, "This test requires win32api to run")
56 57 def test_find_cmd_pythonw():
57 58 """Try to find pythonw on Windows."""
58 59 path = find_cmd('pythonw')
59 60 assert path.lower().endswith('pythonw.exe'), path
60 61
61 62
62 63 @dec.onlyif(lambda : sys.platform != 'win32' or has_pywin32(),
63 64 "This test runs on posix or in win32 with win32api installed")
64 65 def test_find_cmd_fail():
65 66 """Make sure that FindCmdError is raised if we can't find the cmd."""
66 67 nt.assert_raises(FindCmdError,find_cmd,'asdfasdf')
67 68
68 69
69 70 @dec.skip_win32
70 71 def test_arg_split():
71 72 """Ensure that argument lines are correctly split like in a shell."""
72 73 tests = [['hi', ['hi']],
73 74 [u'hi', [u'hi']],
74 75 ['hello there', ['hello', 'there']],
75 76 # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
76 77 # Do not use \N because the tests crash with syntax error in
77 78 # some cases, for example windows python2.6.
78 79 [u'h\u01cello', [u'h\u01cello']],
79 80 ['something "with quotes"', ['something', '"with quotes"']],
80 81 ]
81 82 for argstr, argv in tests:
82 83 nt.assert_equal(arg_split(argstr), argv)
83 84
84 85 @dec.skip_if_not_win32
85 86 def test_arg_split_win32():
86 87 """Ensure that argument lines are correctly split like in a shell."""
87 88 tests = [['hi', ['hi']],
88 89 [u'hi', [u'hi']],
89 90 ['hello there', ['hello', 'there']],
90 91 [u'h\u01cello', [u'h\u01cello']],
91 92 ['something "with quotes"', ['something', 'with quotes']],
92 93 ]
93 94 for argstr, argv in tests:
94 95 nt.assert_equal(arg_split(argstr), argv)
95 96
96 97
97 98 class SubProcessTestCase(tt.TempFileMixin):
98 99 def setUp(self):
99 100 """Make a valid python temp file."""
100 101 lines = [ "import sys",
101 102 "print('on stdout', end='', file=sys.stdout)",
102 103 "print('on stderr', end='', file=sys.stderr)",
103 104 "sys.stdout.flush()",
104 105 "sys.stderr.flush()"]
105 106 self.mktmp('\n'.join(lines))
106 107
107 108 def test_system(self):
108 109 status = system('%s "%s"' % (python, self.fname))
109 110 self.assertEqual(status, 0)
110 111
111 112 def test_system_quotes(self):
112 113 status = system('%s -c "import sys"' % python)
113 114 self.assertEqual(status, 0)
114 115
115 116 def assert_interrupts(self, command):
116 117 """
117 118 Interrupt a subprocess after a second.
118 119 """
119 120 if threading.main_thread() != threading.current_thread():
120 121 raise nt.SkipTest("Can't run this test if not in main thread.")
121 122
123 # Some tests can overwrite SIGINT handler (by using pdb for example),
124 # which then breaks this test, so just make sure it's operating
125 # normally.
126 signal.signal(signal.SIGINT, signal.default_int_handler)
127
122 128 def interrupt():
123 129 # Wait for subprocess to start:
124 130 time.sleep(0.5)
125 131 interrupt_main()
126 132
127 133 threading.Thread(target=interrupt).start()
128 134 start = time.time()
129 135 try:
130 136 result = command()
131 137 except KeyboardInterrupt:
132 138 # Success!
133 return
139 pass
134 140 end = time.time()
135 141 self.assertTrue(
136 142 end - start < 2, "Process didn't die quickly: %s" % (end - start)
137 143 )
138 144 return result
139 145
140 146 def test_system_interrupt(self):
141 147 """
142 148 When interrupted in the way ipykernel interrupts IPython, the
143 149 subprocess is interrupted.
144 150 """
145 151 def command():
146 152 return system('%s -c "import time; time.sleep(5)"' % python)
147 153
148 154 status = self.assert_interrupts(command)
149 155 self.assertNotEqual(
150 156 status, 0, "The process wasn't interrupted. Status: %s" % (status,)
151 157 )
152 158
153 159 def test_getoutput(self):
154 160 out = getoutput('%s "%s"' % (python, self.fname))
155 161 # we can't rely on the order the line buffered streams are flushed
156 162 try:
157 163 self.assertEqual(out, 'on stderron stdout')
158 164 except AssertionError:
159 165 self.assertEqual(out, 'on stdouton stderr')
160 166
161 167 def test_getoutput_quoted(self):
162 168 out = getoutput('%s -c "print (1)"' % python)
163 169 self.assertEqual(out.strip(), '1')
164 170
165 171 #Invalid quoting on windows
166 172 @dec.skip_win32
167 173 def test_getoutput_quoted2(self):
168 174 out = getoutput("%s -c 'print (1)'" % python)
169 175 self.assertEqual(out.strip(), '1')
170 176 out = getoutput("%s -c 'print (\"1\")'" % python)
171 177 self.assertEqual(out.strip(), '1')
172 178
173 179 def test_getoutput_error(self):
174 180 out, err = getoutputerror('%s "%s"' % (python, self.fname))
175 181 self.assertEqual(out, 'on stdout')
176 182 self.assertEqual(err, 'on stderr')
177 183
178 184 def test_get_output_error_code(self):
179 185 quiet_exit = '%s -c "import sys; sys.exit(1)"' % python
180 186 out, err, code = get_output_error_code(quiet_exit)
181 187 self.assertEqual(out, '')
182 188 self.assertEqual(err, '')
183 189 self.assertEqual(code, 1)
184 190 out, err, code = get_output_error_code('%s "%s"' % (python, self.fname))
185 191 self.assertEqual(out, 'on stdout')
186 192 self.assertEqual(err, 'on stderr')
187 193 self.assertEqual(code, 0)
188 194
189 195
General Comments 0
You need to be logged in to leave comments. Login now