• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import SysTestKit from "../kit/SysTestKit";
17
18class OhReport {
19    constructor(attr) {
20        this.delegator = attr.delegator;
21        this.id = 'report';
22        this.index = 0;
23        this.duration = 0;
24    }
25
26    init(coreContext) {
27        this.coreContext = coreContext;
28        this.suiteService = this.coreContext.getDefaultService('suite');
29        this.specService = this.coreContext.getDefaultService('spec');
30    }
31
32    taskStart() {
33    }
34
35    async taskDone() {
36        this.taskDoneTime = new Date().getTime();
37        let summary = this.suiteService.getSummary();
38        let message = '\n' + 'OHOS_REPORT_RESULT: stream=Tests run: ' + summary.total + ', Failure: ' + summary.failure;
39        message += ', Error: ' + summary.error;
40        message += ', Pass: ' + summary.pass;
41        message += ', Ignore: ' + summary.ignore;
42        message += '\n' + 'OHOS_REPORT_CODE: ' + (summary.failure > 0 ? -1 : 0) + '\n';
43        let isHasError = summary.failure > 0 || summary.error > 0;
44        let config = this.coreContext.getDefaultService('config');
45        if(config.isBreakOnError() && isHasError){
46            // 未执行全部说明
47            message += '\n' + 'OHOS_REPORT_RESULT: breakOnError model, Stopping whole test suite if one specific test case failed or error' + '\n';
48        }
49        message += 'OHOS_REPORT_STATUS: taskconsuming=' + summary.duration + '\n';
50        console.info(message);
51        await SysTestKit.print(message);
52        console.info('report print success');
53        this.delegator.finishTest('your test finished!!!', 0, () => {
54            console.info('your test finished!!!');
55        });
56    }
57
58    incorrectFormat() {
59        if (this.coreContext.getDefaultService('config').filterValid.length !== 0) {
60            var value = this.coreContext.getDefaultService('config').filterValid;
61            var message = 'this param ' + value.join(',') + ' is invalid' + '\n';
62            this.delegator.finishTest(message, 0, () => {
63            });
64        }
65    }
66
67    async suiteStart() {
68        let message = '\n' + 'OHOS_REPORT_SUM: ' + this.suiteService.getCurrentRunningSuite().getSpecsNum();
69        message += '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description + '\n';
70        console.info(message);
71        await SysTestKit.print(message);
72        console.info(this.suiteService.getCurrentRunningSuite().description + ' suiteStart print success');
73    }
74
75    async suiteDone() {
76        let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description;
77        message += '\n' + 'OHOS_REPORT_STATUS: suiteconsuming=' + this.suiteService.getCurrentRunningSuite().duration + '\n';
78        console.info(message);
79        await SysTestKit.print(message);
80        console.info(this.suiteService.getCurrentRunningSuite().description + ' suiteDone print success');
81    }
82
83    async specStart() {
84        let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description;
85        message += '\n' + 'OHOS_REPORT_STATUS: current=' + (++this.index);
86        message += '\n' + 'OHOS_REPORT_STATUS: id=JS';
87        message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.specService.getTestTotal();
88        message += '\n' + 'OHOS_REPORT_STATUS: stream=';
89        message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description;
90        message += '\n' + 'OHOS_REPORT_STATUS_CODE: 1' + '\n';
91        console.info(message);
92        await SysTestKit.print(message);
93        console.info(this.specService.currentRunningSpec.description + ' specStart start print success');
94    }
95
96    async specDone() {
97        let message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description;
98        message += '\n' + 'OHOS_REPORT_STATUS: current=' + (this.index);
99        message += '\n' + 'OHOS_REPORT_STATUS: id=JS';
100        message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.specService.getTestTotal();
101        let emsg = '';
102        if (this.specService.currentRunningSpec.error) {
103            message += '\n' + 'OHOS_REPORT_STATUS: stack=' + this.specService.currentRunningSpec.error.message;
104            message += '\n' + 'OHOS_REPORT_STATUS: stream=';
105            message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description;
106            message += '\n' + this.specService.currentRunningSpec.error.message;
107            message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description;
108            message += '\n' + 'OHOS_REPORT_STATUS_CODE: -1' + '\n';
109        } else if (this.specService.currentRunningSpec.result) {
110            if (this.specService.currentRunningSpec.result.failExpects.length > 0) {
111                this.specService.currentRunningSpec.result.failExpects.forEach(failExpect => {
112                    emsg = failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue));
113                });
114                message += '\n' + 'OHOS_REPORT_STATUS: stack=' + emsg;
115                message += '\n' + 'OHOS_REPORT_STATUS: stream=';
116                message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description;
117                message += '\n' + emsg + '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description;
118                message += '\n' + 'OHOS_REPORT_STATUS_CODE: -2' + '\n';
119            } else {
120                message += '\n' + 'OHOS_REPORT_STATUS: stream=';
121                message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description;
122                message += '\n' + 'OHOS_REPORT_STATUS_CODE: 0' + '\n';
123            }
124        } else {
125            message += '\n';
126        }
127        message += 'OHOS_REPORT_STATUS: consuming=' + this.specService.currentRunningSpec.duration + '\n';
128        console.info(message);
129        await SysTestKit.print(message);
130        console.info(this.specService.currentRunningSpec.description + ' specDone end print success');
131    }
132}
133
134export default OhReport;
135