Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow render plot even when analysis errors #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 56 additions & 27 deletions src/testdrive/asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _use_timing_from_stdout(self):
try:
dct = json.loads(self.stdout)
timestamp = dct.get('timestamp')
except (TypeError, JSONDecodeError, AttributeError):
except (TypeError, json.JSONDecodeError, AttributeError):
return
if timestamp:
self._timestamp = timestamp
Expand Down Expand Up @@ -211,6 +211,29 @@ def xref_spec(self):
"""Return a cross-reference to this test case specification."""
return f'<<{self.uuid}_spec>>'


class Table:
def __init__(self, title, dct):
self._title = title
self._dct = dct

def to_asciidoc(self):
yield f'.{self._title}'
yield '[cols="1,4"]'
yield '|==='
yield ''
yield from (row(f'*{k}*', self._dct[k]) for k in sorted(self._dct))
yield ''
yield '|==='

class LiteralBlock:
def __init__(self, block):
self._block = block

def to_asciidoc(self):
yield literal_block(self._block)


class TestDetail:
"""Test detail for a test case."""
def __init__(self, images=(), tables=()):
Expand All @@ -227,15 +250,11 @@ def to_asciidoc(self, objdir):
yield ''
yield f'.{title or os.path.basename(path)}'
yield f'image::{filename}[]'
for (title, dct) in self._tables:
yield ''
yield f'.{title}'
yield '[cols="1,4"]'
yield '|==='
yield ''
yield from (row(f'*{k}*', dct[k]) for k in sorted(dct))

for tbl in self._tables:
yield ''
yield '|==='
yield from tbl.to_asciidoc()

@staticmethod
def _image_item(item):
"""Return (title, path) for the image specified by `item`.
Expand All @@ -249,6 +268,28 @@ def _image_item(item):
if not isinstance(item, str):
return None
return (None, item)

@staticmethod
def _tables_from_output(dct):
tables = []
if dct is not None:
if not isinstance(dct, dict):
return None
analysis = {}
for (key, val) in dct.items():
if isinstance(val, dict):
tables.append(Table(key, val))
elif isinstance(val, list):
try:
analysis[key] = '\n'.join(val)
except TypeError:
return None
else:
analysis[key] = val
if analysis:
tables.insert(0, Table('analysis', analysis))
return tables

@classmethod
def from_output(cls, output):
"""Return an instance of `cls` if `output` is JSON-encoded test detail.
Expand All @@ -270,24 +311,12 @@ def from_output(cls, output):
except ValueError:
return None
images.append((title, path))
tables = []
dct = obj.get('analysis')
if dct is not None:
if not isinstance(dct, dict):
return None
analysis = {}
for (key, val) in dct.items():
if isinstance(val, dict):
tables.append((key, val))
elif isinstance(val, list):
try:
analysis[key] = '\n'.join(val)
except TypeError:
return None
else:
analysis[key] = val
if analysis:
tables.insert(0, ('analysis', analysis))

analysis = obj.get('analysis')
tables = cls._tables_from_output(analysis)
if tables is None and analysis is not None:
tables = [LiteralBlock(json.dumps(analysis, indent=2))]

return cls(images, tables)

class TestSuite(OrderedDict):
Expand Down