From 6092dc56050e51372ceb5aea3098d32461750c95 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Wed, 9 Sep 2020 21:46:29 +0200 Subject: [PATCH 1/2] Edited config saving to strip trailing white spaces before saving Implemented the test for this into test_retain_newline since it can affect the newline character. Added test case for mac line ending '\r'. --- bumpversion/cli.py | 5 ++++- tests/test_cli.py | 29 ++++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/bumpversion/cli.py b/bumpversion/cli.py index b107e10d..9485c0e0 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -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( diff --git a/tests/test_cli.py b/tests/test_cli.py index d41942ba..4cda0515 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 """) @@ -2197,7 +2197,7 @@ 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 @@ -2205,13 +2205,17 @@ def test_retain_newline(tmpdir, configfile, newline): """).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}\n" + b"[bumpversion:file:file.py]\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)]) main(["major"]) @@ -2219,6 +2223,9 @@ def test_retain_newline(tmpdir, configfile, newline): 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" ") 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) From 48865403b82357c4341f7e57f86850008d08d5e4 Mon Sep 17 00:00:00 2001 From: s-weigand Date: Wed, 30 Sep 2020 22:33:12 +0200 Subject: [PATCH 2/2] Added tests for trailing tabs as well as my use case to tests --- tests/test_cli.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 4cda0515..ab518a99 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2209,13 +2209,17 @@ def test_retain_newline(tmpdir, configfile, newline): b"[bumpversion]\n" b"current_version = 0.7.2\n" b"search = {current_version} \n" - b"replace = {new_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"]) @@ -2224,11 +2228,11 @@ def test_retain_newline(tmpdir, configfile, newline): assert newline in new_config # Check that no line ends with tailing whitespaces - assert all([not line.endswith(b" ") for line in new_config.split(newline)]) + 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):