1/* 2 * Copyright (c) 2021-2024 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'; 17import { collectCoverageData } from '../coverage/coverageCollect'; 18import { TAG, PrintTag } from '../../Constant'; 19 20class OhReport { 21 constructor(attr) { 22 this.delegator = attr.delegator; 23 this.abilityDelegatorArguments = attr.abilityDelegatorArguments; 24 this.id = 'report'; 25 this.index = 0; 26 this.duration = 0; 27 this.currentThreadName = 'mainThread'; 28 } 29 30 init(coreContext) { 31 this.coreContext = coreContext; 32 this.suiteService = this.coreContext.getDefaultService('suite'); 33 this.specService = this.coreContext.getDefaultService('spec'); 34 if (SysTestKit.workerPort !== null) { 35 this.currentThreadName = SysTestKit.workerPort.name; 36 } 37 } 38 39 taskStart() { 40 } 41 42 async taskDone() { 43 let summary = this.suiteService.getSummary(); 44 if (this.abilityDelegatorArguments !== null) { 45 this.taskDoneTime = new Date().getTime(); 46 const configService = this.coreContext.getDefaultService('config'); 47 const suiteService = this.coreContext.getDefaultService('suite'); 48 const specService = this.coreContext.getDefaultService('spec'); 49 if (configService['coverage'] === 'true') { 50 await collectCoverageData(); 51 } 52 let message = '\n' + `${PrintTag.OHOS_REPORT_RESULT}: stream=Tests run: ` + summary.total + ', Failure: ' + summary.failure; 53 message += ', Error: ' + summary.error; 54 message += ', Pass: ' + summary.pass; 55 message += ', Ignore: ' + summary.ignore; 56 if (specService.skipSpecNum > 0) { 57 message += ', SkipSpec: ' + specService.skipSpecNum; 58 } 59 message += '\n' + `${PrintTag.OHOS_REPORT_CODE}: ` + (summary.failure > 0 ? -1 : 0) + '\n'; 60 let isHasError = summary.failure > 0 || summary.error > 0; 61 let config = this.coreContext.getDefaultService('config'); 62 if (config.isBreakOnError() && isHasError) { 63 // 未执行全部说明 64 message += '\n' + `${PrintTag.OHOS_REPORT_RESULT}: breakOnError model, Stopping whole test suite if one specific test case failed or error` + '\n'; 65 } 66 message += `${PrintTag.OHOS_REPORT_STATUS}: taskconsuming=` + summary.duration + '\n'; 67 console.info(`${message}`); 68 await SysTestKit.print(message); 69 } 70 if (SysTestKit.workerPort === null || SysTestKit.workerPort === undefined) { 71 // 主线程执行完成 结束任务。 72 console.info(`${TAG}report print success`); 73 this.delegator.finishTest('your test finished!!!', 0, () => { }); 74 } else { 75 // worker线程执行完成将数据发送到主线程中。 76 let sendData = { 77 currentThreadName: this.currentThreadName, 78 summary: summary 79 }; 80 console.info(`${TAG}, send data to mainThread, ${this.currentThreadName}, ${JSON.stringify(sendData)}`); 81 SysTestKit.workerPort.postMessage(sendData); 82 } 83 } 84 85 incorrectFormat() { 86 if (this.coreContext.getDefaultService('config').filterValid.length !== 0) { 87 var value = this.coreContext.getDefaultService('config').filterValid; 88 var message = 'this param ' + value.join(',') + ' is invalid' + '\n'; 89 this.delegator.finishTest(message, 0, () => { 90 }); 91 } 92 } 93 94 incorrectTestSuiteFormat() { 95 if (this.coreContext.getDefaultService('config').filterXdescribe.length !== 0) { 96 let value = this.coreContext.getDefaultService('config').filterXdescribe; 97 let message = 'xdescribe ' + value.join(',') + ' should not contain it' + '\n'; 98 this.delegator.finishTest(message, 0, () => { 99 }); 100 } 101 } 102 async suiteStart() { 103 if (this.abilityDelegatorArguments !== null) { 104 let specArr = []; 105 this.suiteService.getAllChildSuiteNum(this.suiteService.getCurrentRunningSuite(), specArr); 106 let message = '\n' + `${PrintTag.OHOS_REPORT_SUM}: ` + specArr.length; 107 this.suiteService.setCurrentRunningSuiteDesc(this.suiteService.getRootSuite(), this.suiteService.getCurrentRunningSuite(), ''); 108 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc() + '\n'; 109 if (this.suiteService.currentRunningSuite.isSkip) { 110 message += `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.suiteService.currentRunningSuite.skipReason + '\n'; 111 } 112 if (SysTestKit.workerPort !== null) { 113 message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 114 } 115 console.info(`${message}`); 116 await SysTestKit.print(message); 117 console.info(`${TAG}${this.suiteService.getCurrentRunningSuite().description} suiteStart print success`); 118 } 119 } 120 121 async suiteDone() { 122 if (this.abilityDelegatorArguments !== null) { 123 const currentRunningSuite = this.suiteService.getCurrentRunningSuite(); 124 this.suiteService.setCurrentRunningSuiteDesc(this.suiteService.getRootSuite(), this.suiteService.getCurrentRunningSuite(), ''); 125 let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc(); 126 if (this.suiteService.currentRunningSuite.isSkip && this.suiteService.currentRunningSuite.skipReason !== '') { 127 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.suiteService.currentRunningSuite.skipReason; 128 } 129 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: suiteconsuming=` + this.suiteService.getCurrentRunningSuite().duration; 130 if (currentRunningSuite.hookError) { 131 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: ${currentRunningSuite.hookError.message}`; 132 } 133 message += '\n'; 134 if (SysTestKit.workerPort !== null) { 135 message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 136 } 137 console.info(`${message}`); 138 await SysTestKit.print(message); 139 console.info(`${TAG}${this.suiteService.getCurrentRunningSuite().description} suiteDone print success`); 140 } 141 } 142 143 async specStart() { 144 if (this.abilityDelegatorArguments !== null) { 145 let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc(); 146 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (++this.index); 147 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`; 148 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal(); 149 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 150 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 151 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 1` + '\n'; 152 if (this.specService.currentRunningSpec.isSkip) { 153 message += `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n'; 154 } 155 if (SysTestKit.workerPort !== null) { 156 message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 157 } 158 console.info(`${message}`); 159 await SysTestKit.print(message); 160 console.info(`${TAG}${this.specService.currentRunningSpec.description} specStart start print success`); 161 } 162 } 163 164 async specDone() { 165 if (this.abilityDelegatorArguments !== null) { 166 let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc(); 167 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (this.index); 168 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`; 169 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal(); 170 let messageStack = ''; 171 let messageCode = ''; 172 if (this.specService.currentRunningSpec.error) { 173 messageStack = `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.error?.stack?.slice(0, -1); 174 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 175 messageCode += this.specService.currentRunningSpec.expectMsg !== '' ? 176 `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}` : 177 `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}`; 178 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 179 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -1` + '\n'; 180 } else if (this.specService.currentRunningSpec) { 181 if (this.specService.currentRunningSpec.fail) { 182 messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.fail?.stack?.slice(0, -1); 183 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 184 messageCode += this.specService.currentRunningSpec.expectMsg !== '' ? 185 `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}` : 186 `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}`; 187 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 188 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -2` + '\n'; 189 } else { 190 messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 191 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 192 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 0` + '\n'; 193 messageCode += this.specService.currentRunningSpec.isSkip ? (`${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n') : ''; 194 } 195 } else { 196 messageCode += '\n'; 197 } 198 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: consuming=` + this.specService.currentRunningSpec.duration + '\n'; 199 if (SysTestKit.workerPort !== null) { 200 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 201 } 202 console.info(`${message}`); 203 console.info(`\n${messageStack}`); 204 console.info(`\n${messageCode}`); 205 await SysTestKit.print(message); 206 await SysTestKit.print(messageStack); 207 await SysTestKit.print(messageCode); 208 console.info(`${TAG}${this.specService.currentRunningSpec.description} specDone end print success`); 209 } 210 } 211} 212 213export default OhReport; 214