File: test_isatty.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (87 lines) | stat: -rw-r--r-- 3,093 bytes parent folder | download | duplicates (8)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import test.test_support, unittest
import os, popen2, subprocess, sys

def test_isatty(label, thingy):
    os_isatty = os.isatty(thingy.fileno())
    thingy_isatty = thingy.isatty()
    if 'in' in label: expected = stdin_isatty
    elif 'out' in label: expected = stdout_isatty
    elif 'err' in label: expected = stderr_isatty
    else: expected = False
    print '%11s: os.isatty=%.1s | .isatty=%.1s | expected=%.1s' % \
        (label, os_isatty, thingy_isatty, expected)
    assert expected == os_isatty == thingy_isatty, \
        'expected isatty would return %s on %s' % (expected, label)

def test_int_isatty(fd, expected):
    os_isatty = os.isatty(fd)
    print '%11s: os.isatty=%.1s | expected=%.1s' % \
        ('fd %d' % fd, os_isatty, expected)
    assert expected == os_isatty

def test_file_isatty(name):
    if not os.path.exists(name):
        return
    try:
        test_isatty(name, file(name))
    except IOError, e:
        print e # XXX Jython prints 'no such file or directory' - probably
                # 'permission denied' but Java doesn't understand?

def args_list(*args):
    return [sys.executable, __file__] + map(str, args)

class IsattyTest(unittest.TestCase):
    def check_call(self, *args, **kw):
        self.assertEqual(subprocess.check_call(args_list(*args), **kw), 0)

    def test_isatty(self):
        if os.name == 'java': # Jython doesn't allocate ptys here
            self.check_call(False, False, False)
            # XXX not sure how to test anything else
        else:
            self.check_call(True, True, True)
            self.check_call(False, True, True, stdin=subprocess.PIPE)
            self.check_call(True, False, True, stdout=subprocess.PIPE)
            self.check_call(True, True, False, stderr=subprocess.PIPE)

if __name__ == '__main__':
    if len(sys.argv) != 4:
        test.test_support.run_unittest(IsattyTest)
        sys.exit(0)

    stdin_isatty, stdout_isatty, stderr_isatty = map(lambda x: x == 'True',
                                                     sys.argv[1:])

    test_isatty('stdin',  sys.stdin)
    test_isatty('stdout', sys.stdout)
    test_isatty('stderr', sys.stderr)

    test_int_isatty(0, stdin_isatty)
    test_int_isatty(1, stdout_isatty)
    test_int_isatty(2, stderr_isatty)

    test_file_isatty('/dev/stdin')
    test_file_isatty('/dev/stdout')
    test_file_isatty('/dev/stderr')

    try:
        from java.lang import System
        test_isatty('System.in', file(getattr(System, 'in')))
        test_isatty('System.out', file(System.out, 'w'))
        test_isatty('System.err', file(System.err, 'w'))

        from java.io import FileDescriptor, FileInputStream, FileOutputStream
        fd_in = getattr(FileDescriptor, 'in')
        fd_out = FileDescriptor.out
        fd_err = FileDescriptor.err

        test_isatty('FIS(FD.in)', file(FileInputStream(fd_in)))
        test_isatty('FOS(FD.out)', file(FileOutputStream(fd_out)))
        test_isatty('FOS(FD.err)', file(FileOutputStream(fd_err)))
    except ImportError:
        pass

    test_file_isatty('/dev/null')

    sys.exit(0)