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
3 changes: 3 additions & 0 deletions docs/changelog/next_release/169.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion setuptools_git_versioning/subst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 38 additions & 4 deletions tests/test_integration/test_substitution.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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}",
"{}.{}.{}+{}",
Expand Down Expand Up @@ -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",
[
Expand Down
Loading