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