Handle non-utf-8 poms in pom_editor

This commit is contained in:
Michael Simacek 2015-03-24 09:39:34 +01:00
parent 06282ff448
commit 0843b71a85
3 changed files with 178 additions and 29 deletions

View File

@ -0,0 +1,173 @@
From 854ae6dc07cac5b497f545bcb3c4e65c5b38fd9f Mon Sep 17 00:00:00 2001
From: Michael Simacek <msimacek@redhat.com>
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 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-parent</artifactId>
+ <version>17</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
+ <name>Cömmöns Läng</name>
+
+</project>
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 @@
+<?xml version='1.0' encoding='ISO-8859-1'?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-parent</artifactId>
+ <version>17</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.7</version>
+ <name>Cömmöns Läng</name>
+
+</project>
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-parent</artifactId>
+ <version>17</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.6</version>
+ <name>Commons Lang</name>
+ <!-- ニャン -->
+ <description>猫耳</description>
+
+</project>
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 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-parent</artifactId>
+ <version>17</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ <version>2.7</version>
+ <name>Commons Lang</name>
+ <!-- ニャン -->
+ <description>猫耳</description>
+
+</project>
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

View File

@ -1,27 +0,0 @@
From 4374dc79828c75820d119ce221e95b74900afc52 Mon Sep 17 00:00:00 2001
From: Michael Simacek <msimacek@redhat.com>
Date: Mon, 16 Feb 2015 17:32:03 +0100
Subject: [PATCH] [pom_editor] Write temporary XML file as UTF-8
---
java-utils/pom_editor.py | 2 +-
test/data/pom_macros/pom_unicode.xml | 16 ++++++++++++++++
test/data/pom_macros/pom_unicode.xml-want | 16 ++++++++++++++++
test/pom_macros_test.py | 7 +++++++
4 files changed, 40 insertions(+), 1 deletion(-)
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..201ccf1 100644
--- a/java-utils/pom_editor.py
+++ b/java-utils/pom_editor.py
@@ -212,7 +212,7 @@ class XmlFile(object):
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='UTF-8') as prepared:
prepared.write(raw_xml)
self.document = etree.parse(tmpfile)
self.tab = get_indent(self.root)

View File

@ -9,7 +9,7 @@
Name: javapackages-tools
Version: 4.4.0
Release: 2%{?dist}
Release: 3%{?dist}
Summary: Macros and scripts for Java packaging support
@ -17,7 +17,7 @@ License: BSD
URL: https://git.fedorahosted.org/git/javapackages.git
Source0: https://fedorahosted.org/released/javapackages/javapackages-%{version}.tar.xz
Patch0: 0001-pom_editor-Write-temporary-XML-file-as-UTF-8.patch
Patch0: 0001-Handle-non-utf-8-poms-in-pom_editor.patch
BuildArch: noarch
@ -231,6 +231,9 @@ popd
%doc LICENSE
%changelog
* Tue Mar 24 2015 Michael Simacek <msimacek@redhat.com> - 4.4.0-3
- Handle non-utf-8 poms in pom_editor
* Mon Feb 16 2015 Michael Simacek <msimacek@redhat.com> - 4.4.0-2
- Write temporary XML file as UTF-8 in pom_editor