Skip to content

Commit f6051bd

Browse files
committed
#19 added command line argument enabling creation of dummy test case when there are no errors, e.g. for Bamboo compatibility
1 parent 3ed8960 commit f6051bd

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

src/ansible_lint_junit/main.py

+44-34
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def main():
3030
default="ansible-lint-junit.xml", action="store", help="print XML to output file")
3131
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
3232
help="print XML to console as command output", default=False)
33+
parser.add_argument("-d", "--dummy-test", dest="dummy", action="store_true",
34+
help="Adds single (1) dummy test if there were 0 tests and/or 0 errors", default=False)
3335
parser.add_argument('--version', action='version',
3436
version='%(prog)s {version}'.format(version=version()))
3537

@@ -50,42 +52,50 @@ def main():
5052
errors_count = str(len(ansible_lint_output) - 1)
5153
break
5254

53-
testsuite = ET.SubElement(
54-
testsuites, "testsuite", errors=errors_count, failures="0", tests=errors_count, time="0")
55+
if arguments.dummy:
56+
testsuite = ET.SubElement(
57+
testsuites, "testsuite", errors=errors_count, failures="0", tests="1", time="0")
5558

56-
line_regex = re.compile('^(.*?):(\\d+?):\\s(.*)$')
57-
58-
if 0 == len(ansible_lint_output):
59-
ET.SubElement(testsuite, "testcase", name="dummy_testcase.py")
59+
ET.SubElement(testsuite, "testcase",
60+
name="dummy_testcase_ansible_lint_junit")
6061
else:
61-
parsed_lines = []
62-
for line in ansible_lint_output:
63-
if 0 < len(line):
64-
65-
line_match = line_regex.match(line)
66-
67-
if not line_match:
68-
continue
69-
70-
line_data = {
71-
"filename": line_match.group(1),
72-
"line": int(line_match.group(2)),
73-
"error": line_match.group(3),
74-
"text": line_match.group(3),
75-
}
76-
parsed_lines.append(line_data)
77-
78-
testcase = ET.SubElement(
79-
testsuite, "testcase", name="{}-{}".format(line_data['filename'], len(parsed_lines)))
80-
81-
ET.SubElement(
82-
testcase,
83-
"failure",
84-
file=line_data['filename'],
85-
line=str(line_data['line']),
86-
message=line_data['error'],
87-
type="Ansible Lint"
88-
).text = line_data['error']
62+
testsuite = ET.SubElement(
63+
testsuites, "testsuite", errors=errors_count, failures="0", tests=errors_count, time="0")
64+
65+
line_regex = re.compile('^(.*?):(\\d+?):\\s(.*)$')
66+
67+
if 0 == len(ansible_lint_output):
68+
ET.SubElement(testsuite, "testcase",
69+
name="dummy_testcase_ansible_lint_junit")
70+
else:
71+
parsed_lines = []
72+
for line in ansible_lint_output:
73+
if 0 < len(line):
74+
75+
line_match = line_regex.match(line)
76+
77+
if not line_match:
78+
continue
79+
80+
line_data = {
81+
"filename": line_match.group(1),
82+
"line": int(line_match.group(2)),
83+
"error": line_match.group(3),
84+
"text": line_match.group(3),
85+
}
86+
parsed_lines.append(line_data)
87+
88+
testcase = ET.SubElement(
89+
testsuite, "testcase", name="{}-{}".format(line_data['filename'], len(parsed_lines)))
90+
91+
ET.SubElement(
92+
testcase,
93+
"failure",
94+
file=line_data['filename'],
95+
line=str(line_data['line']),
96+
message=line_data['error'],
97+
type="Ansible Lint"
98+
).text = line_data['error']
8999

90100
xml_string = ET.tostring(testsuites, encoding='utf8', method='xml')
91101
xml_nice = minidom.parseString(xml_string)

0 commit comments

Comments
 (0)