From 2ef7d6d24984915e43d8298b882097d2e2754891 Mon Sep 17 00:00:00 2001 From: NimajnebEC Date: Wed, 22 Mar 2023 17:18:48 +0000 Subject: [PATCH 1/8] Implemented fixes and added tests --- .../console/commands/self/self_command.py | 5 +- .../commands/self/test_system_pyproject.py | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/console/commands/self/test_system_pyproject.py diff --git a/src/poetry/console/commands/self/self_command.py b/src/poetry/console/commands/self/self_command.py index 0c1a4ec3f79..5cebbcff149 100644 --- a/src/poetry/console/commands/self/self_command.py +++ b/src/poetry/console/commands/self/self_command.py @@ -77,7 +77,10 @@ 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") + text = content.as_string().replace("\r\n", "\n") # remove carriage return + if text.endswith("\n\n"): + text = text[:-1] # remove extra newline if trailing newline already present + self.system_pyproject.write_text(text, encoding="utf-8") def reset_poetry(self) -> None: with directory(self.system_pyproject.parent): diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py new file mode 100644 index 00000000000..9b79a6d6705 --- /dev/null +++ b/tests/console/commands/self/test_system_pyproject.py @@ -0,0 +1,59 @@ +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()[:-2] # remove trailing newlines + + +@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, newline="" + ) # do not translate newlines + cmd.generate_system_pyproject() + generated = cmd.system_pyproject.read_text() + + for _i, c in enumerate(generated[::-1]): + if c != "\n": + break + + assert (existing_newlines < 2 and _i == 1) or ( + existing_newlines > 1 and _i == existing_newlines + ) + + +def test_generate_system_pyproject_carraige_returns( + example_system_pyproject: str, +): + cmd = SelfCommand() + cmd.system_pyproject.write_text( + example_system_pyproject + "\n" + ) # newlines will be translated + 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 From c233715fbace31b592c0a28bcc88cf4d1728a143 Mon Sep 17 00:00:00 2001 From: NimajnebEC <46959407+NimajnebEC@users.noreply.github.com> Date: Wed, 22 Mar 2023 23:18:00 +0000 Subject: [PATCH 2/8] Removed newline kwarg from test Removed newline kwarg from cmd.system_pyproject.write_text call in tests for compatibility with older versions. --- tests/console/commands/self/test_system_pyproject.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py index 9b79a6d6705..55a3920f84f 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_system_pyproject.py @@ -29,9 +29,7 @@ def test_generate_system_pyproject_trailing_newline( example_system_pyproject: str, ): cmd = SelfCommand() - cmd.system_pyproject.write_text( - example_system_pyproject + "\n" * existing_newlines, newline="" - ) # do not translate newlines + cmd.system_pyproject.write_text(example_system_pyproject + "\n" * existing_newlines) cmd.generate_system_pyproject() generated = cmd.system_pyproject.read_text() @@ -48,9 +46,7 @@ def test_generate_system_pyproject_carraige_returns( example_system_pyproject: str, ): cmd = SelfCommand() - cmd.system_pyproject.write_text( - example_system_pyproject + "\n" - ) # newlines will be translated + 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 From 05ba928c27260abbfbc67731958f37f2378f989b Mon Sep 17 00:00:00 2001 From: NimajnebEC Date: Fri, 24 Mar 2023 09:45:16 +0000 Subject: [PATCH 3/8] Use TOMLFile.write for saving and remove newline --- src/poetry/console/commands/self/self_command.py | 6 ++---- src/poetry/factory.py | 1 - tests/console/commands/self/test_system_pyproject.py | 6 ++---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/poetry/console/commands/self/self_command.py b/src/poetry/console/commands/self/self_command.py index 5cebbcff149..5be8f8b65e7 100644 --- a/src/poetry/console/commands/self/self_command.py +++ b/src/poetry/console/commands/self/self_command.py @@ -77,10 +77,8 @@ def generate_system_pyproject(self) -> None: for key in preserved: content["tool"]["poetry"][key] = preserved[key] # type: ignore[index] - text = content.as_string().replace("\r\n", "\n") # remove carriage return - if text.endswith("\n\n"): - text = text[:-1] # remove extra newline if trailing newline already present - self.system_pyproject.write_text(text, 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..85d8ca9dbf1 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -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( diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py index 55a3920f84f..9c30d4f931d 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_system_pyproject.py @@ -20,7 +20,7 @@ def example_system_pyproject(): Dependency(plugin.name, "^1.2.3", groups=[SelfCommand.ADDITIONAL_PACKAGE_GROUP]) ) content = Factory.create_pyproject_from_package(package) - return content.as_string()[:-2] # remove trailing newlines + return content.as_string().strip("\n") @pytest.mark.parametrize("existing_newlines", [0, 2]) @@ -37,9 +37,7 @@ def test_generate_system_pyproject_trailing_newline( if c != "\n": break - assert (existing_newlines < 2 and _i == 1) or ( - existing_newlines > 1 and _i == existing_newlines - ) + assert _i == existing_newlines def test_generate_system_pyproject_carraige_returns( From 09094f04531b4abbd91dba81994c0a94ca1575dc Mon Sep 17 00:00:00 2001 From: NimajnebEC Date: Wed, 22 Mar 2023 17:18:48 +0000 Subject: [PATCH 4/8] Implemented fixes and added tests --- .../console/commands/self/self_command.py | 5 +- .../commands/self/test_system_pyproject.py | 59 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/console/commands/self/test_system_pyproject.py diff --git a/src/poetry/console/commands/self/self_command.py b/src/poetry/console/commands/self/self_command.py index 0c1a4ec3f79..5cebbcff149 100644 --- a/src/poetry/console/commands/self/self_command.py +++ b/src/poetry/console/commands/self/self_command.py @@ -77,7 +77,10 @@ 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") + text = content.as_string().replace("\r\n", "\n") # remove carriage return + if text.endswith("\n\n"): + text = text[:-1] # remove extra newline if trailing newline already present + self.system_pyproject.write_text(text, encoding="utf-8") def reset_poetry(self) -> None: with directory(self.system_pyproject.parent): diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py new file mode 100644 index 00000000000..9b79a6d6705 --- /dev/null +++ b/tests/console/commands/self/test_system_pyproject.py @@ -0,0 +1,59 @@ +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()[:-2] # remove trailing newlines + + +@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, newline="" + ) # do not translate newlines + cmd.generate_system_pyproject() + generated = cmd.system_pyproject.read_text() + + for _i, c in enumerate(generated[::-1]): + if c != "\n": + break + + assert (existing_newlines < 2 and _i == 1) or ( + existing_newlines > 1 and _i == existing_newlines + ) + + +def test_generate_system_pyproject_carraige_returns( + example_system_pyproject: str, +): + cmd = SelfCommand() + cmd.system_pyproject.write_text( + example_system_pyproject + "\n" + ) # newlines will be translated + 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 From c46fb4f37a19fdc70a9135f579a8ee2a276cafd9 Mon Sep 17 00:00:00 2001 From: NimajnebEC <46959407+NimajnebEC@users.noreply.github.com> Date: Wed, 22 Mar 2023 23:18:00 +0000 Subject: [PATCH 5/8] Removed newline kwarg from test Removed newline kwarg from cmd.system_pyproject.write_text call in tests for compatibility with older versions. --- tests/console/commands/self/test_system_pyproject.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py index 9b79a6d6705..55a3920f84f 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_system_pyproject.py @@ -29,9 +29,7 @@ def test_generate_system_pyproject_trailing_newline( example_system_pyproject: str, ): cmd = SelfCommand() - cmd.system_pyproject.write_text( - example_system_pyproject + "\n" * existing_newlines, newline="" - ) # do not translate newlines + cmd.system_pyproject.write_text(example_system_pyproject + "\n" * existing_newlines) cmd.generate_system_pyproject() generated = cmd.system_pyproject.read_text() @@ -48,9 +46,7 @@ def test_generate_system_pyproject_carraige_returns( example_system_pyproject: str, ): cmd = SelfCommand() - cmd.system_pyproject.write_text( - example_system_pyproject + "\n" - ) # newlines will be translated + 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 From 523faba505fdef862bff7d8efed3cda188c41027 Mon Sep 17 00:00:00 2001 From: NimajnebEC Date: Fri, 24 Mar 2023 09:45:16 +0000 Subject: [PATCH 6/8] Use TOMLFile.write for saving and remove newline --- src/poetry/console/commands/self/self_command.py | 6 ++---- src/poetry/factory.py | 1 - tests/console/commands/self/test_system_pyproject.py | 6 ++---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/poetry/console/commands/self/self_command.py b/src/poetry/console/commands/self/self_command.py index 5cebbcff149..5be8f8b65e7 100644 --- a/src/poetry/console/commands/self/self_command.py +++ b/src/poetry/console/commands/self/self_command.py @@ -77,10 +77,8 @@ def generate_system_pyproject(self) -> None: for key in preserved: content["tool"]["poetry"][key] = preserved[key] # type: ignore[index] - text = content.as_string().replace("\r\n", "\n") # remove carriage return - if text.endswith("\n\n"): - text = text[:-1] # remove extra newline if trailing newline already present - self.system_pyproject.write_text(text, 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..85d8ca9dbf1 100644 --- a/src/poetry/factory.py +++ b/src/poetry/factory.py @@ -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( diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py index 55a3920f84f..9c30d4f931d 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_system_pyproject.py @@ -20,7 +20,7 @@ def example_system_pyproject(): Dependency(plugin.name, "^1.2.3", groups=[SelfCommand.ADDITIONAL_PACKAGE_GROUP]) ) content = Factory.create_pyproject_from_package(package) - return content.as_string()[:-2] # remove trailing newlines + return content.as_string().strip("\n") @pytest.mark.parametrize("existing_newlines", [0, 2]) @@ -37,9 +37,7 @@ def test_generate_system_pyproject_trailing_newline( if c != "\n": break - assert (existing_newlines < 2 and _i == 1) or ( - existing_newlines > 1 and _i == existing_newlines - ) + assert _i == existing_newlines def test_generate_system_pyproject_carraige_returns( From ac692d13b0060337902edd1af735a6723ac5bfd2 Mon Sep 17 00:00:00 2001 From: NimajnebEC <46959407+NimajnebEC@users.noreply.github.com> Date: Sun, 26 Mar 2023 14:27:45 +0100 Subject: [PATCH 7/8] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com> --- tests/console/commands/self/test_system_pyproject.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_system_pyproject.py index 9c30d4f931d..01155c6c449 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_system_pyproject.py @@ -33,14 +33,10 @@ def test_generate_system_pyproject_trailing_newline( cmd.generate_system_pyproject() generated = cmd.system_pyproject.read_text() - for _i, c in enumerate(generated[::-1]): - if c != "\n": - break + assert len(generated) - len(generated.rstrip("\n")) == existing_newlines - assert _i == existing_newlines - -def test_generate_system_pyproject_carraige_returns( +def test_generate_system_pyproject_carriage_returns( example_system_pyproject: str, ): cmd = SelfCommand() From bc6230454cccc12aa08efc468d56302113e31dea Mon Sep 17 00:00:00 2001 From: NimajnebEC Date: Sun, 26 Mar 2023 15:25:48 +0100 Subject: [PATCH 8/8] Remove path parameter and rename test file --- src/poetry/factory.py | 9 +-------- .../{test_system_pyproject.py => test_self_command.py} | 10 +++------- 2 files changed, 4 insertions(+), 15 deletions(-) rename tests/console/commands/self/{test_system_pyproject.py => test_self_command.py} (86%) diff --git a/src/poetry/factory.py b/src/poetry/factory.py index 85d8ca9dbf1..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 @@ -291,11 +289,6 @@ def create_pyproject_from_package( pyproject = cast("TOMLDocument", pyproject) - if path: - path.joinpath("pyproject.toml").write_text( - pyproject.as_string(), encoding="utf-8" - ) - return pyproject @classmethod diff --git a/tests/console/commands/self/test_system_pyproject.py b/tests/console/commands/self/test_self_command.py similarity index 86% rename from tests/console/commands/self/test_system_pyproject.py rename to tests/console/commands/self/test_self_command.py index 9c30d4f931d..c07be3059d5 100644 --- a/tests/console/commands/self/test_system_pyproject.py +++ b/tests/console/commands/self/test_self_command.py @@ -20,7 +20,7 @@ def example_system_pyproject(): Dependency(plugin.name, "^1.2.3", groups=[SelfCommand.ADDITIONAL_PACKAGE_GROUP]) ) content = Factory.create_pyproject_from_package(package) - return content.as_string().strip("\n") + return content.as_string().rstrip("\n") @pytest.mark.parametrize("existing_newlines", [0, 2]) @@ -33,14 +33,10 @@ def test_generate_system_pyproject_trailing_newline( cmd.generate_system_pyproject() generated = cmd.system_pyproject.read_text() - for _i, c in enumerate(generated[::-1]): - if c != "\n": - break + assert len(generated) - len(generated.rstrip("\n")) == existing_newlines - assert _i == existing_newlines - -def test_generate_system_pyproject_carraige_returns( +def test_generate_system_pyproject_carriage_returns( example_system_pyproject: str, ): cmd = SelfCommand()