Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generate_system_pyproject newline issues #7705

Merged
3 changes: 2 additions & 1 deletion src/poetry/console/commands/self/self_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 1 addition & 9 deletions src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
49 changes: 49 additions & 0 deletions tests/console/commands/self/test_self_command.py
Original file line number Diff line number Diff line change
@@ -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