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

Not require to rewrite full cfg-file when bumping the version #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ tag = True
If no `.bumpversion.cfg` exists, `bump2version` will also look into
`setup.cfg` for configuration.

Beware: the default operation of bump2version is rewriting the complete file, causing
the loss of comments and custom formatting. To avoid this, add the cfg-file itself to
be bumped by bump2version. Like:
```ini
[bumpversion:file:.bumpversion.cfg]
```

### Configuration file -- Global configuration

General configuration is grouped in a `[bumpversion]` section.
Expand Down
21 changes: 11 additions & 10 deletions bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,19 @@ def main(original_args=None):
for file_name
in (file_names or positionals[1:])
)

write_config = (not args.dry_run) and config_file_exists
# do not update the config-file, when the updating should be handled by bump2version
if config_file in [f.path for f in files]:
logger.info("Not rewriting '%s' because it is handled by bump2version", config_file)
write_config = False

_check_files_contain_version(files, current_version, context)
_replace_version_in_files(files, current_version, new_version, args.dry_run, context)
_log_list(config, args.new_version)

# store the new version
_update_config_file(
config, config_file, config_newlines, config_file_exists, args.new_version, args.dry_run,
)
_update_config_file(config, config_file, config_newlines, args.new_version, write_config)

# commit and tag
if vcs:
Expand Down Expand Up @@ -631,24 +636,20 @@ def _log_list(config, new_version):
config.remove_option("bumpversion", "new_version")


def _update_config_file(
config, config_file, config_newlines, config_file_exists, new_version, dry_run,
):
def _update_config_file(config, config_file, config_newlines, new_version, write_file):
config.set("bumpversion", "current_version", new_version)
new_config = io.StringIO()
try:
write_to_config_file = (not dry_run) and config_file_exists

logger.info(
"%s to config file %s:",
"Would write" if not write_to_config_file else "Writing",
"Would write" if not write_file else "Writing",
config_file,
)

config.write(new_config)
logger.info(new_config.getvalue())

if write_to_config_file:
if write_file:
with open(config_file, "wt", encoding="utf-8", newline=config_newlines) as f:
f.write(new_config.getvalue().strip() + "\n")

Expand Down
18 changes: 18 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,24 @@ def test_no_configured_files_still_file_args_work(tmpdir, vcs):
assert "1.1.2" == tmpdir.join("please_update_me.txt").read()


def test_cfg_files_not_rewritten_when_specified(tmpdir):
""" when the .bumpversion.cfg is specified as a file to be bumped, the
config should not be rewritten (i.e., losing custom foratting + comments). """
tmpdir.chdir()
initial_config = """[bumpversion]
current_version = 500.0.0
# specify the .bumpversion.cfg here to avoid unintentionally rewriting it
# for example comments are removed or trailing whitespaces
[bumpversion:file:.bumpversion.cfg]

"""

tmpdir.join(".bumpversion.cfg").write(initial_config, mode='w')
main(['major'])
expected_new_config = initial_config.replace('500', '501')
assert expected_new_config == tmpdir.join(".bumpversion.cfg").read(mode='r')


class TestSplitArgsInOptionalAndPositional:

def test_all_optional(self):
Expand Down