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(), currentRunningSuite, ''); 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 const isPromiseError = currentRunningSuite.isPromiseError; 130 if (isPromiseError) { 131 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: shortMsg=Promise(async, await) in describe is not allowed!`; 132 } 133 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: suiteconsuming=` + currentRunningSuite.duration; 134 if (currentRunningSuite.hookError) { 135 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: ${currentRunningSuite.hookError.message}`; 136 } 137 message += '\n'; 138 if (SysTestKit.workerPort !== null) { 139 message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 140 } 141 console.info(`${message}`); 142 await SysTestKit.print(message); 143 console.info(`${TAG}${currentRunningSuite.description} suiteDone print success`); 144 } 145 } 146 147 async specStart() { 148 if (this.abilityDelegatorArguments !== null) { 149 let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc(); 150 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (++this.index); 151 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`; 152 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal(); 153 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 154 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 155 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 1` + '\n'; 156 if (this.specService.currentRunningSpec.isSkip) { 157 message += `${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n'; 158 } 159 if (SysTestKit.workerPort !== null) { 160 message += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 161 } 162 console.info(`${message}`); 163 await SysTestKit.print(message); 164 console.info(`${TAG}${this.specService.currentRunningSpec.description} specStart start print success`); 165 } 166 } 167 168 async specDone() { 169 if (this.abilityDelegatorArguments !== null) { 170 let message = '\n' + `${PrintTag.OHOS_REPORT_STATUS}: class=` + this.suiteService.getCurrentRunningSuiteDesc(); 171 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: current=` + (this.index); 172 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: id=JS`; 173 message += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: numtests=` + this.specService.getTestTotal(); 174 let messageStack = ''; 175 let messageCode = ''; 176 if (this.specService.currentRunningSpec.error) { 177 messageStack = `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.error?.stack?.slice(0, -1); 178 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 179 messageCode += this.specService.currentRunningSpec.expectMsg !== '' ? 180 `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}` : 181 `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.error?.message}`; 182 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 183 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -1` + '\n'; 184 } else if (this.specService.currentRunningSpec) { 185 if (this.specService.currentRunningSpec.fail) { 186 messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stack=` + this.specService.currentRunningSpec.fail?.stack?.slice(0, -1); 187 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 188 messageCode += this.specService.currentRunningSpec.expectMsg !== '' ? 189 `message: ${this.specService.currentRunningSpec.expectMsg}, Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}` : 190 `Error in ${this.specService.currentRunningSpec.description}, ${this.specService.currentRunningSpec.fail?.message}`; 191 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 192 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: -2` + '\n'; 193 } else { 194 messageStack += `${PrintTag.OHOS_REPORT_STATUS}: stream=`; 195 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: test=` + this.specService.currentRunningSpec.description; 196 messageCode += '\n' + `${PrintTag.OHOS_REPORT_STATUS_CODE}: 0` + '\n'; 197 messageCode += this.specService.currentRunningSpec.isSkip ? (`${PrintTag.OHOS_REPORT_STATUS}: skipReason=` + this.specService.currentRunningSpec.skipReason + '\n') : ''; 198 } 199 } else { 200 messageCode += '\n'; 201 } 202 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: consuming=` + this.specService.currentRunningSpec.duration + '\n'; 203 if (SysTestKit.workerPort !== null) { 204 messageCode += `${PrintTag.OHOS_REPORT_STATUS}: currentWorkerName=` + this.currentThreadName; 205 } 206 console.info(`${message}`); 207 console.info(`\n${messageStack}`); 208 console.info(`\n${messageCode}`); 209 await SysTestKit.print(message); 210 await SysTestKit.print(messageStack); 211 await SysTestKit.print(messageCode); 212 console.info(`${TAG}${this.specService.currentRunningSpec.description} specDone end print success`); 213 } 214 } 215} 216 217export default OhReport; 218