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 option to override test suite name #25

Merged
merged 4 commits into from
Jul 13, 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
49 changes: 41 additions & 8 deletions tap2junit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,50 @@
import os
import platform

import yamlish
from junit_xml import TestCase, TestSuite

from tap2junit.tap13 import TAP13 as tap13


def extract_test_info_from_description(description):
# If test description is path-like (contains /), consider the last
# token to be the test name and convert the dirname to classname.
# The test description can also have already a class name style
# (module serapated by '.') so do the same for this case
if description and "/" in description:
sanitize = os.path.normpath(description)
(test_class, test_name) = os.path.split(sanitize)
# Remove possible heading slash and replace / by .
test_class = test_class.lstrip("/").replace("/", ".")
elif description and "." in description:
# Remove possible heading slash and replace / by .
test_name = description.split(".")[-1]
test_class = ".".join(description.split(".")[0:-1])
else:
test_name = description
test_class = None
return (test_class, test_name)


def map_yaml_to_junit(test):
yaml = test.yaml or {}
# Even though the name is `duration_ms` the value is in seconds.
elapsed_sec = yaml.get("duration_ms", None)
t = TestCase(test.description, classname=None, elapsed_sec=elapsed_sec)
(test_class, test_name) = extract_test_info_from_description(test.description)
t = TestCase(test_name, classname=test_class, elapsed_sec=elapsed_sec)
if test.result == "ok":
if test.directive in ("SKIP", "TODO"):
t.add_skipped_info(test.comment)
else:
t.stdout = test.comment

elif test.result == "not ok":
raw_yaml = f"\n{yamlish.dumps(yaml)}" if yaml else ""
err_code = yaml.get("exitcode", 0)
err_severity = yaml.get("severity", "")
err_output = yaml.get("stack", "")
error_message = f"{err_severity} ({err_code})"
err_output = yaml.get("stack", "") or raw_yaml
error_message = yaml.get("message", "") or f"{err_severity} ({err_code})"
if err_code < 0 or err_severity == "crashed":
t.add_error_info(error_message, err_output, err_code)
else:
Expand All @@ -31,17 +54,19 @@ def map_yaml_to_junit(test):
return t


def parse(name, data):
def parse(name, data, package=None):
tap_parser = tap13()
tap_parser.parse(data)
junit_tests = [map_yaml_to_junit(t) for t in tap_parser.tests]
return TestSuite(name, junit_tests, platform.node())
return TestSuite(
name, test_cases=junit_tests, hostname=platform.node(), package=package
)


def convert(in_file, out_file, pretty=True):
def convert(in_file, out_file, pretty=True, name=None, package=None):
input_file = os.path.splitext(in_file.name)[0]
data = in_file.read()
result = parse(input_file, data)
result = parse(name or input_file, data, package)
TestSuite.to_file(out_file, [result], prettyprint=pretty, encoding="utf-8")


Expand All @@ -64,8 +89,16 @@ def main():
arg_parser.add_argument(
"--compact", "-c", action="store_true", help="do not prettify the xml output"
)
arg_parser.add_argument("--name", "-n", help="override test suite name")
arg_parser.add_argument("--package", "-p", help="set package for test suite")
args = arg_parser.parse_args()
convert(args.input, args.output, pretty=not args.compact)
convert(
args.input,
args.output,
pretty=not args.compact,
name=args.name,
package=args.package,
)


if __name__ == "__main__":
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/test4.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TAP version 13
1..9
ok 1 myplugin.module1.test1
ok 2 myplugin.module1.test2
ok 3 myplugin.module2.test1
ok 4 myplugin.module2.test2
ok 5 myplugin.module2.testSub.test1
ok 6 myplugin.module2.testSub.test2
ok 7 myplugin.test1
not ok 8 myplugin.test2
ok 9 myplugin.test3