1/* 2 * Copyright (c) 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 * as yup from 'yup'; 17 18const TAB = ' '; 19 20/** 21 * Describes test configuration format for [test.args.json] file. 22 */ 23export interface TestArguments { 24 25 /** 26 * Common arguments applied to each test mode. Will be overriden 27 * by arguments specified for a certain mode. 28 */ 29 commonArgs?: string; 30 31 /** 32 * Specifies the test modes. If omitted, test will run only in 'default' mode. 33 * 34 * To 'enable' a certain mode for a test, specify corresponding property 35 * in the 'mode' property. Each mode creates additional test result file 36 * with corresponding extension. 37 * 38 * Additional arguments may be provided to a certain mode with a string value. 39 * Empty string means no additional arguments. 40 */ 41 mode?: { 42 43 /** 44 * Optional property that can be used to specify additional arguments 45 * for default mode. Test will run in default mode regardless 46 * of whether this property is specified. 47 */ 48 default?: string; 49 50 /** 51 * Enables 'autofix' mode, runs test with '--autofix' option. 52 * Adds generated autofix suggestions in test result file. 53 */ 54 autofix?: string; 55 56 /** 57 * Enables 'ArkTS 2.0' mode, runs test with '--arkts-2' option. 58 */ 59 arkts2?: string; 60 }; 61} 62 63const testArgsSchema: yup.ObjectSchema<TestArguments> = yup.object({ 64 commonArgs: yup.string(), 65 mode: yup. 66 object({ 67 default: yup.string(), 68 autofix: yup.string(), 69 arkts2: yup.string() 70 }). 71 optional() 72}); 73 74export function validateTestArgs(value: object): TestArguments { 75 try { 76 return testArgsSchema.validateSync(value, { strict: true, abortEarly: false }); 77 } catch (error) { 78 if (error instanceof yup.ValidationError) { 79 let errMsg = ''; 80 for (const msg of error.errors) { 81 errMsg += '\n' + TAB + TAB + msg; 82 } 83 throw new Error(errMsg); 84 } 85 throw error; 86 } 87} 88