Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added How to update a date in a file #142

Merged
merged 1 commit into from
Feb 24, 2024
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
30 changes: 30 additions & 0 deletions docsrc/howtos/update-a-date.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# How to update a date in a file

Many times when bumping a version, you will also want to update a date in a file. This is a common use case for changelogs, but it could be any file that contains a date. In this example, we have an `__init__.py` that looks like this:

```python title="my_package/__init__.py"
__date__ = '2022-12-19'
__version__ = '0.4.0'
```

The desired outcome is to update the date to the current date. For example, if today is February 23, 2024, the __init__.py file should look like this after a `minor` bump:

```python title="my_package/__init__.py"
__date__ = '2024-02-23'
__version__ = '0.5.0'
```

## Setting up the file configurations

We need Bump My Version to update the `__init__.py` file twice: once for the version and once for the date. Here is the necessary configuration:

```toml title=".bumpversion.toml or other config file"
[[tool.bumpversion.files]]
filename = '__init__.py'
search = "__date__ = '\\d{{4}}-\\d{{2}}-\\d{{2}}'"
replace = "__date__ = '{now:%Y-%m-%d}'"
regex = true

[[tool.bumpversion.files]]
filename = '__init__.py'
```
11 changes: 11 additions & 0 deletions tests/fixtures/replace-date-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tool.bumpversion]
current_version = '1.2.3'

[[tool.bumpversion.files]]
filename = 'VERSION'
search = "__date__ = '\\d{{4}}-\\d{{2}}-\\d{{2}}'"
replace = "__date__ = '{now:%Y-%m-%d}'"
regex = true

[[tool.bumpversion.files]]
filename = 'VERSION'
23 changes: 23 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,29 @@ def test_regex_search_with_escaped_chars(tmp_path: Path) -> None:
assert version_path.read_text() == f"## [Release] 1.2.4 {now}"


def test_regex_search_in_toml(tmp_path: Path, fixtures_path: Path) -> None:
"""Tests how-to doc 'update-a-date.md'."""
# Arrange
version_path = tmp_path / "VERSION"
version_path.write_text("__date__ = '1234-56-78'\n__version__ = '1.2.3'")
config_path = tmp_path / ".bumpversion.toml"
shutil.copyfile(fixtures_path / "replace-date-config.toml", config_path)
with inside_dir(tmp_path):
conf = config.get_configuration(config_file=config_path)
version_config = VersionConfig(conf.parse, conf.serialize, conf.search, conf.replace, conf.parts)
current_version = version_config.parse(conf.current_version)

new_version = current_version.bump("patch")
cfg_files = [files.ConfiguredFile(file_cfg, version_config) for file_cfg in conf.files]

# Act
files.modify_files(cfg_files, current_version, new_version, get_context(conf))

# Assert
now = datetime.now().isoformat()[:10]
assert version_path.read_text() == f"__date__ = '{now}'\n__version__ = '1.2.4'"


def test_regex_search_with_caret(tmp_path: Path, fixtures_path: Path) -> None:
"""A search that uses a caret to indicate the beginning of the line works correctly."""
# Arrange
Expand Down
Loading