Files
mandy/tests/test_make_game_dotnet_sdk.py
Snooozs 6c6c6ca868 BUILD-DOTNET-SDKS-DECODE: decode dotnet --list-sdks with errors="replace"
dotnet_sdk_ok() ran `dotnet --list-sdks` with text=True and no errors=
argument. The text decode uses the console code page (cp1252 on a German
Windows), so an SDK path holding a byte that code page leaves undefined
(for example a profile name with Ł, č or ď) raised UnicodeDecodeError in
the reader thread. The check then got stdout None and raised TypeError,
which the caller does not catch, so preflight stopped before the build
began. The neighbouring calls already pass errors="replace".

The new tests/test_make_game_dotnet_sdk.py runs a real python child that
writes the raw bytes, and covers an SDK 8 install, a runtime-only install,
no dotnet on PATH, and an undecodable byte with and without an SDK. On
origin/main the two undecodable-byte cases error; with the fix all five pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 17:42:31 +01:00

65 lines
2.7 KiB
Python

"""make_game.dotnet_sdk_ok() reads `dotnet --list-sdks` as text in the console's code page.
python -I tests/test_make_game_dotnet_sdk.py
The child process is real: it writes its SDK list as bytes, including a byte that UTF-8 and cp1252 both refuse
(a Polish or Czech profile name in an SDK path). Only the executable is redirected to a python child, because
the test cannot install a dotnet; the decoding is the real subprocess.run(text=True) path. Before the fix, the
reader thread fails on the byte and the check raises TypeError instead of answering.
"""
import os
import shutil
import subprocess
import sys
import unittest
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
import make_game # noqa: E402
REAL_RUN = subprocess.run
def child_for(stdout: bytes):
"""A subprocess.run replacement that starts a real python child printing `stdout` as raw bytes for
`dotnet --list-sdks`. Every other argument, including text=True, is passed through unchanged."""
script = f"import sys; sys.stdout.buffer.write({stdout!r})"
def run(args, **kwargs):
if list(args[:2]) == ["dotnet", "--list-sdks"]:
args = [sys.executable, "-I", "-c", script]
return REAL_RUN(args, **kwargs)
return run
class DotnetSdkOkTest(unittest.TestCase):
def check(self, stdout: bytes) -> bool:
with mock.patch.object(make_game.shutil, "which", return_value="dotnet"), \
mock.patch.object(make_game.subprocess, "run", side_effect=child_for(stdout)):
return make_game.dotnet_sdk_ok()
def test_an_sdk_8_or_newer_is_found(self):
self.assertTrue(self.check(b"8.0.100 [C:\\Program Files\\dotnet\\sdk]\n"))
def test_a_runtime_only_install_has_no_sdk(self):
self.assertFalse(self.check(b"7.0.410 [C:\\Program Files\\dotnet\\sdk]\n"))
def test_no_dotnet_on_path_has_no_sdk(self):
with mock.patch.object(make_game.shutil, "which", return_value=None), \
mock.patch.object(make_game.subprocess, "run", side_effect=AssertionError("must not run")):
self.assertFalse(make_game.dotnet_sdk_ok())
def test_an_undecodable_byte_in_an_sdk_path_does_not_stop_the_check(self):
# 0x81 is undefined in cp1252 and is not valid UTF-8 on its own: the text decode fails on it.
self.assertTrue(self.check(b"7.0.410 [C:\\dotnet\\sdk]\n8.0.100 [C:\\Users\\\x81ukasz\\dotnet\\sdk]\n"))
def test_an_undecodable_byte_with_only_a_runtime_has_no_sdk(self):
self.assertFalse(self.check(b"7.0.410 [C:\\Users\\\x81ukasz\\dotnet\\sdk]\n"))
if __name__ == "__main__":
unittest.main()