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
1 change: 0 additions & 1 deletion src/poetry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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(
brooke-ec marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
53 changes: 53 additions & 0 deletions tests/console/commands/self/test_system_pyproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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().strip("\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()

for _i, c in enumerate(generated[::-1]):
if c != "\n":
break

assert _i == existing_newlines
brooke-ec marked this conversation as resolved.
Show resolved Hide resolved


def test_generate_system_pyproject_carraige_returns(
brooke-ec marked this conversation as resolved.
Show resolved Hide resolved
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