Skip to content

Commit

Permalink
Improve description formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Jan 30, 2019
1 parent 8c76bf5 commit 854e2bc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
10 changes: 5 additions & 5 deletions dump2polarion/exporters/testcases_exporter.py
Expand Up @@ -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."""
Expand Down Expand Up @@ -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

Expand Down
59 changes: 53 additions & 6 deletions dump2polarion/exporters/transform.py
Expand Up @@ -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"\[.*\]")


Expand Down Expand Up @@ -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

Expand All @@ -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"] = "<pre>\n{}\n</pre>".format(description)


def add_unique_runid(testcase, run_id=None):
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion tests/data/testcase_complete_cfme.xml
Expand Up @@ -5,7 +5,9 @@
</properties>
<testcase approver-ids="mkourim:approved" assignee-id="mkourim" due-date="2018-09-30" id="ITEM01" initial-estimate="1/4h">
<title>test_manual</title>
<description>Manual tests with many supported fields.&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<description>&lt;pre&gt;
Manual tests with many supported fields.
&lt;/pre&gt;&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<test-steps>
<test-step>
<test-step-column id="step">step1</test-step-column>
Expand Down
4 changes: 3 additions & 1 deletion tests/data/testcase_complete_cfme_param.xml
Expand Up @@ -5,7 +5,9 @@
</properties>
<testcase approver-ids="mkourim:approved" assignee-id="mkourim" due-date="2018-09-30" id="ITEM01" initial-estimate="1/4h">
<title>test_manual</title>
<description>Manual tests with many supported fields.&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<description>&lt;pre&gt;
Manual tests with many supported fields.
&lt;/pre&gt;&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<test-steps>
<test-step>
<test-step-column id="step">step1</test-step-column>
Expand Down
4 changes: 3 additions & 1 deletion tests/data/testcase_fields_cfme.xml
Expand Up @@ -5,7 +5,9 @@
</properties>
<testcase approver-ids="mkourim:approved" assignee-id="mkourim" due-date="2018-09-30" id="ITEM01" initial-estimate="1/4h">
<title>test_manual</title>
<description>Manual tests with many supported fields.&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<description>&lt;pre&gt;
Manual tests with many supported fields.
&lt;/pre&gt;&lt;br id="id123"/&gt;&lt;br/&gt;&lt;a href="https://foo/bar/baz/blob/master/file#L83"&gt;Test Source&lt;/a&gt;</description>
<test-steps>
<test-step>
<test-step-column id="step">step1</test-step-column>
Expand Down

0 comments on commit 854e2bc

Please sign in to comment.