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