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