Skip to content

Commit

Permalink
Merge pull request #59 from hesstobi/master
Browse files Browse the repository at this point in the history
Fix: LF is replaced with CRLF on Windows
  • Loading branch information
c4urself committed Jun 8, 2019
2 parents 22d981e + 4a7aec5 commit c65262e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions bumpversion/cli.py
Expand Up @@ -197,7 +197,9 @@ def main(original_args=None):

logger.info("Reading config file {}:".format(config_file))
# TODO: this is a DEBUG level log
logger.info(io.open(config_file, "rt", encoding="utf-8").read())
with io.open(config_file, "rt", encoding="utf-8") as f:
logger.info(f.read())
config_new_lines = f.newlines

try:
# TODO: we're reading the config file twice.
Expand Down Expand Up @@ -580,7 +582,7 @@ def main(original_args=None):
logger.info(new_config.getvalue())

if write_to_config_file:
with io.open(config_file, "wt", encoding="utf-8") as f:
with io.open(config_file, "wt", encoding="utf-8", newline=config_new_lines) as f:
f.write(new_config.getvalue())

except UnicodeEncodeError:
Expand Down
3 changes: 2 additions & 1 deletion bumpversion/utils.py
Expand Up @@ -91,6 +91,7 @@ def replace(self, current_version, new_version, context, dry_run):

with io.open(self.path, "rt", encoding="utf-8") as f:
file_content_before = f.read()
file_new_lines = f.newlines

context["current_version"] = self._versionconfig.serialize(
current_version, context
Expand Down Expand Up @@ -135,7 +136,7 @@ def replace(self, current_version, new_version, context, dry_run):
)

if not dry_run:
with io.open(self.path, "wt", encoding="utf-8") as f:
with io.open(self.path, "wt", encoding="utf-8", newline=file_new_lines) as f:
f.write(file_content_after)

def __str__(self):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cli.py
Expand Up @@ -1916,6 +1916,28 @@ 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'])
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))

main(["major"])

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


class TestSplitArgsInOptionalAndPositional:

def test_all_optional(self):
Expand Down

0 comments on commit c65262e

Please sign in to comment.