From cf7a1de80a0e1797f42b209dccdbc6c3feb23147 Mon Sep 17 00:00:00 2001 From: Joshua Hopp Date: Fri, 21 Aug 2020 17:54:20 +0200 Subject: [PATCH 1/3] Added encoding support --- bumpversion/utils.py | 6 +++--- bumpversion/version_part.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bumpversion/utils.py b/bumpversion/utils.py index f872f230..9aa59eb1 100644 --- a/bumpversion/utils.py +++ b/bumpversion/utils.py @@ -75,7 +75,7 @@ def contains(self, search): if not search: return False - with open(self.path, "rt", encoding="utf-8") as f: + with open(self.path, "rt", encoding=self._versionconfig.encoding) as f: search_lines = search.splitlines() lookbehind = [] @@ -102,7 +102,7 @@ def contains(self, search): def replace(self, current_version, new_version, context, dry_run): - with open(self.path, "rt", encoding="utf-8") as f: + with open(self.path, "rt", encoding=self._versionconfig.encoding) as f: file_content_before = f.read() file_new_lines = f.newlines @@ -141,7 +141,7 @@ def replace(self, current_version, new_version, context, dry_run): logger.info("%s file %s", "Would not change" if dry_run else "Not changing", self.path) if not dry_run: - with open(self.path, "wt", encoding="utf-8", newline=file_new_lines) as f: + with open(self.path, "wt", encoding=self._versionconfig.encoding, newline=file_new_lines) as f: f.write(file_content_after) def __str__(self): diff --git a/bumpversion/version_part.py b/bumpversion/version_part.py index fc811f57..d91f185e 100644 --- a/bumpversion/version_part.py +++ b/bumpversion/version_part.py @@ -133,7 +133,7 @@ class VersionConfig: Holds a complete representation of a version string """ - def __init__(self, parse, serialize, search, replace, part_configs=None): + def __init__(self, parse, serialize, search, replace, part_configs=None, encoding="utf-8"): try: self.parse_regex = re.compile(parse, re.VERBOSE) @@ -150,6 +150,7 @@ def __init__(self, parse, serialize, search, replace, part_configs=None): self.part_configs = part_configs self.search = search self.replace = replace + self.encoding = encoding def order(self): From aef10d09100d6e6cd25cfa9ee8d035a7aebeb663 Mon Sep 17 00:00:00 2001 From: Joshua Hopp Date: Tue, 17 Nov 2020 22:29:23 +0100 Subject: [PATCH 2/3] Added test for encoding option --- tests/test_cli.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 9ee3b869..db783984 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import argparse import logging import os @@ -2166,6 +2167,33 @@ def test_retain_newline(tmpdir, configfile, newline): assert new_config.endswith(b"[bumpversion:file:file.py]" + newline) +@pytest.mark.parametrize("encoding", [None, "utf-8", "latin1"]) +def test_file_encoding(tmpdir, configfile, encoding): + tmpdir.join("file.py").write_binary(dedent(""" + 0.7.2 + Some encoded Content: äöüß + """).strip().encode(encoding=encoding or "utf-8")) + tmpdir.chdir() + + if encoding is None: + configstring = "" + else: + configstring = "encoding = %s" % encoding + + tmpdir.join(configfile).write_binary(dedent((""" + [bumpversion] + current_version = 0.7.2 + search = {current_version} + replace = {new_version} + [bumpversion:file:file.py] + %s + """) % configstring).strip().encode(encoding='UTF-8')) + + # Ensure the program works (without any exceptions or errors) + # regardless of encoding if the encoding is configured + # correctly + main(["major"]) + class TestSplitArgsInOptionalAndPositional: def test_all_optional(self): From a6cd27ca440adc639a9fd3d0622d52aa61473e6f Mon Sep 17 00:00:00 2001 From: Joshua Hopp Date: Tue, 17 Nov 2020 22:30:17 +0100 Subject: [PATCH 3/3] Documented encoding option in README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 4f6fbf3b..bfa2d6bf 100644 --- a/README.md +++ b/README.md @@ -374,6 +374,14 @@ replace = MyProject=={new_version} Can be multiple lines, templated using [Python Format String Syntax](https://docs.python.org/3/library/string.html#format-string-syntax). +#### `encoding =` + **default:** `UTF-8` + + Encoding to be used with the file. Might need to be set for files that + contain special characters and use a different encoding than UTF-8. + + You can consult [the Python Documentation](https://docs.python.org/3/library/codecs.html#standard-encodings) for a list of possible encodings. + ## Command-line Options Most of the configuration values above can also be given as an option on the command-line.