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