diff --git a/docs/changelog/next_release/169.bugfix.rst b/docs/changelog/next_release/169.bugfix.rst new file mode 100644 index 0000000..38be91d --- /dev/null +++ b/docs/changelog/next_release/169.bugfix.rst @@ -0,0 +1,3 @@ +Fixed ``{timestamp}``, ``{timestamp:}`` and ``{timestamp:%s}`` substitutions failing on Windows with ``ValueError: Invalid format string``. + +``%s`` is not supported by ``strftime`` on every platform, so the unix timestamp is now computed directly. diff --git a/setuptools_git_versioning/subst.py b/setuptools_git_versioning/subst.py index a9ab127..5f46df8 100644 --- a/setuptools_git_versioning/subst.py +++ b/setuptools_git_versioning/subst.py @@ -43,7 +43,8 @@ def substitute_timestamp(template: str) -> str: format_string = fmt or "%s" log.log(DEBUG, "Format: %r", format_string) - result = now.strftime(format_string) + # strftime("%s") is platform-specific and fails on Windows + result = str(int(now.timestamp())) if format_string == "%s" else now.strftime(format_string) log.log(DEBUG, "Value: %r", result) template, _ = TIMESTAMP_REGEXP.subn(result, template, count=1) diff --git a/tests/test_integration/test_substitution.py b/tests/test_integration/test_substitution.py index 3cd1eec..6f8160a 100644 --- a/tests/test_integration/test_substitution.py +++ b/tests/test_integration/test_substitution.py @@ -1,9 +1,10 @@ import re import subprocess -from datetime import datetime +from datetime import datetime, timezone import pytest +from setuptools_git_versioning import subst from tests.lib.util import ( checkout_branch, create_file, @@ -130,9 +131,9 @@ def test_substitution_env(repo, dev_template, pipeline_id, suffix): @pytest.mark.parametrize( ("template", "fmt", "callback"), [ - ("{tag}.post{timestamp}", "{tag}.post{}", lambda dt: (int(dt.strftime("%s")) // 100,)), - ("{tag}.post{timestamp:}", "{tag}.post{}", lambda dt: (int(dt.strftime("%s")) // 100,)), - ("{tag}.post{timestamp:%s}", "{tag}.post{}", lambda dt: (int(dt.strftime("%s")) // 100,)), + ("{tag}.post{timestamp}", "{tag}.post{}", lambda dt: (int(dt.timestamp()) // 100,)), + ("{tag}.post{timestamp:}", "{tag}.post{}", lambda dt: (int(dt.timestamp()) // 100,)), + ("{tag}.post{timestamp:%s}", "{tag}.post{}", lambda dt: (int(dt.timestamp()) // 100,)), ( "{timestamp:%Y}.{timestamp:%m}.{timestamp:%d}+{timestamp:%H%M%S}", "{}.{}.{}+{}", @@ -165,6 +166,39 @@ def test_substitution_timestamp(repo, template, fmt, callback): assert new_value in get_version_setup_py(repo) +class DatetimeWithoutUnixFormat(datetime): + """Fixed current time, and strftime rejects ``%s`` like on Windows.""" + + @classmethod + def now(cls, tz=None): + return cls(2024, 1, 15, 12, 34, 56, tzinfo=timezone.utc) + + def astimezone(self, tz=None): + return self + + def strftime(self, fmt): + if "%s" in fmt: + msg = "Invalid format string" + raise ValueError(msg) + return super().strftime(fmt) + + +@pytest.mark.parametrize( + ("template", "result"), + [ + ("{timestamp}", "1705322096"), + ("{timestamp:}", "1705322096"), + ("{timestamp:%s}", "1705322096"), + ("{tag}.post{timestamp}", "{tag}.post1705322096"), + ("{timestamp:%Y}.{timestamp:%m}", "2024.01"), + ], +) +def test_substitution_timestamp_without_strftime_unix_format(monkeypatch, template, result): + monkeypatch.setattr(subst, "datetime", DatetimeWithoutUnixFormat) + + assert subst.substitute_timestamp(template) == result + + @pytest.mark.parametrize( "template", [