| 1 |
import unittest |
| 2 |
from _apple_support import SystemLog |
| 3 |
from test.support import is_apple |
| 4 |
from unittest.mock import Mock, call |
| 5 |
|
| 6 |
if not is_apple: |
| 7 |
raise unittest.SkipTest("Apple-specific") |
| 8 |
|
| 9 |
|
| 10 |
# Test redirection of stdout and stderr to the Apple system log. |
| 11 |
class TestAppleSystemLogOutput(unittest.TestCase): |
| 12 |
maxDiff = None |
| 13 |
|
| 14 |
def assert_writes(self, output): |
| 15 |
self.assertEqual( |
| 16 |
self.log_write.mock_calls, |
| 17 |
[ |
| 18 |
call(self.log_level, line) |
| 19 |
for line in output |
| 20 |
] |
| 21 |
) |
| 22 |
|
| 23 |
self.log_write.reset_mock() |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
self.log_write = Mock() |
| 27 |
self.log_level = 42 |
| 28 |
self.log = SystemLog(self.log_write, self.log_level, errors="replace") |
| 29 |
|
| 30 |
def test_repr(self): |
| 31 |
self.assertEqual(repr(self.log), "<SystemLog (level 42)>") |
| 32 |
self.assertEqual(repr(self.log.buffer), "<LogStream (level 42)>") |
| 33 |
|
| 34 |
def test_log_config(self): |
| 35 |
self.assertIs(self.log.writable(), True) |
| 36 |
self.assertIs(self.log.readable(), False) |
| 37 |
|
| 38 |
self.assertEqual("UTF-8", self.log.encoding) |
| 39 |
self.assertEqual("replace", self.log.errors) |
| 40 |
|
| 41 |
self.assertIs(self.log.line_buffering, True) |
| 42 |
self.assertIs(self.log.write_through, False) |
| 43 |
|
| 44 |
def test_empty_str(self): |
| 45 |
self.log.write("") |
| 46 |
self.log.flush() |
| 47 |
|
| 48 |
self.assert_writes([]) |
| 49 |
|
| 50 |
def test_simple_str(self): |
| 51 |
self.log.write("hello world\n") |
| 52 |
|
| 53 |
self.assert_writes([b"hello world\n"]) |
| 54 |
|
| 55 |
def test_buffered_str(self): |
| 56 |
self.log.write("h") |
| 57 |
self.log.write("ello") |
| 58 |
self.log.write(" ") |
| 59 |
self.log.write("world\n") |
| 60 |
self.log.write("goodbye.") |
| 61 |
self.log.flush() |
| 62 |
|
| 63 |
self.assert_writes([b"hello world\n", b"goodbye."]) |
| 64 |
|
| 65 |
def test_manual_flush(self): |
| 66 |
self.log.write("Hello") |
| 67 |
|
| 68 |
self.assert_writes([]) |
| 69 |
|
| 70 |
self.log.write(" world\nHere for a while...\nGoodbye") |
| 71 |
self.assert_writes([b"Hello world\n", b"Here for a while...\n"]) |
| 72 |
|
| 73 |
self.log.write(" world\nHello again") |
| 74 |
self.assert_writes([b"Goodbye world\n"]) |
| 75 |
|
| 76 |
self.log.flush() |
| 77 |
self.assert_writes([b"Hello again"]) |
| 78 |
|
| 79 |
def test_non_ascii(self): |
| 80 |
# Spanish |
| 81 |
self.log.write("ol\u00e9\n") |
| 82 |
self.assert_writes([b"ol\xc3\xa9\n"]) |
| 83 |
|
| 84 |
# Chinese |
| 85 |
self.log.write("\u4e2d\u6587\n") |
| 86 |
self.assert_writes([b"\xe4\xb8\xad\xe6\x96\x87\n"]) |
| 87 |
|
| 88 |
# Printing Non-BMP emoji |
| 89 |
self.log.write("\U0001f600\n") |
| 90 |
self.assert_writes([b"\xf0\x9f\x98\x80\n"]) |
| 91 |
|
| 92 |
# Non-encodable surrogates are replaced |
| 93 |
self.log.write("\ud800\udc00\n") |
| 94 |
self.assert_writes([b"??\n"]) |
| 95 |
|
| 96 |
def test_modified_null(self): |
| 97 |
# Null characters are logged using "modified UTF-8". |
| 98 |
self.log.write("\u0000\n") |
| 99 |
self.assert_writes([b"\xc0\x80\n"]) |
| 100 |
self.log.write("a\u0000\n") |
| 101 |
self.assert_writes([b"a\xc0\x80\n"]) |
| 102 |
self.log.write("\u0000b\n") |
| 103 |
self.assert_writes([b"\xc0\x80b\n"]) |
| 104 |
self.log.write("a\u0000b\n") |
| 105 |
self.assert_writes([b"a\xc0\x80b\n"]) |
| 106 |
|
| 107 |
def test_nonstandard_str(self): |
| 108 |
# String subclasses are accepted, but they should be converted |
| 109 |
# to a standard str without calling any of their methods. |
| 110 |
class CustomStr(str): |
| 111 |
def splitlines(self, *args, **kwargs): |
| 112 |
raise AssertionError() |
| 113 |
|
| 114 |
def __len__(self): |
| 115 |
raise AssertionError() |
| 116 |
|
| 117 |
def __str__(self): |
| 118 |
raise AssertionError() |
| 119 |
|
| 120 |
self.log.write(CustomStr("custom\n")) |
| 121 |
self.assert_writes([b"custom\n"]) |
| 122 |
|
| 123 |
def test_non_str(self): |
| 124 |
# Non-string classes are not accepted. |
| 125 |
for obj in [b"", b"hello", None, 42]: |
| 126 |
with self.subTest(obj=obj): |
| 127 |
with self.assertRaisesRegex( |
| 128 |
TypeError, |
| 129 |
fr"write\(\) argument must be str, not " |
| 130 |
fr"{type(obj).__name__}" |
| 131 |
): |
| 132 |
self.log.write(obj) |
| 133 |
|
| 134 |
def test_byteslike_in_buffer(self): |
| 135 |
# The underlying buffer *can* accept bytes-like objects |
| 136 |
self.log.buffer.write(bytearray(b"hello")) |
| 137 |
self.log.flush() |
| 138 |
|
| 139 |
self.log.buffer.write(b"") |
| 140 |
self.log.flush() |
| 141 |
|
| 142 |
self.log.buffer.write(b"goodbye") |
| 143 |
self.log.flush() |
| 144 |
|
| 145 |
self.assert_writes([b"hello", b"goodbye"]) |
| 146 |
|
| 147 |
def test_non_byteslike_in_buffer(self): |
| 148 |
for obj in ["hello", None, 42]: |
| 149 |
with self.subTest(obj=obj): |
| 150 |
with self.assertRaisesRegex( |
| 151 |
TypeError, |
| 152 |
fr"write\(\) argument must be bytes-like, not " |
| 153 |
fr"{type(obj).__name__}" |
| 154 |
): |
| 155 |
self.log.buffer.write(obj) |