• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15import path from 'path';
16import fs from 'fs';
17import mocha from 'mocha';
18import sinon from 'sinon';
19import { expect } from 'chai';
20
21import {
22  	BUILD_ON,
23} from '../../lib/pre_define';
24import {
25	resetComponentCollection,
26  	componentCollection
27} from '../../lib/validate_ui_syntax';
28import {
29	transformLog
30} from '../../lib/process_ui_syntax';
31import {
32  	componentInfo,
33  	resetUtils,
34  	storedFileInfo
35} from '../../lib/utils';
36import {
37	partialUpdateConfig,
38	projectConfig,
39	resetGlobalProgram,
40	resetMain,
41	resources
42} from '../../main';
43import {
44	etsChecker
45} from '../../lib/fast_build/ets_ui/rollup-plugin-ets-checker';
46import {
47	etsTransform
48} from '../../lib/fast_build/ets_ui/rollup-plugin-ets-typescript';
49import processStructComponentV2 from '../../lib/process_struct_componentV2';
50import {
51	RollUpPluginMock
52} from './helpers/mockRollupContext';
53import {
54	PartialUpdateConfig,
55	ProjectConfig
56} from './helpers/projectConfig';
57import {
58	CACHE_PATH,
59	UT_PAGES
60} from './helpers/pathConfig';
61import {
62	parseCode,
63	sourceReplace
64} from './helpers/parser';
65
66const PROJECT_ROOT: string = path.resolve(__dirname, '../../test/transform_ut');
67const DEFAULT_PROJECT: string = 'application';
68const TEST_CASES_PATH: string = path.resolve(PROJECT_ROOT, DEFAULT_PROJECT, 'entry/src/main/ets/pages');
69const OUTPUTS_PATH: string = path.resolve(PROJECT_ROOT, DEFAULT_PROJECT, 'entry/build', CACHE_PATH, 'entry/src/main/ets/pages');
70const MAIN_PAGES: string[] = UT_PAGES.map((p) => `pages/ut/${p}`);
71
72mocha.describe('test UT testcases [non-preview mode]', function () {
73	this.timeout(10000);
74
75	mocha.before(function () {
76		resetUtils();
77		resetGlobalProgram();
78		resetMain();
79		this.rollup = new RollUpPluginMock();
80		this.rollup.build(PROJECT_ROOT, DEFAULT_PROJECT, MAIN_PAGES);
81
82		this.globalProjectConfig = new ProjectConfig();
83		this.globalProjectConfig.setPreview(false);
84		this.globalProjectConfig.setIgnoreWarning(true);
85		this.globalProjectConfig.scan(PROJECT_ROOT, DEFAULT_PROJECT, MAIN_PAGES);
86		this.globalProjectConfig.mockCompileContextInfo(`${PROJECT_ROOT}/${DEFAULT_PROJECT}`, MAIN_PAGES);
87		this.globalProjectConfig.concat(RollUpPluginMock.mockArkProjectConfig(PROJECT_ROOT, DEFAULT_PROJECT, false));
88
89		this.rollup.share.projectConfig.concat(this.globalProjectConfig);
90		Object.assign(projectConfig, this.globalProjectConfig);
91
92		this.globalPartialUpdateConfig = new PartialUpdateConfig();
93		this.globalPartialUpdateConfig.mockDisableArkTSLinter();
94
95		Object.assign(partialUpdateConfig, this.globalPartialUpdateConfig);
96
97		this.etsCheckerPlugin = etsChecker();
98		this.etsTransformPlugin = etsTransform();
99
100		// disable writing to local files
101		sinon.stub(fs, 'writeSync');
102
103		// run etsChecker once
104		const buildStart = this.etsCheckerPlugin.buildStart.bind(this.rollup);
105		buildStart();
106	});
107
108	mocha.after(() => {
109		this.rollup?.share?.flushLogger();
110		delete this.rollup;
111		delete this.globalProjectConfig;
112		delete this.globalPartialUpdateConfig;
113		delete this.etsCheckerPlugin;
114		delete this.etsTransformPlugin;
115
116		resetUtils();
117		resetGlobalProgram();
118		resetMain();
119		sinon.restore();
120	});
121
122	mocha.beforeEach(function () {
123		resources.app["media"] = {icon:16777222};
124		resources.app["font"] = {song:16777223};
125
126		process.env.rawFileResource = './';
127		process.env.compileMode = 'moduleJson';
128		process.env.compiler = BUILD_ON;
129		process.env.compileTool = 'rollup';
130
131		transformLog.errors = [];
132		componentInfo.id = 0;
133		componentCollection.customComponents.clear();
134		resetComponentCollection();
135		storedFileInfo.setCurrentArkTsFile();
136	});
137
138	mocha.afterEach(function () {
139		processStructComponentV2.resetStructMapInEts();
140	});
141
142	UT_PAGES.forEach((utPage, index) => {
143		mocha.it(`1-${index + 1}: test ${utPage}`, function (done) {
144			const sourceFilePath: string = path.resolve(TEST_CASES_PATH, `ut/${utPage}.ets`);
145			const sourceCode: string = fs.readFileSync(sourceFilePath, 'utf-8');
146			const targetFilePath: string = resolveWithExtension(OUTPUTS_PATH, 'ut', utPage);
147			const targetCode: string = fs.readFileSync(targetFilePath, 'utf-8');
148
149			storedFileInfo.addFileCacheInfo(sourceFilePath);
150
151			const transform = this.etsTransformPlugin.transform.bind(this.rollup);
152
153			transform(sourceReplace(sourceCode), sourceFilePath)
154				.then(res => {
155					expect(parseCode(res.code)).eql(parseCode(targetCode));
156					done();
157				})
158				.catch(err => done(err));
159		});
160	});
161});
162
163export function resolveWithExtension(OUTPUTS_PATH: string, directoryName: string, utPage: string): string {
164    const tsPath = path.resolve(OUTPUTS_PATH, `${directoryName}/${utPage}.ts`);
165    if (fs.existsSync(tsPath)) {
166        return tsPath;
167    }
168
169    const jsSamplePath: string = path.resolve(OUTPUTS_PATH, `${directoryName}/${utPage}.js.sample`);
170    if (fs.existsSync(jsSamplePath)) {
171        return jsSamplePath;
172    }
173    throw new Error(`does not exist: ${utPage}.[ts|js.sample]`);
174}
175