Skip to content

Commit

Permalink
Fix for tomlkit regression resulting in inconsistent line endings (#5870
Browse files Browse the repository at this point in the history
)

* init: use PyProjectTOML.save() instead of dumping content to string and writing string to file
* tests: adapt tests implicitly influenced by and add explicit test for consistent line endings in toml files
* require tomlkit >= 0.11.1 for consistent line endings
  • Loading branch information
radoering committed Jul 14, 2022
1 parent 8190761 commit c8ae868
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 49 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ platformdirs = "^2.5.2"
requests = "^2.18"
requests-toolbelt = "^0.9.1"
shellingham = "^1.1"
tomlkit = ">=0.7.0,<1.0.0"
tomlkit = ">=0.11.1,<1.0.0"
# exclude 20.4.5 - 20.4.6 due to https://github.com/pypa/pip/issues/9953
virtualenv = "(>=20.4.3,<20.4.5 || >=20.4.7)"
xattr = { version = "^0.9.7", markers = "sys_platform == 'darwin'" }
Expand Down
9 changes: 5 additions & 4 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,21 @@ def handle(self) -> int:
dev_dependencies=dev_requirements,
)

content = layout_.generate_poetry_content(original=pyproject)
content = layout_.generate_poetry_content()
for section in content:
pyproject.data.append(section, content[section])
if self.io.is_interactive():
self.line("<info>Generated file</info>")
self.line("")
self.line(content)
self.line(pyproject.data.as_string().replace("\r\n", "\n"))
self.line("")

if not self.confirm("Do you confirm generation?", True):
self.line_error("<error>Command aborted</error>")

return 1

with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f:
f.write(content)
pyproject.save()

return 0

Expand Down
21 changes: 7 additions & 14 deletions src/poetry/layouts/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING
from typing import Any

from tomlkit import dumps
from poetry.core.pyproject.toml import PyProjectTOML
from tomlkit import inline_table
from tomlkit import loads
from tomlkit import table
Expand All @@ -17,7 +17,6 @@
if TYPE_CHECKING:
from typing import Mapping

from poetry.core.pyproject.toml import PyProjectTOML
from tomlkit.items import InlineTable


Expand Down Expand Up @@ -118,7 +117,7 @@ def create(self, path: Path, with_tests: bool = True) -> None:

self._write_poetry(path)

def generate_poetry_content(self, original: PyProjectTOML | None = None) -> str:
def generate_poetry_content(self) -> TOMLDocument:
template = POETRY_DEFAULT

content: dict[str, Any] = loads(template)
Expand Down Expand Up @@ -171,12 +170,7 @@ def generate_poetry_content(self, original: PyProjectTOML | None = None) -> str:
assert isinstance(content, TOMLDocument)
content.add("build-system", build_system)

text = dumps(content)

if original and original.file.exists():
text = dumps(original.data) + "\n" + text

return text
return content

def _create_default(self, path: Path, src: bool = True) -> None:
package_path = path / self.package_path
Expand All @@ -199,9 +193,8 @@ def _create_tests(path: Path) -> None:
tests_init.touch(exist_ok=False)

def _write_poetry(self, path: Path) -> None:
pyproject = PyProjectTOML(path / "pyproject.toml")
content = self.generate_poetry_content()

poetry = path / "pyproject.toml"

with poetry.open("w", encoding="utf-8") as f:
f.write(content)
for section in content:
pyproject.data.append(section, content[section])
pyproject.save()
10 changes: 4 additions & 6 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,12 +870,10 @@ def test_add_to_section_that_does_not_exist_yet(
cachy = "^0.2.0"
"""
# At the moment line endings will be inconsistent on Windows.
# See https://github.com/sdispater/tomlkit/issues/200 for details.
# https://github.com/sdispater/tomlkit/pull/201 fixes this issue
# In order to make tests forward compatible for tomlkit downstream tests,
# we replace "\r\n" with "\n" for now.
string_content = content.as_string().replace("\r\n", "\n")
string_content = content.as_string()
if "\r\n" in string_content:
# consistent line endings
expected = expected.replace("\n", "\r\n")

assert expected in string_content

Expand Down
26 changes: 24 additions & 2 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ def test_add_package_with_extras_and_whitespace(tester: CommandTester):
assert "sqlite" in result[0]["extras"]


@pytest.mark.xfail(sys.platform == "win32", reason="regression in tomlkit")
def test_init_existing_pyproject_simple(
tester: CommandTester,
source_dir: Path,
Expand All @@ -901,7 +900,30 @@ def test_init_existing_pyproject_simple(
assert f"{existing_section}\n{init_basic_toml}" in pyproject_file.read_text()


@pytest.mark.xfail(sys.platform == "win32", reason="regression in tomlkit")
@pytest.mark.parametrize("linesep", ["\n", "\r\n"])
def test_init_existing_pyproject_consistent_linesep(
tester: CommandTester,
source_dir: Path,
init_basic_inputs: str,
init_basic_toml: str,
linesep: str,
):
pyproject_file = source_dir / "pyproject.toml"
existing_section = """
[tool.black]
line-length = 88
""".replace(
"\n", linesep
)
with open(pyproject_file, "w", newline="") as f:
f.write(existing_section)
tester.execute(inputs=init_basic_inputs)
with open(pyproject_file, newline="") as f:
content = f.read()
init_basic_toml = init_basic_toml.replace("\n", linesep)
assert f"{existing_section}{linesep}{init_basic_toml}" in content


def test_init_non_interactive_existing_pyproject_add_dependency(
tester: CommandTester,
source_dir: Path,
Expand Down
30 changes: 12 additions & 18 deletions tests/console/commands/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ def test_remove_without_specific_group_removes_from_all_groups(
baz = "^1.0.0"
"""
# At the moment line endings will be inconsistent on Windows.
# See https://github.com/sdispater/tomlkit/issues/200 for details.
# https://github.com/sdispater/tomlkit/pull/201 fixes this issue
# In order to make tests forward compatible for tomlkit downstream tests,
# we replace "\r\n" with "\n" for now.
string_content = content.as_string().replace("\r\n", "\n")
string_content = content.as_string()
if "\r\n" in string_content:
# consistent line endings
expected = expected.replace("\n", "\r\n")

assert expected in string_content

Expand Down Expand Up @@ -150,12 +148,10 @@ def test_remove_without_specific_group_removes_from_specific_groups(
baz = "^1.0.0"
"""
# At the moment line endings will be inconsistent on Windows.
# See https://github.com/sdispater/tomlkit/issues/200 for details.
# https://github.com/sdispater/tomlkit/pull/201 fixes this issue
# In order to make tests forward compatible for tomlkit downstream tests,
# we replace "\r\n" with "\n" for now.
string_content = content.as_string().replace("\r\n", "\n")
string_content = content.as_string()
if "\r\n" in string_content:
# consistent line endings
expected = expected.replace("\n", "\r\n")

assert expected in string_content

Expand Down Expand Up @@ -257,12 +253,10 @@ def test_remove_canonicalized_named_removes_dependency_correctly(
baz = "^1.0.0"
"""
# At the moment line endings will be inconsistent on Windows.
# See https://github.com/sdispater/tomlkit/issues/200 for details.
# https://github.com/sdispater/tomlkit/pull/201 fixes this issue
# In order to make tests forward compatible for tomlkit downstream tests,
# we replace "\r\n" with "\n" for now.
string_content = content.as_string().replace("\r\n", "\n")
string_content = content.as_string()
if "\r\n" in string_content:
# consistent line endings
expected = expected.replace("\n", "\r\n")

assert expected in string_content

Expand Down

0 comments on commit c8ae868

Please sign in to comment.