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

Add support for TAP version 12. #26

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A utility that converts [TAP version 13](https://testanything.org/) to [JUnit](https://junit.org/junit5/). That's it.
A utility that converts [TAP version 12 and 13](https://testanything.org/) to [JUnit](https://junit.org/junit5/). That's it.

Upstream is currently unmaintained at https://bitbucket.org/fedoraqa/pytap13/src/develop/

Expand Down
86 changes: 58 additions & 28 deletions tap2junit/tap13.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import yamlish

RE_VERSION = re.compile(r"^\s*TAP version 13\s*$")
RE_VERSION = re.compile(r"^\s*TAP version (?P<version>.*)$")
RE_PLAN = re.compile(
r"^\s*(?P<start>\d+)\.\.(?P<end>\d+)\s*(#\s*(?P<explanation>.*))?\s*$"
)
Expand Down Expand Up @@ -60,6 +60,23 @@ def __init__(self):
self.__tests_counter = 0
self.tests_planned = None

def _parse_yaml(self, line, in_yaml, in_yaml_block):
indentation = len(line) - len(line.lstrip())
if in_yaml_block and indentation > self.tests[-1]._yaml_block_indentation:
return in_yaml, in_yaml_block
elif RE_YAML_BLOCK.match(line):
self.tests[-1]._yaml_block_indentation = indentation
in_yaml_block = True
elif RE_YAMLISH_END.match(line):
self.tests[-1]._yaml_buffer.append(line.strip())
in_yaml = False
in_yaml_block = False
self.tests[-1].yaml = yamlish.load(self.tests[-1]._yaml_buffer)
else:
self.tests[-1]._yaml_buffer.append(line.rstrip())

return in_yaml, in_yaml_block

def _parse(self, source):
seek_version = True
seek_plan = False
Expand All @@ -68,8 +85,29 @@ def _parse(self, source):
in_test = False
in_yaml = False
in_yaml_block = False
version12 = False
# see https://testanything.org/tap-version-13-specification.html
# To indicate that this is TAP13 the first line must be
# 'TAP version 13'
non_empty_lines = [
line for line in source.getvalue().splitlines() if line.strip()
]
match = RE_VERSION.match(non_empty_lines[0])
if match:
# It is an error if version is anything below 13 (so not an int is an error)
version = int(match.groupdict()["version"])
if version < 13:
raise ValueError("Version specified is less than 13")
else:
# No version, so it is 12: https://testanything.org/tap-specification.html
version12 = True
seek_version = False
seek_plan = True
seek_test = True
self.__tests_counter = 0

for line in source:
if not seek_version and not in_yaml and RE_VERSION.match(line):
if not version12 and not seek_version and RE_VERSION.match(line):
Copy link
Member

@MoLow MoLow Jul 18, 2022

Choose a reason for hiding this comment

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

did you test what happens when inside a yaml and the yaml contains a version line? thats why this tested fornot in_yaml

Copy link
Member

Choose a reason for hiding this comment

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

@cclauss @fmartinsons this has broken #31

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my, this was a mistake.
This flag may have removed when I resolved conflict on my branch.
I open #35 to fix that. Sorry

Copy link
Member

Choose a reason for hiding this comment

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

thanks, I found that out when working on #34

# refack: breaking TAP13 spec, to allow multiple TAP headers
seek_version = True
seek_plan = False
Expand All @@ -81,22 +119,7 @@ def _parse(self, source):
# raise ValueError("Bad TAP format, multiple TAP headers")

if in_yaml:
indentation = len(line) - len(line.lstrip())
if (
in_yaml_block
and indentation > self.tests[-1]._yaml_block_indentation
):
continue
elif RE_YAML_BLOCK.match(line):
self.tests[-1]._yaml_block_indentation = indentation
in_yaml_block = True
elif RE_YAMLISH_END.match(line):
self.tests[-1]._yaml_buffer.append(line.strip())
in_yaml = False
in_yaml_block = False
self.tests[-1].yaml = yamlish.load(self.tests[-1]._yaml_buffer)
else:
self.tests[-1]._yaml_buffer.append(line.rstrip())
in_yaml, in_yaml_block = self._parse_yaml(line, in_yaml, in_yaml_block)
continue

line = line.strip()
Expand All @@ -106,14 +129,19 @@ def _parse(self, source):
self.tests[-1].diagnostics.append(line)
continue
if RE_YAMLISH_START.match(line):
if version12:
raise ValueError(
"Bad TAP format, yaml block detected but no "
"TAP version 13 line"
)
self.tests[-1]._yaml_buffer = [line.strip()]
in_yaml = True
in_yaml_block = False
continue

# this is "beginning" of the parsing, skip all lines until
# version is found
if seek_version:
# this is "beginning" of the parsing for TAP version 13
# skip all lines until version is found
if not version12 and seek_version:
if RE_VERSION.match(line):
seek_version = False
seek_plan = True
Expand All @@ -122,10 +150,10 @@ def _parse(self, source):
continue

if seek_plan:
m = RE_PLAN.match(line)
if m:
d = m.groupdict()
self.tests_planned = int(d.get("end", 0))
match = RE_PLAN.match(line)
if match:
fields = match.groupdict()
self.tests_planned = int(fields.get("end", 0))
seek_plan = False

# Stop processing if tests were found before the plan
Expand All @@ -134,10 +162,10 @@ def _parse(self, source):
break

if seek_test:
m = RE_TEST_LINE.match(line)
if m:
match = RE_TEST_LINE.match(line)
if match:
self.__tests_counter += 1
t_attrs = m.groupdict()
t_attrs = match.groupdict()
if t_attrs["id"] is None:
t_attrs["id"] = self.__tests_counter
t_attrs["id"] = int(t_attrs["id"])
Expand Down Expand Up @@ -178,6 +206,8 @@ def _parse(self, source):
description="Test %s missing" % (i + 1),
comment="DIAG: Test %s not present" % (i + 1),
)
# Even for version 12 file which doesn't specify YAML we use
# this field to ease thing for the caller
t.yaml = {"severity": "missing", "exitcode": -1}
self.tests.append(t)

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/test-plan-at-end.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TAP version 13
ok 1 /MyTest/1
ok 2 /MyTest/2
# My test 2 comments
ok 3 /MyTest/3
not ok 4 /MyTest/3
ok /MyTest/4
1..5
7 changes: 7 additions & 0 deletions test/fixtures/test-tap12.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1..5
ok 1 /MyTest/1
ok 2 /MyTest/2
# My test 2 comments
ok 3 /MyTest/3
not ok 4 /MyTest/3
ok /MyTest/4