From 1da8c043259102d506b71329fbab5bc284473b3b Mon Sep 17 00:00:00 2001 From: Pierrick Guillaume Date: Fri, 11 Jul 2025 11:41:40 +0200 Subject: [PATCH] Add text to Unknown tap line Unknown tap line where simply discarded. Instead, provide a way to retrieve the original text programmatically. This could be used by an alternative CLI application to pass through those lines. Signed-off-by: Pierrick Guillaume --- AUTHORS | 1 + src/tap/line.py | 8 ++++++++ src/tap/parser.py | 4 ++-- tests/test_parser.py | 8 ++++++-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 941bde4..37d10ec 100644 --- a/AUTHORS +++ b/AUTHORS @@ -15,6 +15,7 @@ Contributors * meejah (https://meejah.ca) * Michael F. Lamb (http://datagrok.org) * Nicolas Caniart +* Pierrick Guillaume * Richard Bosworth * Ross Burton * Simon McVittie diff --git a/src/tap/line.py b/src/tap/line.py index 6b3abdb..a48f839 100644 --- a/src/tap/line.py +++ b/src/tap/line.py @@ -203,7 +203,15 @@ class Unknown(Line): This exists for the purpose of a Null Object pattern. """ + def __init__(self, text=None): + self._text = text + @property def category(self): """:returns: ``unknown``""" return "unknown" + + @property + def text(self): + """Get the text.""" + return self._text diff --git a/src/tap/parser.py b/src/tap/parser.py index f43829c..acdd1fd 100644 --- a/src/tap/parser.py +++ b/src/tap/parser.py @@ -133,7 +133,7 @@ def parse_line(self, text, fh=None): if match: return self._parse_version(match) - return Unknown() + return Unknown(text) def _parse_plan(self, match): """Parse a matching plan line.""" @@ -142,7 +142,7 @@ def _parse_plan(self, match): # Only SKIP directives are allowed in the plan. if directive.text and not directive.skip: - return Unknown() + return Unknown(match.string) return Plan(expected_tests, directive) diff --git a/tests/test_parser.py b/tests/test_parser.py index 3157efe..d0de780 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -110,10 +110,12 @@ def test_finds_directive(self): def test_unrecognizable_line(self): """The parser returns an unrecognizable line.""" parser = Parser() + test_line = "This is not a valid TAP line. # srsly" - line = parser.parse_line("This is not a valid TAP line. # srsly") + line = parser.parse_line(test_line) self.assertEqual("unknown", line.category) + self.assertEqual(test_line, line.text) def test_diagnostic_line(self): """The parser extracts a diagnostic line.""" @@ -171,10 +173,12 @@ def test_finds_plan_with_skip(self): def test_ignores_plan_with_any_non_skip_directive(self): """The parser only recognizes SKIP directives in plans.""" parser = Parser() + test_line = "1..42 # TODO will not work." - line = parser.parse_line("1..42 # TODO will not work.") + line = parser.parse_line(test_line) self.assertEqual("unknown", line.category) + self.assertEqual(test_line, line.text) def test_parses_text(self): sample = inspect.cleandoc(