Skip to content
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
7 changes: 5 additions & 2 deletions src/uv_dynamic_versioning/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

def read(root: str):
pyproject = Path(root) / "pyproject.toml"
return pyproject.read_text()
# TOML documents are UTF-8 by spec, so don't let the locale encoding decide.
return pyproject.read_text(encoding="utf-8")


def parse(text: str):
Expand Down Expand Up @@ -51,7 +52,9 @@ def _get_from_file_version(config: schemas.UvDynamicVersioning) -> str | None:
return None

source, pattern = (config.from_file.source, config.from_file.pattern)
content = Path(source).read_text().strip()
# `pattern` is meant for source files (e.g. `__init__.py`), whose non-version
# content can be non-ASCII, so don't let the locale encoding decide either.
content = Path(source).read_text(encoding="utf-8").strip()

if pattern is None:
return content
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/with-from-file-non-ascii/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""バージョン情報 — version metadata."""

__version__ = "0.0.0"
15 changes: 15 additions & 0 deletions tests/fixtures/with-from-file-non-ascii/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "dummy"
dynamic = ["version"]
description = ""
readme = "README.md"
requires-python = ">=3.10"
dependencies = []

[build-system]
requires = ["hatchling", "uv-dynamic-versioning"]
build-backend = "hatchling.build"

[tool.uv-dynamic-versioning.from-file]
source = "tests/fixtures/with-from-file-non-ascii/_version.py"
pattern = "^__version__ = \"(.+)\"$"
15 changes: 15 additions & 0 deletions tests/fixtures/with-non-ascii/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[project]
name = "dummy"
dynamic = ["version"]
# A non-ASCII comment — em dash (U+2014), as found in real pyproject.toml files.
description = "パッケージ"
readme = "README.md"
requires-python = ">=3.10"
dependencies = []

[build-system]
requires = ["hatchling", "uv-dynamic-versioning"]
build-backend = "hatchling.build"

[tool.uv-dynamic-versioning]
style = "pep440"
35 changes: 34 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from pathlib import Path

import pytest
from dunamai import Style, Version
from git import Repo

from uv_dynamic_versioning import schemas
from uv_dynamic_versioning.main import get_version
from uv_dynamic_versioning.base import BasePlugin
from uv_dynamic_versioning.main import get_version, read


@pytest.mark.usefixtures("semver_tag")
Expand Down Expand Up @@ -108,3 +111,33 @@ def test_get_version_with_highest_tag_selects_highest_version(repo: Repo):
assert version_highest.startswith("1.0.0")
finally:
repo.delete_tag(low_tag)


@pytest.fixture
def non_utf8_locale(monkeypatch: pytest.MonkeyPatch):
read_text = Path.read_text

def patched(self: Path, encoding: str | None = None, errors=None, **kwargs):
return read_text(self, encoding=encoding or "ascii", errors=errors, **kwargs)

monkeypatch.setattr(Path, "read_text", patched)


@pytest.mark.usefixtures("non_utf8_locale")
def test_read_decodes_utf8_regardless_of_locale():
# `pyproject.toml` is a TOML document, which is UTF-8 by spec, so `read` must not
# decode it with the locale encoding: on a non-UTF-8 locale that either raises
# (`UnicodeDecodeError` under `LC_ALL=C` or a Chinese Windows locale) or silently
# mojibakes the document.
root = "tests/fixtures/with-non-ascii"
expected = (Path(root) / "pyproject.toml").read_text(encoding="utf-8")
assert read(root) == expected


@pytest.mark.usefixtures("non_utf8_locale")
def test_from_file_decodes_utf8_regardless_of_locale():
# Same as above for the `from-file` source: with a `pattern` it is meant to be
# pointed at a source file, whose surrounding content can be non-ASCII.
plugin = BasePlugin()
plugin.root = "tests/fixtures/with-from-file-non-ascii" # type: ignore[attr-defined]
assert get_version(plugin.project_config)[0] == "0.0.0"