»Core Development>Code coverage>Lib/test/test_winconsoleio.py

Python code coverage for Lib/test/test_winconsoleio.py

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