2020-05-25 16:42:40 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2020 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the the Apache License v2.0 with LLVM Exceptions (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://llvm.org/LICENSE.txt
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2020-06-03 13:40:22 +02:00
|
|
|
import os
|
2020-05-25 16:42:40 +02:00
|
|
|
from typing import Optional
|
|
|
|
from lxml import etree
|
2020-06-03 13:40:22 +02:00
|
|
|
from phabtalk.phabtalk import Report, Step
|
2020-05-25 16:42:40 +02:00
|
|
|
|
|
|
|
|
2020-06-03 13:40:22 +02:00
|
|
|
def run(working_dir: str, test_results: str, step: Optional[Step], report: Optional[Report]):
|
2020-05-25 16:42:40 +02:00
|
|
|
if report is None:
|
|
|
|
report = Report() # For debugging.
|
2020-06-03 13:40:22 +02:00
|
|
|
if step is None:
|
|
|
|
step = Step()
|
|
|
|
path = os.path.join(working_dir, test_results)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
logging.warning(f'{path} is not found')
|
|
|
|
step.success = False
|
|
|
|
step.messages.append(f'test report "{path}" is not found')
|
2020-05-25 16:42:40 +02:00
|
|
|
return
|
2020-06-03 13:40:22 +02:00
|
|
|
report.add_artifact(working_dir, test_results, 'test results')
|
2020-05-25 16:42:40 +02:00
|
|
|
success = True
|
2020-06-03 13:40:22 +02:00
|
|
|
root_node = etree.parse(path)
|
2020-05-25 16:42:40 +02:00
|
|
|
for test_case in root_node.xpath('//testcase'):
|
|
|
|
test_result = 'pass'
|
|
|
|
if test_case.find('failure') is not None:
|
|
|
|
test_result = 'fail'
|
|
|
|
if test_case.find('skipped') is not None:
|
|
|
|
test_result = 'skip'
|
|
|
|
report.test_stats[test_result] += 1
|
|
|
|
if test_result == 'fail':
|
|
|
|
success = False
|
|
|
|
failure = test_case.find('failure')
|
|
|
|
test_result = {
|
|
|
|
'name': test_case.attrib['name'],
|
|
|
|
'namespace': test_case.attrib['classname'],
|
|
|
|
'result': test_result,
|
|
|
|
'duration': float(test_case.attrib['time']),
|
|
|
|
'details': failure.text
|
|
|
|
}
|
|
|
|
report.unit.append(test_result)
|
|
|
|
|
2020-06-03 13:40:22 +02:00
|
|
|
msg = f'{report.test_stats["pass"]} tests passed, {report.test_stats["fail"]} failed and ' \
|
2020-05-25 16:42:40 +02:00
|
|
|
f'{report.test_stats["skip"]} were skipped.\n'
|
2020-06-03 13:40:22 +02:00
|
|
|
if not success:
|
|
|
|
step.success = False
|
2020-05-25 16:42:40 +02:00
|
|
|
for test_case in report.unit:
|
|
|
|
if test_case['result'] == 'fail':
|
|
|
|
msg += f'{test_case["namespace"]}/{test_case["name"]}\n'
|
2020-06-03 13:40:22 +02:00
|
|
|
logging.debug(f'report: {report}')
|
|
|
|
logging.debug(f'step: {step}')
|
2020-05-25 16:42:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Processes results from xml report')
|
2020-06-03 13:40:22 +02:00
|
|
|
parser.add_argument('test-report', default='build/test-results.xml')
|
2020-05-25 16:42:40 +02:00
|
|
|
parser.add_argument('--log-level', type=str, default='INFO')
|
|
|
|
args = parser.parse_args()
|
|
|
|
logging.basicConfig(level=args.log_level)
|
2020-06-03 13:40:22 +02:00
|
|
|
run(os.getcwd(), args.test_report, None, None)
|