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