##// END OF EJS Templates
Set up shell command-line tab-completion for ipython...
Set up shell command-line tab-completion for ipython Set up shell command-line tab-completion using argcomplete and ipython/traitlets#811 argcomplete supports following setuptools console_scripts to the corresponding package's __main__.py to look for a PYTHON_ARGCOMPLETE_OK marker.

File last commit:

r27446:89fe38cd
r27960:3455b573
Show More
test_process.py
188 lines | 6.0 KiB | text/x-python | PythonLexer
Brian Granger
Adding test_platutils.py.
r1976 """
Tests for platutils.py
"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Brian Granger
Adding test_platutils.py.
r1976 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
Itamar Turner-Trauring
Fix the signal handler that was breaking the test
r25514 import signal
Julian Taylor
allow test_process to succeed without python installed
r10607 import os
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 import time
from _thread import interrupt_main # Py 3
import threading
Brian Granger
Adding test_platutils.py.
r1976
Samuel Gaist
[utils][tests][process] Remove nose
r26924 import pytest
Brian Granger
Adding test_platutils.py.
r1976
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 from IPython.utils.process import (find_cmd, FindCmdError, arg_split,
Paul Ivanov
test for get_output_error_code
r11826 system, getoutput, getoutputerror,
get_output_error_code)
Itamar Turner-Trauring
Test for filling stderr buffer.
r25507 from IPython.utils.capture import capture_output
Brian Granger
Adding test_platutils.py.
r1976 from IPython.testing import decorators as dec
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 from IPython.testing import tools as tt
Brian Granger
Adding test_platutils.py.
r1976
Julian Taylor
allow test_process to succeed without python installed
r10607 python = os.path.basename(sys.executable)
Brian Granger
Adding test_platutils.py.
r1976 #-----------------------------------------------------------------------------
# Tests
#-----------------------------------------------------------------------------
@dec.skip_win32
Fernando Perez
More win32 fixes and refinements to test suite.
r2453 def test_find_cmd_ls():
Brian Granger
Adding test_platutils.py.
r1976 """Make sure we can find the full path to ls."""
Samuel Gaist
[utils][tests][process] Remove nose
r26924 path = find_cmd("ls")
assert path.endswith("ls")
Brian Granger
Adding test_platutils.py.
r1976
Fernando Perez
More win32 fixes and refinements to test suite.
r2453
Nikita Kniazev
find_cmd is not using pywin32 for a while already
r27043 @dec.skip_if_not_win32
Fernando Perez
More win32 fixes and refinements to test suite.
r2453 def test_find_cmd_pythonw():
Brian Granger
Adding test_platutils.py.
r1976 """Try to find pythonw on Windows."""
path = find_cmd('pythonw')
Thomas Kluyver
Fix process test on Windows...
r21123 assert path.lower().endswith('pythonw.exe'), path
Brian Granger
Adding test_platutils.py.
r1976
Fernando Perez
More win32 fixes and refinements to test suite.
r2453
Brian Granger
Adding test_platutils.py.
r1976 def test_find_cmd_fail():
"""Make sure that FindCmdError is raised if we can't find the cmd."""
Samuel Gaist
[utils][tests][process] Remove nose
r26924 pytest.raises(FindCmdError, find_cmd, "asdfasdf")
Administrator
Added platutils.get_long_path_name to expand paths with "~" on win32....
r1986
Matthias Bussonnier
remove nose from test_process
r26717
Jörgen Stenarson
Special version of arg_split for win32
r5512 @dec.skip_win32
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 @pytest.mark.parametrize(
Matthias Bussonnier
autoformat
r27444 "argstr, argv",
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 [
Matthias Bussonnier
autoformat
r27444 ("hi", ["hi"]),
("hello there", ["hello", "there"]),
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 # \u01ce == \N{LATIN SMALL LETTER A WITH CARON}
# Do not use \N because the tests crash with syntax error in
# some cases, for example windows python2.6.
Matthias Bussonnier
remove/deduplicate tests with u-prefix
r27445 ("h\u01cello", ["h\u01cello"]),
Matthias Bussonnier
autoformat
r27444 ('something "with quotes"', ["something", '"with quotes"']),
],
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 )
Nate Rush
utils/tests: move to pytest.mark.parameterize in test_process.py
r27421 def test_arg_split(argstr, argv):
Fernando Perez
Work around a bug in Python's shlex module with unicode input....
r2650 """Ensure that argument lines are correctly split like in a shell."""
Nate Rush
utils/tests: move to pytest.mark.parameterize in test_process.py
r27421 assert arg_split(argstr) == argv
Jörgen Stenarson
Fixing some errors in the tests
r5513 @dec.skip_if_not_win32
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 @pytest.mark.parametrize(
Matthias Bussonnier
autoformat
r27444 "argstr,argv",
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 [
Matthias Bussonnier
autoformat
r27444 ("hi", ["hi"]),
("hello there", ["hello", "there"]),
Matthias Bussonnier
remove/deduplicate tests with u-prefix
r27445 ("h\u01cello", ["h\u01cello"]),
Matthias Bussonnier
autoformat
r27444 ('something "with quotes"', ["something", "with quotes"]),
],
Nate Rush
utils/tests: fix quotes in test_text.py
r27423 )
Nate Rush
utils/tests: move to pytest.mark.parameterize in test_process.py
r27421 def test_arg_split_win32(argstr, argv):
Jörgen Stenarson
Special version of arg_split for win32
r5512 """Ensure that argument lines are correctly split like in a shell."""
Nate Rush
utils/tests: move to pytest.mark.parameterize in test_process.py
r27421 assert arg_split(argstr) == argv
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Matthias Bussonnier
Fix Mixin Inheritence in Xunit unittest....
r25113 class SubProcessTestCase(tt.TempFileMixin):
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 def setUp(self):
"""Make a valid python temp file."""
Paul Ivanov
the __future__ is now.
r22963 lines = [ "import sys",
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 "print('on stdout', end='', file=sys.stdout)",
"print('on stderr', end='', file=sys.stderr)",
"sys.stdout.flush()",
"sys.stderr.flush()"]
self.mktmp('\n'.join(lines))
def test_system(self):
Matthias Bussonnier
apply pyupgrade
r27446 status = system(f'{python} "{self.fname}"')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(status, 0)
MinRK
fix arguments for commands in _process_posix...
r5296
def test_system_quotes(self):
Julian Taylor
allow test_process to succeed without python installed
r10607 status = system('%s -c "import sys"' % python)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(status, 0)
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
Itamar Turner-Trauring
Add interrupt test for getoutput(); disabled for now.
r25502 def assert_interrupts(self, command):
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 """
Itamar Turner-Trauring
Add interrupt test for getoutput(); disabled for now.
r25502 Interrupt a subprocess after a second.
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 """
if threading.main_thread() != threading.current_thread():
Samuel Gaist
[utils][tests][process] Remove nose
r26924 raise pytest.skip("Can't run this test if not in main thread.")
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497
Itamar Turner-Trauring
Fix the signal handler that was breaking the test
r25514 # Some tests can overwrite SIGINT handler (by using pdb for example),
# which then breaks this test, so just make sure it's operating
# normally.
signal.signal(signal.SIGINT, signal.default_int_handler)
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 def interrupt():
# Wait for subprocess to start:
time.sleep(0.5)
interrupt_main()
threading.Thread(target=interrupt).start()
Itamar Turner-Trauring
Make sure it exited quickly.
r25501 start = time.time()
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 try:
Itamar Turner-Trauring
Add interrupt test for getoutput(); disabled for now.
r25502 result = command()
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497 except KeyboardInterrupt:
# Success!
Itamar Turner-Trauring
Fix the signal handler that was breaking the test
r25514 pass
Itamar Turner-Trauring
Make sure it exited quickly.
r25501 end = time.time()
self.assertTrue(
end - start < 2, "Process didn't die quickly: %s" % (end - start)
)
Itamar Turner-Trauring
Add interrupt test for getoutput(); disabled for now.
r25502 return result
def test_system_interrupt(self):
"""
When interrupted in the way ipykernel interrupts IPython, the
subprocess is interrupted.
"""
def command():
return system('%s -c "import time; time.sleep(5)"' % python)
status = self.assert_interrupts(command)
self.assertNotEqual(
Matthias Bussonnier
apply pyupgrade
r27446 status, 0, f"The process wasn't interrupted. Status: {status}"
Itamar Turner-Trauring
Add interrupt test for getoutput(); disabled for now.
r25502 )
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908 def test_getoutput(self):
Matthias Bussonnier
apply pyupgrade
r27446 out = getoutput(f'{python} "{self.fname}"')
Julian Taylor
fix documentation and test of getoutput...
r10606 # we can't rely on the order the line buffered streams are flushed
try:
self.assertEqual(out, 'on stderron stdout')
except AssertionError:
self.assertEqual(out, 'on stdouton stderr')
Fernando Perez
Fully refactored subprocess handling on all platforms....
r2908
MinRK
fix arguments for commands in _process_posix...
r5296 def test_getoutput_quoted(self):
Julian Taylor
allow test_process to succeed without python installed
r10607 out = getoutput('%s -c "print (1)"' % python)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(out.strip(), '1')
Jörgen Stenarson
Splitting tests for getoutput_quoted into all and non-win32
r5514
#Invalid quoting on windows
@dec.skip_win32
def test_getoutput_quoted2(self):
Julian Taylor
allow test_process to succeed without python installed
r10607 out = getoutput("%s -c 'print (1)'" % python)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(out.strip(), '1')
Julian Taylor
allow test_process to succeed without python installed
r10607 out = getoutput("%s -c 'print (\"1\")'" % python)
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(out.strip(), '1')
MinRK
fix arguments for commands in _process_posix...
r5296
Julian Taylor
fix documentation and test of getoutput...
r10606 def test_getoutput_error(self):
Matthias Bussonnier
apply pyupgrade
r27446 out, err = getoutputerror(f'{python} "{self.fname}"')
Bradley M. Froehle
s/assertEquals/assertEqual/
r7874 self.assertEqual(out, 'on stdout')
self.assertEqual(err, 'on stderr')
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497
Paul Ivanov
test for get_output_error_code
r11826 def test_get_output_error_code(self):
quiet_exit = '%s -c "import sys; sys.exit(1)"' % python
out, err, code = get_output_error_code(quiet_exit)
self.assertEqual(out, '')
self.assertEqual(err, '')
self.assertEqual(code, 1)
Matthias Bussonnier
apply pyupgrade
r27446 out, err, code = get_output_error_code(f'{python} "{self.fname}"')
Paul Ivanov
test for get_output_error_code
r11826 self.assertEqual(out, 'on stdout')
self.assertEqual(err, 'on stderr')
self.assertEqual(code, 0)
Itamar Turner-Trauring
Test that the system() subprocess can be interrupted.
r25497