##// END OF EJS Templates
Skip broken test on PY2
Thomas Kluyver -
Show More
@@ -1,100 +1,101 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for io.py"""
2 """Tests for io.py"""
3
3
4 # Copyright (c) IPython Development Team.
4 # Copyright (c) IPython Development Team.
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 from __future__ import print_function
7 from __future__ import print_function
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import io as stdlib_io
10 import io as stdlib_io
11 import os.path
11 import os.path
12 import stat
12 import stat
13 import sys
13 import sys
14
14
15 from subprocess import Popen, PIPE
15 from subprocess import Popen, PIPE
16 import unittest
16 import unittest
17
17
18 import nose.tools as nt
18 import nose.tools as nt
19
19
20 from IPython.testing.decorators import skipif, skip_win32
20 from IPython.testing.decorators import skipif, skip_win32, py3_only
21 from IPython.utils.io import IOStream, Tee, capture_output
21 from IPython.utils.io import IOStream, Tee, capture_output
22 from IPython.utils.py3compat import doctest_refactor_print, PY3
22 from IPython.utils.py3compat import doctest_refactor_print, PY3
23 from IPython.utils.tempdir import TemporaryDirectory
23 from IPython.utils.tempdir import TemporaryDirectory
24
24
25 if PY3:
25 if PY3:
26 from io import StringIO
26 from io import StringIO
27 else:
27 else:
28 from StringIO import StringIO
28 from StringIO import StringIO
29
29
30
30
31 def test_tee_simple():
31 def test_tee_simple():
32 "Very simple check with stdout only"
32 "Very simple check with stdout only"
33 chan = StringIO()
33 chan = StringIO()
34 text = 'Hello'
34 text = 'Hello'
35 tee = Tee(chan, channel='stdout')
35 tee = Tee(chan, channel='stdout')
36 print(text, file=chan)
36 print(text, file=chan)
37 nt.assert_equal(chan.getvalue(), text+"\n")
37 nt.assert_equal(chan.getvalue(), text+"\n")
38
38
39
39
40 class TeeTestCase(unittest.TestCase):
40 class TeeTestCase(unittest.TestCase):
41
41
42 def tchan(self, channel, check='close'):
42 def tchan(self, channel, check='close'):
43 trap = StringIO()
43 trap = StringIO()
44 chan = StringIO()
44 chan = StringIO()
45 text = 'Hello'
45 text = 'Hello'
46
46
47 std_ori = getattr(sys, channel)
47 std_ori = getattr(sys, channel)
48 setattr(sys, channel, trap)
48 setattr(sys, channel, trap)
49
49
50 tee = Tee(chan, channel=channel)
50 tee = Tee(chan, channel=channel)
51 print(text, end='', file=chan)
51 print(text, end='', file=chan)
52 setattr(sys, channel, std_ori)
52 setattr(sys, channel, std_ori)
53 trap_val = trap.getvalue()
53 trap_val = trap.getvalue()
54 nt.assert_equal(chan.getvalue(), text)
54 nt.assert_equal(chan.getvalue(), text)
55 if check=='close':
55 if check=='close':
56 tee.close()
56 tee.close()
57 else:
57 else:
58 del tee
58 del tee
59
59
60 def test(self):
60 def test(self):
61 for chan in ['stdout', 'stderr']:
61 for chan in ['stdout', 'stderr']:
62 for check in ['close', 'del']:
62 for check in ['close', 'del']:
63 self.tchan(chan, check)
63 self.tchan(chan, check)
64
64
65 def test_io_init():
65 def test_io_init():
66 """Test that io.stdin/out/err exist at startup"""
66 """Test that io.stdin/out/err exist at startup"""
67 for name in ('stdin', 'stdout', 'stderr'):
67 for name in ('stdin', 'stdout', 'stderr'):
68 cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name)
68 cmd = doctest_refactor_print("from IPython.utils import io;print io.%s.__class__"%name)
69 p = Popen([sys.executable, '-c', cmd],
69 p = Popen([sys.executable, '-c', cmd],
70 stdout=PIPE)
70 stdout=PIPE)
71 p.wait()
71 p.wait()
72 classname = p.stdout.read().strip().decode('ascii')
72 classname = p.stdout.read().strip().decode('ascii')
73 # __class__ is a reference to the class object in Python 3, so we can't
73 # __class__ is a reference to the class object in Python 3, so we can't
74 # just test for string equality.
74 # just test for string equality.
75 assert 'IPython.utils.io.IOStream' in classname, classname
75 assert 'IPython.utils.io.IOStream' in classname, classname
76
76
77 @py3_only
77 def test_IOStream_init():
78 def test_IOStream_init():
78 """IOStream initializes from a file-like object missing attributes. """
79 """IOStream initializes from a file-like object missing attributes. """
79 # Cause a failure from getattr and dir(). (Issue #6386)
80 # Cause a failure from getattr and dir(). (Issue #6386)
80 class BadStringIO(stdlib_io.StringIO):
81 class BadStringIO(stdlib_io.StringIO):
81 def __dir__(self):
82 def __dir__(self):
82 attrs = super(BadStringIO, self).__dir__()
83 attrs = super(BadStringIO, self).__dir__()
83 attrs.append('name')
84 attrs.append('name')
84 return attrs
85 return attrs
85
86
86 iostream = IOStream(BadStringIO())
87 iostream = IOStream(BadStringIO())
87 iostream.write(u'hi, bad iostream\n')
88 iostream.write(u'hi, bad iostream\n')
88 assert not hasattr(iostream, 'name')
89 assert not hasattr(iostream, 'name')
89
90
90 def test_capture_output():
91 def test_capture_output():
91 """capture_output() context works"""
92 """capture_output() context works"""
92
93
93 with capture_output() as io:
94 with capture_output() as io:
94 print('hi, stdout')
95 print('hi, stdout')
95 print('hi, stderr', file=sys.stderr)
96 print('hi, stderr', file=sys.stderr)
96
97
97 nt.assert_equal(io.stdout, 'hi, stdout\n')
98 nt.assert_equal(io.stdout, 'hi, stdout\n')
98 nt.assert_equal(io.stderr, 'hi, stderr\n')
99 nt.assert_equal(io.stderr, 'hi, stderr\n')
99
100
100
101
General Comments 0
You need to be logged in to leave comments. Login now