From 854ae6dc07cac5b497f545bcb3c4e65c5b38fd9f Mon Sep 17 00:00:00 2001 From: Michael Simacek Date: Mon, 16 Feb 2015 17:32:03 +0100 Subject: [PATCH] Handle non-utf-8 poms in pom_editor --- java-utils/pom_editor.py | 5 +++-- test/data/pom_macros/pom_non_unicode.xml | 14 ++++++++++++++ test/data/pom_macros/pom_non_unicode.xml-want | 14 ++++++++++++++ test/data/pom_macros/pom_unicode.xml | 16 ++++++++++++++++ test/data/pom_macros/pom_unicode.xml-want | 16 ++++++++++++++++ test/pom_macros_test.py | 21 ++++++++++++++++++--- 6 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 test/data/pom_macros/pom_non_unicode.xml create mode 100644 test/data/pom_macros/pom_non_unicode.xml-want create mode 100644 test/data/pom_macros/pom_unicode.xml create mode 100644 test/data/pom_macros/pom_unicode.xml-want diff --git a/java-utils/pom_editor.py b/java-utils/pom_editor.py index 1c30878..fd7125c 100644 --- a/java-utils/pom_editor.py +++ b/java-utils/pom_editor.py @@ -207,12 +207,13 @@ class XmlFile(object): def __init__(self, xmlpath): self.xmlpath = xmlpath - with io.open(self.xmlpath, encoding='UTF-8') as raw_xml: + encoding = etree.parse(xmlpath).docinfo.encoding + with io.open(self.xmlpath, encoding=encoding) as raw_xml: raw_xml = raw_xml.read() raw_xml = self._preprocess_raw(raw_xml) self.xml_declaration = re.match(r'\<\?xml\s[^?]*\?\>', raw_xml) tmpfile = self.xmlpath + '.tmp' - with io.open(tmpfile, 'w') as prepared: + with io.open(tmpfile, 'w', encoding=encoding) as prepared: prepared.write(raw_xml) self.document = etree.parse(tmpfile) self.tab = get_indent(self.root) diff --git a/test/data/pom_macros/pom_non_unicode.xml b/test/data/pom_macros/pom_non_unicode.xml new file mode 100644 index 0000000..1d8b103 --- /dev/null +++ b/test/data/pom_macros/pom_non_unicode.xml @@ -0,0 +1,14 @@ + + + + org.apache.commons + commons-parent + 17 + + 4.0.0 + commons-lang + commons-lang + 2.6 + Cömmöns Läng + + diff --git a/test/data/pom_macros/pom_non_unicode.xml-want b/test/data/pom_macros/pom_non_unicode.xml-want new file mode 100644 index 0000000..37960df --- /dev/null +++ b/test/data/pom_macros/pom_non_unicode.xml-want @@ -0,0 +1,14 @@ + + + + org.apache.commons + commons-parent + 17 + + 4.0.0 + commons-lang + commons-lang + 2.7 + Cömmöns Läng + + diff --git a/test/data/pom_macros/pom_unicode.xml b/test/data/pom_macros/pom_unicode.xml new file mode 100644 index 0000000..8c45d7c --- /dev/null +++ b/test/data/pom_macros/pom_unicode.xml @@ -0,0 +1,16 @@ + + + + org.apache.commons + commons-parent + 17 + + 4.0.0 + commons-lang + commons-lang + 2.6 + Commons Lang + + 猫耳 + + diff --git a/test/data/pom_macros/pom_unicode.xml-want b/test/data/pom_macros/pom_unicode.xml-want new file mode 100644 index 0000000..4e916fa --- /dev/null +++ b/test/data/pom_macros/pom_unicode.xml-want @@ -0,0 +1,16 @@ + + + + org.apache.commons + commons-parent + 17 + + 4.0.0 + commons-lang + commons-lang + 2.7 + Commons Lang + + 猫耳 + + diff --git a/test/pom_macros_test.py b/test/pom_macros_test.py index 7abe7f4..08dedb4 100644 --- a/test/pom_macros_test.py +++ b/test/pom_macros_test.py @@ -1,5 +1,6 @@ import unittest import os +import io from shutil import copytree from shutil import rmtree @@ -36,12 +37,12 @@ def check_result(pom_path): res = not report return report, res -def get_result_literally(pom_path): - with open(pom_path, 'r') as gotfile: +def get_result_literally(pom_path, encoding='UTF-8'): + with io.open(pom_path, 'r', encoding=encoding) as gotfile: got = gotfile.read().split('\n') wantpath = '{pom}-want'.format(pom=os.path.basename(pom_path)) - with open(wantpath, 'r') as wantfile: + with io.open(wantpath, 'r', encoding=encoding) as wantfile: want = wantfile.read().split('\n') return got, want @@ -546,6 +547,20 @@ class PomMacrosTest(unittest.TestCase): got, want = get_result_literally(pom_path) self.assertEqual(got, want) + @exec_macro("pom_xpath_set pom:project/pom:version 2.7", "pom_unicode.xml") + def test_unicode(self, stdin, stderr, returncode, pom_path): + self.assertEqual(returncode, 0, stderr) + + got, want = get_result_literally(pom_path) + self.assertEqual(got, want) + + @exec_macro("pom_xpath_set pom:project/pom:version 2.7", "pom_non_unicode.xml") + def test_non_unicode(self, stdin, stderr, returncode, pom_path): + self.assertEqual(returncode, 0, stderr) + + got, want = get_result_literally(pom_path, encoding='ISO-8859-1') + self.assertEqual(got, want) + @exec_macro("pom_remove_parent", "unparsable_xml.pom") def test_unparsable_xml(self, stdin, stderr, returncode, pom_path): self.assertEqual(returncode, 1, stderr) -- 2.1.0