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