1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2022 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18from devicetest.core.constants import RunResult
19from devicetest.core.exception import DeviceTestError
20from devicetest.core.report import ReportHandler
21from devicetest.error import ErrorMessage
22from devicetest.log.logger import DeviceTestLog as Log
23from xdevice import SuiteReporter
24
25
26class UploadResultHandler:
27
28    def __init__(self, report_path):
29        self.__test_runner = None
30        self.__report_path = None
31        self.__upload_suitereporter_lock = False
32        self.report_handler = ReportHandler(report_path)
33
34    def set_test_runner(self, test_runner):
35        self.__test_runner = test_runner
36        Log.info("set test runner finish")
37
38    def get_error_msg(self, test_runner, is_cur_case_error=False):
39        return ErrorMessage.Common.Code_0201004
40
41    def flash_os_test_results(self, test_runner, test_results):
42        cur_case_error_msg = self.get_error_msg(test_runner,
43                                                is_cur_case_error=True)
44        if not test_results:
45            test_result = test_runner.record_cls_result(
46                test_runner.project.execute_case_name, None, None, None, cur_case_error_msg)
47            test_results.append(test_result)
48        else:
49            if test_results[-1].get("result").strip() == RunResult.FAILED \
50                    and not test_results[0].get("error").strip():
51                test_results[0]["error"] = cur_case_error_msg
52        return test_results
53
54    def upload_suite_reporter(self, test_results=None):
55        report_result_tuple = self.report_handler.generate_test_report(
56            self.__test_runner, test_results)
57        SuiteReporter.append_report_result(report_result_tuple)
58        Log.debug("result tuple:{}".format(report_result_tuple))
59        Log.info("upload suitereporter success.")
60
61    def upload_suitereporter(self, is_stoped=False):
62        try:
63            self.upload_suite_reporter()
64        except DeviceTestError as err:
65            Log.error(err)
66        except Exception:
67            Log.error(ErrorMessage.Common.Code_0201003, is_traceback=True)
68