Skip to content

Commit

Permalink
Make sure strings are XML compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Sep 4, 2019
1 parent 9335aa7 commit 6bf17b3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dump2polarion/exporters/requirements_exporter.py
Expand Up @@ -222,12 +222,12 @@ def _requirement_element(self, parent_element, req_data):
requirement = etree.SubElement(parent_element, "requirement", attrs)

title_el = etree.SubElement(requirement, "title")
title_el.text = title
title_el.text = utils.get_unicode_str(title)

description = req_data.get("description")
if description:
description_el = etree.SubElement(requirement, "description")
description_el.text = description
description_el.text = utils.get_unicode_str(description)

self._fill_custom_fields(requirement, custom_fields)

Expand Down
8 changes: 4 additions & 4 deletions dump2polarion/exporters/testcases_exporter.py
Expand Up @@ -267,11 +267,11 @@ def _add_test_steps(parent, testcase_data):
for index, step in enumerate(steps):
test_step = etree.SubElement(test_steps, "test-step")
test_step_col = etree.SubElement(test_step, "test-step-column", id="step")
test_step_col.text = step
test_step_col.text = utils.get_unicode_str(step)

test_res_col = etree.SubElement(test_step, "test-step-column", id="expectedResult")
try:
test_res_col.text = results[index]
test_res_col.text = utils.get_unicode_str(results[index])
except IndexError:
test_res_col.text = ""
else:
Expand Down Expand Up @@ -365,12 +365,12 @@ def _testcase_element(self, parent_element, testcase_data):
testcase = etree.SubElement(parent_element, "testcase", attrs)

title_el = etree.SubElement(testcase, "title")
title_el.text = testcase_title
title_el.text = utils.get_unicode_str(testcase_title)

description = testcase_data.get("description")
if description:
description_el = etree.SubElement(testcase, "description")
description_el.text = description
description_el.text = utils.get_unicode_str(description)

self._add_test_steps(testcase, testcase_data)
self._fill_custom_fields(testcase, custom_fields)
Expand Down
15 changes: 10 additions & 5 deletions dump2polarion/utils.py
Expand Up @@ -10,6 +10,7 @@
import logging
import os
import random
import re
import string
from collections import OrderedDict

Expand All @@ -26,15 +27,19 @@
logger = logging.getLogger(__name__)

NO_BLANKS_PARSER = etree.XMLParser(remove_blank_text=True)
# from https://stackoverflow.com/a/25920392
VALID_XML_RE = re.compile("[^\u0020-\uD7FF\u0009\u000A\u000D\uE000-\uFFFD\U00010000-\U0010FFFF]+")


def get_unicode_str(obj):
"""Makes sure obj is a unicode string."""
"""Makes sure obj is a valid XML unicode string."""
if isinstance(obj, six.text_type):
return obj
if isinstance(obj, six.binary_type):
return obj.decode("utf-8", errors="ignore")
return six.text_type(obj)
text = obj
elif isinstance(obj, six.binary_type):
text = obj.decode("utf-8", errors="ignore")
else:
text = six.text_type(obj)
return VALID_XML_RE.sub("", text)


def init_log(log_level):
Expand Down

0 comments on commit 6bf17b3

Please sign in to comment.