Home | History | Annotate | Download | only in test
      1 '''Tests for WindowsConsoleIO
      2 '''
      3 
      4 import io
      5 import os
      6 import sys
      7 import tempfile
      8 import unittest
      9 from test import support
     10 
     11 if sys.platform != 'win32':
     12     raise unittest.SkipTest("test only relevant on win32")
     13 
     14 from _testconsole import write_input
     15 
     16 ConIO = io._WindowsConsoleIO
     17 
     18 class WindowsConsoleIOTests(unittest.TestCase):
     19     def test_abc(self):
     20         self.assertTrue(issubclass(ConIO, io.RawIOBase))
     21         self.assertFalse(issubclass(ConIO, io.BufferedIOBase))
     22         self.assertFalse(issubclass(ConIO, io.TextIOBase))
     23 
     24     def test_open_fd(self):
     25         self.assertRaisesRegex(ValueError,
     26             "negative file descriptor", ConIO, -1)
     27 
     28         fd, _ = tempfile.mkstemp()
     29         try:
     30             # Windows 10: "Cannot open non-console file"
     31             # Earlier: "Cannot open console output buffer for reading"
     32             self.assertRaisesRegex(ValueError,
     33                 "Cannot open (console|non-console file)", ConIO, fd)
     34         finally:
     35             os.close(fd)
     36 
     37         try:
     38             f = ConIO(0)
     39         except ValueError:
     40             # cannot open console because it's not a real console
     41             pass
     42         else:
     43             self.assertTrue(f.readable())
     44             self.assertFalse(f.writable())
     45             self.assertEqual(0, f.fileno())
     46             f.close()   # multiple close should not crash
     47             f.close()
     48 
     49         try:
     50             f = ConIO(1, 'w')
     51         except ValueError:
     52             # cannot open console because it's not a real console
     53             pass
     54         else:
     55             self.assertFalse(f.readable())
     56             self.assertTrue(f.writable())
     57             self.assertEqual(1, f.fileno())
     58             f.close()
     59             f.close()
     60 
     61         try:
     62             f = ConIO(2, 'w')
     63         except ValueError:
     64             # cannot open console because it's not a real console
     65             pass
     66         else:
     67             self.assertFalse(f.readable())
     68             self.assertTrue(f.writable())
     69             self.assertEqual(2, f.fileno())
     70             f.close()
     71             f.close()
     72 
     73     def test_open_name(self):
     74         self.assertRaises(ValueError, ConIO, sys.executable)
     75 
     76         f = ConIO("CON")
     77         self.assertTrue(f.readable())
     78         self.assertFalse(f.writable())
     79         self.assertIsNotNone(f.fileno())
     80         f.close()   # multiple close should not crash
     81         f.close()
     82 
     83         f = ConIO('CONIN$')
     84         self.assertTrue(f.readable())
     85         self.assertFalse(f.writable())
     86         self.assertIsNotNone(f.fileno())
     87         f.close()
     88         f.close()
     89 
     90         f = ConIO('CONOUT$', 'w')
     91         self.assertFalse(f.readable())
     92         self.assertTrue(f.writable())
     93         self.assertIsNotNone(f.fileno())
     94         f.close()
     95         f.close()
     96 
     97         f = open('C:/con', 'rb', buffering=0)
     98         self.assertIsInstance(f, ConIO)
     99         f.close()
    100 
    101     @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1),
    102         "test does not work on Windows 7 and earlier")
    103     def test_conin_conout_names(self):
    104         f = open(r'\\.\conin$', 'rb', buffering=0)
    105         self.assertIsInstance(f, ConIO)
    106         f.close()
    107 
    108         f = open('//?/conout$', 'wb', buffering=0)
    109         self.assertIsInstance(f, ConIO)
    110         f.close()
    111 
    112     def test_conout_path(self):
    113         temp_path = tempfile.mkdtemp()
    114         self.addCleanup(support.rmtree, temp_path)
    115 
    116         conout_path = os.path.join(temp_path, 'CONOUT$')
    117 
    118         with open(conout_path, 'wb', buffering=0) as f:
    119             if sys.getwindowsversion()[:2] > (6, 1):
    120                 self.assertIsInstance(f, ConIO)
    121             else:
    122                 self.assertNotIsInstance(f, ConIO)
    123 
    124     def assertStdinRoundTrip(self, text):
    125         stdin = open('CONIN$', 'r')
    126         old_stdin = sys.stdin
    127         try:
    128             sys.stdin = stdin
    129             write_input(
    130                 stdin.buffer.raw,
    131                 (text + '\r\n').encode('utf-16-le', 'surrogatepass')
    132             )
    133             actual = input()
    134         finally:
    135             sys.stdin = old_stdin
    136         self.assertEqual(actual, text)
    137 
    138     def test_input(self):
    139         # ASCII
    140         self.assertStdinRoundTrip('abc123')
    141         # Non-ASCII
    142         self.assertStdinRoundTrip('')
    143         # Combining characters
    144         self.assertStdinRoundTrip('AB AA')
    145         # Non-BMP
    146         self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
    147 
    148     def test_partial_reads(self):
    149         # Test that reading less than 1 full character works when stdin
    150         # contains multibyte UTF-8 sequences
    151         source = '\r\n'.encode('utf-16-le')
    152         expected = '\r\n'.encode('utf-8')
    153         for read_count in range(1, 16):
    154             with open('CONIN$', 'rb', buffering=0) as stdin:
    155                 write_input(stdin, source)
    156 
    157                 actual = b''
    158                 while not actual.endswith(b'\n'):
    159                     b = stdin.read(read_count)
    160                     actual += b
    161 
    162                 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
    163 
    164     def test_partial_surrogate_reads(self):
    165         # Test that reading less than 1 full character works when stdin
    166         # contains surrogate pairs that cannot be decoded to UTF-8 without
    167         # reading an extra character.
    168         source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
    169         expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
    170         for read_count in range(1, 16):
    171             with open('CONIN$', 'rb', buffering=0) as stdin:
    172                 write_input(stdin, source)
    173 
    174                 actual = b''
    175                 while not actual.endswith(b'\n'):
    176                     b = stdin.read(read_count)
    177                     actual += b
    178 
    179                 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
    180 
    181     def test_ctrl_z(self):
    182         with open('CONIN$', 'rb', buffering=0) as stdin:
    183             source = '\xC4\x1A\r\n'.encode('utf-16-le')
    184             expected = '\xC4'.encode('utf-8')
    185             write_input(stdin, source)
    186             a, b = stdin.read(1), stdin.readall()
    187             self.assertEqual(expected[0:1], a)
    188             self.assertEqual(expected[1:], b)
    189 
    190 if __name__ == "__main__":
    191     unittest.main()
    192