Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/semantic_release/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ def from_raw_config( # noqa: C901
]
for allowed_dir in allowed_directories:
if (proj_toml := allowed_dir.joinpath("pyproject.toml")).exists():
config_toml = tomlkit.parse(proj_toml.read_text())
config_toml = tomlkit.parse(proj_toml.read_text(encoding="utf-8"))
project_metadata = config_toml.unwrap().get("project", project_metadata)
break

Expand Down
4 changes: 2 additions & 2 deletions src/semantic_release/version/declarations/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def content(self) -> str:
if not self._path.exists():
raise FileNotFoundError(f"path {self._path!r} does not exist")

self._content = self._path.read_text()
self._content = self._path.read_text(encoding="utf-8")

return self._content

Expand Down Expand Up @@ -164,7 +164,7 @@ def update_file_w_version(
if new_content == self.content:
return None

self._path.write_text(new_content)
self._path.write_text(new_content, encoding="utf-8")
del self.content

return self._path
Expand Down
4 changes: 2 additions & 2 deletions src/semantic_release/version/declarations/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def content(self) -> str:
if not self._path.exists():
raise FileNotFoundError(f"path {self._path!r} does not exist")

self._content = self._path.read_text()
self._content = self._path.read_text(encoding="utf-8")

return self._content

Expand Down Expand Up @@ -119,7 +119,7 @@ def update_file_w_version(
if new_content == self.content:
return None

self._path.write_text(new_content)
self._path.write_text(new_content, encoding="utf-8")
del self.content

return self._path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,29 @@ def test_pattern_declaration_w_invalid_definition(
replacement_def,
default_tag_format_str,
)


def test_pattern_declaration_reads_and_writes_utf8_whatever_the_locale_encoding(
tmp_path: Path,
):
"""
Given a UTF-8 file with non-ASCII text,
When the version is stamped,
Then only the version changes
"""
test_file = tmp_path / "module.py"
# The cp1252 and cp949 code pages cannot decode the UTF-8 bytes of this text
content = dedent(
"""\
__version__ = "1.0.0"
__author__ = "✨ Łukasz"
"""
)
test_file.write_bytes(content.encode("utf-8"))

PatternVersionDeclaration(
test_file, '__version__ = "(?P<version>.*)"', VersionStampType.NUMBER_FORMAT
).update_file_w_version(Version.parse("1.0.1"))

actual = test_file.read_text(encoding="utf-8")
assert content.replace("1.0.0", "1.0.1") == actual
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,30 @@ def test_toml_declaration_w_invalid_definition(
"""
with pytest.raises(ValueError, match=error_msg):
TomlVersionDeclaration.from_string_definition(replacement_def)


def test_toml_declaration_reads_and_writes_utf8_whatever_the_locale_encoding(
tmp_path: Path,
):
"""
Given a UTF-8 file with non-ASCII text,
When the version is stamped,
Then only the version changes
"""
test_file = tmp_path / "pyproject.toml"
# The cp1252 and cp949 code pages cannot decode the UTF-8 bytes of this text
content = dedent(
"""\
[project]
version = "1.0.0"
description = "✨ by Łukasz"
"""
)
test_file.write_bytes(content.encode("utf-8"))

TomlVersionDeclaration(
test_file, "project.version", VersionStampType.NUMBER_FORMAT
).update_file_w_version(Version.parse("1.0.1"))

actual = test_file.read_text(encoding="utf-8")
assert content.replace("1.0.0", "1.0.1") == actual