File: test_io.py

package info (click to toggle)
ipython 9.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,624 kB
  • sloc: python: 45,268; sh: 317; makefile: 168
file content (60 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# encoding: utf-8
"""Tests for io.py"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.


import sys
from io import StringIO

import unittest

from IPython.utils.io import Tee, capture_output


def test_tee_simple():
    "Very simple check with stdout only"
    chan = StringIO()
    text = "Hello"
    tee = Tee(chan, channel="stdout")
    print(text, file=chan)
    assert chan.getvalue() == text + "\n"
    tee.close()


class TeeTestCase(unittest.TestCase):
    def tchan(self, channel):
        trap = StringIO()
        chan = StringIO()
        text = "Hello"

        std_ori = getattr(sys, channel)
        setattr(sys, channel, trap)

        tee = Tee(chan, channel=channel)

        print(text, end="", file=chan)
        trap_val = trap.getvalue()
        self.assertEqual(chan.getvalue(), text)

        tee.close()

        setattr(sys, channel, std_ori)
        assert getattr(sys, channel) == std_ori

    def test(self):
        for chan in ["stdout", "stderr"]:
            self.tchan(chan)


class TestIOStream(unittest.TestCase):
    def test_capture_output(self):
        """capture_output() context works"""

        with capture_output() as io:
            print("hi, stdout")
            print("hi, stderr", file=sys.stderr)

        self.assertEqual(io.stdout, "hi, stdout\n")
        self.assertEqual(io.stderr, "hi, stderr\n")