|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import io |
| 7 | +import unittest |
| 8 | +from unittest import mock |
| 9 | + |
| 10 | +from azure.cli.command_modules.containerapp import _ssh_utils |
| 11 | +from azure.cli.command_modules.containerapp._ssh_utils import ( |
| 12 | + _write_to_terminal, |
| 13 | + _decode_and_output_to_terminal, |
| 14 | +) |
| 15 | + |
| 16 | +# Lobster emoji (U+1F99E): valid UTF-8, but has no representation in cp1252. |
| 17 | +EMOJI = "\U0001f99e" |
| 18 | + |
| 19 | + |
| 20 | +class _NarrowStdout: |
| 21 | + """Mimics a console whose text codec (e.g. cp1252) cannot encode every |
| 22 | + character. Its ``write`` re-encodes like a real console, so emoji raises |
| 23 | + ``UnicodeEncodeError``; bytes written via the fallback land in ``buffer``.""" |
| 24 | + |
| 25 | + def __init__(self, encoding="cp1252"): |
| 26 | + self.encoding = encoding |
| 27 | + self.buffer = io.BytesIO() |
| 28 | + |
| 29 | + def write(self, text): |
| 30 | + text.encode(self.encoding) # raises UnicodeEncodeError on unencodable chars |
| 31 | + |
| 32 | + def flush(self): |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +class SshUtilsTerminalOutputTest(unittest.TestCase): |
| 37 | + def test_write_falls_back_when_char_not_encodable(self): |
| 38 | + text = f"hello {EMOJI} world" |
| 39 | + stdout = _NarrowStdout(encoding="cp1252") |
| 40 | + |
| 41 | + with mock.patch.object(_ssh_utils.sys, "stdout", stdout): |
| 42 | + _write_to_terminal(text) # must not raise UnicodeEncodeError |
| 43 | + |
| 44 | + self.assertEqual(stdout.buffer.getvalue(), |
| 45 | + text.encode("cp1252", errors="backslashreplace")) |
| 46 | + |
| 47 | + def test_write_uses_print_fast_path_when_encodable(self): |
| 48 | + stdout = _NarrowStdout(encoding="utf-8") |
| 49 | + |
| 50 | + with mock.patch.object(_ssh_utils.sys, "stdout", stdout): |
| 51 | + _write_to_terminal("plain ascii") |
| 52 | + |
| 53 | + # Encodable text goes through print(); the fallback buffer stays empty. |
| 54 | + self.assertEqual(stdout.buffer.getvalue(), b"") |
| 55 | + |
| 56 | + def test_decode_and_output_handles_emoji_without_disconnecting(self): |
| 57 | + payload = f"openclaw {EMOJI} v1".encode("utf-8") |
| 58 | + response = bytes([_ssh_utils.SSH_PROXY_FORWARD, |
| 59 | + _ssh_utils.SSH_CLUSTER_STDOUT]) + payload |
| 60 | + connection = mock.MagicMock() |
| 61 | + stdout = _NarrowStdout(encoding="cp1252") |
| 62 | + |
| 63 | + with mock.patch.object(_ssh_utils.sys, "stdout", stdout): |
| 64 | + _decode_and_output_to_terminal(connection, response, ["utf-8", "latin_1"]) |
| 65 | + |
| 66 | + connection.disconnect.assert_not_called() |
| 67 | + self.assertIn(b"openclaw", stdout.buffer.getvalue()) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + unittest.main() |
0 commit comments