| 1 |
# Copyright (c) IPython Development Team. |
| 2 |
# Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import base64 |
| 5 |
import os |
| 6 |
import sys |
| 7 |
from tempfile import TemporaryDirectory |
| 8 |
import unittest |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from jupyter_console.ptshell import ZMQTerminalInteractiveShell |
| 14 |
|
| 15 |
|
| 16 |
SCRIPT_PATH = os.path.join( |
| 17 |
os.path.abspath(os.path.dirname(__file__)), 'writetofile.py') |
| 18 |
|
| 19 |
class NonCommunicatingShell(ZMQTerminalInteractiveShell): |
| 20 |
"""A testing shell class that doesn't attempt to communicate with the kernel""" |
| 21 |
def init_kernel_info(self): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
class ZMQTerminalInteractiveShellTestCase(unittest.TestCase): |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
self.shell = NonCommunicatingShell() |
| 29 |
self.raw = b'dummy data' |
| 30 |
self.mime = 'image/png' |
| 31 |
self.data = {self.mime: base64.encodebytes(self.raw).decode('ascii')} |
| 32 |
|
| 33 |
def test_call_pil_by_default(self): |
| 34 |
pil_called_with = [] |
| 35 |
|
| 36 |
def pil_called(data, mime): |
| 37 |
pil_called_with.append(data) |
| 38 |
|
| 39 |
def raise_if_called(*args, **kwds): |
| 40 |
assert False |
| 41 |
|
| 42 |
shell = self.shell |
| 43 |
shell.handle_image_PIL = pil_called |
| 44 |
shell.handle_image_stream = raise_if_called |
| 45 |
shell.handle_image_tempfile = raise_if_called |
| 46 |
shell.handle_image_callable = raise_if_called |
| 47 |
|
| 48 |
shell.handle_image(None, None) # arguments are dummy |
| 49 |
assert len(pil_called_with) == 1 |
| 50 |
|
| 51 |
def test_handle_image_PIL(self): |
| 52 |
pytest.importorskip('PIL') |
| 53 |
from PIL import Image, ImageShow |
| 54 |
|
| 55 |
open_called_with = [] |
| 56 |
show_called_with = [] |
| 57 |
|
| 58 |
def fake_open(arg): |
| 59 |
open_called_with.append(arg) |
| 60 |
|
| 61 |
def fake_show(img): |
| 62 |
show_called_with.append(img) |
| 63 |
|
| 64 |
with patch.object(Image, 'open', fake_open), \ |
| 65 |
patch.object(ImageShow, 'show', fake_show): |
| 66 |
self.shell.handle_image_PIL(self.data, self.mime) |
| 67 |
|
| 68 |
self.assertEqual(len(open_called_with), 1) |
| 69 |
self.assertEqual(len(show_called_with), 1) |
| 70 |
self.assertEqual(open_called_with[0].getvalue(), self.raw) |
| 71 |
|
| 72 |
def check_handler_with_file(self, inpath, handler): |
| 73 |
shell = self.shell |
| 74 |
configname = '{0}_image_handler'.format(handler) |
| 75 |
funcname = 'handle_image_{0}'.format(handler) |
| 76 |
|
| 77 |
assert hasattr(shell, configname) |
| 78 |
assert hasattr(shell, funcname) |
| 79 |
|
| 80 |
with TemporaryDirectory() as tmpdir: |
| 81 |
outpath = os.path.join(tmpdir, 'data') |
| 82 |
cmd = [sys.executable, SCRIPT_PATH, inpath, outpath] |
| 83 |
setattr(shell, configname, cmd) |
| 84 |
getattr(shell, funcname)(self.data, self.mime) |
| 85 |
# cmd is called and file is closed. So it's safe to open now. |
| 86 |
with open(outpath, 'rb') as file: |
| 87 |
transferred = file.read() |
| 88 |
|
| 89 |
self.assertEqual(transferred, self.raw) |
| 90 |
|
| 91 |
def test_handle_image_stream(self): |
| 92 |
self.check_handler_with_file('-', 'stream') |
| 93 |
|
| 94 |
def test_handle_image_tempfile(self): |
| 95 |
self.check_handler_with_file('{file}', 'tempfile') |
| 96 |
|
| 97 |
def test_handle_image_callable(self): |
| 98 |
called_with = [] |
| 99 |
self.shell.callable_image_handler = called_with.append |
| 100 |
self.shell.handle_image_callable(self.data, self.mime) |
| 101 |
self.assertEqual(len(called_with), 1) |
| 102 |
assert called_with[0] is self.data |