From 854e2bca891b9fb4224e791d58f55e1b6b769de3 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Wed, 30 Jan 2019 10:21:44 +0100 Subject: [PATCH] Improve description formatting --- dump2polarion/exporters/testcases_exporter.py | 10 ++-- dump2polarion/exporters/transform.py | 59 +++++++++++++++++-- tests/data/testcase_complete_cfme.xml | 4 +- tests/data/testcase_complete_cfme_param.xml | 4 +- tests/data/testcase_fields_cfme.xml | 4 +- 5 files changed, 67 insertions(+), 14 deletions(-) diff --git a/dump2polarion/exporters/testcases_exporter.py b/dump2polarion/exporters/testcases_exporter.py index faf0108..92191f2 100644 --- a/dump2polarion/exporters/testcases_exporter.py +++ b/dump2polarion/exporters/testcases_exporter.py @@ -118,11 +118,11 @@ def __init__(self, testcases_data, config, transform_func=None): "(" + ")|(".join(self.config.get("blacklisted_tests")) + ")" ) - def _transform_result(self, result): - """Calls transform function on result.""" + def _transform_testcase(self, testcase_data): + """Calls transform function on testcase data.""" if self._transform_func: - result = self._transform_func(result) - return result or None + testcase_data = self._transform_func(testcase_data) + return testcase_data or None def _top_element(self): """Returns top XML element.""" @@ -314,7 +314,7 @@ def _testcase_element(self, parent_element, testcase_data): return testcase_data = self._fill_project_defaults(testcase_data) self._fill_automation_repo(testcase_data) - testcase_data = self._transform_result(testcase_data) + testcase_data = self._transform_testcase(testcase_data) if not testcase_data: return diff --git a/dump2polarion/exporters/transform.py b/dump2polarion/exporters/transform.py index 1d532fc..1229827 100644 --- a/dump2polarion/exporters/transform.py +++ b/dump2polarion/exporters/transform.py @@ -10,12 +10,17 @@ import copy import hashlib +import logging +import os import re from docutils.core import publish_parts from dump2polarion.exporters.verdicts import Verdicts +# pylint: disable=invalid-name +logger = logging.getLogger(__name__) + TEST_PARAM_RE = re.compile(r"\[.*\]") @@ -96,12 +101,16 @@ def get_testcase_id(testcase, append_str): '5acc5dc795a620c6b4491b681e5da39c' >>> get_testcase_id({"title": "TestClass.test_name", "id": "TestClass.test_name"}, "vmaas_") '5acc5dc795a620c6b4491b681e5da39c' - >>> str(get_testcase_id({"title": "TestClass.test_name", "id": "test"}, "vmaas_")) - 'test' + >>> get_testcase_id({"title": "TestClass.test_name", "id": "test_name"}, "vmaas_") + '5acc5dc795a620c6b4491b681e5da39c' + >>> get_testcase_id({"title": "some title", "id": "TestClass.test_name"}, "vmaas_") + '2ea7695b73763331f8a0c4aec75362b8' + >>> str(get_testcase_id({"title": "some title", "id": "some_id"}, "vmaas_")) + 'some_id' """ testcase_title = testcase.get("title") testcase_id = testcase.get("id") - if not testcase_id or testcase_title in testcase_id: + if not testcase_id or testcase_id.lower().startswith("test"): testcase_id = gen_unique_id("{}{}".format(append_str, testcase_title)) return testcase_id @@ -125,13 +134,50 @@ def set_cfme_caselevel(testcase, caselevels): def parse_rst_description(testcase): - """Creates an HTML version of the description.""" - description = testcase["description"] + """Creates an HTML version of the RST formatted description.""" + description = testcase.get("description") if not description: return - testcase["description"] = publish_parts(description, writer_name="html")["html_body"] + try: + with open(os.devnull, "w") as devnull: + testcase["description"] = publish_parts( + description, + writer_name="html", + settings_overrides={"report_level": 2, "halt_level": 2, "warning_stream": devnull}, + )["html_body"] + # pylint: disable=broad-except + except Exception as exp: + testcase_id = testcase.get("nodeid") or testcase.get("id") or testcase.get("title") + logger.error("%s: description: %s", str(exp), testcase_id) + + +def preformat_plain_description(testcase): + """Creates a preformatted HTML version of the description.""" + description = testcase.get("description") + + if not description: + return + + # naive approach to removing indent from pytest docstrings + nodeid = testcase.get("nodeid") or "" + indent = None + if "::Test" in nodeid: + indent = 8 * " " + elif "::test_" in nodeid: + indent = 4 * " " + + if indent: + orig_lines = description.split("\n") + new_lines = [] + for line in orig_lines: + if line.startswith(indent): + line = line.replace(indent, "", 1) + new_lines.append(line) + description = "\n".join(new_lines) + + testcase["description"] = "
\n{}\n
".format(description) def add_unique_runid(testcase, run_id=None): @@ -268,6 +314,7 @@ def testcase_transform(testcase): setup_parametrization(testcase, parametrize) set_cfme_caselevel(testcase, caselevels) + preformat_plain_description(testcase) add_unique_runid(testcase, run_id) add_automation_link(testcase) diff --git a/tests/data/testcase_complete_cfme.xml b/tests/data/testcase_complete_cfme.xml index 1b54e39..3d195b1 100644 --- a/tests/data/testcase_complete_cfme.xml +++ b/tests/data/testcase_complete_cfme.xml @@ -5,7 +5,9 @@ test_manual - Manual tests with many supported fields.<br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> + <pre> +Manual tests with many supported fields. +</pre><br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> step1 diff --git a/tests/data/testcase_complete_cfme_param.xml b/tests/data/testcase_complete_cfme_param.xml index f1bea62..4806925 100644 --- a/tests/data/testcase_complete_cfme_param.xml +++ b/tests/data/testcase_complete_cfme_param.xml @@ -5,7 +5,9 @@ test_manual - Manual tests with many supported fields.<br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> + <pre> +Manual tests with many supported fields. +</pre><br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> step1 diff --git a/tests/data/testcase_fields_cfme.xml b/tests/data/testcase_fields_cfme.xml index 71214f3..c6006c5 100644 --- a/tests/data/testcase_fields_cfme.xml +++ b/tests/data/testcase_fields_cfme.xml @@ -5,7 +5,9 @@ test_manual - Manual tests with many supported fields.<br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> + <pre> +Manual tests with many supported fields. +</pre><br id="id123"/><br/><a href="https://foo/bar/baz/blob/master/file#L83">Test Source</a> step1