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