0f84e4befe550d4386d24264648abf1323e682ff
[SubU] /
1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2 import sys
3 from unittest import TestCase, main
4
5 from ..ansitowin32 import StreamWrapper, AnsiToWin32
6 from .utils import pycharm, replace_by, replace_original_by, StreamTTY, StreamNonTTY
7
8
9 def is_a_tty(stream):
10     return StreamWrapper(stream, None).isatty()
11
12 class IsattyTest(TestCase):
13
14     def test_TTY(self):
15         tty = StreamTTY()
16         self.assertTrue(is_a_tty(tty))
17         with pycharm():
18             self.assertTrue(is_a_tty(tty))
19
20     def test_nonTTY(self):
21         non_tty = StreamNonTTY()
22         self.assertFalse(is_a_tty(non_tty))
23         with pycharm():
24             self.assertFalse(is_a_tty(non_tty))
25
26     def test_withPycharm(self):
27         with pycharm():
28             self.assertTrue(is_a_tty(sys.stderr))
29             self.assertTrue(is_a_tty(sys.stdout))
30
31     def test_withPycharmTTYOverride(self):
32         tty = StreamTTY()
33         with pycharm(), replace_by(tty):
34             self.assertTrue(is_a_tty(tty))
35
36     def test_withPycharmNonTTYOverride(self):
37         non_tty = StreamNonTTY()
38         with pycharm(), replace_by(non_tty):
39             self.assertFalse(is_a_tty(non_tty))
40
41     def test_withPycharmNoneOverride(self):
42         with pycharm():
43             with replace_by(None), replace_original_by(None):
44                 self.assertFalse(is_a_tty(None))
45                 self.assertFalse(is_a_tty(StreamNonTTY()))
46                 self.assertTrue(is_a_tty(StreamTTY()))
47
48     def test_withPycharmStreamWrapped(self):
49         with pycharm():
50             self.assertTrue(AnsiToWin32(StreamTTY()).stream.isatty())
51             self.assertFalse(AnsiToWin32(StreamNonTTY()).stream.isatty())
52             self.assertTrue(AnsiToWin32(sys.stdout).stream.isatty())
53             self.assertTrue(AnsiToWin32(sys.stderr).stream.isatty())
54
55
56 if __name__ == '__main__':
57     main()