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