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