• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-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 * as fs from 'node:fs';
17import * as path from 'node:path';
18import * as yup from 'yup';
19
20const ARGS_CONFIG_EXT = '.args.json';
21const TAB = '    ';
22
23/**
24 * Describes test configuration format for [test.args.json] file.
25 */
26export interface TestArguments {
27
28  /**
29   * Common arguments applied to each test mode. Will be overriden
30   * by arguments specified for a certain mode.
31   */
32  commonArgs?: string;
33
34  /**
35   * Specifies the test modes. If omitted, test will run only in 'default' mode.
36   *
37   * To 'enable' a certain mode for a test, specify corresponding property
38   * in the 'mode' property. Each mode creates additional test result file
39   * with corresponding extension.
40   *
41   * Additional arguments may be provided to a certain mode with a string value.
42   * Empty string means no additional arguments.
43   */
44  mode?: {
45
46    /**
47     * Optional property that can be used to specify additional arguments
48     * for default mode. Test will run in default mode regardless
49     * of whether this property is specified.
50     */
51    default?: string;
52
53    /**
54     * Enables 'autofix' mode, runs test with '--autofix' option.
55     * Adds generated autofix suggestions in test result file.
56     */
57    autofix?: string;
58
59    /**
60     * Enables 'ArkTS 2.0' mode, runs test with '--arkts-2' option.
61     */
62    arkts2?: string;
63
64    /**
65     * Enables 'migrate' mode, runs test with '--migrate' option.
66     * Performs code migration and produces new test code.
67     */
68    migrate?: string;
69  };
70}
71
72const testArgsSchema: yup.ObjectSchema<TestArguments> = yup.object({
73  commonArgs: yup.string(),
74  mode: yup.
75    object({
76      default: yup.string(),
77      autofix: yup.string(),
78      arkts2: yup.string()
79    }).
80    optional()
81});
82
83export function validateTestArgs(value: object): TestArguments {
84  try {
85    return testArgsSchema.validateSync(value, { strict: true, abortEarly: false });
86  } catch (error) {
87    if (error instanceof yup.ValidationError) {
88      let errMsg = '';
89      for (const msg of error.errors) {
90        errMsg += '\n' + TAB + TAB + msg;
91      }
92      throw new Error(errMsg);
93    }
94    throw error;
95  }
96}
97
98export function readTestArgsFile(testDir: string, testFile: string): TestArguments | undefined {
99  const argsFileName = path.join(testDir, testFile + ARGS_CONFIG_EXT);
100  if (fs.existsSync(argsFileName)) {
101    try {
102      const data = fs.readFileSync(argsFileName).toString();
103      const json = JSON.parse(data);
104      return validateTestArgs(json);
105    } catch (error) {
106      throw new Error(`Failed to process ${argsFileName}: ${(error as Error).message}`);
107    }
108  }
109  return undefined;
110}
111