diff --git a/src/poetry/console/commands/self/self_command.py b/src/poetry/console/commands/self/self_command.py index 0c1a4ec3f79..5be8f8b65e7 100644 --- a/src/poetry/console/commands/self/self_command.py +++ b/src/poetry/console/commands/self/self_command.py @@ -77,7 +77,8 @@ def generate_system_pyproject(self) -> None: for key in preserved: content["tool"]["poetry"][key] = preserved[key] # type: ignore[index] - self.system_pyproject.write_text(content.as_string(), encoding="utf-8") + pyproject = PyProjectTOML(self.system_pyproject) + pyproject.file.write(content) def reset_poetry(self) -> None: with directory(self.system_pyproject.parent): diff --git a/src/poetry/factory.py b/src/poetry/factory.py index 43960b43285..c8ec61d4cdd 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -190,9 +190,7 @@ def create_package_source( ) @classmethod - def create_pyproject_from_package( - cls, package: Package, path: Path | None = None - ) -> TOMLDocument: + def create_pyproject_from_package(cls, package: Package) -> TOMLDocument: import tomlkit from poetry.utils.dependency_specification import dependency_to_specification @@ -290,12 +288,6 @@ def create_pyproject_from_package( content["extras"] = extras_section pyproject = cast("TOMLDocument", pyproject) - pyproject.add(tomlkit.nl()) - - if path: - path.joinpath("pyproject.toml").write_text( - pyproject.as_string(), encoding="utf-8" - ) return pyproject diff --git a/tests/console/commands/self/test_self_command.py b/tests/console/commands/self/test_self_command.py new file mode 100644 index 00000000000..c07be3059d5 --- /dev/null +++ b/tests/console/commands/self/test_self_command.py @@ -0,0 +1,49 @@ +from __future__ import annotations + +import pytest + +from poetry.core.packages.dependency import Dependency +from poetry.core.packages.package import Package +from poetry.core.packages.project_package import ProjectPackage + +from poetry.__version__ import __version__ +from poetry.console.commands.self.self_command import SelfCommand +from poetry.factory import Factory + + +@pytest.fixture +def example_system_pyproject(): + package = ProjectPackage("poetry-instance", __version__) + plugin = Package("poetry-plugin", "1.2.3") + + package.add_dependency( + Dependency(plugin.name, "^1.2.3", groups=[SelfCommand.ADDITIONAL_PACKAGE_GROUP]) + ) + content = Factory.create_pyproject_from_package(package) + return content.as_string().rstrip("\n") + + +@pytest.mark.parametrize("existing_newlines", [0, 2]) +def test_generate_system_pyproject_trailing_newline( + existing_newlines: int, + example_system_pyproject: str, +): + cmd = SelfCommand() + cmd.system_pyproject.write_text(example_system_pyproject + "\n" * existing_newlines) + cmd.generate_system_pyproject() + generated = cmd.system_pyproject.read_text() + + assert len(generated) - len(generated.rstrip("\n")) == existing_newlines + + +def test_generate_system_pyproject_carriage_returns( + example_system_pyproject: str, +): + cmd = SelfCommand() + cmd.system_pyproject.write_text(example_system_pyproject + "\n") + cmd.generate_system_pyproject() + + with open(cmd.system_pyproject, newline="") as f: # do not translate newlines + generated = f.read() + + assert "\r\r" not in generated