| 1 |
"""Test installing editor hooks""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from IPython import get_ipython |
| 7 |
from IPython.lib import editorhooks |
| 8 |
|
| 9 |
|
| 10 |
def test_install_editor(): |
| 11 |
called = [] |
| 12 |
|
| 13 |
def fake_popen(*args, **kwargs): |
| 14 |
called.append( |
| 15 |
{ |
| 16 |
"args": args, |
| 17 |
"kwargs": kwargs, |
| 18 |
} |
| 19 |
) |
| 20 |
return mock.MagicMock(**{"wait.return_value": 0}) |
| 21 |
|
| 22 |
editorhooks.install_editor("foo -l {line} -f {filename}", wait=False) |
| 23 |
|
| 24 |
with mock.patch("subprocess.Popen", fake_popen): |
| 25 |
get_ipython().hooks.editor("the file", 64) |
| 26 |
|
| 27 |
assert len(called) == 1 |
| 28 |
args = called[0]["args"] |
| 29 |
kwargs = called[0]["kwargs"] |
| 30 |
|
| 31 |
assert kwargs == {"shell": True} |
| 32 |
|
| 33 |
if sys.platform.startswith("win"): |
| 34 |
expected = ["foo", "-l", "64", "-f", "the file"] |
| 35 |
else: |
| 36 |
expected = "foo -l 64 -f 'the file'" |
| 37 |
cmd = args[0] |
| 38 |
assert cmd == expected |