##// END OF EJS Templates
autoformat
Matthias Bussonnier -
Show More
@@ -1,509 +1,509 b''
1 1 # encoding: utf-8
2 2 """Tests for IPython.utils.path.py"""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import os
8 8 import shutil
9 9 import sys
10 10 import tempfile
11 11 import unittest
12 12 from contextlib import contextmanager
13 13 from unittest.mock import patch
14 14 from os.path import join, abspath
15 15 from importlib import reload
16 16
17 17 import pytest
18 18
19 19 import IPython
20 20 from IPython import paths
21 21 from IPython.testing import decorators as dec
22 22 from IPython.testing.decorators import (
23 23 skip_if_not_win32,
24 24 skip_win32,
25 25 onlyif_unicode_paths,
26 26 )
27 27 from IPython.testing.tools import make_tempfile
28 28 from IPython.utils import path
29 29 from IPython.utils.tempdir import TemporaryDirectory
30 30
31 31
32 32 # Platform-dependent imports
33 33 try:
34 34 import winreg as wreg
35 35 except ImportError:
36 36 #Fake _winreg module on non-windows platforms
37 37 import types
38 38 wr_name = "winreg"
39 39 sys.modules[wr_name] = types.ModuleType(wr_name)
40 40 try:
41 41 import winreg as wreg
42 42 except ImportError:
43 43 import _winreg as wreg
44 44 #Add entries that needs to be stubbed by the testing code
45 45 (wreg.OpenKey, wreg.QueryValueEx,) = (None, None)
46 46
47 47 #-----------------------------------------------------------------------------
48 48 # Globals
49 49 #-----------------------------------------------------------------------------
50 50 env = os.environ
51 51 TMP_TEST_DIR = tempfile.mkdtemp()
52 52 HOME_TEST_DIR = join(TMP_TEST_DIR, "home_test_dir")
53 53 #
54 54 # Setup/teardown functions/decorators
55 55 #
56 56
57 57 def setup_module():
58 58 """Setup testenvironment for the module:
59 59
60 60 - Adds dummy home dir tree
61 61 """
62 62 # Do not mask exceptions here. In particular, catching WindowsError is a
63 63 # problem because that exception is only defined on Windows...
64 64 os.makedirs(os.path.join(HOME_TEST_DIR, 'ipython'))
65 65
66 66
67 67 def teardown_module():
68 68 """Teardown testenvironment for the module:
69 69
70 70 - Remove dummy home dir tree
71 71 """
72 72 # Note: we remove the parent test dir, which is the root of all test
73 73 # subdirs we may have created. Use shutil instead of os.removedirs, so
74 74 # that non-empty directories are all recursively removed.
75 75 shutil.rmtree(TMP_TEST_DIR)
76 76
77 77
78 78 def setup_environment():
79 79 """Setup testenvironment for some functions that are tested
80 80 in this module. In particular this functions stores attributes
81 81 and other things that we need to stub in some test functions.
82 82 This needs to be done on a function level and not module level because
83 83 each testfunction needs a pristine environment.
84 84 """
85 85 global oldstuff, platformstuff
86 86 oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())
87 87
88 88 def teardown_environment():
89 89 """Restore things that were remembered by the setup_environment function
90 90 """
91 91 (oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff
92 92 os.chdir(old_wd)
93 93 reload(path)
94 94
95 95 for key in list(env):
96 96 if key not in oldenv:
97 97 del env[key]
98 98 env.update(oldenv)
99 99 if hasattr(sys, 'frozen'):
100 100 del sys.frozen
101 101
102 102
103 103 # Build decorator that uses the setup_environment/setup_environment
104 104 @pytest.fixture
105 105 def environment():
106 106 setup_environment()
107 107 yield
108 108 teardown_environment()
109 109
110 110
111 111 with_environment = pytest.mark.usefixtures("environment")
112 112
113 113
114 114 @skip_if_not_win32
115 115 @with_environment
116 116 def test_get_home_dir_1():
117 117 """Testcase for py2exe logic, un-compressed lib
118 118 """
119 119 unfrozen = path.get_home_dir()
120 120 sys.frozen = True
121 121
122 122 #fake filename for IPython.__init__
123 123 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Lib/IPython/__init__.py"))
124 124
125 125 home_dir = path.get_home_dir()
126 126 assert home_dir == unfrozen
127 127
128 128
129 129 @skip_if_not_win32
130 130 @with_environment
131 131 def test_get_home_dir_2():
132 132 """Testcase for py2exe logic, compressed lib
133 133 """
134 134 unfrozen = path.get_home_dir()
135 135 sys.frozen = True
136 136 #fake filename for IPython.__init__
137 137 IPython.__file__ = abspath(join(HOME_TEST_DIR, "Library.zip/IPython/__init__.py")).lower()
138 138
139 139 home_dir = path.get_home_dir(True)
140 140 assert home_dir == unfrozen
141 141
142 142
143 143 @skip_win32
144 144 @with_environment
145 145 def test_get_home_dir_3():
146 146 """get_home_dir() uses $HOME if set"""
147 147 env["HOME"] = HOME_TEST_DIR
148 148 home_dir = path.get_home_dir(True)
149 149 # get_home_dir expands symlinks
150 150 assert home_dir == os.path.realpath(env["HOME"])
151 151
152 152
153 153 @with_environment
154 154 def test_get_home_dir_4():
155 155 """get_home_dir() still works if $HOME is not set"""
156 156
157 157 if 'HOME' in env: del env['HOME']
158 158 # this should still succeed, but we don't care what the answer is
159 159 home = path.get_home_dir(False)
160 160
161 161 @skip_win32
162 162 @with_environment
163 163 def test_get_home_dir_5():
164 164 """raise HomeDirError if $HOME is specified, but not a writable dir"""
165 165 env['HOME'] = abspath(HOME_TEST_DIR+'garbage')
166 166 # set os.name = posix, to prevent My Documents fallback on Windows
167 167 os.name = 'posix'
168 168 pytest.raises(path.HomeDirError, path.get_home_dir, True)
169 169
170 170 # Should we stub wreg fully so we can run the test on all platforms?
171 171 @skip_if_not_win32
172 172 @with_environment
173 173 def test_get_home_dir_8():
174 174 """Using registry hack for 'My Documents', os=='nt'
175 175
176 176 HOMESHARE, HOMEDRIVE, HOMEPATH, USERPROFILE and others are missing.
177 177 """
178 178 os.name = 'nt'
179 179 # Remove from stub environment all keys that may be set
180 180 for key in ['HOME', 'HOMESHARE', 'HOMEDRIVE', 'HOMEPATH', 'USERPROFILE']:
181 181 env.pop(key, None)
182 182
183 183 class key:
184 184 def __enter__(self):
185 185 pass
186 186 def Close(self):
187 187 pass
188 188 def __exit__(*args, **kwargs):
189 189 pass
190 190
191 191 with patch.object(wreg, 'OpenKey', return_value=key()), \
192 192 patch.object(wreg, 'QueryValueEx', return_value=[abspath(HOME_TEST_DIR)]):
193 193 home_dir = path.get_home_dir()
194 194 assert home_dir == abspath(HOME_TEST_DIR)
195 195
196 196 @with_environment
197 197 def test_get_xdg_dir_0():
198 198 """test_get_xdg_dir_0, check xdg_dir"""
199 199 reload(path)
200 200 path._writable_dir = lambda path: True
201 201 path.get_home_dir = lambda : 'somewhere'
202 202 os.name = "posix"
203 203 sys.platform = "linux2"
204 204 env.pop('IPYTHON_DIR', None)
205 205 env.pop('IPYTHONDIR', None)
206 206 env.pop('XDG_CONFIG_HOME', None)
207 207
208 208 assert path.get_xdg_dir() == os.path.join("somewhere", ".config")
209 209
210 210
211 211 @with_environment
212 212 def test_get_xdg_dir_1():
213 213 """test_get_xdg_dir_1, check nonexistent xdg_dir"""
214 214 reload(path)
215 215 path.get_home_dir = lambda : HOME_TEST_DIR
216 216 os.name = "posix"
217 217 sys.platform = "linux2"
218 218 env.pop('IPYTHON_DIR', None)
219 219 env.pop('IPYTHONDIR', None)
220 220 env.pop('XDG_CONFIG_HOME', None)
221 221 assert path.get_xdg_dir() is None
222 222
223 223 @with_environment
224 224 def test_get_xdg_dir_2():
225 225 """test_get_xdg_dir_2, check xdg_dir default to ~/.config"""
226 226 reload(path)
227 227 path.get_home_dir = lambda : HOME_TEST_DIR
228 228 os.name = "posix"
229 229 sys.platform = "linux2"
230 230 env.pop('IPYTHON_DIR', None)
231 231 env.pop('IPYTHONDIR', None)
232 232 env.pop('XDG_CONFIG_HOME', None)
233 233 cfgdir=os.path.join(path.get_home_dir(), '.config')
234 234 if not os.path.exists(cfgdir):
235 235 os.makedirs(cfgdir)
236 236
237 237 assert path.get_xdg_dir() == cfgdir
238 238
239 239 @with_environment
240 240 def test_get_xdg_dir_3():
241 241 """test_get_xdg_dir_3, check xdg_dir not used on non-posix systems"""
242 242 reload(path)
243 243 path.get_home_dir = lambda : HOME_TEST_DIR
244 244 os.name = "nt"
245 245 sys.platform = "win32"
246 246 env.pop('IPYTHON_DIR', None)
247 247 env.pop('IPYTHONDIR', None)
248 248 env.pop('XDG_CONFIG_HOME', None)
249 249 cfgdir=os.path.join(path.get_home_dir(), '.config')
250 250 os.makedirs(cfgdir, exist_ok=True)
251 251
252 252 assert path.get_xdg_dir() is None
253 253
254 254 def test_filefind():
255 255 """Various tests for filefind"""
256 256 f = tempfile.NamedTemporaryFile()
257 257 # print 'fname:',f.name
258 258 alt_dirs = paths.get_ipython_dir()
259 259 t = path.filefind(f.name, alt_dirs)
260 260 # print 'found:',t
261 261
262 262
263 263 @dec.skip_if_not_win32
264 264 def test_get_long_path_name_win32():
265 265 with TemporaryDirectory() as tmpdir:
266 266
267 267 # Make a long path. Expands the path of tmpdir prematurely as it may already have a long
268 268 # path component, so ensure we include the long form of it
269 269 long_path = os.path.join(path.get_long_path_name(tmpdir), 'this is my long path name')
270 270 os.makedirs(long_path)
271 271
272 272 # Test to see if the short path evaluates correctly.
273 273 short_path = os.path.join(tmpdir, 'THISIS~1')
274 274 evaluated_path = path.get_long_path_name(short_path)
275 275 assert evaluated_path.lower() == long_path.lower()
276 276
277 277
278 278 @dec.skip_win32
279 279 def test_get_long_path_name():
280 280 p = path.get_long_path_name("/usr/local")
281 281 assert p == "/usr/local"
282 282
283 283
284 284 class TestRaiseDeprecation(unittest.TestCase):
285 285
286 286 @dec.skip_win32 # can't create not-user-writable dir on win
287 287 @with_environment
288 288 def test_not_writable_ipdir(self):
289 289 tmpdir = tempfile.mkdtemp()
290 290 os.name = "posix"
291 291 env.pop('IPYTHON_DIR', None)
292 292 env.pop('IPYTHONDIR', None)
293 293 env.pop('XDG_CONFIG_HOME', None)
294 294 env['HOME'] = tmpdir
295 295 ipdir = os.path.join(tmpdir, '.ipython')
296 296 os.mkdir(ipdir, 0o555)
297 297 try:
298 298 open(os.path.join(ipdir, "_foo_"), 'w').close()
299 299 except IOError:
300 300 pass
301 301 else:
302 302 # I can still write to an unwritable dir,
303 303 # assume I'm root and skip the test
304 304 pytest.skip("I can't create directories that I can't write to")
305 305
306 306 with self.assertWarnsRegex(UserWarning, 'is not a writable location'):
307 307 ipdir = paths.get_ipython_dir()
308 308 env.pop('IPYTHON_DIR', None)
309 309
310 310 @with_environment
311 311 def test_get_py_filename():
312 312 os.chdir(TMP_TEST_DIR)
313 313 with make_tempfile("foo.py"):
314 314 assert path.get_py_filename("foo.py") == "foo.py"
315 315 assert path.get_py_filename("foo") == "foo.py"
316 316 with make_tempfile("foo"):
317 317 assert path.get_py_filename("foo") == "foo"
318 318 pytest.raises(IOError, path.get_py_filename, "foo.py")
319 319 pytest.raises(IOError, path.get_py_filename, "foo")
320 320 pytest.raises(IOError, path.get_py_filename, "foo.py")
321 321 true_fn = "foo with spaces.py"
322 322 with make_tempfile(true_fn):
323 323 assert path.get_py_filename("foo with spaces") == true_fn
324 324 assert path.get_py_filename("foo with spaces.py") == true_fn
325 325 pytest.raises(IOError, path.get_py_filename, '"foo with spaces.py"')
326 326 pytest.raises(IOError, path.get_py_filename, "'foo with spaces.py'")
327 327
328 328 @onlyif_unicode_paths
329 329 def test_unicode_in_filename():
330 330 """When a file doesn't exist, the exception raised should be safe to call
331 331 str() on - i.e. in Python 2 it must only have ASCII characters.
332 332
333 333 https://github.com/ipython/ipython/issues/875
334 334 """
335 335 try:
336 336 # these calls should not throw unicode encode exceptions
337 337 path.get_py_filename('fooéè.py')
338 338 except IOError as ex:
339 339 str(ex)
340 340
341 341
342 342 class TestShellGlob(unittest.TestCase):
343 343
344 344 @classmethod
345 345 def setUpClass(cls):
346 346 cls.filenames_start_with_a = ['a0', 'a1', 'a2']
347 347 cls.filenames_end_with_b = ['0b', '1b', '2b']
348 348 cls.filenames = cls.filenames_start_with_a + cls.filenames_end_with_b
349 349 cls.tempdir = TemporaryDirectory()
350 350 td = cls.tempdir.name
351 351
352 352 with cls.in_tempdir():
353 353 # Create empty files
354 354 for fname in cls.filenames:
355 355 open(os.path.join(td, fname), 'w').close()
356 356
357 357 @classmethod
358 358 def tearDownClass(cls):
359 359 cls.tempdir.cleanup()
360 360
361 361 @classmethod
362 362 @contextmanager
363 363 def in_tempdir(cls):
364 364 save = os.getcwd()
365 365 try:
366 366 os.chdir(cls.tempdir.name)
367 367 yield
368 368 finally:
369 369 os.chdir(save)
370 370
371 371 def check_match(self, patterns, matches):
372 372 with self.in_tempdir():
373 373 # glob returns unordered list. that's why sorted is required.
374 374 assert sorted(path.shellglob(patterns)) == sorted(matches)
375 375
376 376 def common_cases(self):
377 377 return [
378 378 (['*'], self.filenames),
379 379 (['a*'], self.filenames_start_with_a),
380 380 (['*c'], ['*c']),
381 381 (['*', 'a*', '*b', '*c'], self.filenames
382 382 + self.filenames_start_with_a
383 383 + self.filenames_end_with_b
384 384 + ['*c']),
385 385 (['a[012]'], self.filenames_start_with_a),
386 386 ]
387 387
388 388 @skip_win32
389 389 def test_match_posix(self):
390 390 for (patterns, matches) in self.common_cases() + [
391 391 ([r'\*'], ['*']),
392 392 ([r'a\*', 'a*'], ['a*'] + self.filenames_start_with_a),
393 393 ([r'a\[012]'], ['a[012]']),
394 394 ]:
395 395 self.check_match(patterns, matches)
396 396
397 397 @skip_if_not_win32
398 398 def test_match_windows(self):
399 399 for (patterns, matches) in self.common_cases() + [
400 400 # In windows, backslash is interpreted as path
401 401 # separator. Therefore, you can't escape glob
402 402 # using it.
403 403 ([r'a\*', 'a*'], [r'a\*'] + self.filenames_start_with_a),
404 404 ([r'a\[012]'], [r'a\[012]']),
405 405 ]:
406 406 self.check_match(patterns, matches)
407 407
408 408
409 409 @pytest.mark.parametrize(
410 'globstr, unescaped_globstr',
410 "globstr, unescaped_globstr",
411 411 [
412 412 (r"\*\[\!\]\?", "*[!]?"),
413 413 (r"\\*", r"\*"),
414 414 (r"\\\*", r"\*"),
415 415 (r"\\a", r"\a"),
416 (r"\a", r"\a")
417 ]
416 (r"\a", r"\a"),
417 ],
418 418 )
419 419 def test_unescape_glob(globstr, unescaped_globstr):
420 420 assert path.unescape_glob(globstr) == unescaped_globstr
421 421
422 422
423 423 @onlyif_unicode_paths
424 424 def test_ensure_dir_exists():
425 425 with TemporaryDirectory() as td:
426 426 d = os.path.join(td, 'βˆ‚ir')
427 427 path.ensure_dir_exists(d) # create it
428 428 assert os.path.isdir(d)
429 429 path.ensure_dir_exists(d) # no-op
430 430 f = os.path.join(td, 'Ζ’ile')
431 431 open(f, 'w').close() # touch
432 432 with pytest.raises(IOError):
433 433 path.ensure_dir_exists(f)
434 434
435 435 class TestLinkOrCopy(unittest.TestCase):
436 436 def setUp(self):
437 437 self.tempdir = TemporaryDirectory()
438 438 self.src = self.dst("src")
439 439 with open(self.src, "w") as f:
440 440 f.write("Hello, world!")
441 441
442 442 def tearDown(self):
443 443 self.tempdir.cleanup()
444 444
445 445 def dst(self, *args):
446 446 return os.path.join(self.tempdir.name, *args)
447 447
448 448 def assert_inode_not_equal(self, a, b):
449 449 assert (
450 450 os.stat(a).st_ino != os.stat(b).st_ino
451 451 ), "%r and %r do reference the same indoes" % (a, b)
452 452
453 453 def assert_inode_equal(self, a, b):
454 454 assert (
455 455 os.stat(a).st_ino == os.stat(b).st_ino
456 456 ), "%r and %r do not reference the same indoes" % (a, b)
457 457
458 458 def assert_content_equal(self, a, b):
459 459 with open(a) as a_f:
460 460 with open(b) as b_f:
461 461 assert a_f.read() == b_f.read()
462 462
463 463 @skip_win32
464 464 def test_link_successful(self):
465 465 dst = self.dst("target")
466 466 path.link_or_copy(self.src, dst)
467 467 self.assert_inode_equal(self.src, dst)
468 468
469 469 @skip_win32
470 470 def test_link_into_dir(self):
471 471 dst = self.dst("some_dir")
472 472 os.mkdir(dst)
473 473 path.link_or_copy(self.src, dst)
474 474 expected_dst = self.dst("some_dir", os.path.basename(self.src))
475 475 self.assert_inode_equal(self.src, expected_dst)
476 476
477 477 @skip_win32
478 478 def test_target_exists(self):
479 479 dst = self.dst("target")
480 480 open(dst, "w").close()
481 481 path.link_or_copy(self.src, dst)
482 482 self.assert_inode_equal(self.src, dst)
483 483
484 484 @skip_win32
485 485 def test_no_link(self):
486 486 real_link = os.link
487 487 try:
488 488 del os.link
489 489 dst = self.dst("target")
490 490 path.link_or_copy(self.src, dst)
491 491 self.assert_content_equal(self.src, dst)
492 492 self.assert_inode_not_equal(self.src, dst)
493 493 finally:
494 494 os.link = real_link
495 495
496 496 @skip_if_not_win32
497 497 def test_windows(self):
498 498 dst = self.dst("target")
499 499 path.link_or_copy(self.src, dst)
500 500 self.assert_content_equal(self.src, dst)
501 501
502 502 def test_link_twice(self):
503 503 # Linking the same file twice shouldn't leave duplicates around.
504 504 # See https://github.com/ipython/ipython/issues/6450
505 505 dst = self.dst('target')
506 506 path.link_or_copy(self.src, dst)
507 507 path.link_or_copy(self.src, dst)
508 508 self.assert_inode_equal(self.src, dst)
509 509 assert sorted(os.listdir(self.tempdir.name)) == ["src", "target"]
@@ -1,191 +1,191 b''
1 1 # encoding: utf-8
2 2 """
3 3 Tests for platutils.py
4 4 """
5 5
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import sys
18 18 import signal
19 19 import os
20 20 import time
21 21 from _thread import interrupt_main # Py 3
22 22 import threading
23 23
24 24 import pytest
25 25
26 26 from IPython.utils.process import (find_cmd, FindCmdError, arg_split,
27 27 system, getoutput, getoutputerror,
28 28 get_output_error_code)
29 29 from IPython.utils.capture import capture_output
30 30 from IPython.testing import decorators as dec
31 31 from IPython.testing import tools as tt
32 32
33 33 python = os.path.basename(sys.executable)
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Tests
37 37 #-----------------------------------------------------------------------------
38 38
39 39
40 40 @dec.skip_win32
41 41 def test_find_cmd_ls():
42 42 """Make sure we can find the full path to ls."""
43 43 path = find_cmd("ls")
44 44 assert path.endswith("ls")
45 45
46 46
47 47 @dec.skip_if_not_win32
48 48 def test_find_cmd_pythonw():
49 49 """Try to find pythonw on Windows."""
50 50 path = find_cmd('pythonw')
51 51 assert path.lower().endswith('pythonw.exe'), path
52 52
53 53
54 54 def test_find_cmd_fail():
55 55 """Make sure that FindCmdError is raised if we can't find the cmd."""
56 56 pytest.raises(FindCmdError, find_cmd, "asdfasdf")
57 57
58 58
59 59 @dec.skip_win32
60 60 @pytest.mark.parametrize(
61 'argstr, argv',
61 "argstr, argv",
62 62 [
63 ('hi', ['hi']),
64 (u'hi', [u'hi']),
65 ('hello there', ['hello', 'there']),
63 ("hi", ["hi"]),
64 (u"hi", [u"hi"]),
65 ("hello there", ["hello", "there"]),
66 66 # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
67 67 # Do not use \N because the tests crash with syntax error in
68 68 # some cases, for example windows python2.6.
69 (u'h\u01cello', [u'h\u01cello']),
70 ('something "with quotes"', ['something', '"with quotes"']),
71 ]
69 (u"h\u01cello", [u"h\u01cello"]),
70 ('something "with quotes"', ["something", '"with quotes"']),
71 ],
72 72 )
73 73 def test_arg_split(argstr, argv):
74 74 """Ensure that argument lines are correctly split like in a shell."""
75 75 assert arg_split(argstr) == argv
76 76
77 77
78 78 @dec.skip_if_not_win32
79 79 @pytest.mark.parametrize(
80 'argstr,argv',
80 "argstr,argv",
81 81 [
82 ('hi', ['hi']),
83 (u'hi', [u'hi']),
84 ('hello there', ['hello', 'there']),
85 (u'h\u01cello', [u'h\u01cello']),
86 ('something "with quotes"', ['something', 'with quotes']),
87 ]
82 ("hi", ["hi"]),
83 (u"hi", [u"hi"]),
84 ("hello there", ["hello", "there"]),
85 (u"h\u01cello", [u"h\u01cello"]),
86 ('something "with quotes"', ["something", "with quotes"]),
87 ],
88 88 )
89 89 def test_arg_split_win32(argstr, argv):
90 90 """Ensure that argument lines are correctly split like in a shell."""
91 91 assert arg_split(argstr) == argv
92 92
93 93
94 94 class SubProcessTestCase(tt.TempFileMixin):
95 95 def setUp(self):
96 96 """Make a valid python temp file."""
97 97 lines = [ "import sys",
98 98 "print('on stdout', end='', file=sys.stdout)",
99 99 "print('on stderr', end='', file=sys.stderr)",
100 100 "sys.stdout.flush()",
101 101 "sys.stderr.flush()"]
102 102 self.mktmp('\n'.join(lines))
103 103
104 104 def test_system(self):
105 105 status = system('%s "%s"' % (python, self.fname))
106 106 self.assertEqual(status, 0)
107 107
108 108 def test_system_quotes(self):
109 109 status = system('%s -c "import sys"' % python)
110 110 self.assertEqual(status, 0)
111 111
112 112 def assert_interrupts(self, command):
113 113 """
114 114 Interrupt a subprocess after a second.
115 115 """
116 116 if threading.main_thread() != threading.current_thread():
117 117 raise pytest.skip("Can't run this test if not in main thread.")
118 118
119 119 # Some tests can overwrite SIGINT handler (by using pdb for example),
120 120 # which then breaks this test, so just make sure it's operating
121 121 # normally.
122 122 signal.signal(signal.SIGINT, signal.default_int_handler)
123 123
124 124 def interrupt():
125 125 # Wait for subprocess to start:
126 126 time.sleep(0.5)
127 127 interrupt_main()
128 128
129 129 threading.Thread(target=interrupt).start()
130 130 start = time.time()
131 131 try:
132 132 result = command()
133 133 except KeyboardInterrupt:
134 134 # Success!
135 135 pass
136 136 end = time.time()
137 137 self.assertTrue(
138 138 end - start < 2, "Process didn't die quickly: %s" % (end - start)
139 139 )
140 140 return result
141 141
142 142 def test_system_interrupt(self):
143 143 """
144 144 When interrupted in the way ipykernel interrupts IPython, the
145 145 subprocess is interrupted.
146 146 """
147 147 def command():
148 148 return system('%s -c "import time; time.sleep(5)"' % python)
149 149
150 150 status = self.assert_interrupts(command)
151 151 self.assertNotEqual(
152 152 status, 0, "The process wasn't interrupted. Status: %s" % (status,)
153 153 )
154 154
155 155 def test_getoutput(self):
156 156 out = getoutput('%s "%s"' % (python, self.fname))
157 157 # we can't rely on the order the line buffered streams are flushed
158 158 try:
159 159 self.assertEqual(out, 'on stderron stdout')
160 160 except AssertionError:
161 161 self.assertEqual(out, 'on stdouton stderr')
162 162
163 163 def test_getoutput_quoted(self):
164 164 out = getoutput('%s -c "print (1)"' % python)
165 165 self.assertEqual(out.strip(), '1')
166 166
167 167 #Invalid quoting on windows
168 168 @dec.skip_win32
169 169 def test_getoutput_quoted2(self):
170 170 out = getoutput("%s -c 'print (1)'" % python)
171 171 self.assertEqual(out.strip(), '1')
172 172 out = getoutput("%s -c 'print (\"1\")'" % python)
173 173 self.assertEqual(out.strip(), '1')
174 174
175 175 def test_getoutput_error(self):
176 176 out, err = getoutputerror('%s "%s"' % (python, self.fname))
177 177 self.assertEqual(out, 'on stdout')
178 178 self.assertEqual(err, 'on stderr')
179 179
180 180 def test_get_output_error_code(self):
181 181 quiet_exit = '%s -c "import sys; sys.exit(1)"' % python
182 182 out, err, code = get_output_error_code(quiet_exit)
183 183 self.assertEqual(out, '')
184 184 self.assertEqual(err, '')
185 185 self.assertEqual(code, 1)
186 186 out, err, code = get_output_error_code('%s "%s"' % (python, self.fname))
187 187 self.assertEqual(out, 'on stdout')
188 188 self.assertEqual(err, 'on stderr')
189 189 self.assertEqual(code, 0)
190 190
191 191
General Comments 0
You need to be logged in to leave comments. Login now