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 */ 15class OhReport { 16 constructor(attr) { 17 this.delegator = attr.delegator 18 this.id = 'report' 19 this.index = 0 20 } 21 22 init(coreContext) { 23 this.coreContext = coreContext 24 this.suiteService = this.coreContext.getDefaultService('suite') 25 this.specService = this.coreContext.getDefaultService('spec') 26 } 27 28 taskStart() { 29 } 30 31 taskDone() { 32 let summary = this.suiteService.getSummary() 33 var message = '\n' + 'OHOS_REPORT_RESULT: stream=Tests run: ' + summary.total + ', Failures: ' + summary.failure 34 message += ' \n' + 'OHOS_REPORT_CODE: ' + (summary.failure > 0 ? -1 : 0) + '\n' 35 this.delegator.print(message).then(() => { 36 console.info('report print success'); 37 this.delegator.finishTest('your test finished!!!', 0, () => {}) 38 }); 39 } 40 41 incorrectFormat () { 42 if (this.coreContext.getDefaultService('config').filterValid.length !== 0) { 43 var value = this.coreContext.getDefaultService('config').filterValid 44 var message = 'this param '+value.join(',')+' is invalid' 45 this.delegator.finishTest(message, 0, () => {}) 46 return 47 } 48 } 49 50 suiteStart() { 51 let suiteService = this.coreContext.getDefaultService('suite') 52 var message = '\n' + 'OHOS_REPORT_SUM: '+suiteService.getSummary().total 53 message += '\n' + 'OHOS_REPORT_STATUS: class=' + suiteService.getCurrentRunningSuite().description 54 this.delegator.print(message).then(() => { 55 console.info(suiteService.getCurrentRunningSuite().description + ' print success'); 56 }); 57 } 58 59 suiteDone() { 60 } 61 62 specStart() { 63 let suiteService = this.coreContext.getDefaultService('suite') 64 var message = '\n' + 'OHOS_REPORT_STATUS: class=' + suiteService.getCurrentRunningSuite().description 65 message += '\n' + 'OHOS_REPORT_STATUS: current=' + (++this.index) 66 message += '\n' + 'OHOS_REPORT_STATUS: id=JS' 67 message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + suiteService.getSummary().total 68 message += '\n' + 'OHOS_REPORT_STATUS: stream=' 69 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description 70 message += '\n' + 'OHOS_REPORT_STATUS_CODE: 1' + '\n' 71 this.delegator.print(message).then(() => { 72 console.info(this.specService.currentRunningSpec.description + ' start print success'); 73 }); 74 } 75 76 specDone() { 77 var message = '\n' + 'OHOS_REPORT_STATUS: class=' + this.suiteService.getCurrentRunningSuite().description 78 message += '\n' + 'OHOS_REPORT_STATUS: current=' + (this.index) 79 message += '\n' + 'OHOS_REPORT_STATUS: id=JS' 80 message += '\n' + 'OHOS_REPORT_STATUS: numtests=' + this.suiteService.getSummary().total 81 let emsg = '' 82 if (this.specService.currentRunningSpec.error) { 83 message += '\n' + 'OHOS_REPORT_STATUS: stack=' + this.specService.currentRunningSpec.error 84 message += '\n' + 'OHOS_REPORT_STATUS: stream=' 85 message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description 86 message += '\n' + this.specService.currentRunningSpec.error 87 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description 88 message += '\n' + 'OHOS_REPORT_STATUS_CODE: -1' + '\n' 89 } else if (this.specService.currentRunningSpec.result) { 90 if (this.specService.currentRunningSpec.result.failExpects.length > 0) { 91 this.specService.currentRunningSpec.result.failExpects.forEach(failExpect => { 92 emsg = failExpect.message || ('expect ' + failExpect.actualValue + ' ' + failExpect.checkFunc + ' ' + (failExpect.expectValue || '')) 93 }) 94 message += '\n' + 'OHOS_REPORT_STATUS: stack=' + emsg 95 message += '\n' + 'OHOS_REPORT_STATUS: stream=' 96 message += '\n' + 'Error in ' + this.specService.currentRunningSpec.description 97 message += '\n' + emsg + '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description 98 message += '\n' + 'OHOS_REPORT_STATUS_CODE: -2' + '\n' 99 } else { 100 message += '\n' + 'OHOS_REPORT_STATUS: stream=' 101 message += '\n' + 'OHOS_REPORT_STATUS: test=' + this.specService.currentRunningSpec.description 102 message += '\n' + 'OHOS_REPORT_STATUS_CODE: 0' + '\n' 103 } 104 } else { 105 message += '\n' 106 } 107 this.delegator.print(message).then(() => { 108 console.info(this.specService.currentRunningSpec.description + ' end print success'); 109 }); 110 } 111} 112 113export default OhReport 114