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