Skip to content

Commit 68643a9

Browse files
committed
Added how-to doc.
- "How to update a date in a file"
1 parent 84fa589 commit 68643a9

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

docsrc/howtos/update-a-date.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# How to update a date in a file
2+
3+
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:
4+
5+
```python title="my_package/__init__.py"
6+
__date__ = '2022-12-19'
7+
__version__ = '0.4.0'
8+
```
9+
10+
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:
11+
12+
```python title="my_package/__init__.py"
13+
__date__ = '2024-02-23'
14+
__version__ = '0.5.0'
15+
```
16+
17+
## Setting up the file configurations
18+
19+
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:
20+
21+
```toml title=".bumpversion.toml or other config file"
22+
[[tool.bumpversion.files]]
23+
filename = '__init__.py'
24+
search = "__date__ = '\\d{{4}}-\\d{{2}}-\\d{{2}}'"
25+
replace = "__date__ = '{now:%Y-%m-%d}'"
26+
regex = true
27+
28+
[[tool.bumpversion.files]]
29+
filename = '__init__.py'
30+
```
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[tool.bumpversion]
2+
current_version = '1.2.3'
3+
4+
[[tool.bumpversion.files]]
5+
filename = 'VERSION'
6+
search = "__date__ = '\\d{{4}}-\\d{{2}}-\\d{{2}}'"
7+
replace = "__date__ = '{now:%Y-%m-%d}'"
8+
regex = true
9+
10+
[[tool.bumpversion.files]]
11+
filename = 'VERSION'

tests/test_files.py

+23
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,29 @@ def test_regex_search_with_escaped_chars(tmp_path: Path) -> None:
410410
assert version_path.read_text() == f"## [Release] 1.2.4 {now}"
411411

412412

413+
def test_regex_search_in_toml(tmp_path: Path, fixtures_path: Path) -> None:
414+
"""Tests how-to doc 'update-a-date.md'."""
415+
# Arrange
416+
version_path = tmp_path / "VERSION"
417+
version_path.write_text("__date__ = '1234-56-78'\n__version__ = '1.2.3'")
418+
config_path = tmp_path / ".bumpversion.toml"
419+
shutil.copyfile(fixtures_path / "replace-date-config.toml", config_path)
420+
with inside_dir(tmp_path):
421+
conf = config.get_configuration(config_file=config_path)
422+
version_config = VersionConfig(conf.parse, conf.serialize, conf.search, conf.replace, conf.parts)
423+
current_version = version_config.parse(conf.current_version)
424+
425+
new_version = current_version.bump("patch")
426+
cfg_files = [files.ConfiguredFile(file_cfg, version_config) for file_cfg in conf.files]
427+
428+
# Act
429+
files.modify_files(cfg_files, current_version, new_version, get_context(conf))
430+
431+
# Assert
432+
now = datetime.now().isoformat()[:10]
433+
assert version_path.read_text() == f"__date__ = '{now}'\n__version__ = '1.2.4'"
434+
435+
413436
def test_regex_search_with_caret(tmp_path: Path, fixtures_path: Path) -> None:
414437
"""A search that uses a caret to indicate the beginning of the line works correctly."""
415438
# Arrange

0 commit comments

Comments
 (0)