##// END OF EJS Templates
Don't use io.rprint in jupyter_client.session...
Don't use io.rprint in jupyter_client.session use log.warn

File last commit:

r21111:87b8ee5c
r21112:3408aef4
Show More
test_io.py
201 lines | 6.0 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""Tests for io.py"""
Min RK
preserve umask in atomic_writing...
r19932 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 from __future__ import print_function
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 from __future__ import absolute_import
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690 import io as stdlib_io
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 import os.path
Thomas Kluyver
Copy file metadata in atomic save...
r17831 import stat
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import sys
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 from subprocess import Popen, PIPE
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 import unittest
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
import nose.tools as nt
Min RK
preserve umask in atomic_writing...
r19932 from IPython.testing.decorators import skipif, skip_win32
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 from IPython.utils.io import (Tee, capture_output, unicode_std_stream,
atomic_writing,
)
Thomas Kluyver
Use StringIO.StringIO on Python 2....
r13366 from IPython.utils.py3compat import doctest_refactor_print, PY3
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 from IPython.utils.tempdir import TemporaryDirectory
Thomas Kluyver
Use StringIO.StringIO on Python 2....
r13366
if PY3:
from io import StringIO
else:
from StringIO import StringIO
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def test_tee_simple():
"Very simple check with stdout only"
chan = StringIO()
text = 'Hello'
tee = Tee(chan, channel='stdout')
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print(text, file=chan)
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 nt.assert_equal(chan.getvalue(), text+"\n")
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 class TeeTestCase(unittest.TestCase):
Brian Granger
Work to address the review comments on Fernando's branch....
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
use print function in module with `print >>`
r7817 print(text, end='', file=chan)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 setattr(sys, channel, std_ori)
trap_val = trap.getvalue()
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(chan.getvalue(), text)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if check=='close':
tee.close()
else:
del tee
def test(self):
for chan in ['stdout', 'stderr']:
for check in ['close', 'del']:
Thomas Kluyver
Remove most uses of ParametricTestCase
r12372 self.tchan(chan, check)
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800
def test_io_init():
"""Test that io.stdin/out/err exist at startup"""
for name in ('stdin', 'stdout', 'stderr'):
Thomas Kluyver
Various fixes to tests in IPython.utils.
r4891 cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name)
p = Popen([sys.executable, '-c', cmd],
MinRK
io.Term.cin/out/err replaced by io.stdin/out/err...
r3800 stdout=PIPE)
p.wait()
Thomas Kluyver
Various fixes to tests in IPython.utils.
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
move capture_output util from parallel tests to utils.io
r7324
def test_capture_output():
"""capture_output() context works"""
with capture_output() as io:
Matthias BUSSONNIER
use print function in module with `print >>`
r7817 print('hi, stdout')
print('hi, stderr', file=sys.stderr)
MinRK
move capture_output util from parallel tests to utils.io
r7324
Bradley M. Froehle
s/nt.assert_equals/nt.assert_equal/
r7875 nt.assert_equal(io.stdout, 'hi, stdout\n')
nt.assert_equal(io.stderr, 'hi, stderr\n')
Thomas Kluyver
Add unicode_std_stream function to write UTF-8 to stdout/stderr
r13690
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570
def test_atomic_writing():
class CustomExc(Exception): pass
with TemporaryDirectory() as td:
f1 = os.path.join(td, 'penguin')
with stdlib_io.open(f1, 'w') as f:
f.write(u'Before')
Thomas Kluyver
Copy file metadata in atomic save...
r17831
if os.name != 'nt':
os.chmod(f1, 0o701)
orig_mode = stat.S_IMODE(os.stat(f1).st_mode)
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570
Thomas Kluyver
Preserve symlinks in atomic writing
r17832 f2 = os.path.join(td, 'flamingo')
try:
os.symlink(f1, f2)
have_symlink = True
Thomas Kluyver
Interpret another error as missing symlink support on Windows...
r18074 except (AttributeError, NotImplementedError, OSError):
# AttributeError: Python doesn't support it
# NotImplementedError: The system doesn't support it
# OSError: The user lacks the privilege (Windows)
Thomas Kluyver
Preserve symlinks in atomic writing
r17832 have_symlink = False
Thomas Kluyver
Rework atomic_writing with tests & docstring
r17570 with nt.assert_raises(CustomExc):
with atomic_writing(f1) as f:
f.write(u'Failing write')
raise CustomExc
# Because of the exception, the file should not have been modified
with stdlib_io.open(f1, 'r') as f:
nt.assert_equal(f.read(), u'Before')
with atomic_writing(f1) as f:
f.write(u'Overwritten')
with stdlib_io.open(f1, 'r') as f:
nt.assert_equal(f.read(), u'Overwritten')
Thomas Kluyver
Copy file metadata in atomic save...
r17831
if os.name != 'nt':
mode = stat.S_IMODE(os.stat(f1).st_mode)
nt.assert_equal(mode, orig_mode)
Thomas Kluyver
Preserve symlinks in atomic writing
r17832
if have_symlink:
# Check that writing over a file preserves a symlink
with atomic_writing(f2) as f:
f.write(u'written from symlink')
with stdlib_io.open(f1, 'r') as f:
Min RK
don't use text mode in mkstemp...
r18799 nt.assert_equal(f.read(), u'written from symlink')
Min RK
preserve umask in atomic_writing...
r19932 def _save_umask():
global umask
umask = os.umask(0)
os.umask(umask)
def _restore_umask():
os.umask(umask)
@skip_win32
@nt.with_setup(_save_umask, _restore_umask)
def test_atomic_writing_umask():
with TemporaryDirectory() as td:
os.umask(0o022)
f1 = os.path.join(td, '1')
with atomic_writing(f1) as f:
f.write(u'1')
mode = stat.S_IMODE(os.stat(f1).st_mode)
nt.assert_equal(mode, 0o644, '{:o} != 644'.format(mode))
os.umask(0o057)
f2 = os.path.join(td, '2')
with atomic_writing(f2) as f:
f.write(u'2')
mode = stat.S_IMODE(os.stat(f2).st_mode)
nt.assert_equal(mode, 0o620, '{:o} != 620'.format(mode))
Min RK
don't use text mode in mkstemp...
r18799 def test_atomic_writing_newlines():
with TemporaryDirectory() as td:
path = os.path.join(td, 'testfile')
lf = u'a\nb\nc\n'
plat = lf.replace(u'\n', os.linesep)
crlf = lf.replace(u'\n', u'\r\n')
# test default
with stdlib_io.open(path, 'w') as f:
f.write(lf)
with stdlib_io.open(path, 'r', newline='') as f:
read = f.read()
nt.assert_equal(read, plat)
# test newline=LF
with stdlib_io.open(path, 'w', newline='\n') as f:
f.write(lf)
with stdlib_io.open(path, 'r', newline='') as f:
read = f.read()
nt.assert_equal(read, lf)
# test newline=CRLF
with atomic_writing(path, newline='\r\n') as f:
f.write(lf)
with stdlib_io.open(path, 'r', newline='') as f:
read = f.read()
nt.assert_equal(read, crlf)
# test newline=no convert
text = u'crlf\r\ncr\rlf\n'
with atomic_writing(path, newline='') as f:
f.write(text)
with stdlib_io.open(path, 'r', newline='') as f:
read = f.read()
nt.assert_equal(read, text)