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
8 changes: 7 additions & 1 deletion .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import sys
import tempfile
import yaml
from datetime import datetime
from datetime import date, datetime
from pathlib import Path
from typing import Dict, List
import build.util
Expand Down Expand Up @@ -1169,6 +1169,12 @@ def _process_version_file(content, version, version_path) -> str:
raise ValueError(
f"Could not find version string in {version_path}. File was not modified."
)

# Optionally update the `__release_date__` date string, if it exists, in the format YYYY-MM-DD
date_pattern = r"(__release_date__\s*=\s*[\"'])([^\"']+)([\"'].*)"
today_iso = date.today().isoformat() # Get today's date in YYYY-MM-DD format
date_replacement_string = f"\\g<1>{today_iso}\\g<3>"
new_content, _ = re.subn(date_pattern, date_replacement_string, new_content)
return new_content


Expand Down
40 changes: 39 additions & 1 deletion .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import subprocess
import yaml
import unittest.mock
from datetime import datetime
from datetime import date, datetime
from pathlib import Path
from unittest.mock import MagicMock, mock_open

Expand Down Expand Up @@ -1105,6 +1105,44 @@ def test_update_version_for_library_success_proto_only_setup_py(mocker):
)


def test_update_version_for_library_success_with_date_string(mocker):
m = mock_open()

mock_rglob = mocker.patch("pathlib.Path.rglob")
mock_rglob.side_effect = [
[],
[pathlib.Path("repo/setup.py")],
[pathlib.Path("repo/samples/snippet_metadata.json")],
]
mock_shutil_copy = mocker.patch("shutil.copy")
mock_content = 'version = "1.2.2"\n__release_date__ = "2025-11-03"'
mock_json_metadata = {"clientLibrary": {"version": "0.1.0"}}
today_iso = date.today().isoformat()

with unittest.mock.patch("cli.open", m):
mocker.patch("cli._read_text_file", return_value=mock_content)
mocker.patch("cli._read_json_file", return_value=mock_json_metadata)
_update_version_for_library(
"repo", "output", "packages/google-cloud-language", "1.2.3"
)

handle = m()
assert (
handle.write.call_args_list[0].args[0]
== f'version = "1.2.3"\n__release_date__ = "{today_iso}"'
)
# Get all the arguments passed to the mock's write method
# and join them into a single string.
written_content = "".join(
[call.args[0] for call in handle.write.call_args_list[1:]]
)
# Create the expected output string with the correct formatting.
assert (
written_content
== '{\n "clientLibrary": {\n "version": "1.2.3"\n }\n}\n'
)


def test_update_version_for_library_success_proto_only_pyproject_toml(mocker):
m = mock_open()

Expand Down
Loading