test_io.py
94 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
|
r2498 | # encoding: utf-8 | ||
"""Tests for io.py""" | ||||
|
r19932 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
|
r2498 | |||
|
r13690 | import io as stdlib_io | ||
|
r17570 | import os.path | ||
|
r17831 | import stat | ||
|
r2498 | import sys | ||
|
r23108 | from io import StringIO | ||
|
r2498 | |||
|
r3800 | from subprocess import Popen, PIPE | ||
|
r12372 | import unittest | ||
|
r2498 | |||
import nose.tools as nt | ||||
|
r19932 | from IPython.testing.decorators import skipif, skip_win32 | ||
|
r23229 | from IPython.utils.io import IOStream, Tee, capture_output | ||
|
r23108 | from IPython.utils.py3compat import doctest_refactor_print | ||
|
r17570 | from IPython.utils.tempdir import TemporaryDirectory | ||
|
r13366 | |||
|
r2498 | |||
def test_tee_simple(): | ||||
"Very simple check with stdout only" | ||||
chan = StringIO() | ||||
text = 'Hello' | ||||
tee = Tee(chan, channel='stdout') | ||||
|
r7817 | print(text, file=chan) | ||
|
r4891 | nt.assert_equal(chan.getvalue(), text+"\n") | ||
|
r2498 | |||
|
r12372 | class TeeTestCase(unittest.TestCase): | ||
|
r2498 | |||
def tchan(self, channel, check='close'): | ||||
trap = StringIO() | ||||
chan = StringIO() | ||||
text = 'Hello' | ||||
std_ori = getattr(sys, channel) | ||||
setattr(sys, channel, trap) | ||||
tee = Tee(chan, channel=channel) | ||||
|
r7817 | print(text, end='', file=chan) | ||
|
r2498 | setattr(sys, channel, std_ori) | ||
trap_val = trap.getvalue() | ||||
|
r7875 | nt.assert_equal(chan.getvalue(), text) | ||
|
r2498 | if check=='close': | ||
tee.close() | ||||
else: | ||||
del tee | ||||
def test(self): | ||||
for chan in ['stdout', 'stderr']: | ||||
for check in ['close', 'del']: | ||||
|
r12372 | self.tchan(chan, check) | ||
|
r3800 | |||
def test_io_init(): | ||||
"""Test that io.stdin/out/err exist at startup""" | ||||
for name in ('stdin', 'stdout', 'stderr'): | ||||
|
r4891 | cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name) | ||
p = Popen([sys.executable, '-c', cmd], | ||||
|
r3800 | stdout=PIPE) | ||
p.wait() | ||||
|
r4891 | classname = p.stdout.read().strip().decode('ascii') | ||
# __class__ is a reference to the class object in Python 3, so we can't | ||||
# just test for string equality. | ||||
assert 'IPython.utils.io.IOStream' in classname, classname | ||||
|
r7324 | |||
|
r23229 | def test_IOStream_init(): | ||
"""IOStream initializes from a file-like object missing attributes. """ | ||||
# Cause a failure from getattr and dir(). (Issue #6386) | ||||
class BadStringIO(StringIO): | ||||
def __dir__(self): | ||||
attrs = super(StringIO, self).__dir__() | ||||
attrs.append('name') | ||||
return attrs | ||||
iostream = IOStream(BadStringIO()) | ||||
iostream.write('hi, bad iostream\n') | ||||
assert not hasattr(iostream, 'name') | ||||
|
r7324 | def test_capture_output(): | ||
"""capture_output() context works""" | ||||
with capture_output() as io: | ||||
|
r7817 | print('hi, stdout') | ||
print('hi, stderr', file=sys.stderr) | ||||
|
r7324 | |||
|
r7875 | nt.assert_equal(io.stdout, 'hi, stdout\n') | ||
nt.assert_equal(io.stderr, 'hi, stderr\n') | ||||
|
r13690 | |||
|
r17570 | |||