Uploaded Test files

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-12 11:05:57 -05:00
parent f584ad9d97
commit 2e81cb7d99
16627 changed files with 2065359 additions and 102444 deletions

View file

@ -0,0 +1,6 @@
import pytest
@pytest.fixture(autouse=True)
def env_setup(monkeypatch):
monkeypatch.setenv("JUPYTER_CONSOLE_TEST", "1")

View file

@ -0,0 +1,84 @@
"""Tests for two-process terminal frontend"""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import shutil
import sys
import tempfile
from subprocess import check_output
from nose import SkipTest
from traitlets.tests.utils import check_help_all_output
from ipython_genutils.testing import decorators as dec
@dec.skip_win32
def test_console_starts():
"""test that `jupyter console` starts a terminal"""
p, pexpect, t = start_console()
p.sendline('5')
idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=t)
idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=t)
stop_console(p, pexpect, t)
def test_help_output():
"""jupyter console --help-all works"""
check_help_all_output('jupyter_console')
def test_display_text():
"Ensure display protocol plain/text key is supported"
# equivalent of:
#
# x = %lsmagic
# from IPython.display import display; display(x);
p, pexpect, t = start_console()
p.sendline('x = %lsmagic')
p.expect(r'In \[\d+\]', timeout=t)
p.sendline('from IPython.display import display; display(x);')
p.expect(r'Available line magics:', timeout=t)
p.expect(r'In \[\d+\]', timeout=t)
stop_console(p, pexpect, t)
def stop_console(p, pexpect, t):
"Stop a running `jupyter console` running via pexpect"
# send ctrl-D;ctrl-D to exit
p.sendeof()
p.sendeof()
p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=t)
if p.isalive():
p.terminate()
def start_console():
"Start `jupyter console` using pexpect"
import pexpect
args = ['-m', 'jupyter_console', '--colors=NoColor']
cmd = sys.executable
env = os.environ.copy()
env["JUPYTER_CONSOLE_TEST"] = "1"
env["PROMPT_TOOLKIT_NO_CPR"] = "1"
try:
p = pexpect.spawn(cmd, args=args, env=env)
except IOError:
raise SkipTest("Couldn't find command %s" % cmd)
# timeout after one minute
t = 60
idx = p.expect(r'In \[\d+\]', timeout=t)
return p, pexpect, t
def test_generate_config():
"""jupyter console --generate-config works"""
td = tempfile.mkdtemp()
try:
check_output([sys.executable, '-m', 'jupyter_console', '--generate-config'],
env={'JUPYTER_CONFIG_DIR': td},
)
assert os.path.isfile(os.path.join(td, 'jupyter_console_config.py'))
finally:
shutil.rmtree(td)

View file

@ -0,0 +1,102 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
import sys
import unittest
import base64
from unittest.mock import patch
from jupyter_console.ptshell import ZMQTerminalInteractiveShell
from ipython_genutils.tempdir import TemporaryDirectory
from ipython_genutils.testing.decorators import skip_without
from ipython_genutils.ipstruct import Struct
SCRIPT_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'writetofile.py')
class NonCommunicatingShell(ZMQTerminalInteractiveShell):
"""A testing shell class that doesn't attempt to communicate with the kernel"""
def init_kernel_info(self):
pass
class ZMQTerminalInteractiveShellTestCase(unittest.TestCase):
def setUp(self):
self.shell = NonCommunicatingShell()
self.raw = b'dummy data'
self.mime = 'image/png'
self.data = {self.mime: base64.encodebytes(self.raw).decode('ascii')}
def test_call_pil_by_default(self):
pil_called_with = []
def pil_called(data, mime):
pil_called_with.append(data)
def raise_if_called(*args, **kwds):
assert False
shell = self.shell
shell.handle_image_PIL = pil_called
shell.handle_image_stream = raise_if_called
shell.handle_image_tempfile = raise_if_called
shell.handle_image_callable = raise_if_called
shell.handle_image(None, None) # arguments are dummy
assert len(pil_called_with) == 1
@skip_without('PIL')
def test_handle_image_PIL(self):
from PIL import Image, ImageShow
open_called_with = []
show_called_with = []
def fake_open(arg):
open_called_with.append(arg)
def fake_show(img):
show_called_with.append(img)
with patch.object(Image, 'open', fake_open), \
patch.object(ImageShow, 'show', fake_show):
self.shell.handle_image_PIL(self.data, self.mime)
self.assertEqual(len(open_called_with), 1)
self.assertEqual(len(show_called_with), 1)
self.assertEqual(open_called_with[0].getvalue(), self.raw)
def check_handler_with_file(self, inpath, handler):
shell = self.shell
configname = '{0}_image_handler'.format(handler)
funcname = 'handle_image_{0}'.format(handler)
assert hasattr(shell, configname)
assert hasattr(shell, funcname)
with TemporaryDirectory() as tmpdir:
outpath = os.path.join(tmpdir, 'data')
cmd = [sys.executable, SCRIPT_PATH, inpath, outpath]
setattr(shell, configname, cmd)
getattr(shell, funcname)(self.data, self.mime)
# cmd is called and file is closed. So it's safe to open now.
with open(outpath, 'rb') as file:
transferred = file.read()
self.assertEqual(transferred, self.raw)
def test_handle_image_stream(self):
self.check_handler_with_file('-', 'stream')
def test_handle_image_tempfile(self):
self.check_handler_with_file('{file}', 'tempfile')
def test_handle_image_callable(self):
called_with = []
self.shell.callable_image_handler = called_with.append
self.shell.handle_image_callable(self.data, self.mime)
self.assertEqual(len(called_with), 1)
assert called_with[0] is self.data

View file

@ -0,0 +1,29 @@
#-----------------------------------------------------------------------------
# Copyright (C) 2012 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
"""
Copy data from input file to output file for testing.
Command line usage:
python writetofile.py INPUT OUTPUT
Binary data from INPUT file is copied to OUTPUT file.
If INPUT is '-', stdin is used.
"""
if __name__ == '__main__':
import sys
(inpath, outpath) = sys.argv[1:]
if inpath == '-':
infile = sys.stdin.buffer
else:
infile = open(inpath, 'rb')
open(outpath, 'w+b').write(infile.read())