Pin text I/O to UTF-8 and fail CI on locale-dependent reads/writes (#3296)

Co-authored-by: ShuQingDollarVoyager <57471784+ShuQingDollarVoyager@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Max 2026-08-16 12:41:53 +01:00 • committed by GitHub
commit 5285e936a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 76 additions and 57 deletions

View file

@ -16,7 +16,7 @@ def temp_file():
File is automatically cleaned up after the test if it still exists.
"""
content = "test content"
with NamedTemporaryFile(mode="w", delete=False) as f:
with NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as f:
f.write(content)
path = Path(f.name).resolve()
yield path

View file

@ -13,9 +13,9 @@ def test_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:
tmp = tmp_path_factory.mktemp("test_files")
# Create test files
(tmp / "example.py").write_text("print('hello world')")
(tmp / "readme.md").write_text("# Test Directory\nThis is a test.")
(tmp / "config.json").write_text('{"test": true}')
(tmp / "example.py").write_text("print('hello world')", encoding="utf-8")
(tmp / "readme.md").write_text("# Test Directory\nThis is a test.", encoding="utf-8")
(tmp / "config.json").write_text('{"test": true}', encoding="utf-8")
return tmp
@ -38,7 +38,7 @@ def resources(mcp: MCPServer, test_dir: Path) -> MCPServer:
def read_example_py() -> str:
"""Read the example.py file"""
try:
return (test_dir / "example.py").read_text()
return (test_dir / "example.py").read_text(encoding="utf-8")
except FileNotFoundError:
return "File not found"
@ -46,7 +46,7 @@ def resources(mcp: MCPServer, test_dir: Path) -> MCPServer:
def read_readme_md() -> str:
"""Read the readme.md file"""
try: # pragma: no cover
return (test_dir / "readme.md").read_text()
return (test_dir / "readme.md").read_text(encoding="utf-8")
except FileNotFoundError: # pragma: no cover
return "File not found"
@ -54,7 +54,7 @@ def resources(mcp: MCPServer, test_dir: Path) -> MCPServer:
def read_config_json() -> str:
"""Read the config.json file"""
try: # pragma: no cover
return (test_dir / "config.json").read_text()
return (test_dir / "config.json").read_text(encoding="utf-8")
except FileNotFoundError: # pragma: no cover
return "File not found"

View file

@ -819,7 +819,7 @@ class TestServerResources:
# Create a text file
text_file = tmp_path / "test.txt"
text_file.write_text("Hello from file!")
text_file.write_text("Hello from file!", encoding="utf-8")
resource = FileResource(uri="file://test.txt", name="test.txt", path=text_file)
mcp.add_resource(resource)