13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport * as fs from 'node:fs';
173af6ab5fSopenharmony_ciimport * as path from 'node:path';
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ciconst TS_EXT = ".ts";
203af6ab5fSopenharmony_ciconst ETS_EXT = ".ets";
213af6ab5fSopenharmony_ciconst TSX_EXT = ".tsx";
223af6ab5fSopenharmony_ciconst STS_EXT = ".sts";
233af6ab5fSopenharmony_ciconst D_TS_EXT = '.d.ts';
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ciclass Mode {
263af6ab5fSopenharmony_ci    static DEFAULT = 1;
273af6ab5fSopenharmony_ci    static AUTOFIX = 2
283af6ab5fSopenharmony_ci}
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_ciconst RESULT_EXT = [];
313af6ab5fSopenharmony_ciRESULT_EXT[Mode.DEFAULT] = '.json';
323af6ab5fSopenharmony_ciRESULT_EXT[Mode.AUTOFIX] = '.autofix.json';
333af6ab5fSopenharmony_ciconst AUTOFIX_SKIP_EXT = '.autofix.skip';
343af6ab5fSopenharmony_ciconst DIFF_EXT = '.diff';
353af6ab5fSopenharmony_ciconst RESULTS_DIR = 'results'
363af6ab5fSopenharmony_ci
373af6ab5fSopenharmony_cilet testDirs = [];
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_ci// forces to update all tests regardless of whether there was diff in a test result
403af6ab5fSopenharmony_cilet force_update = false;
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_cifor (let arg of process.argv.slice(2)) {
433af6ab5fSopenharmony_ci    if (arg === '--force')
443af6ab5fSopenharmony_ci        force_update = true;
453af6ab5fSopenharmony_ci    else
463af6ab5fSopenharmony_ci        testDirs.push(arg);
473af6ab5fSopenharmony_ci}
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ciconst DEFAULT_COPYRIGHT =  [
503af6ab5fSopenharmony_ci    "Copyright (c) 2024 Huawei Device Co., Ltd.",
513af6ab5fSopenharmony_ci    "Licensed under the Apache License, Version 2.0 (the 'License');",
523af6ab5fSopenharmony_ci    "you may not use this file except in compliance with the License.",
533af6ab5fSopenharmony_ci    "You may obtain a copy of the License at",
543af6ab5fSopenharmony_ci    "",
553af6ab5fSopenharmony_ci    "http://www.apache.org/licenses/LICENSE-2.0",
563af6ab5fSopenharmony_ci    "",
573af6ab5fSopenharmony_ci    "Unless required by applicable law or agreed to in writing, software",
583af6ab5fSopenharmony_ci    "distributed under the License is distributed on an 'AS IS' BASIS,",
593af6ab5fSopenharmony_ci    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.",
603af6ab5fSopenharmony_ci    "See the License for the specific language governing permissions and",
613af6ab5fSopenharmony_ci    "limitations under the License."
623af6ab5fSopenharmony_ci];
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_cifunction readTestFile(filePath) {
653af6ab5fSopenharmony_ci    try {
663af6ab5fSopenharmony_ci        let resultFile = fs.readFileSync(filePath).toString();
673af6ab5fSopenharmony_ci        return JSON.parse(resultFile);
683af6ab5fSopenharmony_ci    } catch (error) {
693af6ab5fSopenharmony_ci        return undefined;
703af6ab5fSopenharmony_ci    }
713af6ab5fSopenharmony_ci}
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_cifunction updateTest(testDir, testFile, mode) {
743af6ab5fSopenharmony_ci    // Temporary solution: rename '.sts' extension to '.ts'
753af6ab5fSopenharmony_ci    if (testFile.endsWith(STS_EXT)) {
763af6ab5fSopenharmony_ci        testFile = testFile.replace(STS_EXT, TS_EXT);
773af6ab5fSopenharmony_ci    }
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    let resultExt = RESULT_EXT[mode];
803af6ab5fSopenharmony_ci    let resultFileWithExt = testFile + resultExt;
813af6ab5fSopenharmony_ci    let resultFilePath = path.join(testDir, resultFileWithExt);
823af6ab5fSopenharmony_ci
833af6ab5fSopenharmony_ci    // Do not update autofix result if test is skipped
843af6ab5fSopenharmony_ci    if (mode === Mode.AUTOFIX && fs.existsSync(path.join(testDir, testFile + AUTOFIX_SKIP_EXT))) {
853af6ab5fSopenharmony_ci        return;
863af6ab5fSopenharmony_ci    }
873af6ab5fSopenharmony_ci
883af6ab5fSopenharmony_ci    // Update test result when:
893af6ab5fSopenharmony_ci    // - '.diff' exists
903af6ab5fSopenharmony_ci    // - expected '.json' doesn't exist 
913af6ab5fSopenharmony_ci    // - 'force' option is enabled
923af6ab5fSopenharmony_ci    if (fs.existsSync(resultFilePath) && !fs.existsSync(path.join(testDir, RESULTS_DIR, resultFileWithExt + DIFF_EXT)) && !force_update) {
933af6ab5fSopenharmony_ci        return;
943af6ab5fSopenharmony_ci    }
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci    let expectedResult = readTestFile(resultFilePath);
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_ci    const copyright = expectedResult?.copyright ?? DEFAULT_COPYRIGHT;
993af6ab5fSopenharmony_ci
1003af6ab5fSopenharmony_ci    let actualResult = readTestFile(path.join(testDir, RESULTS_DIR, resultFileWithExt));
1013af6ab5fSopenharmony_ci    if (!actualResult || !actualResult.nodes) {
1023af6ab5fSopenharmony_ci        console.log(`Failed to update ${resultFileWithExt}: couldn't read ACTUAL result file.`);
1033af6ab5fSopenharmony_ci        return;
1043af6ab5fSopenharmony_ci    }
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci    // Write file with actual test results.
1073af6ab5fSopenharmony_ci    let newResultJSON = JSON.stringify({ copyright, nodes: actualResult.nodes }, null, 4);
1083af6ab5fSopenharmony_ci    fs.writeFileSync(resultFilePath, newResultJSON);
1093af6ab5fSopenharmony_ci
1103af6ab5fSopenharmony_ci    console.log(`Updated ${resultFileWithExt}`);
1113af6ab5fSopenharmony_ci}
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_cifor (let testDir of testDirs) {
1143af6ab5fSopenharmony_ci    if (!fs.existsSync(path.join(testDir, RESULTS_DIR))) continue;
1153af6ab5fSopenharmony_ci
1163af6ab5fSopenharmony_ci    // Get tests from test directory.
1173af6ab5fSopenharmony_ci    let testFiles = fs.readdirSync(testDir).filter(x =>
1183af6ab5fSopenharmony_ci        (x.trimEnd().endsWith(TS_EXT) && !x.trimEnd().endsWith(D_TS_EXT)) ||
1193af6ab5fSopenharmony_ci        x.trimEnd().endsWith(TSX_EXT) ||
1203af6ab5fSopenharmony_ci        x.trimEnd().endsWith(ETS_EXT) ||
1213af6ab5fSopenharmony_ci        x.trimEnd().endsWith(STS_EXT)
1223af6ab5fSopenharmony_ci    );
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ci    if (!testFiles) continue;
1253af6ab5fSopenharmony_ci
1263af6ab5fSopenharmony_ci    // Update result for each test for Default and Autofix modes:
1273af6ab5fSopenharmony_ci    for (let testFile of testFiles) {
1283af6ab5fSopenharmony_ci        updateTest(testDir, testFile, Mode.DEFAULT);
1293af6ab5fSopenharmony_ci        updateTest(testDir, testFile, Mode.AUTOFIX);
1303af6ab5fSopenharmony_ci    }
1313af6ab5fSopenharmony_ci}