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