diff --git a/dump2polarion/exporters/requirements_exporter.py b/dump2polarion/exporters/requirements_exporter.py index 6c105ab..1b4d3f8 100644 --- a/dump2polarion/exporters/requirements_exporter.py +++ b/dump2polarion/exporters/requirements_exporter.py @@ -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) diff --git a/dump2polarion/exporters/testcases_exporter.py b/dump2polarion/exporters/testcases_exporter.py index ded9fa2..2a29d74 100644 --- a/dump2polarion/exporters/testcases_exporter.py +++ b/dump2polarion/exporters/testcases_exporter.py @@ -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: @@ -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) diff --git a/dump2polarion/utils.py b/dump2polarion/utils.py index bda0b46..e0aeabf 100644 --- a/dump2polarion/utils.py +++ b/dump2polarion/utils.py @@ -10,6 +10,7 @@ import logging import os import random +import re import string from collections import OrderedDict @@ -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):