test_io.py
86 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2498 | # encoding: utf-8 | ||
"""Tests for io.py""" | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r5390 | # Copyright (C) 2008-2011 The IPython Development Team | ||
Brian Granger
|
r2498 | # | ||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r7817 | from __future__ import print_function | ||
Brian Granger
|
r2498 | |||
import sys | ||||
MinRK
|
r4794 | from StringIO import StringIO | ||
MinRK
|
r3800 | from subprocess import Popen, PIPE | ||
Thomas Kluyver
|
r12372 | import unittest | ||
Brian Granger
|
r2498 | |||
import nose.tools as nt | ||||
MinRK
|
r7324 | from IPython.utils.io import Tee, capture_output | ||
Thomas Kluyver
|
r4891 | from IPython.utils.py3compat import doctest_refactor_print | ||
Brian Granger
|
r2498 | |||
#----------------------------------------------------------------------------- | ||||
# Tests | ||||
#----------------------------------------------------------------------------- | ||||
def test_tee_simple(): | ||||
"Very simple check with stdout only" | ||||
chan = StringIO() | ||||
text = 'Hello' | ||||
tee = Tee(chan, channel='stdout') | ||||
Matthias BUSSONNIER
|
r7817 | print(text, file=chan) | ||
Thomas Kluyver
|
r4891 | nt.assert_equal(chan.getvalue(), text+"\n") | ||
Brian Granger
|
r2498 | |||
Thomas Kluyver
|
r12372 | class TeeTestCase(unittest.TestCase): | ||
Brian Granger
|
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) | ||||
Matthias BUSSONNIER
|
r7817 | print(text, end='', file=chan) | ||
Brian Granger
|
r2498 | setattr(sys, channel, std_ori) | ||
trap_val = trap.getvalue() | ||||
Bradley M. Froehle
|
r7875 | nt.assert_equal(chan.getvalue(), text) | ||
Brian Granger
|
r2498 | if check=='close': | ||
tee.close() | ||||
else: | ||||
del tee | ||||
def test(self): | ||||
for chan in ['stdout', 'stderr']: | ||||
for check in ['close', 'del']: | ||||
Thomas Kluyver
|
r12372 | self.tchan(chan, check) | ||
MinRK
|
r3800 | |||
def test_io_init(): | ||||
"""Test that io.stdin/out/err exist at startup""" | ||||
for name in ('stdin', 'stdout', 'stderr'): | ||||
Thomas Kluyver
|
r4891 | cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name) | ||
p = Popen([sys.executable, '-c', cmd], | ||||
MinRK
|
r3800 | stdout=PIPE) | ||
p.wait() | ||||
Thomas Kluyver
|
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 | ||||
MinRK
|
r7324 | |||
def test_capture_output(): | ||||
"""capture_output() context works""" | ||||
with capture_output() as io: | ||||
Matthias BUSSONNIER
|
r7817 | print('hi, stdout') | ||
print('hi, stderr', file=sys.stderr) | ||||
MinRK
|
r7324 | |||
Bradley M. Froehle
|
r7875 | nt.assert_equal(io.stdout, 'hi, stdout\n') | ||
nt.assert_equal(io.stderr, 'hi, stderr\n') | ||||