• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ClassFilter, NotClassFilter, SuiteAndItNameFilter, TestTypesFilter, NestFilter } from './Filter';
17import { TAG, TESTTYPE, LEVEL, SIZE, KEYSET } from '../../Constant';
18const STRESS_RULE = /^[1-9]\d*$/;
19
20class ConfigService {
21    constructor(attr) {
22        this.id = attr.id;
23        this.supportAsync = true; // 默认异步处理测试用例
24        this.random = false;
25        this.filterValid = [];
26        this.filter = 0;
27        this.flag = false;
28        this.suite = null;
29        this.itName = null;
30        this.testType = null;
31        this.level = null;
32        this.size = null;
33        this.class = null;
34        this.notClass = null;
35        this.timeout = null;
36        // 遇错即停模式配置
37        this.breakOnError = false;
38        // 压力测试配置
39        this.stress = null;
40        this.skipMessage = false;
41        this.runSkipped = '';
42        this.filterXdescribe = [];
43    }
44
45    init(coreContext) {
46        this.coreContext = coreContext;
47    }
48
49    isNormalInteger(str) {
50        const n = Math.floor(Number(str));
51        return n !== Infinity && String(n) === String(str) && n >= 0;
52    }
53
54
55    getStress() {
56        if (this.stress === undefined || this.stress === '' || this.stress === null) {
57            return 1;
58        }
59        return !this.stress.match(STRESS_RULE) ? 1 : Number.parseInt(this.stress);
60    }
61
62    basicParamValidCheck(params) {
63        let size = params.size;
64        if (size !== undefined && size !== '' && size !== null) {
65            let sizeArray = ['small', 'medium', 'large'];
66            if (sizeArray.indexOf(size) === -1) {
67                this.filterValid.push('size:' + size);
68            }
69        }
70        let level = params.level;
71        if (level !== undefined && level !== '' && level !== null) {
72            let levelArray = ['0', '1', '2', '3', '4'];
73            if (levelArray.indexOf(level) === -1) {
74                this.filterValid.push('level:' + level);
75            }
76        }
77        let testType = params.testType;
78        if (testType !== undefined && testType !== '' && testType !== null) {
79            let testTypeArray = ['function', 'performance', 'power', 'reliability', 'security',
80                'global', 'compatibility', 'user', 'standard', 'safety', 'resilience'];
81            if (testTypeArray.indexOf(testType) === -1) {
82                this.filterValid.push('testType:' + testType);
83            }
84        }
85    }
86
87    filterParamValidCheck(params) {
88        let timeout = params.timeout;
89        if (timeout !== undefined && timeout !== '' && timeout !== null) {
90            if (!this.isNormalInteger(timeout)) {
91                this.filterValid.push('timeout:' + timeout);
92            }
93        }
94
95        let paramKeys = ['dryRun', 'random', 'breakOnError', 'coverage', 'skipMessage'];
96        for (const key of paramKeys) {
97            if (params[key] !== undefined && params[key] !== 'true' && params[key] !== 'false') {
98                this.filterValid.push(`${key}:${params[key]}`);
99            }
100        }
101
102        // 压力测试参数验证,正整数
103        if (params.stress !== undefined && params.stress !== '' && params.stress !== null) {
104            if (!params.stress.match(STRESS_RULE)) {
105                this.filterValid.push('stress:' + params.stress);
106            }
107        }
108
109        let nameRule = /^[A-Za-z]{1}[\w#,.]*$/;
110        let paramClassKeys = ['class', 'notClass'];
111        for (const key of paramClassKeys) {
112            if (params[key] !== undefined && params[key] !== '' && params[key] !== null) {
113                let classArray = params[key].split(',');
114                classArray.forEach(item => !item.match(nameRule) ? this.filterValid.push(`${key}:${params[key]}`) : null);
115            }
116        }
117    }
118
119    setConfig(params) {
120        this.basicParamValidCheck(params);
121        this.filterParamValidCheck(params);
122        try {
123            this.class = params.class;
124            this.notClass = params.notClass;
125            this.flag = params.flag || { flag: false };
126            this.suite = params.suite;
127            this.itName = params.itName;
128            this.filter = params.filter;
129            this.testType = params.testType;
130            this.level = params.level;
131            this.size = params.size;
132            this.timeout = params.timeout;
133            this.dryRun = params.dryRun;
134            this.breakOnError = params.breakOnError;
135            this.random = params.random === 'true' ? true : false;
136            this.stress = params.stress;
137            this.coverage = params.coverage;
138            this.skipMessage = params.skipMessage;
139            this.runSkipped = params.runSkipped;
140            this.filterParam = {
141                testType: TESTTYPE,
142                level: LEVEL,
143                size: SIZE
144            };
145            this.parseParams();
146        } catch (err) {
147            console.info(`${TAG}setConfig error: ${err.message}`);
148        }
149    }
150
151    parseParams() {
152        if (this.filter != null) {
153            return;
154        }
155        let testTypeFilter = 0;
156        let sizeFilter = 0;
157        let levelFilter = 0;
158        if (this.testType != null) {
159            testTypeFilter = this.testType.split(',')
160                .map(item => this.filterParam.testType[item] || 0)
161                .reduce((pre, cur) => pre | cur, 0);
162        }
163        if (this.level != null) {
164            levelFilter = this.level.split(',')
165                .map(item => this.filterParam.level[item] || 0)
166                .reduce((pre, cur) => pre | cur, 0);
167        }
168        if (this.size != null) {
169            sizeFilter = this.size.split(',')
170                .map(item => this.filterParam.size[item] || 0)
171                .reduce((pre, cur) => pre | cur, 0);
172        }
173        this.filter = testTypeFilter | sizeFilter | levelFilter;
174        console.info(`${TAG}filter params:${this.filter}`);
175    }
176
177    isCurrentSuite(description) {
178        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
179            let suiteArray = this.suite.split(',');
180            return suiteArray.indexOf(description) !== -1;
181        }
182        return false;
183    }
184
185    filterSuite(currentSuiteName) {
186        let filterArray = [];
187        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
188            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, '', this.suite));
189        }
190        if (this.class !== undefined && this.class !== '' && this.class !== null) {
191            filterArray.push(new ClassFilter(currentSuiteName, '', this.class));
192        }
193        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
194            filterArray.push(new NotClassFilter(currentSuiteName, '', this.notClass));
195        }
196
197        let result = filterArray.map(item => item.filterSuite()).reduce((pre, cur) => pre || cur, false);
198        return result;
199    }
200
201    filterDesc(currentSuiteName, desc, fi, coreContext) {
202        let filterArray = [];
203        if (this.itName !== undefined && this.itName !== '' && this.itName !== null) {
204            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, desc, this.itName));
205        }
206        if (this.class !== undefined && this.class !== '' && this.class !== null) {
207            filterArray.push(new ClassFilter(currentSuiteName, desc, this.class));
208        }
209        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
210            filterArray.push(new NotClassFilter(currentSuiteName, desc, this.notClass));
211        }
212        if (typeof (this.filter) !== 'undefined' && this.filter !== 0 && fi !== 0) {
213            filterArray.push(new TestTypesFilter('', '', fi, this.filter));
214        }
215        let result = filterArray.map(item => item.filterIt()).reduce((pre, cur) => pre || cur, false);
216        return result;
217    }
218
219    filterWithNest(desc, filter) {
220        let filterArray = [];
221        const nestFilter = new NestFilter();
222        const targetSuiteArray = this.coreContext.getDefaultService('suite').targetSuiteArray;
223        const targetSpecArray = this.coreContext.getDefaultService('suite').targetSpecArray;
224        const suiteStack = this.coreContext.getDefaultService('suite').suitesStack;
225        let isFilter = nestFilter.filterNestName(targetSuiteArray, targetSpecArray, suiteStack, desc);
226        const isFullRun = this.coreContext.getDefaultService('suite').fullRun;
227        if (typeof (this.filter) !== 'undefined' && this.filter !== 0 && filter !== 0) {
228            filterArray.push(new TestTypesFilter('', '', filter, this.filter));
229            return filterArray.map(item => item.filterIt()).reduce((pre, cur) => pre || cur, false);
230        }
231        if (isFilter && !isFullRun) {
232            return true;
233        }
234        return nestFilter.filterNotClass(this.notClass, suiteStack, desc);
235
236    }
237
238    isRandom() {
239        return this.random || false;
240    }
241
242    isBreakOnError() {
243        return this.breakOnError !== 'true' ? false : true;
244    }
245
246    setSupportAsync(value) {
247        this.supportAsync = value;
248    }
249
250    isSupportAsync() {
251        return this.supportAsync;
252    }
253
254    translateParams(parameters) {
255        const keySet = new Set(KEYSET);
256        let targetParams = {};
257        for (const key in parameters) {
258            if (keySet.has(key)) {
259                var newKey = key.replace('-s ', '');
260                targetParams[newKey] = parameters[key];
261            }
262        }
263        return targetParams;
264    }
265    translateParamsToString(parameters) {
266        const keySet = new Set(KEYSET);
267        let targetParams = '';
268        for (const key in parameters) {
269            if (keySet.has(key)) {
270                targetParams += ' ' + key + ' ' + parameters[key];
271            }
272        }
273        return targetParams.trim();
274    }
275
276    execute() {
277    }
278
279    checkIfSuiteInSkipRun(desc) {
280        return this.runSkipped.split(',').some(item => {
281            return item === desc || item.startsWith(desc + '.') || item.startsWith(desc + '#') || desc.startsWith(item + '.') || this.runSkipped === 'skipped';
282        });
283    }
284
285    checkIfSpecInSkipRun(desc) {
286        return this.runSkipped.split(',').some(item => {
287            if (item.includes('#')) {
288                return item === desc;
289            } else {
290                return desc.startsWith(item + '.') || desc.startsWith(item + '#') || this.runSkipped === 'skipped';
291            }
292        }
293        );
294    }
295}
296
297export {
298    ConfigService
299};
300