• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-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';
18
19const TS_EXT = ".ts";
20const D_TS_EXT = '.d.ts';
21const TSX_EXT = ".tsx";
22const ETS_EXT = ".ets";
23
24class Mode {
25  static DEFAULT = 1;
26  static AUTOFIX = 2;
27  static ARKTS2 = 3;
28  static MIGRATE = 4;
29}
30
31const LINT_RESULT_EXT = [];
32LINT_RESULT_EXT[Mode.DEFAULT] = '.json';
33LINT_RESULT_EXT[Mode.AUTOFIX] = '.autofix.json';
34LINT_RESULT_EXT[Mode.ARKTS2] = '.arkts2.json';
35LINT_RESULT_EXT[Mode.MIGRATE] = '.migrate.json';
36const DIFF_EXT = '.diff';
37const RESULTS_DIR = 'results';
38const TEST_ARGS_EXT = '.args.json';
39const MIGRATE_RESULT_SUFFIX = '.migrate';
40
41let testDirs = [];
42
43// forces to update all tests regardless of whether there was diff in a test result
44let force_update = false;
45
46for (let arg of process.argv.slice(2)) {
47  if (arg === '--force')
48    force_update = true;
49  else
50    testDirs.push(arg);
51}
52
53const DEFAULT_COPYRIGHT = [
54  "Copyright (c) 2025 Huawei Device Co., Ltd.",
55  "Licensed under the Apache License, Version 2.0 (the 'License');",
56  "you may not use this file except in compliance with the License.",
57  "You may obtain a copy of the License at",
58  "",
59  "http://www.apache.org/licenses/LICENSE-2.0",
60  "",
61  "Unless required by applicable law or agreed to in writing, software",
62  "distributed under the License is distributed on an 'AS IS' BASIS,",
63  "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.",
64  "See the License for the specific language governing permissions and",
65  "limitations under the License."
66];
67
68function readJsonFile(filePath) {
69  try {
70    let resultFile = fs.readFileSync(filePath).toString();
71    return JSON.parse(resultFile);
72  } catch (error) {
73    return undefined;
74  }
75}
76
77function updateTestFile(testDir, testFile) {
78  let testModes = [Mode.DEFAULT];
79
80  const testArgsFile = path.join(testDir, testFile + TEST_ARGS_EXT);
81  if (fs.existsSync(testArgsFile)) {
82    const testArgs = readJsonFile(testArgsFile);
83    if (testArgs?.mode?.autofix !== undefined) testModes.push(Mode.AUTOFIX);
84    if (testArgs?.mode?.arkts2 !== undefined) testModes.push(Mode.ARKTS2);
85    if (testArgs?.mode?.migrate !== undefined) testModes.push(Mode.MIGRATE);
86  }
87
88  for (const mode of testModes) {
89    updateTest(testDir, testFile, mode);
90  }
91}
92
93function updateTest(testDir, testFile, mode) {
94  updateLintResult(testDir, testFile, mode);
95  if (mode === Mode.MIGRATE) {
96    updateMigrateResult(testDir, testFile);
97  }
98}
99
100function updateLintResult(testDir, testFile, mode) {
101  let resultExt = LINT_RESULT_EXT[mode];
102  let resultFileWithExt = testFile + resultExt;
103  let resultFilePath = path.join(testDir, resultFileWithExt);
104
105  // Update test result when:
106  // - '.diff' exists
107  // - expected '.json' for specified test mode doesn't exist
108  // - 'force' option is enabled
109  if (fs.existsSync(resultFilePath) && !fs.existsSync(path.join(testDir, RESULTS_DIR, resultFileWithExt + DIFF_EXT)) && !force_update) {
110    return;
111  }
112
113  let expectedResult = readJsonFile(resultFilePath);
114
115  const copyright = expectedResult?.copyright ?? DEFAULT_COPYRIGHT;
116
117  let actualResult = readJsonFile(path.join(testDir, RESULTS_DIR, resultFileWithExt));
118  if (!actualResult || !actualResult.result) {
119    console.log(`Failed to update ${resultFileWithExt}: couldn't process ACTUAL result file.`);
120    return;
121  }
122
123  // Write file with actual test results.
124  try {
125    const newResultJSON = JSON.stringify({ copyright, result: actualResult.result }, null, 4);
126    fs.writeFileSync(resultFilePath, newResultJSON);
127  }
128  catch (e) {
129    console.log(`Failed to update ${resultFileWithExt}: ${e.message || e}`);
130    return;
131  }
132
133
134  console.log(`Updated ${resultFileWithExt}`);
135}
136
137function updateMigrateResult(testDir, testFile) {
138  let resultExt = MIGRATE_RESULT_SUFFIX + path.extname(testFile);
139  let resultFileWithExt = testFile + resultExt;
140  let resultFilePath = path.join(testDir, resultFileWithExt);
141
142  // Update test result when:
143  // - '.diff' exists
144  // - expected result file doesn't exist
145  // - 'force' option is enabled
146  if (fs.existsSync(resultFilePath) && !fs.existsSync(path.join(testDir, RESULTS_DIR, resultFileWithExt + DIFF_EXT)) && !force_update) {
147    return;
148  }
149
150  try {
151    const actualResultFilePath = path.join(testDir, RESULTS_DIR, resultFileWithExt);
152    fs.copyFileSync(actualResultFilePath, resultFilePath);
153  }
154  catch (e) {
155    console.log(`Failed to update ${resultFileWithExt}: ${e.message || e}`);
156    return;
157  }
158
159  console.log(`Updated ${resultFileWithExt}`);
160}
161
162for (let testDir of testDirs) {
163  if (!fs.existsSync(path.join(testDir, RESULTS_DIR))) continue;
164
165  // Get tests from test directory.
166  let testFiles = fs.readdirSync(testDir).filter(x => {
167    const file = x.trimEnd();
168    return (
169      (file.endsWith(TS_EXT) && !file.endsWith(D_TS_EXT)) ||
170        file.endsWith(TSX_EXT) ||
171        file.endsWith(ETS_EXT)
172      ) && !file.endsWith(MIGRATE_RESULT_SUFFIX + path.extname(file))
173  });
174
175  if (!testFiles) continue;
176
177  // Update result for each test:
178  for (let testFile of testFiles) {
179    updateTestFile(testDir, testFile);
180  }
181}