• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 { ServiceAttrIF } from '../../interface';
17import { TAG } from '../../Constant';
18import { Core } from '../../core';
19import { SpecService } from './SpecService';
20import { SuiteService } from './SuiteService';
21import { SysTestKit } from '../kit/SysTestKit';
22import { ConfigService } from '../config/configService';
23class ReportService {
24  public id: string;
25  public coreContext: Core;
26  public specService: SpecService;
27  public suiteService: SuiteService;
28  public duration: int;
29  private index: byte;
30  constructor(attr: ServiceAttrIF) {
31    this.id = attr.id;
32    this.coreContext = new Core();
33    this.specService = new SpecService({ id: '' });
34    this.suiteService = new SuiteService({ id: '' });
35    this.duration = 0;
36    this.index = 0;
37  }
38
39  init(coreContext: Core) {
40    this.coreContext = coreContext;
41    const suite = coreContext.getDefaultService('suite');
42    if (suite !== null) {
43      this.suiteService = suite as SuiteService;
44    }
45    const spec = coreContext.getDefaultService('spec');
46    if (spec !== null) {
47      this.specService = spec as SpecService;
48    }
49    this.duration = 0;
50  }
51
52  taskStart() {
53    console.info(`${TAG}[start] start run suites`);
54  }
55
56  suiteStart() {
57    console.info(`${TAG}[suite start]${this.suiteService.getCurrentRunningSuite().description}`);
58  }
59
60  specStart() {
61    const spec = this.specService.currentRunningSpec;
62    if (spec) {
63      console.info(`${TAG}start running case '${spec.description}'`);
64      this.index = (this.index + 1) as byte;
65      spec.startTime = SysTestKit.getRealTime();
66    }
67  }
68
69  specDone() {
70    const spec = this.specService.currentRunningSpec;
71    const suite = this.suiteService.currentRunningSuite;
72    if (spec) {
73      spec.duration = SysTestKit.getRealTime() - spec.startTime;
74      suite.duration += spec.duration;
75      const err = spec.error;
76      const fail = spec.fail;
77      if (err) {
78        this.formatPrint('error', spec.description + ' ; consuming ' + spec.duration + 'ms');
79        this.formatPrint('errorDetail', err.message);
80      } else if (fail) {
81        this.formatPrint('fail', spec.description + ' ; consuming ' + spec.duration + 'ms');
82        this.formatPrint('failDetail', fail.message);
83      } else {
84        this.formatPrint('pass', spec.description + ' ; consuming ' + spec.duration + 'ms');
85      }
86    }
87  }
88
89  suiteDone() {
90    let suite = this.suiteService.currentRunningSuite;
91    let message = suite.hookError ? `, ${suite.hookError?.message}` : '';
92    console.info(`[suite end] ${suite.description} consuming ${suite.duration} ms${message}`);
93  }
94
95  taskDone() {
96    let msg = '';
97    let summary = this.suiteService.getSummary();
98    msg = 'total cases:' + summary.total + ';failure ' + summary.failure + ',' + 'error ' + summary.error;
99    msg += ',pass ' + summary.pass + '; consuming ' + summary.duration + 'ms';
100    console.info(`${TAG}${msg}`);
101    console.info(`${TAG}[end] run suites end`);
102  }
103
104  incorrectFormat() {
105    const config = this.coreContext.getDefaultService('config');
106    if (config !== null) {
107      const configService = config as ConfigService;
108      if (configService.filterValid.length !== 0) {
109        configService.filterValid.forEach((item: string) => {
110          console.info(`${TAG}this param ${item} is invalid`);
111        });
112      }
113    }
114  }
115
116  incorrectTestSuiteFormat() {
117    const config = this.coreContext.getDefaultService('config');
118    if (config !== null) {
119      const configService = config as ConfigService;
120      if (configService.filterXdescribe.length !== 0) {
121        configService.filterXdescribe.forEach((item: string) => {
122          console.info(`${TAG}xdescribe: ${item} should not contain it`);
123        });
124      }
125    }
126  }
127
128  formatPrint(type: string, msg: string) {
129    switch (type) {
130      case 'pass':
131        console.info(`${TAG}[pass]${msg}`);
132        break;
133      case 'fail':
134        console.info(`${TAG}[fail]${msg}`);
135        break;
136      case 'failDetail':
137        console.info(`${TAG}[failDetail]${msg}`);
138        break;
139      case 'error':
140        console.info(`${TAG}[error]${msg}`);
141        break;
142      case 'errorDetail':
143        console.info(`${TAG}[errorDetail]${msg}`);
144        break;
145    }
146  }
147
148  sleep(numberMillis: int) {
149    const exitTime = new Date().getTime() + numberMillis;
150    while (new Date().getTime() < exitTime) {}
151    return;
152  }
153}
154
155export { ReportService };
156