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

support glob keyword in configfile #150

Merged
merged 2 commits into from Aug 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -296,7 +296,11 @@ values =

### Configuration file -- File specific configuration

This configuration is in the section: `[bumpversion:file:…]`
This configuration is in the section: `[bumpversion:file:…]` or `[bumpversion:glob:…]`

Both, `file:` and `glob:` are configured the same. Their difference is that
file will match file names directly like `requirements.txt`. While glob also
matches multiple files via wildcards like `**/pom.xml`.

Note: The configuration file format requires each section header to be
unique. If you want to process a certain file multiple times,
Expand Down
11 changes: 8 additions & 3 deletions bumpversion/cli.py
@@ -1,5 +1,6 @@
import argparse
from datetime import datetime
import glob
import io
import itertools
import logging
Expand Down Expand Up @@ -49,7 +50,7 @@
# bumpversion:file ( suffix with spaces):value
RE_DETECT_SECTION_TYPE = re.compile(
r"^bumpversion:"
r"((?P<file>file)(\s*\(\s*(?P<file_suffix>[^\):]+)\)?)?|(?P<part>part)):"
r"((?P<file>file|glob)(\s*\(\s*(?P<file_suffix>[^\):]+)\)?)?|(?P<part>part)):"
r"(?P<value>.+)",
)

Expand Down Expand Up @@ -353,8 +354,12 @@ def _load_configuration(config_file, explicit_config, defaults):
if "replace" not in section_config:
section_config["replace"] = defaults.get("replace", "{new_version}")

files.append(ConfiguredFile(filename, VersionConfig(**section_config)))

version_config = VersionConfig(**section_config)
if section_type.get("file") == "glob":
ekohl marked this conversation as resolved.
Show resolved Hide resolved
for filename_glob in glob.glob(filename, recursive=True):
files.append(ConfiguredFile(filename_glob, version_config))
else:
files.append(ConfiguredFile(filename, version_config))
return config, config_file_exists, config_newlines, part_configs, files


Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli.py
Expand Up @@ -292,6 +292,36 @@ def test_default_config_files(tmpdir, configfile):
assert "0.10.3" == tmpdir.join("file2").read()


def test_glob_keyword(tmpdir, configfile):
tmpdir.join("file1.txt").write("0.9.34")
tmpdir.join("file2.txt").write("0.9.34")
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.9.34
new_version: 0.9.35
[bumpversion:glob:*.txt]""")

tmpdir.chdir()
main(["patch"])
assert "0.9.35" == tmpdir.join("file1.txt").read()
assert "0.9.35" == tmpdir.join("file2.txt").read()

def test_glob_keyword_recursive(tmpdir, configfile):
tmpdir.mkdir("subdir").mkdir("subdir2")
file1 = tmpdir.join("subdir").join("file1.txt")
file1.write("0.9.34")
file2 = tmpdir.join("subdir").join("subdir2").join("file2.txt")
file2.write("0.9.34")
tmpdir.join(configfile).write("""[bumpversion]
current_version: 0.9.34
new_version: 0.9.35
[bumpversion:glob:**/*.txt]""")

tmpdir.chdir()
main(["patch"])
assert "0.9.35" == file1.read()
assert "0.9.35" == file2.read()


def test_file_keyword_with_suffix_is_accepted(tmpdir, configfile, file_keyword):
tmpdir.join("file2").write("0.10.2")
tmpdir.join(configfile).write(
Expand Down