Skip to content
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
5 changes: 4 additions & 1 deletion bumpversion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,10 @@ def _update_config_file(

if write_to_config_file:
with open(config_file, "wt", encoding="utf-8", newline=config_newlines) as f:
f.write(new_config.getvalue().strip() + "\n")
config_lines = new_config.getvalue().strip().split("\n")
# prevent tailing white spaces
config_lines = [line.rstrip(" ") + "\n" for line in config_lines]
f.writelines(config_lines)

except UnicodeEncodeError:
warnings.warn(
Expand Down
35 changes: 23 additions & 12 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,11 +1851,11 @@ def test_non_matching_search_does_not_modify_file(tmpdir):

changelog_content = dedent("""
# Unreleased

* bullet point A

# Release v'older' (2019-09-17)

* bullet point B
""")

Expand Down Expand Up @@ -2197,31 +2197,42 @@ def test_correct_interpolation_for_setup_cfg_files(tmpdir, configfile):
assert "current_version = 1.0.0" in tmpdir.join(configfile).read()


@pytest.mark.parametrize("newline", [b'\n', b'\r\n'])
@pytest.mark.parametrize("newline", [b'\n', b'\r\n', b'\r'])
def test_retain_newline(tmpdir, configfile, newline):
tmpdir.join("file.py").write_binary(dedent("""
0.7.2
Some Content
""").strip().encode(encoding='UTF-8').replace(b'\n', newline))
tmpdir.chdir()

tmpdir.join(configfile).write_binary(dedent("""
[bumpversion]
current_version = 0.7.2
search = {current_version}
replace = {new_version}
[bumpversion:file:file.py]
""").strip().encode(encoding='UTF-8').replace(b'\n', newline))
tmpdir.join(configfile).write_binary((
b"[bumpversion]\n"
b"current_version = 0.7.2\n"
b"search = {current_version} \n"
b"replace = {new_version}\t\t\n"
b"[bumpversion:file:file.py]\n"
b"[metadata]\n"
b"classifiers = \n"
b"\tDevelopment Status :: 4 - Beta\n"
).strip().replace(b'\n', newline))

# Ensure that the old config has the tailing whitespaces
old_config = tmpdir.join(configfile).read_binary()
assert any([line.endswith(b" ") for line in old_config.split(newline)])
assert any([line.endswith(b"\t") for line in old_config.split(newline)])

main(["major"])

assert newline in tmpdir.join("file.py").read_binary()
new_config = tmpdir.join(configfile).read_binary()
assert newline in new_config

# Check that no line ends with tailing whitespaces
assert all([not (line.endswith(b" ") or line.endswith(b"\t")) for line in new_config.split(newline)])

# Ensure there is only a single newline (not two) at the end of the file
# and that it is of the right type
assert new_config.endswith(b"[bumpversion:file:file.py]" + newline)
assert new_config.endswith(b"\tDevelopment Status :: 4 - Beta" + newline)


def test_no_configured_files(tmpdir, vcs):
Expand Down