Skip to content

Commit 3e45ebb

Browse files
codambroCody D'Ambrosiopre-commit-ci[bot]
authored
Support raw yaml block in tracker (#167)
* Update tracker.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add yaml to result * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add unit tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add mocks * fix unittests * Update test_line.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix formatting and test * Update test_line.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove indent variable --------- Co-authored-by: Cody D'Ambrosio <cody.dambrosio@hpe.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c80077e commit 3e45ebb

4 files changed

Lines changed: 92 additions & 5 deletions

File tree

src/tap/line.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,16 @@ def __str__(self):
111111
diagnostics = ""
112112
if self.diagnostics is not None:
113113
diagnostics = "\n" + self.diagnostics.rstrip()
114-
return f"{is_not}ok {self.number} {self.description}{directive}{diagnostics}"
114+
yaml_block = ""
115+
if self._yaml_block is not None:
116+
indented_yaml_block = "\n".join(
117+
f" {line}" for line in self._yaml_block.splitlines()
118+
)
119+
yaml_block = f"\n ---\n{indented_yaml_block}\n ..."
120+
return (
121+
f"{is_not}ok {self.number} {self.description}{directive}"
122+
f"{diagnostics}{yaml_block}"
123+
)
115124

116125

117126
class Plan(Line):

src/tap/tracker.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,39 @@ def _track(self, class_name):
7373
if self.combined:
7474
self.combined_test_cases_seen.append(class_name)
7575

76-
def add_ok(self, class_name, description, directive="", diagnostics=None):
76+
def add_ok(
77+
self,
78+
class_name,
79+
description,
80+
directive="",
81+
diagnostics=None,
82+
raw_yaml_block=None,
83+
):
7784
result = Result(
7885
ok=True,
7986
number=self._get_next_line_number(class_name),
8087
description=description,
8188
diagnostics=diagnostics,
8289
directive=Directive(directive),
90+
raw_yaml_block=raw_yaml_block,
8391
)
8492
self._add_line(class_name, result)
8593

86-
def add_not_ok(self, class_name, description, directive="", diagnostics=None):
94+
def add_not_ok(
95+
self,
96+
class_name,
97+
description,
98+
directive="",
99+
diagnostics=None,
100+
raw_yaml_block=None,
101+
):
87102
result = Result(
88103
ok=False,
89104
number=self._get_next_line_number(class_name),
90105
description=description,
91106
diagnostics=diagnostics,
92107
directive=Directive(directive),
108+
raw_yaml_block=raw_yaml_block,
93109
)
94110
self._add_line(class_name, result)
95111

tests/test_line.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
from tap.directive import Directive
44
from tap.line import Line, Result
55

6+
try:
7+
import yaml # noqa
8+
from more_itertools import peekable # noqa
9+
10+
have_yaml = True
11+
except ImportError:
12+
have_yaml = False
13+
614

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

4048
def test_str_diagnostics(self):
41-
result = Result(False, 43, "failing", diagnostics="# more info")
42-
self.assertEqual("not ok 43 failing\n# more info", str(result))
49+
result = Result(False, 45, "failing", diagnostics="# more info")
50+
self.assertEqual("not ok 45 failing\n# more info", str(result))
51+
52+
def test_yaml_block(self):
53+
raw_yaml_block = """\
54+
message: test_message
55+
severity: fail
56+
"""
57+
result = Result(False, 46, "passing", raw_yaml_block=raw_yaml_block)
58+
if have_yaml:
59+
self.assertEqual(result.yaml_block["message"], "test_message")
60+
self.assertIn(
61+
" ---\n message: test_message\n severity: fail\n ...", str(result)
62+
)
63+
else:
64+
self.assertIsNone(result.yaml_block)

tests/test_tracker.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
from tap.tests import TestCase
88
from tap.tracker import Tracker
99

10+
try:
11+
import yaml # noqa
12+
from more_itertools import peekable # noqa
13+
14+
have_yaml = True
15+
except ImportError:
16+
have_yaml = False
17+
1018

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

306+
def test_adds_ok_with_yaml_block(self):
307+
tracker = Tracker()
308+
tracker.add_ok(
309+
"FakeTestCase",
310+
"a description",
311+
raw_yaml_block="""\
312+
message: test_message
313+
severity: pass
314+
""",
315+
)
316+
line = tracker._test_cases["FakeTestCase"][0]
317+
if have_yaml:
318+
self.assertEqual("test_message", line.yaml_block["message"])
319+
else:
320+
self.assertIsNone(line.yaml_block)
321+
322+
def test_adds_not_ok_with_yaml_block(self):
323+
tracker = Tracker()
324+
tracker.add_not_ok(
325+
"FakeTestCase",
326+
"a description",
327+
raw_yaml_block="""\
328+
message: test_message
329+
severity: fail
330+
""",
331+
)
332+
line = tracker._test_cases["FakeTestCase"][0]
333+
if have_yaml:
334+
self.assertEqual("test_message", line.yaml_block["message"])
335+
else:
336+
self.assertIsNone(line.yaml_block)
337+
298338
def test_header_displayed_by_default(self):
299339
tracker = Tracker()
300340
self.assertTrue(tracker.header)

0 commit comments

Comments
 (0)