Show More
@@ -1,89 +1,87 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 | |
|
8 | 8 | import sys |
|
9 | 9 | from io import StringIO |
|
10 | 10 | |
|
11 | 11 | from subprocess import Popen, PIPE |
|
12 | 12 | import unittest |
|
13 | 13 | |
|
14 | import nose.tools as nt | |
|
15 | ||
|
16 | 14 | from IPython.utils.io import IOStream, Tee, capture_output |
|
17 | 15 | |
|
18 | 16 | |
|
19 | 17 | def test_tee_simple(): |
|
20 | 18 | "Very simple check with stdout only" |
|
21 | 19 | chan = StringIO() |
|
22 | 20 | text = 'Hello' |
|
23 | 21 | tee = Tee(chan, channel='stdout') |
|
24 | 22 | print(text, file=chan) |
|
25 |
|
|
|
23 | assert chan.getvalue() == text + "\n" | |
|
26 | 24 | |
|
27 | 25 | |
|
28 | 26 | class TeeTestCase(unittest.TestCase): |
|
29 | 27 | |
|
30 | 28 | def tchan(self, channel): |
|
31 | 29 | trap = StringIO() |
|
32 | 30 | chan = StringIO() |
|
33 | 31 | text = 'Hello' |
|
34 | 32 | |
|
35 | 33 | std_ori = getattr(sys, channel) |
|
36 | 34 | setattr(sys, channel, trap) |
|
37 | 35 | |
|
38 | 36 | tee = Tee(chan, channel=channel) |
|
39 | 37 | |
|
40 | 38 | print(text, end='', file=chan) |
|
41 | 39 | trap_val = trap.getvalue() |
|
42 |
|
|
|
40 | self.assertEqual(chan.getvalue(), text) | |
|
43 | 41 | |
|
44 | 42 | tee.close() |
|
45 | 43 | |
|
46 | 44 | setattr(sys, channel, std_ori) |
|
47 | 45 | assert getattr(sys, channel) == std_ori |
|
48 | 46 | |
|
49 | 47 | def test(self): |
|
50 | 48 | for chan in ['stdout', 'stderr']: |
|
51 | 49 | self.tchan(chan) |
|
52 | 50 | |
|
53 | 51 | def test_io_init(): |
|
54 | 52 | """Test that io.stdin/out/err exist at startup""" |
|
55 | 53 | for name in ('stdin', 'stdout', 'stderr'): |
|
56 | 54 | cmd = "from IPython.utils import io;print(io.%s.__class__)"%name |
|
57 | 55 | with Popen([sys.executable, '-c', cmd], stdout=PIPE) as p: |
|
58 | 56 | p.wait() |
|
59 | 57 | classname = p.stdout.read().strip().decode('ascii') |
|
60 | 58 | # __class__ is a reference to the class object in Python 3, so we can't |
|
61 | 59 | # just test for string equality. |
|
62 | 60 | assert 'IPython.utils.io.IOStream' in classname, classname |
|
63 | 61 | |
|
64 | 62 | class TestIOStream(unittest.TestCase): |
|
65 | 63 | |
|
66 | 64 | def test_IOStream_init(self): |
|
67 | 65 | """IOStream initializes from a file-like object missing attributes. """ |
|
68 | 66 | # Cause a failure from getattr and dir(). (Issue #6386) |
|
69 | 67 | class BadStringIO(StringIO): |
|
70 | 68 | def __dir__(self): |
|
71 | 69 | attrs = super().__dir__() |
|
72 | 70 | attrs.append('name') |
|
73 | 71 | return attrs |
|
74 | 72 | with self.assertWarns(DeprecationWarning): |
|
75 | 73 | iostream = IOStream(BadStringIO()) |
|
76 | 74 | iostream.write('hi, bad iostream\n') |
|
77 | 75 | |
|
78 | 76 | assert not hasattr(iostream, 'name') |
|
79 | 77 | iostream.close() |
|
80 | 78 | |
|
81 | 79 | def test_capture_output(self): |
|
82 | 80 | """capture_output() context works""" |
|
83 | 81 | |
|
84 | 82 | with capture_output() as io: |
|
85 |
print( |
|
|
86 |
print( |
|
|
87 | ||
|
88 |
|
|
|
89 |
|
|
|
83 | print("hi, stdout") | |
|
84 | print("hi, stderr", file=sys.stderr) | |
|
85 | ||
|
86 | self.assertEqual(io.stdout, "hi, stdout\n") | |
|
87 | self.assertEqual(io.stderr, "hi, stderr\n") |
General Comments 0
You need to be logged in to leave comments.
Login now