Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion tools/tests/test_pr_overlap_encoding.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import contextmanager
import io
import json
import os
from pathlib import Path
Expand All @@ -16,6 +17,23 @@
TREE = "a" * 40


@contextmanager
def simulated_cp1252():
if hasattr(subprocess, "_text_encoding"):
with patch.object(subprocess, "_text_encoding", return_value="cp1252"):
yield
else:
wrapper = io.TextIOWrapper

def fake_wrapper(buffer, encoding=None, *args, **kwargs):
if encoding == "locale":
encoding = "cp1252"
return wrapper(buffer, encoding=encoding, *args, **kwargs)

with patch.object(io, "TextIOWrapper", new=fake_wrapper):
yield


@contextmanager
def command_output(stdout, stderr="", returncode=0):
code = ("import os, sys\n"
Expand All @@ -26,7 +44,7 @@ def command_output(stdout, stderr="", returncode=0):
def run(command, **kwargs):
return RUN([sys.executable, "-c", code], **kwargs)

with patch.object(subprocess, "_text_encoding", return_value="cp1252"), \
with simulated_cp1252(), \
patch.object(subprocess, "run", side_effect=run):
yield

Expand Down Expand Up @@ -75,6 +93,14 @@ def test_merge_error_preserves_unicode(self):
with self.assertRaisesRegex(RuntimeError, UNICODE):
pr_overlap.merge("main", "topic")

def test_default_encoding_uses_simulated_cp1252(self):
code = "import os;os.write(1," + repr(UNICODE.encode("utf-8")) + ")"
with simulated_cp1252():
default = RUN([sys.executable, "-c", code], capture_output=True, text=True, errors="replace")
self.assertEqual(default.stdout, UNICODE.encode("utf-8").decode("cp1252", errors="replace"))
explicit = RUN([sys.executable, "-c", code], capture_output=True, text=True, encoding="utf-8")
self.assertEqual(explicit.stdout, UNICODE)

def test_git_diff_survives_invalid_utf8(self):
with TemporaryDirectory() as directory:
repo = Path(directory)
Expand Down
Loading