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