Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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
12 changes: 11 additions & 1 deletion src/tap/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ def __str__(self):
diagnostics = ""
if self.diagnostics is not None:
diagnostics = "\n" + self.diagnostics.rstrip()
return f"{is_not}ok {self.number} {self.description}{directive}{diagnostics}"
yaml_block = ""
if self._yaml_block is not None:
indent = " "
indented_yaml_block = "\n".join(
f"{indent}{line}" for line in self._yaml_block.splitlines()
)
yaml_block = f"\n{indent}---\n{indented_yaml_block}\n{indent}..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking... should there be a trailing newline here? I suspect the answer is no because is probably printed later, but I would hate to mistakenly combine test line like <end of yaml>...not ok 43 yada yada

Suggested change
yaml_block = f"\n{indent}---\n{indented_yaml_block}\n{indent}..."
yaml_block = f"\n{indent}---\n{indented_yaml_block}\n{indent}...\n"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline needed here. in Tracker.generate_tap_report it calls print for each Line object. a Result is still a "Line" (even though it's actually multi-line).

    def generate_tap_report(self, test_case, tap_lines, out_file):
        self._write_test_case_header(test_case, out_file)

        for tap_line in tap_lines:
            print(tap_line, file=out_file)

Plus for reassurance, you can see that even if there was no yaml block added, diagnostics in this method did not have a trailing newline either

return (
f"{is_not}ok {self.number} {self.description}{directive}"
f"{diagnostics}{yaml_block}"
)


class Plan(Line):
Expand Down
20 changes: 18 additions & 2 deletions src/tap/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,39 @@ def _track(self, class_name):
if self.combined:
self.combined_test_cases_seen.append(class_name)

def add_ok(self, class_name, description, directive="", diagnostics=None):
def add_ok(
self,
class_name,
description,
directive="",
diagnostics=None,
raw_yaml_block=None,
):
result = Result(
ok=True,
number=self._get_next_line_number(class_name),
description=description,
diagnostics=diagnostics,
directive=Directive(directive),
raw_yaml_block=raw_yaml_block,
)
self._add_line(class_name, result)

def add_not_ok(self, class_name, description, directive="", diagnostics=None):
def add_not_ok(
self,
class_name,
description,
directive="",
diagnostics=None,
raw_yaml_block=None,
):
result = Result(
ok=False,
number=self._get_next_line_number(class_name),
description=description,
diagnostics=diagnostics,
directive=Directive(directive),
raw_yaml_block=raw_yaml_block,
)
self._add_line(class_name, result)

Expand Down
26 changes: 24 additions & 2 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
from tap.directive import Directive
from tap.line import Line, Result

try:
import yaml # noqa
from more_itertools import peekable # noqa

have_yaml = True
except ImportError:
have_yaml = False


class TestLine(unittest.TestCase):
"""Tests for tap.line.Line"""
Expand Down Expand Up @@ -38,5 +46,19 @@ def test_str_directive(self):
self.assertEqual("ok 44 passing # SKIP a reason", str(result))

def test_str_diagnostics(self):
result = Result(False, 43, "failing", diagnostics="# more info")
self.assertEqual("not ok 43 failing\n# more info", str(result))
result = Result(False, 45, "failing", diagnostics="# more info")
self.assertEqual("not ok 45 failing\n# more info", str(result))

def test_yaml_block(self):
raw_yaml_block = """\
message: test_message
severity: fail
"""
result = Result(False, 46, "passing", raw_yaml_block=raw_yaml_block)
if have_yaml:
self.assertEqual(result.yaml_block["message"], "test_message")
self.assertIn(
" ---\n message: test_message\n severity: fail\n ...", str(result)
)
else:
self.assertIsNone(result.yaml_block)
40 changes: 40 additions & 0 deletions tests/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from tap.tests import TestCase
from tap.tracker import Tracker

try:
import yaml # noqa
from more_itertools import peekable # noqa

have_yaml = True
except ImportError:
have_yaml = False


class TestTracker(TestCase):
def _make_header(self, test_case):
Expand Down Expand Up @@ -295,6 +303,38 @@ def test_adds_not_ok_with_diagnostics(self):
line = tracker._test_cases["FakeTestCase"][0]
self.assertEqual("# more info\n", line.diagnostics)

def test_adds_ok_with_yaml_block(self):
tracker = Tracker()
tracker.add_ok(
"FakeTestCase",
"a description",
raw_yaml_block="""\
message: test_message
severity: pass
""",
)
line = tracker._test_cases["FakeTestCase"][0]
if have_yaml:
self.assertEqual("test_message", line.yaml_block["message"])
else:
self.assertIsNone(line.yaml_block)

def test_adds_not_ok_with_yaml_block(self):
tracker = Tracker()
tracker.add_not_ok(
"FakeTestCase",
"a description",
raw_yaml_block="""\
message: test_message
severity: fail
""",
)
line = tracker._test_cases["FakeTestCase"][0]
if have_yaml:
self.assertEqual("test_message", line.yaml_block["message"])
else:
self.assertIsNone(line.yaml_block)

def test_header_displayed_by_default(self):
tracker = Tracker()
self.assertTrue(tracker.header)
Expand Down