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