From b857f324d3406fe9fe78efba48a942279c48520e Mon Sep 17 00:00:00 2001 From: Jimisola Laursen Date: Sun, 22 Nov 2026 04:15:11 +0000 Subject: [PATCH] fix: read and write pyproject.toml and reqstool_config.yml as UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The plugin opened pyproject.toml and the generated reqstool_config.yml with the platform default encoding, which is cp1252 on Windows. - `poetry install` crashed with UnicodeDecodeError when pyproject.toml contains a UTF-8 character whose bytes cp1252 leaves undefined (for example "Á" or most non-Latin scripts). - reqstool_config.yml was written in cp1252, so a non-ASCII dataset path produced invalid UTF-8 for reqstool, or raised UnicodeEncodeError for characters cp1252 cannot encode. Pass encoding="utf-8" at all five call sites (TOML is UTF-8 by spec). Signed-off-by: Jimisola Laursen Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Mt2FAXZTXnbKG2Ho6GYy1z --- src/reqstool_python_poetry_plugin/plugin.py | 10 +-- .../test_plugin.py | 73 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/reqstool_python_poetry_plugin/plugin.py b/src/reqstool_python_poetry_plugin/plugin.py index 82fe898..632fca5 100644 --- a/src/reqstool_python_poetry_plugin/plugin.py +++ b/src/reqstool_python_poetry_plugin/plugin.py @@ -75,10 +75,10 @@ def _cleanup_pyproject_install_after_install(self) -> None: pyproject_path = Path(str(self._poetry.package.root_dir)) / "pyproject.toml" if not pyproject_path.exists(): return - content = pyproject_path.read_text() + content = pyproject_path.read_text(encoding="utf-8") cleaned = re.sub(r"\n{3,}", "\n\n", content) if cleaned != content: - pyproject_path.write_text(cleaned) + pyproject_path.write_text(cleaned, encoding="utf-8") self._cleo_io.write_line("[reqstool] Cleaned up excess blank lines in pyproject.toml") @Requirements("POETRY_PLUGIN_003") @@ -105,7 +105,7 @@ def _update_sdist_include(self) -> None: f"{reqstool_output_directory}/**/*", ] - with open(pyproject_path) as f: + with open(pyproject_path, encoding="utf-8") as f: data = tomlkit.load(f) tool_poetry = data.get("tool", {}).get("poetry", {}) @@ -119,7 +119,7 @@ def _update_sdist_include(self) -> None: if added: tool_poetry["include"] = existing_includes - with open(pyproject_path, "w") as f: + with open(pyproject_path, "w", encoding="utf-8") as f: tomlkit.dump(data, f) for entry in added: self._cleo_io.write_line(f"[reqstool] Added to pyproject.toml include: {entry}") @@ -204,7 +204,7 @@ def _generate_reqstool_config(self) -> None: output_path = self.get_reqstool_config_file(self._poetry) - with open(output_path, "w") as f: + with open(output_path, "w", encoding="utf-8") as f: f.write(f"{self.YAML_LANGUAGE_SERVER}\n") f.write(f"# version: {self._poetry.package.version}\n") yaml.dump(reqstool_yaml_data, f) diff --git a/tests/unit/reqstool_python_poetry_plugin/test_plugin.py b/tests/unit/reqstool_python_poetry_plugin/test_plugin.py index 80eb968..0db54e0 100644 --- a/tests/unit/reqstool_python_poetry_plugin/test_plugin.py +++ b/tests/unit/reqstool_python_poetry_plugin/test_plugin.py @@ -171,3 +171,76 @@ def test_generate_reqstool_config_normalizes_test_results_to_a_list(tmp_path, mo output = tmp_path / "reqstool_config.yml" data = YAML().load(output.read_text()) assert data["resources"]["test_results"] == ["build/junit.xml"] + + +# --------------------------------------------------------------------------- +# Text files are UTF-8 whatever the platform default encoding is (cp1252 on Windows) +# --------------------------------------------------------------------------- + +# "Á" is C3 81 in UTF-8 and 0x81 is undefined in cp1252; "✓" has no cp1252 mapping at all. +_NON_ASCII = "Ágnes Müller ✓" + + +@pytest.fixture +def cp1252_default_encoding(monkeypatch): + """Make open() and Path.read_text()/write_text() default to cp1252, as on a Windows machine.""" + import builtins + import pathlib + + real_open = builtins.open + real_read_text = pathlib.Path.read_text + real_write_text = pathlib.Path.write_text + + def open_cp1252_default(file, mode="r", *args, **kwargs): + if "b" not in mode and kwargs.get("encoding") is None and len(args) < 2: + kwargs["encoding"] = "cp1252" + return real_open(file, mode, *args, **kwargs) + + def read_text_cp1252_default(self, encoding=None, errors=None, newline=None): + return real_read_text(self, encoding=encoding or "cp1252", errors=errors, newline=newline) + + def write_text_cp1252_default(self, data, encoding=None, errors=None, newline=None): + return real_write_text(self, data, encoding=encoding or "cp1252", errors=errors, newline=newline) + + monkeypatch.setattr("reqstool_python_poetry_plugin.plugin.open", open_cp1252_default, raising=False) + monkeypatch.setattr(pathlib.Path, "read_text", read_text_cp1252_default) + monkeypatch.setattr(pathlib.Path, "write_text", write_text_cp1252_default) + + +@SVCs("SVC_POETRY_PLUGIN_003") +def test_update_sdist_include_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding): + pyproject_path = tmp_path / "pyproject.toml" + pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8")) + + plugin = _make_plugin(tmp_path) + plugin._update_sdist_include() + + data = tomlkit.loads(pyproject_path.read_bytes().decode("utf-8")) + assert data["tool"]["poetry"]["authors"] == [_NON_ASCII] + assert "reqstool_config.yml" in list(data["tool"]["poetry"]["include"]) + + +@SVCs("SVC_POETRY_PLUGIN_005") +def test_cleanup_pyproject_preserves_utf8_pyproject(tmp_path, cp1252_default_encoding): + pyproject_path = tmp_path / "pyproject.toml" + pyproject_path.write_bytes(f'[tool.poetry]\nname = "x"\n\n\n\nauthors = ["{_NON_ASCII}"]\n'.encode("utf-8")) + + plugin = _make_plugin(tmp_path) + plugin._cleanup_pyproject_install_after_install() + + expected = f'[tool.poetry]\nname = "x"\n\nauthors = ["{_NON_ASCII}"]\n' + assert pyproject_path.read_bytes().decode("utf-8") == expected + + +@SVCs("SVC_POETRY_PLUGIN_001") +def test_generate_reqstool_config_is_written_as_utf8(tmp_path, monkeypatch, cp1252_default_encoding): + monkeypatch.chdir(tmp_path) + dataset_dir = tmp_path / "dokumentation ✓" + dataset_dir.mkdir() + (dataset_dir / "requirements.yml").write_bytes(b"requirements: []\n") + + plugin = _make_plugin(tmp_path, reqstool_config={"dataset_directory": "dokumentation ✓"}) + plugin._generate_reqstool_config() + + written = (tmp_path / "reqstool_config.yml").read_bytes().decode("utf-8") + assert YAML().load(written)["resources"]["requirements"] == "dokumentation ✓/requirements.yml"