aboutsummaryrefslogtreecommitdiff
path: root/test/test_pty.py
blob: 6606ff7bf743bfaba0d5cdd95bafd395dace8c2d (plain)
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
#!/usr/bin/env python
#
# This file is part of pySerial - Cross platform serial port support for Python
# (C) 2016 Chris Liechti <cliechti@gmx.net>
#
# SPDX-License-Identifier:    BSD-3-Clause
"""
Test PTY related functionality.
"""

import os
import sys

try:
    import pty
except ImportError:
    pty = None
import unittest
import serial

DATA = b'Hello\n'

@unittest.skipIf(pty is None, "pty module not supported on platform")
class Test_Pty_Serial_Open(unittest.TestCase):
    """Test PTY serial open"""

    def setUp(self):
        # Open PTY
        self.master, self.slave = pty.openpty()

    def test_pty_serial_open_slave(self):
        with serial.Serial(os.ttyname(self.slave), timeout=1) as slave:
            pass  # OK

    def test_pty_serial_write(self):
        with serial.Serial(os.ttyname(self.slave), timeout=1) as slave:
            with os.fdopen(self.master, "wb") as fd:
                fd.write(DATA)
                fd.flush()
                out = slave.read(len(DATA))
                self.assertEqual(DATA, out)

    def test_pty_serial_read(self):
        with serial.Serial(os.ttyname(self.slave), timeout=1) as slave:
            with os.fdopen(self.master, "rb") as fd:
                slave.write(DATA)
                slave.flush()
                out = fd.read(len(DATA))
                self.assertEqual(DATA, out)

if __name__ == '__main__':
    sys.stdout.write(__doc__)
    # When this module is executed from the command-line, it runs all its tests
    unittest.main()