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