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

Issue #49 - Crashes due to not specified encoding when working with pyproject.toml file #50

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def get_config(start: Path = None) -> Mapping:
pyproject_path = _get_pyproject_path(start)
if pyproject_path is None:
return _default_config()["tool"]["poetry-dynamic-versioning"]
pyproject = tomlkit.parse(pyproject_path.read_text())
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
result = _deep_merge_dicts(_default_config(), pyproject)["tool"]["poetry-dynamic-versioning"]
return result

Expand Down Expand Up @@ -226,19 +226,19 @@ def _substitute_version(
for match in root.glob(str(file_glob)):
files.add(match.resolve())
for file in files:
original_content = file.read_text()
original_content = file.read_text(encoding="utf-8")
new_content = original_content
for pattern in patterns:
new_content = re.sub(
pattern, r"\g<1>{}\g<2>".format(version), new_content, flags=re.MULTILINE
)
if original_content != new_content:
_state.project(name).substitutions[file] = original_content
file.write_text(new_content)
file.write_text(new_content, encoding="utf-8")


def _apply_version(version: str, config: Mapping, pyproject_path: Path) -> str:
pyproject = tomlkit.parse(pyproject_path.read_text())
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
if pyproject["tool"]["poetry"]["version"] != version:
pyproject["tool"]["poetry"]["version"] = version

Expand All @@ -248,7 +248,7 @@ def _apply_version(version: str, config: Mapping, pyproject_path: Path) -> str:
if not _state.cli_mode:
pyproject["tool"]["poetry-dynamic-versioning"]["enable"] = False

pyproject_path.write_text(tomlkit.dumps(pyproject))
pyproject_path.write_text(tomlkit.dumps(pyproject), encoding="utf-8")

name = pyproject["tool"]["poetry"]["name"]

Expand All @@ -269,20 +269,20 @@ def _revert_version() -> None:
pyproject_path = _get_pyproject_path(state.path)
if pyproject_path is None:
return
pyproject = tomlkit.parse(pyproject_path.read_text())
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
pyproject["tool"]["poetry"]["version"] = state.original_version

if not _state.cli_mode:
# We can't get here unless the plugin was enabled initially,
# so we re-enable it after we've temporarily disabled it.
pyproject["tool"]["poetry-dynamic-versioning"]["enable"] = True

pyproject_path.write_text(tomlkit.dumps(pyproject))
pyproject_path.write_text(tomlkit.dumps(pyproject), encoding="utf-8")
state.original_version = None

if state.substitutions:
for file, content in state.substitutions.items():
file.write_text(content)
file.write_text(content, encoding="utf-8")
state.substitutions.clear()


Expand Down Expand Up @@ -341,7 +341,7 @@ def alt_poetry_create(cls, *args, **kwargs):
pyproject_path = _get_pyproject_path(cwd)
if pyproject_path is None:
raise RuntimeError("Unable to find pyproject.toml")
pyproject = tomlkit.parse(pyproject_path.read_text())
pyproject = tomlkit.parse(pyproject_path.read_text(encoding="utf-8"))
name = pyproject["tool"]["poetry"]["name"]

if not _state.cli_mode:
Expand Down
2 changes: 1 addition & 1 deletion tests/project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "project"
version = "0.0.999"
description = ""
description = "Some special character like Ø, and Ř in UTF-8 encoding."
authors = []

[tool.poetry.dependencies]
Expand Down